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