Move to kernel style SPDX license identifiers
[lttng-ust.git] / libringbuffer / frontend_types.h
CommitLineData
852c2936 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
852c2936 3 *
e92f3e28
MD
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
c0c0989a 6 * Ring Buffer Library Synchronization Header (types).
852c2936
MD
7 *
8 * See ring_buffer_frontend.c for more information on wait-free algorithms.
852c2936
MD
9 */
10
c0c0989a
MJ
11#ifndef _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
12#define _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
13
fb31eb73 14#include <stdint.h>
a6352fd4 15#include <string.h>
03d2d293 16#include <time.h> /* for timer_t */
a6352fd4 17
14641deb
MD
18#include <urcu/list.h>
19#include <urcu/uatomic.h>
14641deb 20
4318ae1b 21#include <lttng/ringbuffer-config.h>
44c72f10 22#include <usterr-signal-safe.h>
4931a13e 23#include "backend_types.h"
1d498196 24#include "shm_internal.h"
03d2d293 25#include "shm_types.h"
a3bb4b27 26#include "vatomic.h"
852c2936
MD
27
28/*
29 * A switch is done during tracing or as a final flush after tracing (so it
30 * won't write in the new sub-buffer).
31 */
32enum switch_mode { SWITCH_ACTIVE, SWITCH_FLUSH };
33
852c2936 34/* channel: collection of per-cpu ring buffers. */
74d81a6c 35#define RB_CHANNEL_PADDING 32
852c2936 36struct channel {
14641deb 37 int record_disabled;
852c2936
MD
38 unsigned long commit_count_mask; /*
39 * Commit count mask, removing
40 * the MSBs corresponding to
41 * bits used to represent the
42 * subbuffer index.
43 */
44
03d2d293
MD
45 unsigned long switch_timer_interval; /* Buffer flush (us) */
46 timer_t switch_timer;
47 int switch_timer_enabled;
48
49 unsigned long read_timer_interval; /* Reader wakeup (us) */
34a91bdb
MD
50 timer_t read_timer;
51 int read_timer_enabled;
52
852c2936 53 int finalized; /* Has channel been finalized */
a3f61e7f 54 size_t priv_data_offset;
74d81a6c 55 unsigned int nr_streams; /* Number of streams */
03d2d293 56 struct lttng_ust_shm_handle *handle;
b2c5f61a
MD
57 /* Extended options. */
58 union {
59 struct {
60 int32_t blocking_timeout_ms;
61 } s;
62 char padding[RB_CHANNEL_PADDING];
63 } u;
de85e7c3
MD
64 /*
65 * Associated backend contains a variable-length array. Needs to
66 * be last member.
67 */
68 struct channel_backend backend; /* Associated backend */
b728d87e 69} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
852c2936
MD
70
71/* Per-subbuffer commit counters used on the hot path */
3c8964ba 72#define RB_COMMIT_COUNT_HOT_PADDING 16
852c2936
MD
73struct commit_counters_hot {
74 union v_atomic cc; /* Commit counter */
75 union v_atomic seq; /* Consecutive commits */
3c8964ba 76 char padding[RB_COMMIT_COUNT_HOT_PADDING];
b728d87e 77} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
852c2936
MD
78
79/* Per-subbuffer commit counters used only on cold paths */
3c8964ba 80#define RB_COMMIT_COUNT_COLD_PADDING 24
852c2936
MD
81struct commit_counters_cold {
82 union v_atomic cc_sb; /* Incremented _once_ at sb switch */
3c8964ba 83 char padding[RB_COMMIT_COUNT_COLD_PADDING];
b728d87e 84} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
852c2936 85
852c2936 86/* ring buffer state */
a9ff648c
MD
87#define RB_CRASH_DUMP_ABI_LEN 256
88#define RB_RING_BUFFER_PADDING 60
89
90#define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
91
92/*
93 * The 128-bit magic number is xor'd in the process data so it does not
94 * cause a false positive when searching for buffers by scanning memory.
95 * The actual magic number is:
96 * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1,
97 * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17,
98 */
99#define RB_CRASH_DUMP_ABI_MAGIC_XOR \
100 { \
101 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, \
102 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, \
103 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, \
104 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
105 }
106
107#define RB_CRASH_ENDIAN 0x1234
108
109#define RB_CRASH_DUMP_ABI_MAJOR 0
110#define RB_CRASH_DUMP_ABI_MINOR 0
111
112enum lttng_crash_type {
113 LTTNG_CRASH_TYPE_UST = 0,
114 LTTNG_CRASH_TYPE_KERNEL = 1,
115};
116
117struct lttng_crash_abi {
118 uint8_t magic[RB_CRASH_DUMP_ABI_MAGIC_LEN];
119 uint64_t mmap_length; /* Overall lenght of crash record */
120 uint16_t endian; /*
121 * { 0x12, 0x34 }: big endian
122 * { 0x34, 0x12 }: little endian
123 */
124 uint16_t major; /* Major number. */
125 uint16_t minor; /* Minor number. */
126 uint8_t word_size; /* Word size (bytes). */
127 uint8_t layout_type; /* enum lttng_crash_type */
128
129 struct {
130 uint32_t prod_offset;
131 uint32_t consumed_offset;
132 uint32_t commit_hot_array;
133 uint32_t commit_hot_seq;
134 uint32_t buf_wsb_array;
135 uint32_t buf_wsb_id;
136 uint32_t sb_array;
137 uint32_t sb_array_shmp_offset;
138 uint32_t sb_backend_p_offset;
139 uint32_t content_size;
140 uint32_t packet_size;
141 } __attribute__((packed)) offset;
142 struct {
143 uint8_t prod_offset;
144 uint8_t consumed_offset;
145 uint8_t commit_hot_seq;
146 uint8_t buf_wsb_id;
147 uint8_t sb_array_shmp_offset;
148 uint8_t sb_backend_p_offset;
149 uint8_t content_size;
150 uint8_t packet_size;
151 } __attribute__((packed)) length;
152 struct {
153 uint32_t commit_hot_array;
154 uint32_t buf_wsb_array;
155 uint32_t sb_array;
156 } __attribute__((packed)) stride;
157
158 uint64_t buf_size; /* Size of the buffer */
159 uint64_t subbuf_size; /* Sub-buffer size */
160 uint64_t num_subbuf; /* Number of sub-buffers for writer */
161 uint32_t mode; /* Buffer mode: 0: overwrite, 1: discard */
162} __attribute__((packed));
163
4cfec15c 164struct lttng_ust_lib_ring_buffer {
a9ff648c
MD
165 /* First 32 bytes are for the buffer crash dump ABI */
166 struct lttng_crash_abi crash_abi;
167
168 /* 32 bytes cache-hot cacheline */
169 union v_atomic __attribute__((aligned(32))) offset;
170 /* Current offset in the buffer */
a6352fd4 171 DECLARE_SHMP(struct commit_counters_hot, commit_hot);
852c2936 172 /* Commit count per sub-buffer */
14641deb 173 long consumed; /*
852c2936
MD
174 * Current offset in the buffer
175 * standard atomic access (shared)
176 */
14641deb 177 int record_disabled;
a9ff648c
MD
178 /* End of cache-hot 32 bytes cacheline */
179
852c2936
MD
180 union v_atomic last_tsc; /*
181 * Last timestamp written in the buffer.
182 */
183
a9ff648c
MD
184 struct lttng_ust_lib_ring_buffer_backend backend;
185 /* Associated backend */
852c2936 186
a6352fd4 187 DECLARE_SHMP(struct commit_counters_cold, commit_cold);
852c2936 188 /* Commit count per sub-buffer */
6c737d05
MD
189 DECLARE_SHMP(uint64_t, ts_end); /*
190 * timestamp_end per sub-buffer.
191 * Time is sampled by the
192 * switch_*_end() callbacks
193 * which are the last space
194 * reservation performed in the
195 * sub-buffer before it can be
196 * fully committed and
197 * delivered. This time value is
198 * then read by the deliver
199 * callback, performed by the
200 * last commit before the buffer
201 * becomes readable.
202 */
14641deb 203 long active_readers; /*
852c2936
MD
204 * Active readers count
205 * standard atomic access (shared)
206 */
207 /* Dropped records */
208 union v_atomic records_lost_full; /* Buffer full */
209 union v_atomic records_lost_wrap; /* Nested wrap-around */
210 union v_atomic records_lost_big; /* Events too big */
211 union v_atomic records_count; /* Number of records written */
212 union v_atomic records_overrun; /* Number of overwritten records */
14641deb 213 //wait_queue_head_t read_wait; /* reader buffer-level wait queue */
852c2936 214 int finalized; /* buffer has been finalized */
852c2936
MD
215 unsigned long get_subbuf_consumed; /* Read-side consumed */
216 unsigned long prod_snapshot; /* Producer count snapshot */
217 unsigned long cons_snapshot; /* Consumer count snapshot */
34a91bdb 218 unsigned int get_subbuf:1; /* Sub-buffer being held by reader */
5d61a504 219 /* shmp pointer to self */
4cfec15c 220 DECLARE_SHMP(struct lttng_ust_lib_ring_buffer, self);
3c8964ba 221 char padding[RB_RING_BUFFER_PADDING];
b728d87e 222} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
852c2936
MD
223
224static inline
225void *channel_get_private(struct channel *chan)
226{
a3f61e7f 227 return ((char *) chan) + chan->priv_data_offset;
852c2936
MD
228}
229
0d4aa2df
MD
230#ifndef __rb_same_type
231#define __rb_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
232#endif
233
852c2936
MD
234/*
235 * Issue warnings and disable channels upon internal error.
4cfec15c 236 * Can receive struct lttng_ust_lib_ring_buffer or struct lttng_ust_lib_ring_buffer_backend
852c2936
MD
237 * parameters.
238 */
239#define CHAN_WARN_ON(c, cond) \
240 ({ \
241 struct channel *__chan; \
b5a3dfa5 242 int _____ret = caa_unlikely(cond); \
852c2936 243 if (_____ret) { \
0d4aa2df 244 if (__rb_same_type(*(c), struct channel_backend)) \
14641deb 245 __chan = caa_container_of((void *) (c), \
852c2936
MD
246 struct channel, \
247 backend); \
0d4aa2df 248 else if (__rb_same_type(*(c), struct channel)) \
852c2936
MD
249 __chan = (void *) (c); \
250 else \
251 BUG_ON(1); \
14641deb 252 uatomic_inc(&__chan->record_disabled); \
852c2936
MD
253 WARN_ON(1); \
254 } \
257ecc17 255 _____ret = _____ret; /* For clang "unused result". */ \
852c2936
MD
256 })
257
e92f3e28 258#endif /* _LTTNG_RING_BUFFER_FRONTEND_TYPES_H */
This page took 0.040553 seconds and 4 git commands to generate.