Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | /* |
2 | * ring_buffer_frontend.c | |
3 | * | |
4 | * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * Ring buffer wait-free buffer synchronization. Producer-consumer and flight | |
7 | * recorder (overwrite) modes. See thesis: | |
8 | * | |
9 | * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D. | |
10 | * dissertation, Ecole Polytechnique de Montreal. | |
11 | * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf | |
12 | * | |
13 | * - Algorithm presentation in Chapter 5: | |
14 | * "Lockless Multi-Core High-Throughput Buffering". | |
15 | * - Algorithm formal verification in Section 8.6: | |
16 | * "Formal verification of LTTng" | |
17 | * | |
18 | * Author: | |
19 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
20 | * | |
21 | * Inspired from LTT and RelayFS: | |
22 | * Karim Yaghmour <karim@opersys.com> | |
23 | * Tom Zanussi <zanussi@us.ibm.com> | |
24 | * Bob Wisniewski <bob@watson.ibm.com> | |
25 | * And from K42 : | |
26 | * Bob Wisniewski <bob@watson.ibm.com> | |
27 | * | |
28 | * Buffer reader semantic : | |
29 | * | |
30 | * - get_subbuf_size | |
31 | * while buffer is not finalized and empty | |
32 | * - get_subbuf | |
33 | * - if return value != 0, continue | |
34 | * - splice one subbuffer worth of data to a pipe | |
35 | * - splice the data from pipe to disk/network | |
36 | * - put_subbuf | |
37 | * | |
38 | * Dual LGPL v2.1/GPL v2 license. | |
39 | */ | |
40 | ||
a6352fd4 | 41 | #include <sys/types.h> |
431d5cf0 MD |
42 | #include <sys/mman.h> |
43 | #include <sys/stat.h> | |
44 | #include <fcntl.h> | |
14641deb | 45 | #include <urcu/compiler.h> |
a6352fd4 | 46 | #include <urcu/ref.h> |
35897f8b | 47 | #include <helper.h> |
14641deb | 48 | |
a6352fd4 | 49 | #include "smp.h" |
4318ae1b | 50 | #include <lttng/ringbuffer-config.h> |
2fed87ae | 51 | #include "vatomic.h" |
4931a13e MD |
52 | #include "backend.h" |
53 | #include "frontend.h" | |
a6352fd4 | 54 | #include "shm.h" |
852c2936 | 55 | |
431d5cf0 MD |
56 | #ifndef max |
57 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
58 | #endif | |
59 | ||
2432c3c9 MD |
60 | /* |
61 | * Use POSIX SHM: shm_open(3) and shm_unlink(3). | |
62 | * close(2) to close the fd returned by shm_open. | |
63 | * shm_unlink releases the shared memory object name. | |
64 | * ftruncate(2) sets the size of the memory object. | |
65 | * mmap/munmap maps the shared memory obj to a virtual address in the | |
66 | * calling proceess (should be done both in libust and consumer). | |
67 | * See shm_overview(7) for details. | |
68 | * Pass file descriptor returned by shm_open(3) to ltt-sessiond through | |
69 | * a UNIX socket. | |
70 | * | |
71 | * Since we don't need to access the object using its name, we can | |
72 | * immediately shm_unlink(3) it, and only keep the handle with its file | |
73 | * descriptor. | |
74 | */ | |
75 | ||
852c2936 MD |
76 | /* |
77 | * Internal structure representing offsets to use at a sub-buffer switch. | |
78 | */ | |
79 | struct switch_offsets { | |
80 | unsigned long begin, end, old; | |
81 | size_t pre_header_padding, size; | |
82 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, | |
83 | switch_old_end:1; | |
84 | }; | |
85 | ||
a6352fd4 | 86 | __thread unsigned int lib_ring_buffer_nesting; |
852c2936 | 87 | |
45e9e699 MD |
88 | /* |
89 | * TODO: this is unused. Errors are saved within the ring buffer. | |
90 | * Eventually, allow consumerd to print these errors. | |
91 | */ | |
852c2936 MD |
92 | static |
93 | void lib_ring_buffer_print_errors(struct channel *chan, | |
4cfec15c | 94 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
b68d3dc0 MD |
95 | struct lttng_ust_shm_handle *handle) |
96 | __attribute__((unused)); | |
852c2936 | 97 | |
852c2936 MD |
98 | /** |
99 | * lib_ring_buffer_reset - Reset ring buffer to initial values. | |
100 | * @buf: Ring buffer. | |
101 | * | |
102 | * Effectively empty the ring buffer. Should be called when the buffer is not | |
103 | * used for writing. The ring buffer can be opened for reading, but the reader | |
104 | * should not be using the iterator concurrently with reset. The previous | |
105 | * current iterator record is reset. | |
106 | */ | |
4cfec15c | 107 | void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 108 | struct lttng_ust_shm_handle *handle) |
852c2936 | 109 | { |
1d498196 | 110 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 111 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
112 | unsigned int i; |
113 | ||
114 | /* | |
115 | * Reset iterator first. It will put the subbuffer if it currently holds | |
116 | * it. | |
117 | */ | |
852c2936 MD |
118 | v_set(config, &buf->offset, 0); |
119 | for (i = 0; i < chan->backend.num_subbuf; i++) { | |
4746ae29 MD |
120 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->cc, 0); |
121 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->seq, 0); | |
122 | v_set(config, &shmp_index(handle, buf->commit_cold, i)->cc_sb, 0); | |
852c2936 | 123 | } |
a6352fd4 MD |
124 | uatomic_set(&buf->consumed, 0); |
125 | uatomic_set(&buf->record_disabled, 0); | |
852c2936 | 126 | v_set(config, &buf->last_tsc, 0); |
1d498196 | 127 | lib_ring_buffer_backend_reset(&buf->backend, handle); |
852c2936 MD |
128 | /* Don't reset number of active readers */ |
129 | v_set(config, &buf->records_lost_full, 0); | |
130 | v_set(config, &buf->records_lost_wrap, 0); | |
131 | v_set(config, &buf->records_lost_big, 0); | |
132 | v_set(config, &buf->records_count, 0); | |
133 | v_set(config, &buf->records_overrun, 0); | |
134 | buf->finalized = 0; | |
135 | } | |
852c2936 MD |
136 | |
137 | /** | |
138 | * channel_reset - Reset channel to initial values. | |
139 | * @chan: Channel. | |
140 | * | |
141 | * Effectively empty the channel. Should be called when the channel is not used | |
142 | * for writing. The channel can be opened for reading, but the reader should not | |
143 | * be using the iterator concurrently with reset. The previous current iterator | |
144 | * record is reset. | |
145 | */ | |
146 | void channel_reset(struct channel *chan) | |
147 | { | |
148 | /* | |
149 | * Reset iterators first. Will put the subbuffer if held for reading. | |
150 | */ | |
a6352fd4 | 151 | uatomic_set(&chan->record_disabled, 0); |
852c2936 MD |
152 | /* Don't reset commit_count_mask, still valid */ |
153 | channel_backend_reset(&chan->backend); | |
154 | /* Don't reset switch/read timer interval */ | |
155 | /* Don't reset notifiers and notifier enable bits */ | |
156 | /* Don't reset reader reference count */ | |
157 | } | |
852c2936 MD |
158 | |
159 | /* | |
160 | * Must be called under cpu hotplug protection. | |
161 | */ | |
4cfec15c | 162 | int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
a6352fd4 | 163 | struct channel_backend *chanb, int cpu, |
38fae1d3 | 164 | struct lttng_ust_shm_handle *handle, |
1d498196 | 165 | struct shm_object *shmobj) |
852c2936 | 166 | { |
4cfec15c | 167 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
14641deb | 168 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
a3f61e7f | 169 | void *priv = channel_get_private(chan); |
852c2936 | 170 | size_t subbuf_header_size; |
2fed87ae | 171 | uint64_t tsc; |
852c2936 MD |
172 | int ret; |
173 | ||
174 | /* Test for cpu hotplug */ | |
175 | if (buf->backend.allocated) | |
176 | return 0; | |
177 | ||
a6352fd4 | 178 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, |
1d498196 | 179 | cpu, handle, shmobj); |
852c2936 MD |
180 | if (ret) |
181 | return ret; | |
182 | ||
1d498196 MD |
183 | align_shm(shmobj, __alignof__(struct commit_counters_hot)); |
184 | set_shmp(buf->commit_hot, | |
185 | zalloc_shm(shmobj, | |
186 | sizeof(struct commit_counters_hot) * chan->backend.num_subbuf)); | |
187 | if (!shmp(handle, buf->commit_hot)) { | |
852c2936 MD |
188 | ret = -ENOMEM; |
189 | goto free_chanbuf; | |
190 | } | |
191 | ||
1d498196 MD |
192 | align_shm(shmobj, __alignof__(struct commit_counters_cold)); |
193 | set_shmp(buf->commit_cold, | |
194 | zalloc_shm(shmobj, | |
195 | sizeof(struct commit_counters_cold) * chan->backend.num_subbuf)); | |
196 | if (!shmp(handle, buf->commit_cold)) { | |
852c2936 MD |
197 | ret = -ENOMEM; |
198 | goto free_commit; | |
199 | } | |
200 | ||
852c2936 MD |
201 | /* |
202 | * Write the subbuffer header for first subbuffer so we know the total | |
203 | * duration of data gathering. | |
204 | */ | |
205 | subbuf_header_size = config->cb.subbuffer_header_size(); | |
206 | v_set(config, &buf->offset, subbuf_header_size); | |
4746ae29 | 207 | subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id); |
1d498196 MD |
208 | tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan)); |
209 | config->cb.buffer_begin(buf, tsc, 0, handle); | |
4746ae29 | 210 | v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc); |
852c2936 MD |
211 | |
212 | if (config->cb.buffer_create) { | |
1d498196 | 213 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle); |
852c2936 MD |
214 | if (ret) |
215 | goto free_init; | |
216 | } | |
852c2936 | 217 | buf->backend.allocated = 1; |
852c2936 MD |
218 | return 0; |
219 | ||
220 | /* Error handling */ | |
221 | free_init: | |
a6352fd4 | 222 | /* commit_cold will be freed by shm teardown */ |
852c2936 | 223 | free_commit: |
a6352fd4 | 224 | /* commit_hot will be freed by shm teardown */ |
852c2936 | 225 | free_chanbuf: |
852c2936 MD |
226 | return ret; |
227 | } | |
228 | ||
1d498196 | 229 | #if 0 |
852c2936 MD |
230 | static void switch_buffer_timer(unsigned long data) |
231 | { | |
4cfec15c | 232 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
1d498196 | 233 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 234 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
235 | |
236 | /* | |
237 | * Only flush buffers periodically if readers are active. | |
238 | */ | |
824f40b8 | 239 | if (uatomic_read(&buf->active_readers) || uatomic_read(&buf->active_shadow_readers)) |
1d498196 | 240 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle); |
852c2936 | 241 | |
a6352fd4 MD |
242 | //TODO timers |
243 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
244 | // mod_timer_pinned(&buf->switch_timer, | |
245 | // jiffies + chan->switch_timer_interval); | |
246 | //else | |
247 | // mod_timer(&buf->switch_timer, | |
248 | // jiffies + chan->switch_timer_interval); | |
852c2936 | 249 | } |
1d498196 | 250 | #endif //0 |
852c2936 | 251 | |
4cfec15c | 252 | static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 253 | struct lttng_ust_shm_handle *handle) |
852c2936 | 254 | { |
1d498196 | 255 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 256 | //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
257 | |
258 | if (!chan->switch_timer_interval || buf->switch_timer_enabled) | |
259 | return; | |
a6352fd4 MD |
260 | //TODO |
261 | //init_timer(&buf->switch_timer); | |
262 | //buf->switch_timer.function = switch_buffer_timer; | |
263 | //buf->switch_timer.expires = jiffies + chan->switch_timer_interval; | |
264 | //buf->switch_timer.data = (unsigned long)buf; | |
265 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
266 | // add_timer_on(&buf->switch_timer, buf->backend.cpu); | |
267 | //else | |
268 | // add_timer(&buf->switch_timer); | |
852c2936 MD |
269 | buf->switch_timer_enabled = 1; |
270 | } | |
271 | ||
4cfec15c | 272 | static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 273 | struct lttng_ust_shm_handle *handle) |
852c2936 | 274 | { |
1d498196 | 275 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 MD |
276 | |
277 | if (!chan->switch_timer_interval || !buf->switch_timer_enabled) | |
278 | return; | |
279 | ||
a6352fd4 MD |
280 | //TODO |
281 | //del_timer_sync(&buf->switch_timer); | |
852c2936 MD |
282 | buf->switch_timer_enabled = 0; |
283 | } | |
284 | ||
1d498196 | 285 | #if 0 |
852c2936 MD |
286 | /* |
287 | * Polling timer to check the channels for data. | |
288 | */ | |
289 | static void read_buffer_timer(unsigned long data) | |
290 | { | |
4cfec15c | 291 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
1d498196 | 292 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 293 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
294 | |
295 | CHAN_WARN_ON(chan, !buf->backend.allocated); | |
296 | ||
824f40b8 | 297 | if (uatomic_read(&buf->active_readers) || uatomic_read(&buf->active_shadow_readers)) |
852c2936 | 298 | && lib_ring_buffer_poll_deliver(config, buf, chan)) { |
a6352fd4 MD |
299 | //TODO |
300 | //wake_up_interruptible(&buf->read_wait); | |
301 | //wake_up_interruptible(&chan->read_wait); | |
852c2936 MD |
302 | } |
303 | ||
a6352fd4 MD |
304 | //TODO |
305 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
306 | // mod_timer_pinned(&buf->read_timer, | |
307 | // jiffies + chan->read_timer_interval); | |
308 | //else | |
309 | // mod_timer(&buf->read_timer, | |
310 | // jiffies + chan->read_timer_interval); | |
852c2936 | 311 | } |
1d498196 | 312 | #endif //0 |
852c2936 | 313 | |
4cfec15c | 314 | static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 315 | struct lttng_ust_shm_handle *handle) |
852c2936 | 316 | { |
1d498196 | 317 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 318 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
319 | |
320 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
321 | || !chan->read_timer_interval | |
322 | || buf->read_timer_enabled) | |
323 | return; | |
324 | ||
a6352fd4 MD |
325 | //TODO |
326 | //init_timer(&buf->read_timer); | |
327 | //buf->read_timer.function = read_buffer_timer; | |
328 | //buf->read_timer.expires = jiffies + chan->read_timer_interval; | |
329 | //buf->read_timer.data = (unsigned long)buf; | |
852c2936 | 330 | |
a6352fd4 MD |
331 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
332 | // add_timer_on(&buf->read_timer, buf->backend.cpu); | |
333 | //else | |
334 | // add_timer(&buf->read_timer); | |
852c2936 MD |
335 | buf->read_timer_enabled = 1; |
336 | } | |
337 | ||
4cfec15c | 338 | static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 339 | struct lttng_ust_shm_handle *handle) |
852c2936 | 340 | { |
1d498196 | 341 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 342 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
343 | |
344 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
345 | || !chan->read_timer_interval | |
346 | || !buf->read_timer_enabled) | |
347 | return; | |
348 | ||
a6352fd4 MD |
349 | //TODO |
350 | //del_timer_sync(&buf->read_timer); | |
852c2936 MD |
351 | /* |
352 | * do one more check to catch data that has been written in the last | |
353 | * timer period. | |
354 | */ | |
1d498196 | 355 | if (lib_ring_buffer_poll_deliver(config, buf, chan, handle)) { |
a6352fd4 MD |
356 | //TODO |
357 | //wake_up_interruptible(&buf->read_wait); | |
358 | //wake_up_interruptible(&chan->read_wait); | |
852c2936 MD |
359 | } |
360 | buf->read_timer_enabled = 0; | |
361 | } | |
362 | ||
1d498196 | 363 | static void channel_unregister_notifiers(struct channel *chan, |
38fae1d3 | 364 | struct lttng_ust_shm_handle *handle) |
852c2936 | 365 | { |
4cfec15c | 366 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
367 | int cpu; |
368 | ||
852c2936 | 369 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
852c2936 | 370 | for_each_possible_cpu(cpu) { |
4cfec15c | 371 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
a6352fd4 | 372 | |
1d498196 MD |
373 | lib_ring_buffer_stop_switch_timer(buf, handle); |
374 | lib_ring_buffer_stop_read_timer(buf, handle); | |
852c2936 | 375 | } |
852c2936 | 376 | } else { |
4cfec15c | 377 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 378 | |
1d498196 MD |
379 | lib_ring_buffer_stop_switch_timer(buf, handle); |
380 | lib_ring_buffer_stop_read_timer(buf, handle); | |
852c2936 | 381 | } |
8d8a24c8 | 382 | //channel_backend_unregister_notifiers(&chan->backend); |
852c2936 MD |
383 | } |
384 | ||
38fae1d3 | 385 | static void channel_free(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 386 | int shadow) |
852c2936 | 387 | { |
824f40b8 MD |
388 | if (!shadow) |
389 | channel_backend_free(&chan->backend, handle); | |
431d5cf0 | 390 | /* chan is freed by shm teardown */ |
1d498196 MD |
391 | shm_object_table_destroy(handle->table); |
392 | free(handle); | |
852c2936 MD |
393 | } |
394 | ||
395 | /** | |
396 | * channel_create - Create channel. | |
397 | * @config: ring buffer instance configuration | |
398 | * @name: name of the channel | |
a3f61e7f MD |
399 | * @priv_data: ring buffer client private data area pointer (output) |
400 | * @priv_data_size: length, in bytes, of the private data area. | |
d028eddb | 401 | * @priv_data_init: initialization data for private data. |
852c2936 MD |
402 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
403 | * address mapping. It is used only by RING_BUFFER_STATIC | |
404 | * configuration. It can be set to NULL for other backends. | |
405 | * @subbuf_size: subbuffer size | |
406 | * @num_subbuf: number of subbuffers | |
407 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with | |
408 | * padding to let readers get those sub-buffers. | |
409 | * Used for live streaming. | |
410 | * @read_timer_interval: Time interval (in us) to wake up pending readers. | |
411 | * | |
412 | * Holds cpu hotplug. | |
413 | * Returns NULL on failure. | |
414 | */ | |
4cfec15c | 415 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
416 | const char *name, |
417 | void **priv_data, | |
418 | size_t priv_data_align, | |
419 | size_t priv_data_size, | |
d028eddb | 420 | void *priv_data_init, |
a3f61e7f | 421 | void *buf_addr, size_t subbuf_size, |
852c2936 | 422 | size_t num_subbuf, unsigned int switch_timer_interval, |
193183fb | 423 | unsigned int read_timer_interval, |
ef9ff354 | 424 | int **shm_fd, int **wait_fd, uint64_t **memory_map_size) |
852c2936 | 425 | { |
1d498196 | 426 | int ret, cpu; |
a3f61e7f | 427 | size_t shmsize, chansize; |
852c2936 | 428 | struct channel *chan; |
38fae1d3 | 429 | struct lttng_ust_shm_handle *handle; |
1d498196 | 430 | struct shm_object *shmobj; |
193183fb | 431 | struct shm_ref *ref; |
852c2936 MD |
432 | |
433 | if (lib_ring_buffer_check_config(config, switch_timer_interval, | |
434 | read_timer_interval)) | |
435 | return NULL; | |
436 | ||
38fae1d3 | 437 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
431d5cf0 MD |
438 | if (!handle) |
439 | return NULL; | |
440 | ||
1d498196 MD |
441 | /* Allocate table for channel + per-cpu buffers */ |
442 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
443 | if (!handle->table) | |
444 | goto error_table_alloc; | |
852c2936 | 445 | |
1d498196 MD |
446 | /* Calculate the shm allocation layout */ |
447 | shmsize = sizeof(struct channel); | |
c1fca457 | 448 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp)); |
1d498196 | 449 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
4cfec15c | 450 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * num_possible_cpus(); |
1d498196 | 451 | else |
4cfec15c | 452 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp); |
a3f61e7f MD |
453 | chansize = shmsize; |
454 | shmsize += offset_align(shmsize, priv_data_align); | |
455 | shmsize += priv_data_size; | |
a6352fd4 | 456 | |
1d498196 | 457 | shmobj = shm_object_table_append(handle->table, shmsize); |
b5a14697 MD |
458 | if (!shmobj) |
459 | goto error_append; | |
57773204 | 460 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
a3f61e7f | 461 | set_shmp(handle->chan, zalloc_shm(shmobj, chansize)); |
57773204 MD |
462 | assert(handle->chan._ref.index == 0); |
463 | assert(handle->chan._ref.offset == 0); | |
1d498196 | 464 | chan = shmp(handle, handle->chan); |
a6352fd4 | 465 | if (!chan) |
1d498196 | 466 | goto error_append; |
a6352fd4 | 467 | |
a3f61e7f MD |
468 | /* space for private data */ |
469 | if (priv_data_size) { | |
470 | DECLARE_SHMP(void, priv_data_alloc); | |
471 | ||
472 | align_shm(shmobj, priv_data_align); | |
473 | chan->priv_data_offset = shmobj->allocated_len; | |
474 | set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size)); | |
475 | if (!shmp(handle, priv_data_alloc)) | |
476 | goto error_append; | |
477 | *priv_data = channel_get_private(chan); | |
d028eddb | 478 | memcpy(*priv_data, priv_data_init, priv_data_size); |
a3f61e7f MD |
479 | } else { |
480 | chan->priv_data_offset = -1; | |
481 | *priv_data = NULL; | |
482 | } | |
483 | ||
484 | ret = channel_backend_init(&chan->backend, name, config, | |
1d498196 | 485 | subbuf_size, num_subbuf, handle); |
852c2936 | 486 | if (ret) |
1d498196 | 487 | goto error_backend_init; |
852c2936 MD |
488 | |
489 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); | |
a6352fd4 MD |
490 | //TODO |
491 | //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); | |
492 | //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); | |
a6352fd4 MD |
493 | //TODO |
494 | //init_waitqueue_head(&chan->read_wait); | |
495 | //init_waitqueue_head(&chan->hp_wait); | |
852c2936 MD |
496 | |
497 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
852c2936 MD |
498 | /* |
499 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
500 | * in early initcall, it will not be notified of secondary cpus. | |
501 | * In that off case, we need to allocate for all possible cpus. | |
502 | */ | |
852c2936 | 503 | for_each_possible_cpu(cpu) { |
4cfec15c | 504 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
1d498196 MD |
505 | lib_ring_buffer_start_switch_timer(buf, handle); |
506 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 507 | } |
852c2936 | 508 | } else { |
4cfec15c | 509 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 510 | |
1d498196 MD |
511 | lib_ring_buffer_start_switch_timer(buf, handle); |
512 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 513 | } |
193183fb MD |
514 | ref = &handle->chan._ref; |
515 | shm_get_object_data(handle, ref, shm_fd, wait_fd, memory_map_size); | |
431d5cf0 | 516 | return handle; |
852c2936 | 517 | |
1d498196 MD |
518 | error_backend_init: |
519 | error_append: | |
520 | shm_object_table_destroy(handle->table); | |
521 | error_table_alloc: | |
431d5cf0 | 522 | free(handle); |
852c2936 MD |
523 | return NULL; |
524 | } | |
852c2936 | 525 | |
38fae1d3 | 526 | struct lttng_ust_shm_handle *channel_handle_create(int shm_fd, int wait_fd, |
193183fb MD |
527 | uint64_t memory_map_size) |
528 | { | |
38fae1d3 | 529 | struct lttng_ust_shm_handle *handle; |
193183fb MD |
530 | struct shm_object *object; |
531 | ||
38fae1d3 | 532 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
193183fb MD |
533 | if (!handle) |
534 | return NULL; | |
535 | ||
536 | /* Allocate table for channel + per-cpu buffers */ | |
537 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
538 | if (!handle->table) | |
539 | goto error_table_alloc; | |
540 | /* Add channel object */ | |
541 | object = shm_object_table_append_shadow(handle->table, | |
542 | shm_fd, wait_fd, memory_map_size); | |
543 | if (!object) | |
544 | goto error_table_object; | |
57773204 MD |
545 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
546 | handle->chan._ref.index = 0; | |
547 | handle->chan._ref.offset = 0; | |
193183fb MD |
548 | return handle; |
549 | ||
550 | error_table_object: | |
551 | shm_object_table_destroy(handle->table); | |
552 | error_table_alloc: | |
553 | free(handle); | |
554 | return NULL; | |
555 | } | |
556 | ||
38fae1d3 | 557 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
193183fb MD |
558 | int shm_fd, int wait_fd, uint64_t memory_map_size) |
559 | { | |
560 | struct shm_object *object; | |
561 | ||
562 | /* Add stream object */ | |
563 | object = shm_object_table_append_shadow(handle->table, | |
564 | shm_fd, wait_fd, memory_map_size); | |
565 | if (!object) | |
566 | return -1; | |
567 | return 0; | |
568 | } | |
569 | ||
852c2936 | 570 | static |
38fae1d3 | 571 | void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 572 | int shadow) |
852c2936 | 573 | { |
824f40b8 | 574 | channel_free(chan, handle, shadow); |
852c2936 MD |
575 | } |
576 | ||
577 | /** | |
578 | * channel_destroy - Finalize, wait for q.s. and destroy channel. | |
579 | * @chan: channel to destroy | |
580 | * | |
581 | * Holds cpu hotplug. | |
431d5cf0 MD |
582 | * Call "destroy" callback, finalize channels, decrement the channel |
583 | * reference count. Note that when readers have completed data | |
584 | * consumption of finalized channels, get_subbuf() will return -ENODATA. | |
a3f61e7f | 585 | * They should release their handle at that point. |
852c2936 | 586 | */ |
a3f61e7f | 587 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
824f40b8 | 588 | int shadow) |
852c2936 | 589 | { |
824f40b8 MD |
590 | if (shadow) { |
591 | channel_release(chan, handle, shadow); | |
a3f61e7f | 592 | return; |
824f40b8 MD |
593 | } |
594 | ||
1d498196 | 595 | channel_unregister_notifiers(chan, handle); |
852c2936 | 596 | |
45e9e699 MD |
597 | /* |
598 | * Note: the consumer takes care of finalizing and switching the | |
599 | * buffers. | |
600 | */ | |
852c2936 | 601 | |
431d5cf0 MD |
602 | /* |
603 | * sessiond/consumer are keeping a reference on the shm file | |
604 | * descriptor directly. No need to refcount. | |
605 | */ | |
824f40b8 | 606 | channel_release(chan, handle, shadow); |
a3f61e7f | 607 | return; |
852c2936 | 608 | } |
852c2936 | 609 | |
4cfec15c MD |
610 | struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
611 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 612 | struct channel *chan, int cpu, |
38fae1d3 | 613 | struct lttng_ust_shm_handle *handle, |
ef9ff354 MD |
614 | int **shm_fd, int **wait_fd, |
615 | uint64_t **memory_map_size) | |
852c2936 | 616 | { |
381c0f1e MD |
617 | struct shm_ref *ref; |
618 | ||
619 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
620 | ref = &chan->backend.buf[0].shmp._ref; | |
621 | shm_get_object_data(handle, ref, shm_fd, wait_fd, | |
622 | memory_map_size); | |
1d498196 | 623 | return shmp(handle, chan->backend.buf[0].shmp); |
381c0f1e | 624 | } else { |
e095d803 MD |
625 | if (cpu >= num_possible_cpus()) |
626 | return NULL; | |
381c0f1e MD |
627 | ref = &chan->backend.buf[cpu].shmp._ref; |
628 | shm_get_object_data(handle, ref, shm_fd, wait_fd, | |
629 | memory_map_size); | |
1d498196 | 630 | return shmp(handle, chan->backend.buf[cpu].shmp); |
381c0f1e | 631 | } |
852c2936 | 632 | } |
852c2936 | 633 | |
4cfec15c | 634 | int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 635 | struct lttng_ust_shm_handle *handle, |
824f40b8 | 636 | int shadow) |
852c2936 | 637 | { |
824f40b8 MD |
638 | if (shadow) { |
639 | if (uatomic_cmpxchg(&buf->active_shadow_readers, 0, 1) != 0) | |
640 | return -EBUSY; | |
641 | cmm_smp_mb(); | |
642 | return 0; | |
643 | } | |
a6352fd4 | 644 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
852c2936 | 645 | return -EBUSY; |
a6352fd4 | 646 | cmm_smp_mb(); |
852c2936 MD |
647 | return 0; |
648 | } | |
852c2936 | 649 | |
4cfec15c | 650 | void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 651 | struct lttng_ust_shm_handle *handle, |
824f40b8 | 652 | int shadow) |
852c2936 | 653 | { |
1d498196 | 654 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 | 655 | |
824f40b8 MD |
656 | if (shadow) { |
657 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_shadow_readers) != 1); | |
658 | cmm_smp_mb(); | |
659 | uatomic_dec(&buf->active_shadow_readers); | |
660 | return; | |
661 | } | |
a6352fd4 MD |
662 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
663 | cmm_smp_mb(); | |
664 | uatomic_dec(&buf->active_readers); | |
852c2936 MD |
665 | } |
666 | ||
667 | /** | |
668 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) | |
669 | * @buf: ring buffer | |
670 | * @consumed: consumed count indicating the position where to read | |
671 | * @produced: produced count, indicates position when to stop reading | |
672 | * | |
673 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
674 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 MD |
675 | */ |
676 | ||
4cfec15c | 677 | int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 678 | unsigned long *consumed, unsigned long *produced, |
38fae1d3 | 679 | struct lttng_ust_shm_handle *handle) |
852c2936 | 680 | { |
1d498196 | 681 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 682 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
683 | unsigned long consumed_cur, write_offset; |
684 | int finalized; | |
685 | ||
14641deb | 686 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
687 | /* |
688 | * Read finalized before counters. | |
689 | */ | |
a6352fd4 MD |
690 | cmm_smp_rmb(); |
691 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 MD |
692 | /* |
693 | * No need to issue a memory barrier between consumed count read and | |
694 | * write offset read, because consumed count can only change | |
695 | * concurrently in overwrite mode, and we keep a sequence counter | |
696 | * identifier derived from the write offset to check we are getting | |
697 | * the same sub-buffer we are expecting (the sub-buffers are atomically | |
698 | * "tagged" upon writes, tags are checked upon read). | |
699 | */ | |
700 | write_offset = v_read(config, &buf->offset); | |
701 | ||
702 | /* | |
703 | * Check that we are not about to read the same subbuffer in | |
704 | * which the writer head is. | |
705 | */ | |
706 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
707 | == 0) | |
708 | goto nodata; | |
709 | ||
710 | *consumed = consumed_cur; | |
711 | *produced = subbuf_trunc(write_offset, chan); | |
712 | ||
713 | return 0; | |
714 | ||
715 | nodata: | |
716 | /* | |
717 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
718 | * of "raw_spin_is_locked" memory ordering. | |
719 | */ | |
720 | if (finalized) | |
721 | return -ENODATA; | |
852c2936 MD |
722 | else |
723 | return -EAGAIN; | |
724 | } | |
852c2936 MD |
725 | |
726 | /** | |
727 | * lib_ring_buffer_put_snapshot - move consumed counter forward | |
728 | * @buf: ring buffer | |
729 | * @consumed_new: new consumed count value | |
730 | */ | |
4cfec15c | 731 | void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 732 | unsigned long consumed_new, |
38fae1d3 | 733 | struct lttng_ust_shm_handle *handle) |
852c2936 | 734 | { |
4cfec15c | 735 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 736 | struct channel *chan = shmp(handle, bufb->chan); |
852c2936 MD |
737 | unsigned long consumed; |
738 | ||
824f40b8 MD |
739 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
740 | && uatomic_read(&buf->active_shadow_readers) != 1); | |
852c2936 MD |
741 | |
742 | /* | |
743 | * Only push the consumed value forward. | |
744 | * If the consumed cmpxchg fails, this is because we have been pushed by | |
745 | * the writer in flight recorder mode. | |
746 | */ | |
a6352fd4 | 747 | consumed = uatomic_read(&buf->consumed); |
852c2936 | 748 | while ((long) consumed - (long) consumed_new < 0) |
a6352fd4 MD |
749 | consumed = uatomic_cmpxchg(&buf->consumed, consumed, |
750 | consumed_new); | |
852c2936 | 751 | } |
852c2936 MD |
752 | |
753 | /** | |
754 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading | |
755 | * @buf: ring buffer | |
756 | * @consumed: consumed count indicating the position where to read | |
757 | * | |
758 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
759 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 | 760 | */ |
4cfec15c | 761 | int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 762 | unsigned long consumed, |
38fae1d3 | 763 | struct lttng_ust_shm_handle *handle) |
852c2936 | 764 | { |
1d498196 | 765 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 766 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
767 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
768 | int ret; | |
769 | int finalized; | |
770 | ||
771 | retry: | |
14641deb | 772 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
773 | /* |
774 | * Read finalized before counters. | |
775 | */ | |
a6352fd4 MD |
776 | cmm_smp_rmb(); |
777 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 | 778 | consumed_idx = subbuf_index(consumed, chan); |
4746ae29 | 779 | commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb); |
852c2936 MD |
780 | /* |
781 | * Make sure we read the commit count before reading the buffer | |
782 | * data and the write offset. Correct consumed offset ordering | |
783 | * wrt commit count is insured by the use of cmpxchg to update | |
784 | * the consumed offset. | |
852c2936 | 785 | */ |
a6352fd4 MD |
786 | /* |
787 | * Local rmb to match the remote wmb to read the commit count | |
788 | * before the buffer data and the write offset. | |
789 | */ | |
790 | cmm_smp_rmb(); | |
852c2936 MD |
791 | |
792 | write_offset = v_read(config, &buf->offset); | |
793 | ||
794 | /* | |
795 | * Check that the buffer we are getting is after or at consumed_cur | |
796 | * position. | |
797 | */ | |
798 | if ((long) subbuf_trunc(consumed, chan) | |
799 | - (long) subbuf_trunc(consumed_cur, chan) < 0) | |
800 | goto nodata; | |
801 | ||
802 | /* | |
803 | * Check that the subbuffer we are trying to consume has been | |
804 | * already fully committed. | |
805 | */ | |
806 | if (((commit_count - chan->backend.subbuf_size) | |
807 | & chan->commit_count_mask) | |
808 | - (buf_trunc(consumed_cur, chan) | |
809 | >> chan->backend.num_subbuf_order) | |
810 | != 0) | |
811 | goto nodata; | |
812 | ||
813 | /* | |
814 | * Check that we are not about to read the same subbuffer in | |
815 | * which the writer head is. | |
816 | */ | |
817 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
818 | == 0) | |
819 | goto nodata; | |
820 | ||
821 | /* | |
822 | * Failure to get the subbuffer causes a busy-loop retry without going | |
823 | * to a wait queue. These are caused by short-lived race windows where | |
824 | * the writer is getting access to a subbuffer we were trying to get | |
825 | * access to. Also checks that the "consumed" buffer count we are | |
826 | * looking for matches the one contained in the subbuffer id. | |
827 | */ | |
828 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
829 | consumed_idx, buf_trunc_val(consumed, chan), |
830 | handle); | |
852c2936 MD |
831 | if (ret) |
832 | goto retry; | |
833 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); | |
834 | ||
835 | buf->get_subbuf_consumed = consumed; | |
836 | buf->get_subbuf = 1; | |
837 | ||
838 | return 0; | |
839 | ||
840 | nodata: | |
841 | /* | |
842 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
843 | * of "raw_spin_is_locked" memory ordering. | |
844 | */ | |
845 | if (finalized) | |
846 | return -ENODATA; | |
852c2936 MD |
847 | else |
848 | return -EAGAIN; | |
849 | } | |
852c2936 MD |
850 | |
851 | /** | |
852 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access | |
853 | * @buf: ring buffer | |
854 | */ | |
4cfec15c | 855 | void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 856 | struct lttng_ust_shm_handle *handle) |
852c2936 | 857 | { |
4cfec15c | 858 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 859 | struct channel *chan = shmp(handle, bufb->chan); |
4cfec15c | 860 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
861 | unsigned long read_sb_bindex, consumed_idx, consumed; |
862 | ||
824f40b8 MD |
863 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
864 | && uatomic_read(&buf->active_shadow_readers) != 1); | |
852c2936 MD |
865 | |
866 | if (!buf->get_subbuf) { | |
867 | /* | |
868 | * Reader puts a subbuffer it did not get. | |
869 | */ | |
870 | CHAN_WARN_ON(chan, 1); | |
871 | return; | |
872 | } | |
873 | consumed = buf->get_subbuf_consumed; | |
874 | buf->get_subbuf = 0; | |
875 | ||
876 | /* | |
877 | * Clear the records_unread counter. (overruns counter) | |
878 | * Can still be non-zero if a file reader simply grabbed the data | |
879 | * without using iterators. | |
880 | * Can be below zero if an iterator is used on a snapshot more than | |
881 | * once. | |
882 | */ | |
883 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
884 | v_add(config, v_read(config, | |
4746ae29 | 885 | &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread), |
852c2936 | 886 | &bufb->records_read); |
4746ae29 | 887 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0); |
852c2936 MD |
888 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
889 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
890 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); | |
891 | ||
892 | /* | |
893 | * Exchange the reader subbuffer with the one we put in its place in the | |
894 | * writer subbuffer table. Expect the original consumed count. If | |
895 | * update_read_sb_index fails, this is because the writer updated the | |
896 | * subbuffer concurrently. We should therefore keep the subbuffer we | |
897 | * currently have: it has become invalid to try reading this sub-buffer | |
898 | * consumed count value anyway. | |
899 | */ | |
900 | consumed_idx = subbuf_index(consumed, chan); | |
901 | update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
902 | consumed_idx, buf_trunc_val(consumed, chan), |
903 | handle); | |
852c2936 MD |
904 | /* |
905 | * update_read_sb_index return value ignored. Don't exchange sub-buffer | |
906 | * if the writer concurrently updated it. | |
907 | */ | |
908 | } | |
852c2936 MD |
909 | |
910 | /* | |
911 | * cons_offset is an iterator on all subbuffer offsets between the reader | |
912 | * position and the writer position. (inclusive) | |
913 | */ | |
914 | static | |
4cfec15c | 915 | void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
916 | struct channel *chan, |
917 | unsigned long cons_offset, | |
1d498196 | 918 | int cpu, |
38fae1d3 | 919 | struct lttng_ust_shm_handle *handle) |
852c2936 | 920 | { |
4cfec15c | 921 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
922 | unsigned long cons_idx, commit_count, commit_count_sb; |
923 | ||
924 | cons_idx = subbuf_index(cons_offset, chan); | |
4746ae29 MD |
925 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc); |
926 | commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb); | |
852c2936 MD |
927 | |
928 | if (subbuf_offset(commit_count, chan) != 0) | |
4d3c9523 | 929 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
930 | "commit count in subbuffer %lu,\n" |
931 | "expecting multiples of %lu bytes\n" | |
932 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", | |
933 | chan->backend.name, cpu, cons_idx, | |
934 | chan->backend.subbuf_size, | |
935 | commit_count, commit_count_sb); | |
936 | ||
4d3c9523 | 937 | DBG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
852c2936 MD |
938 | chan->backend.name, cpu, commit_count); |
939 | } | |
940 | ||
941 | static | |
4cfec15c | 942 | void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 943 | struct channel *chan, |
1d498196 | 944 | void *priv, int cpu, |
38fae1d3 | 945 | struct lttng_ust_shm_handle *handle) |
852c2936 | 946 | { |
4cfec15c | 947 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
948 | unsigned long write_offset, cons_offset; |
949 | ||
852c2936 MD |
950 | /* |
951 | * No need to order commit_count, write_offset and cons_offset reads | |
952 | * because we execute at teardown when no more writer nor reader | |
953 | * references are left. | |
954 | */ | |
955 | write_offset = v_read(config, &buf->offset); | |
a6352fd4 | 956 | cons_offset = uatomic_read(&buf->consumed); |
852c2936 | 957 | if (write_offset != cons_offset) |
4d3c9523 | 958 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
959 | "non-consumed data\n" |
960 | " [ %lu bytes written, %lu bytes read ]\n", | |
961 | chan->backend.name, cpu, write_offset, cons_offset); | |
962 | ||
a6352fd4 | 963 | for (cons_offset = uatomic_read(&buf->consumed); |
852c2936 MD |
964 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
965 | chan) | |
966 | - cons_offset) > 0; | |
967 | cons_offset = subbuf_align(cons_offset, chan)) | |
968 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, | |
1d498196 | 969 | cpu, handle); |
852c2936 MD |
970 | } |
971 | ||
972 | static | |
973 | void lib_ring_buffer_print_errors(struct channel *chan, | |
4cfec15c | 974 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
38fae1d3 | 975 | struct lttng_ust_shm_handle *handle) |
852c2936 | 976 | { |
4cfec15c | 977 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
a3f61e7f | 978 | void *priv = channel_get_private(chan); |
852c2936 | 979 | |
4d3c9523 | 980 | DBG("ring buffer %s, cpu %d: %lu records written, " |
852c2936 MD |
981 | "%lu records overrun\n", |
982 | chan->backend.name, cpu, | |
983 | v_read(config, &buf->records_count), | |
984 | v_read(config, &buf->records_overrun)); | |
985 | ||
986 | if (v_read(config, &buf->records_lost_full) | |
987 | || v_read(config, &buf->records_lost_wrap) | |
988 | || v_read(config, &buf->records_lost_big)) | |
4d3c9523 | 989 | DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n" |
852c2936 MD |
990 | " [ %lu buffer full, %lu nest buffer wrap-around, " |
991 | "%lu event too big ]\n", | |
992 | chan->backend.name, cpu, | |
993 | v_read(config, &buf->records_lost_full), | |
994 | v_read(config, &buf->records_lost_wrap), | |
995 | v_read(config, &buf->records_lost_big)); | |
996 | ||
1d498196 | 997 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle); |
852c2936 MD |
998 | } |
999 | ||
1000 | /* | |
1001 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. | |
1002 | * | |
1003 | * Only executed when the buffer is finalized, in SWITCH_FLUSH. | |
1004 | */ | |
1005 | static | |
4cfec15c | 1006 | void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1007 | struct channel *chan, |
1008 | struct switch_offsets *offsets, | |
2fed87ae | 1009 | uint64_t tsc, |
38fae1d3 | 1010 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1011 | { |
4cfec15c | 1012 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1013 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
1014 | unsigned long commit_count; | |
1015 | ||
1d498196 | 1016 | config->cb.buffer_begin(buf, tsc, oldidx, handle); |
852c2936 MD |
1017 | |
1018 | /* | |
1019 | * Order all writes to buffer before the commit count update that will | |
1020 | * determine that the subbuffer is full. | |
1021 | */ | |
a6352fd4 | 1022 | cmm_smp_wmb(); |
852c2936 | 1023 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1024 | &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1025 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 MD |
1026 | /* Check if the written buffer has to be delivered */ |
1027 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, | |
1d498196 | 1028 | commit_count, oldidx, handle); |
852c2936 MD |
1029 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1030 | offsets->old, commit_count, | |
1d498196 MD |
1031 | config->cb.subbuffer_header_size(), |
1032 | handle); | |
852c2936 MD |
1033 | } |
1034 | ||
1035 | /* | |
1036 | * lib_ring_buffer_switch_old_end: switch old subbuffer | |
1037 | * | |
1038 | * Note : offset_old should never be 0 here. It is ok, because we never perform | |
1039 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller | |
1040 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty | |
1041 | * subbuffer. | |
1042 | */ | |
1043 | static | |
4cfec15c | 1044 | void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1045 | struct channel *chan, |
1046 | struct switch_offsets *offsets, | |
2fed87ae | 1047 | uint64_t tsc, |
38fae1d3 | 1048 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1049 | { |
4cfec15c | 1050 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1051 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
1052 | unsigned long commit_count, padding_size, data_size; | |
1053 | ||
1054 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; | |
1055 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1056 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size, |
1057 | handle); | |
852c2936 MD |
1058 | |
1059 | /* | |
1060 | * Order all writes to buffer before the commit count update that will | |
1061 | * determine that the subbuffer is full. | |
1062 | */ | |
a6352fd4 | 1063 | cmm_smp_wmb(); |
4746ae29 MD |
1064 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1065 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 | 1066 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
1d498196 | 1067 | commit_count, oldidx, handle); |
852c2936 MD |
1068 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1069 | offsets->old, commit_count, | |
1d498196 | 1070 | padding_size, handle); |
852c2936 MD |
1071 | } |
1072 | ||
1073 | /* | |
1074 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. | |
1075 | * | |
1076 | * This code can be executed unordered : writers may already have written to the | |
1077 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
1078 | * that this code is executed before the deliver of this sub-buffer. | |
1079 | */ | |
1080 | static | |
4cfec15c | 1081 | void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1082 | struct channel *chan, |
1083 | struct switch_offsets *offsets, | |
2fed87ae | 1084 | uint64_t tsc, |
38fae1d3 | 1085 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1086 | { |
4cfec15c | 1087 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1088 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
1089 | unsigned long commit_count; | |
1090 | ||
1d498196 | 1091 | config->cb.buffer_begin(buf, tsc, beginidx, handle); |
852c2936 MD |
1092 | |
1093 | /* | |
1094 | * Order all writes to buffer before the commit count update that will | |
1095 | * determine that the subbuffer is full. | |
1096 | */ | |
a6352fd4 | 1097 | cmm_smp_wmb(); |
852c2936 | 1098 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1099 | &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
1100 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc); | |
852c2936 MD |
1101 | /* Check if the written buffer has to be delivered */ |
1102 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, | |
1d498196 | 1103 | commit_count, beginidx, handle); |
852c2936 MD |
1104 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
1105 | offsets->begin, commit_count, | |
1d498196 MD |
1106 | config->cb.subbuffer_header_size(), |
1107 | handle); | |
852c2936 MD |
1108 | } |
1109 | ||
1110 | /* | |
1111 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer | |
1112 | * | |
1113 | * The only remaining threads could be the ones with pending commits. They will | |
1114 | * have to do the deliver themselves. | |
1115 | */ | |
1116 | static | |
4cfec15c | 1117 | void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 MD |
1118 | struct channel *chan, |
1119 | struct switch_offsets *offsets, | |
2fed87ae | 1120 | uint64_t tsc, |
38fae1d3 | 1121 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1122 | { |
4cfec15c | 1123 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1124 | unsigned long endidx = subbuf_index(offsets->end - 1, chan); |
1125 | unsigned long commit_count, padding_size, data_size; | |
1126 | ||
1127 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; | |
1128 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1129 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size, |
1130 | handle); | |
852c2936 MD |
1131 | |
1132 | /* | |
1133 | * Order all writes to buffer before the commit count update that will | |
1134 | * determine that the subbuffer is full. | |
1135 | */ | |
a6352fd4 | 1136 | cmm_smp_wmb(); |
4746ae29 MD |
1137 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
1138 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc); | |
852c2936 | 1139 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1, |
1d498196 | 1140 | commit_count, endidx, handle); |
852c2936 MD |
1141 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, |
1142 | offsets->end, commit_count, | |
1d498196 | 1143 | padding_size, handle); |
852c2936 MD |
1144 | } |
1145 | ||
1146 | /* | |
1147 | * Returns : | |
1148 | * 0 if ok | |
1149 | * !0 if execution must be aborted. | |
1150 | */ | |
1151 | static | |
1152 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, | |
4cfec15c | 1153 | struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1154 | struct channel *chan, |
1155 | struct switch_offsets *offsets, | |
2fed87ae | 1156 | uint64_t *tsc) |
852c2936 | 1157 | { |
4cfec15c | 1158 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1159 | unsigned long off; |
1160 | ||
1161 | offsets->begin = v_read(config, &buf->offset); | |
1162 | offsets->old = offsets->begin; | |
1163 | offsets->switch_old_start = 0; | |
1164 | off = subbuf_offset(offsets->begin, chan); | |
1165 | ||
1166 | *tsc = config->cb.ring_buffer_clock_read(chan); | |
1167 | ||
1168 | /* | |
1169 | * Ensure we flush the header of an empty subbuffer when doing the | |
1170 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the | |
1171 | * total data gathering duration even if there were no records saved | |
1172 | * after the last buffer switch. | |
1173 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. | |
1174 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of | |
1175 | * subbuffer header as appropriate. | |
1176 | * The next record that reserves space will be responsible for | |
1177 | * populating the following subbuffer header. We choose not to populate | |
1178 | * the next subbuffer header here because we want to be able to use | |
a6352fd4 MD |
1179 | * SWITCH_ACTIVE for periodical buffer flush, which must |
1180 | * guarantee that all the buffer content (records and header | |
1181 | * timestamps) are visible to the reader. This is required for | |
1182 | * quiescence guarantees for the fusion merge. | |
852c2936 MD |
1183 | */ |
1184 | if (mode == SWITCH_FLUSH || off > 0) { | |
b5a3dfa5 | 1185 | if (caa_unlikely(off == 0)) { |
852c2936 MD |
1186 | /* |
1187 | * The client does not save any header information. | |
1188 | * Don't switch empty subbuffer on finalize, because it | |
1189 | * is invalid to deliver a completely empty subbuffer. | |
1190 | */ | |
1191 | if (!config->cb.subbuffer_header_size()) | |
1192 | return -1; | |
1193 | /* | |
1194 | * Need to write the subbuffer start header on finalize. | |
1195 | */ | |
1196 | offsets->switch_old_start = 1; | |
1197 | } | |
1198 | offsets->begin = subbuf_align(offsets->begin, chan); | |
1199 | } else | |
1200 | return -1; /* we do not have to switch : buffer is empty */ | |
1201 | /* Note: old points to the next subbuf at offset 0 */ | |
1202 | offsets->end = offsets->begin; | |
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | /* | |
1207 | * Force a sub-buffer switch. This operation is completely reentrant : can be | |
1208 | * called while tracing is active with absolutely no lock held. | |
1209 | * | |
1210 | * Note, however, that as a v_cmpxchg is used for some atomic | |
1211 | * operations, this function must be called from the CPU which owns the buffer | |
1212 | * for a ACTIVE flush. | |
1213 | */ | |
4cfec15c | 1214 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, |
38fae1d3 | 1215 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1216 | { |
1d498196 | 1217 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 1218 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1219 | struct switch_offsets offsets; |
1220 | unsigned long oldidx; | |
2fed87ae | 1221 | uint64_t tsc; |
852c2936 MD |
1222 | |
1223 | offsets.size = 0; | |
1224 | ||
1225 | /* | |
1226 | * Perform retryable operations. | |
1227 | */ | |
1228 | do { | |
1229 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, | |
1230 | &tsc)) | |
1231 | return; /* Switch not needed */ | |
1232 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) | |
1233 | != offsets.old); | |
1234 | ||
1235 | /* | |
1236 | * Atomically update last_tsc. This update races against concurrent | |
1237 | * atomic updates, but the race will always cause supplementary full TSC | |
1238 | * records, never the opposite (missing a full TSC record when it would | |
1239 | * be needed). | |
1240 | */ | |
1241 | save_last_tsc(config, buf, tsc); | |
1242 | ||
1243 | /* | |
1244 | * Push the reader if necessary | |
1245 | */ | |
1246 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); | |
1247 | ||
1248 | oldidx = subbuf_index(offsets.old, chan); | |
1d498196 | 1249 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
852c2936 MD |
1250 | |
1251 | /* | |
1252 | * May need to populate header start on SWITCH_FLUSH. | |
1253 | */ | |
1254 | if (offsets.switch_old_start) { | |
1d498196 | 1255 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
852c2936 MD |
1256 | offsets.old += config->cb.subbuffer_header_size(); |
1257 | } | |
1258 | ||
1259 | /* | |
1260 | * Switch old subbuffer. | |
1261 | */ | |
1d498196 | 1262 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
852c2936 | 1263 | } |
852c2936 MD |
1264 | |
1265 | /* | |
1266 | * Returns : | |
1267 | * 0 if ok | |
1268 | * -ENOSPC if event size is too large for packet. | |
1269 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
1270 | * -EIO if data cannot be written into the buffer for any other reason. | |
1271 | */ | |
1272 | static | |
4cfec15c | 1273 | int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1274 | struct channel *chan, |
1275 | struct switch_offsets *offsets, | |
4cfec15c | 1276 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 | 1277 | { |
4cfec15c | 1278 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
38fae1d3 | 1279 | struct lttng_ust_shm_handle *handle = ctx->handle; |
852c2936 MD |
1280 | unsigned long reserve_commit_diff; |
1281 | ||
1282 | offsets->begin = v_read(config, &buf->offset); | |
1283 | offsets->old = offsets->begin; | |
1284 | offsets->switch_new_start = 0; | |
1285 | offsets->switch_new_end = 0; | |
1286 | offsets->switch_old_end = 0; | |
1287 | offsets->pre_header_padding = 0; | |
1288 | ||
1289 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); | |
1290 | if ((int64_t) ctx->tsc == -EIO) | |
1291 | return -EIO; | |
1292 | ||
1293 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
1294 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; | |
1295 | ||
b5a3dfa5 | 1296 | if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
852c2936 MD |
1297 | offsets->switch_new_start = 1; /* For offsets->begin */ |
1298 | } else { | |
1299 | offsets->size = config->cb.record_header_size(config, chan, | |
1300 | offsets->begin, | |
1301 | &offsets->pre_header_padding, | |
1302 | ctx); | |
1303 | offsets->size += | |
1304 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1305 | ctx->largest_align) | |
1306 | + ctx->data_size; | |
b5a3dfa5 | 1307 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) + |
852c2936 MD |
1308 | offsets->size > chan->backend.subbuf_size)) { |
1309 | offsets->switch_old_end = 1; /* For offsets->old */ | |
1310 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1311 | } | |
1312 | } | |
b5a3dfa5 | 1313 | if (caa_unlikely(offsets->switch_new_start)) { |
852c2936 MD |
1314 | unsigned long sb_index; |
1315 | ||
1316 | /* | |
1317 | * We are typically not filling the previous buffer completely. | |
1318 | */ | |
b5a3dfa5 | 1319 | if (caa_likely(offsets->switch_old_end)) |
852c2936 MD |
1320 | offsets->begin = subbuf_align(offsets->begin, chan); |
1321 | offsets->begin = offsets->begin | |
1322 | + config->cb.subbuffer_header_size(); | |
1323 | /* Test new buffer integrity */ | |
1324 | sb_index = subbuf_index(offsets->begin, chan); | |
1325 | reserve_commit_diff = | |
1326 | (buf_trunc(offsets->begin, chan) | |
1327 | >> chan->backend.num_subbuf_order) | |
1328 | - ((unsigned long) v_read(config, | |
4746ae29 | 1329 | &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb) |
852c2936 | 1330 | & chan->commit_count_mask); |
b5a3dfa5 | 1331 | if (caa_likely(reserve_commit_diff == 0)) { |
852c2936 | 1332 | /* Next subbuffer not being written to. */ |
b5a3dfa5 | 1333 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
852c2936 MD |
1334 | subbuf_trunc(offsets->begin, chan) |
1335 | - subbuf_trunc((unsigned long) | |
a6352fd4 | 1336 | uatomic_read(&buf->consumed), chan) |
852c2936 MD |
1337 | >= chan->backend.buf_size)) { |
1338 | /* | |
1339 | * We do not overwrite non consumed buffers | |
1340 | * and we are full : record is lost. | |
1341 | */ | |
1342 | v_inc(config, &buf->records_lost_full); | |
1343 | return -ENOBUFS; | |
1344 | } else { | |
1345 | /* | |
1346 | * Next subbuffer not being written to, and we | |
1347 | * are either in overwrite mode or the buffer is | |
1348 | * not full. It's safe to write in this new | |
1349 | * subbuffer. | |
1350 | */ | |
1351 | } | |
1352 | } else { | |
1353 | /* | |
1354 | * Next subbuffer reserve offset does not match the | |
1355 | * commit offset. Drop record in producer-consumer and | |
1356 | * overwrite mode. Caused by either a writer OOPS or too | |
1357 | * many nested writes over a reserve/commit pair. | |
1358 | */ | |
1359 | v_inc(config, &buf->records_lost_wrap); | |
1360 | return -EIO; | |
1361 | } | |
1362 | offsets->size = | |
1363 | config->cb.record_header_size(config, chan, | |
1364 | offsets->begin, | |
1365 | &offsets->pre_header_padding, | |
1366 | ctx); | |
1367 | offsets->size += | |
1368 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1369 | ctx->largest_align) | |
1370 | + ctx->data_size; | |
b5a3dfa5 | 1371 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) |
852c2936 MD |
1372 | + offsets->size > chan->backend.subbuf_size)) { |
1373 | /* | |
1374 | * Record too big for subbuffers, report error, don't | |
1375 | * complete the sub-buffer switch. | |
1376 | */ | |
1377 | v_inc(config, &buf->records_lost_big); | |
1378 | return -ENOSPC; | |
1379 | } else { | |
1380 | /* | |
1381 | * We just made a successful buffer switch and the | |
1382 | * record fits in the new subbuffer. Let's write. | |
1383 | */ | |
1384 | } | |
1385 | } else { | |
1386 | /* | |
1387 | * Record fits in the current buffer and we are not on a switch | |
1388 | * boundary. It's safe to write. | |
1389 | */ | |
1390 | } | |
1391 | offsets->end = offsets->begin + offsets->size; | |
1392 | ||
b5a3dfa5 | 1393 | if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
852c2936 MD |
1394 | /* |
1395 | * The offset_end will fall at the very beginning of the next | |
1396 | * subbuffer. | |
1397 | */ | |
1398 | offsets->switch_new_end = 1; /* For offsets->begin */ | |
1399 | } | |
1400 | return 0; | |
1401 | } | |
1402 | ||
1403 | /** | |
1404 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. | |
1405 | * @ctx: ring buffer context. | |
1406 | * | |
1407 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, | |
1408 | * -EIO for other errors, else returns 0. | |
1409 | * It will take care of sub-buffer switching. | |
1410 | */ | |
4cfec15c | 1411 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 MD |
1412 | { |
1413 | struct channel *chan = ctx->chan; | |
38fae1d3 | 1414 | struct lttng_ust_shm_handle *handle = ctx->handle; |
4cfec15c MD |
1415 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
1416 | struct lttng_ust_lib_ring_buffer *buf; | |
852c2936 MD |
1417 | struct switch_offsets offsets; |
1418 | int ret; | |
1419 | ||
1420 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1d498196 | 1421 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
852c2936 | 1422 | else |
1d498196 | 1423 | buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 MD |
1424 | ctx->buf = buf; |
1425 | ||
1426 | offsets.size = 0; | |
1427 | ||
1428 | do { | |
1429 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, | |
1430 | ctx); | |
b5a3dfa5 | 1431 | if (caa_unlikely(ret)) |
852c2936 | 1432 | return ret; |
b5a3dfa5 | 1433 | } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
852c2936 MD |
1434 | offsets.end) |
1435 | != offsets.old)); | |
1436 | ||
1437 | /* | |
1438 | * Atomically update last_tsc. This update races against concurrent | |
1439 | * atomic updates, but the race will always cause supplementary full TSC | |
1440 | * records, never the opposite (missing a full TSC record when it would | |
1441 | * be needed). | |
1442 | */ | |
1443 | save_last_tsc(config, buf, ctx->tsc); | |
1444 | ||
1445 | /* | |
1446 | * Push the reader if necessary | |
1447 | */ | |
1448 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); | |
1449 | ||
1450 | /* | |
1451 | * Clear noref flag for this subbuffer. | |
1452 | */ | |
1453 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1d498196 MD |
1454 | subbuf_index(offsets.end - 1, chan), |
1455 | handle); | |
852c2936 MD |
1456 | |
1457 | /* | |
1458 | * Switch old subbuffer if needed. | |
1459 | */ | |
b5a3dfa5 | 1460 | if (caa_unlikely(offsets.switch_old_end)) { |
852c2936 | 1461 | lib_ring_buffer_clear_noref(config, &buf->backend, |
1d498196 MD |
1462 | subbuf_index(offsets.old - 1, chan), |
1463 | handle); | |
1464 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); | |
852c2936 MD |
1465 | } |
1466 | ||
1467 | /* | |
1468 | * Populate new subbuffer. | |
1469 | */ | |
b5a3dfa5 | 1470 | if (caa_unlikely(offsets.switch_new_start)) |
1d498196 | 1471 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 | 1472 | |
b5a3dfa5 | 1473 | if (caa_unlikely(offsets.switch_new_end)) |
1d498196 | 1474 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 MD |
1475 | |
1476 | ctx->slot_size = offsets.size; | |
1477 | ctx->pre_offset = offsets.begin; | |
1478 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; | |
1479 | return 0; | |
1480 | } |