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