Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | /* |
2 | * ring_buffer_frontend.c | |
3 | * | |
e92f3e28 MD |
4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; only | |
9 | * version 2.1 of the License. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | * | |
852c2936 MD |
20 | * |
21 | * Ring buffer wait-free buffer synchronization. Producer-consumer and flight | |
22 | * recorder (overwrite) modes. See thesis: | |
23 | * | |
24 | * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D. | |
25 | * dissertation, Ecole Polytechnique de Montreal. | |
26 | * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf | |
27 | * | |
28 | * - Algorithm presentation in Chapter 5: | |
29 | * "Lockless Multi-Core High-Throughput Buffering". | |
30 | * - Algorithm formal verification in Section 8.6: | |
31 | * "Formal verification of LTTng" | |
32 | * | |
33 | * Author: | |
34 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
35 | * | |
36 | * Inspired from LTT and RelayFS: | |
37 | * Karim Yaghmour <karim@opersys.com> | |
38 | * Tom Zanussi <zanussi@us.ibm.com> | |
39 | * Bob Wisniewski <bob@watson.ibm.com> | |
40 | * And from K42 : | |
41 | * Bob Wisniewski <bob@watson.ibm.com> | |
42 | * | |
43 | * Buffer reader semantic : | |
44 | * | |
45 | * - get_subbuf_size | |
46 | * while buffer is not finalized and empty | |
47 | * - get_subbuf | |
48 | * - if return value != 0, continue | |
49 | * - splice one subbuffer worth of data to a pipe | |
50 | * - splice the data from pipe to disk/network | |
51 | * - put_subbuf | |
852c2936 MD |
52 | */ |
53 | ||
5ad63a16 | 54 | #define _GNU_SOURCE |
a6352fd4 | 55 | #include <sys/types.h> |
431d5cf0 MD |
56 | #include <sys/mman.h> |
57 | #include <sys/stat.h> | |
58 | #include <fcntl.h> | |
14641deb | 59 | #include <urcu/compiler.h> |
a6352fd4 | 60 | #include <urcu/ref.h> |
8c90a710 | 61 | #include <urcu/tls-compat.h> |
35897f8b | 62 | #include <helper.h> |
14641deb | 63 | |
a6352fd4 | 64 | #include "smp.h" |
4318ae1b | 65 | #include <lttng/ringbuffer-config.h> |
2fed87ae | 66 | #include "vatomic.h" |
4931a13e MD |
67 | #include "backend.h" |
68 | #include "frontend.h" | |
a6352fd4 | 69 | #include "shm.h" |
f645cfa7 | 70 | #include "tlsfixup.h" |
bdcf8d82 | 71 | #include "../liblttng-ust/compat.h" /* For ENODATA */ |
852c2936 | 72 | |
431d5cf0 MD |
73 | #ifndef max |
74 | #define max(a, b) ((a) > (b) ? (a) : (b)) | |
75 | #endif | |
76 | ||
64493e4f MD |
77 | /* Print DBG() messages about events lost only every 1048576 hits */ |
78 | #define DBG_PRINT_NR_LOST (1UL << 20) | |
79 | ||
2432c3c9 MD |
80 | /* |
81 | * Use POSIX SHM: shm_open(3) and shm_unlink(3). | |
82 | * close(2) to close the fd returned by shm_open. | |
83 | * shm_unlink releases the shared memory object name. | |
84 | * ftruncate(2) sets the size of the memory object. | |
85 | * mmap/munmap maps the shared memory obj to a virtual address in the | |
86 | * calling proceess (should be done both in libust and consumer). | |
87 | * See shm_overview(7) for details. | |
88 | * Pass file descriptor returned by shm_open(3) to ltt-sessiond through | |
89 | * a UNIX socket. | |
90 | * | |
91 | * Since we don't need to access the object using its name, we can | |
92 | * immediately shm_unlink(3) it, and only keep the handle with its file | |
93 | * descriptor. | |
94 | */ | |
95 | ||
852c2936 MD |
96 | /* |
97 | * Internal structure representing offsets to use at a sub-buffer switch. | |
98 | */ | |
99 | struct switch_offsets { | |
100 | unsigned long begin, end, old; | |
101 | size_t pre_header_padding, size; | |
102 | unsigned int switch_new_start:1, switch_new_end:1, switch_old_start:1, | |
103 | switch_old_end:1; | |
104 | }; | |
105 | ||
8c90a710 | 106 | DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting); |
852c2936 | 107 | |
45e9e699 MD |
108 | /* |
109 | * TODO: this is unused. Errors are saved within the ring buffer. | |
110 | * Eventually, allow consumerd to print these errors. | |
111 | */ | |
852c2936 MD |
112 | static |
113 | void lib_ring_buffer_print_errors(struct channel *chan, | |
4cfec15c | 114 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
b68d3dc0 MD |
115 | struct lttng_ust_shm_handle *handle) |
116 | __attribute__((unused)); | |
852c2936 | 117 | |
852c2936 MD |
118 | /** |
119 | * lib_ring_buffer_reset - Reset ring buffer to initial values. | |
120 | * @buf: Ring buffer. | |
121 | * | |
122 | * Effectively empty the ring buffer. Should be called when the buffer is not | |
123 | * used for writing. The ring buffer can be opened for reading, but the reader | |
124 | * should not be using the iterator concurrently with reset. The previous | |
125 | * current iterator record is reset. | |
126 | */ | |
4cfec15c | 127 | void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 128 | struct lttng_ust_shm_handle *handle) |
852c2936 | 129 | { |
1d498196 | 130 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 131 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
132 | unsigned int i; |
133 | ||
134 | /* | |
135 | * Reset iterator first. It will put the subbuffer if it currently holds | |
136 | * it. | |
137 | */ | |
852c2936 MD |
138 | v_set(config, &buf->offset, 0); |
139 | for (i = 0; i < chan->backend.num_subbuf; i++) { | |
4746ae29 MD |
140 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->cc, 0); |
141 | v_set(config, &shmp_index(handle, buf->commit_hot, i)->seq, 0); | |
142 | v_set(config, &shmp_index(handle, buf->commit_cold, i)->cc_sb, 0); | |
852c2936 | 143 | } |
a6352fd4 MD |
144 | uatomic_set(&buf->consumed, 0); |
145 | uatomic_set(&buf->record_disabled, 0); | |
852c2936 | 146 | v_set(config, &buf->last_tsc, 0); |
1d498196 | 147 | lib_ring_buffer_backend_reset(&buf->backend, handle); |
852c2936 MD |
148 | /* Don't reset number of active readers */ |
149 | v_set(config, &buf->records_lost_full, 0); | |
150 | v_set(config, &buf->records_lost_wrap, 0); | |
151 | v_set(config, &buf->records_lost_big, 0); | |
152 | v_set(config, &buf->records_count, 0); | |
153 | v_set(config, &buf->records_overrun, 0); | |
154 | buf->finalized = 0; | |
155 | } | |
852c2936 MD |
156 | |
157 | /** | |
158 | * channel_reset - Reset channel to initial values. | |
159 | * @chan: Channel. | |
160 | * | |
161 | * Effectively empty the channel. Should be called when the channel is not used | |
162 | * for writing. The channel can be opened for reading, but the reader should not | |
163 | * be using the iterator concurrently with reset. The previous current iterator | |
164 | * record is reset. | |
165 | */ | |
166 | void channel_reset(struct channel *chan) | |
167 | { | |
168 | /* | |
169 | * Reset iterators first. Will put the subbuffer if held for reading. | |
170 | */ | |
a6352fd4 | 171 | uatomic_set(&chan->record_disabled, 0); |
852c2936 MD |
172 | /* Don't reset commit_count_mask, still valid */ |
173 | channel_backend_reset(&chan->backend); | |
174 | /* Don't reset switch/read timer interval */ | |
175 | /* Don't reset notifiers and notifier enable bits */ | |
176 | /* Don't reset reader reference count */ | |
177 | } | |
852c2936 MD |
178 | |
179 | /* | |
180 | * Must be called under cpu hotplug protection. | |
181 | */ | |
4cfec15c | 182 | int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
a6352fd4 | 183 | struct channel_backend *chanb, int cpu, |
38fae1d3 | 184 | struct lttng_ust_shm_handle *handle, |
1d498196 | 185 | struct shm_object *shmobj) |
852c2936 | 186 | { |
4cfec15c | 187 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
14641deb | 188 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
a3f61e7f | 189 | void *priv = channel_get_private(chan); |
852c2936 | 190 | size_t subbuf_header_size; |
2fed87ae | 191 | uint64_t tsc; |
852c2936 MD |
192 | int ret; |
193 | ||
194 | /* Test for cpu hotplug */ | |
195 | if (buf->backend.allocated) | |
196 | return 0; | |
197 | ||
a6352fd4 | 198 | ret = lib_ring_buffer_backend_create(&buf->backend, &chan->backend, |
1d498196 | 199 | cpu, handle, shmobj); |
852c2936 MD |
200 | if (ret) |
201 | return ret; | |
202 | ||
1d498196 MD |
203 | align_shm(shmobj, __alignof__(struct commit_counters_hot)); |
204 | set_shmp(buf->commit_hot, | |
205 | zalloc_shm(shmobj, | |
206 | sizeof(struct commit_counters_hot) * chan->backend.num_subbuf)); | |
207 | if (!shmp(handle, buf->commit_hot)) { | |
852c2936 MD |
208 | ret = -ENOMEM; |
209 | goto free_chanbuf; | |
210 | } | |
211 | ||
1d498196 MD |
212 | align_shm(shmobj, __alignof__(struct commit_counters_cold)); |
213 | set_shmp(buf->commit_cold, | |
214 | zalloc_shm(shmobj, | |
215 | sizeof(struct commit_counters_cold) * chan->backend.num_subbuf)); | |
216 | if (!shmp(handle, buf->commit_cold)) { | |
852c2936 MD |
217 | ret = -ENOMEM; |
218 | goto free_commit; | |
219 | } | |
220 | ||
852c2936 MD |
221 | /* |
222 | * Write the subbuffer header for first subbuffer so we know the total | |
223 | * duration of data gathering. | |
224 | */ | |
225 | subbuf_header_size = config->cb.subbuffer_header_size(); | |
226 | v_set(config, &buf->offset, subbuf_header_size); | |
4746ae29 | 227 | subbuffer_id_clear_noref(config, &shmp_index(handle, buf->backend.buf_wsb, 0)->id); |
1d498196 MD |
228 | tsc = config->cb.ring_buffer_clock_read(shmp(handle, buf->backend.chan)); |
229 | config->cb.buffer_begin(buf, tsc, 0, handle); | |
4746ae29 | 230 | v_add(config, subbuf_header_size, &shmp_index(handle, buf->commit_hot, 0)->cc); |
852c2936 MD |
231 | |
232 | if (config->cb.buffer_create) { | |
1d498196 | 233 | ret = config->cb.buffer_create(buf, priv, cpu, chanb->name, handle); |
852c2936 MD |
234 | if (ret) |
235 | goto free_init; | |
236 | } | |
852c2936 | 237 | buf->backend.allocated = 1; |
852c2936 MD |
238 | return 0; |
239 | ||
240 | /* Error handling */ | |
241 | free_init: | |
a6352fd4 | 242 | /* commit_cold will be freed by shm teardown */ |
852c2936 | 243 | free_commit: |
a6352fd4 | 244 | /* commit_hot will be freed by shm teardown */ |
852c2936 | 245 | free_chanbuf: |
852c2936 MD |
246 | return ret; |
247 | } | |
248 | ||
1d498196 | 249 | #if 0 |
852c2936 MD |
250 | static void switch_buffer_timer(unsigned long data) |
251 | { | |
4cfec15c | 252 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
1d498196 | 253 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 254 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
255 | |
256 | /* | |
257 | * Only flush buffers periodically if readers are active. | |
258 | */ | |
74d81a6c | 259 | if (uatomic_read(&buf->active_readers)) |
1d498196 | 260 | lib_ring_buffer_switch_slow(buf, SWITCH_ACTIVE, handle); |
852c2936 | 261 | |
a6352fd4 MD |
262 | //TODO timers |
263 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
264 | // mod_timer_pinned(&buf->switch_timer, | |
265 | // jiffies + chan->switch_timer_interval); | |
266 | //else | |
267 | // mod_timer(&buf->switch_timer, | |
268 | // jiffies + chan->switch_timer_interval); | |
852c2936 | 269 | } |
1d498196 | 270 | #endif //0 |
852c2936 | 271 | |
4cfec15c | 272 | static void lib_ring_buffer_start_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); |
4cfec15c | 276 | //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
277 | |
278 | if (!chan->switch_timer_interval || buf->switch_timer_enabled) | |
279 | return; | |
a6352fd4 MD |
280 | //TODO |
281 | //init_timer(&buf->switch_timer); | |
282 | //buf->switch_timer.function = switch_buffer_timer; | |
283 | //buf->switch_timer.expires = jiffies + chan->switch_timer_interval; | |
284 | //buf->switch_timer.data = (unsigned long)buf; | |
285 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
286 | // add_timer_on(&buf->switch_timer, buf->backend.cpu); | |
287 | //else | |
288 | // add_timer(&buf->switch_timer); | |
852c2936 MD |
289 | buf->switch_timer_enabled = 1; |
290 | } | |
291 | ||
4cfec15c | 292 | static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 293 | struct lttng_ust_shm_handle *handle) |
852c2936 | 294 | { |
1d498196 | 295 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 MD |
296 | |
297 | if (!chan->switch_timer_interval || !buf->switch_timer_enabled) | |
298 | return; | |
299 | ||
a6352fd4 MD |
300 | //TODO |
301 | //del_timer_sync(&buf->switch_timer); | |
852c2936 MD |
302 | buf->switch_timer_enabled = 0; |
303 | } | |
304 | ||
1d498196 | 305 | #if 0 |
852c2936 MD |
306 | /* |
307 | * Polling timer to check the channels for data. | |
308 | */ | |
309 | static void read_buffer_timer(unsigned long data) | |
310 | { | |
4cfec15c | 311 | struct lttng_ust_lib_ring_buffer *buf = (struct lttng_ust_lib_ring_buffer *)data; |
1d498196 | 312 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 313 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
314 | |
315 | CHAN_WARN_ON(chan, !buf->backend.allocated); | |
316 | ||
74d81a6c | 317 | if (uatomic_read(&buf->active_readers)) |
852c2936 | 318 | && lib_ring_buffer_poll_deliver(config, buf, chan)) { |
a6352fd4 MD |
319 | //TODO |
320 | //wake_up_interruptible(&buf->read_wait); | |
321 | //wake_up_interruptible(&chan->read_wait); | |
852c2936 MD |
322 | } |
323 | ||
a6352fd4 MD |
324 | //TODO |
325 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
326 | // mod_timer_pinned(&buf->read_timer, | |
327 | // jiffies + chan->read_timer_interval); | |
328 | //else | |
329 | // mod_timer(&buf->read_timer, | |
330 | // jiffies + chan->read_timer_interval); | |
852c2936 | 331 | } |
1d498196 | 332 | #endif //0 |
852c2936 | 333 | |
4cfec15c | 334 | static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 335 | struct lttng_ust_shm_handle *handle) |
852c2936 | 336 | { |
1d498196 | 337 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 338 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
339 | |
340 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
341 | || !chan->read_timer_interval | |
342 | || buf->read_timer_enabled) | |
343 | return; | |
344 | ||
a6352fd4 MD |
345 | //TODO |
346 | //init_timer(&buf->read_timer); | |
347 | //buf->read_timer.function = read_buffer_timer; | |
348 | //buf->read_timer.expires = jiffies + chan->read_timer_interval; | |
349 | //buf->read_timer.data = (unsigned long)buf; | |
852c2936 | 350 | |
a6352fd4 MD |
351 | //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) |
352 | // add_timer_on(&buf->read_timer, buf->backend.cpu); | |
353 | //else | |
354 | // add_timer(&buf->read_timer); | |
852c2936 MD |
355 | buf->read_timer_enabled = 1; |
356 | } | |
357 | ||
4cfec15c | 358 | static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 359 | struct lttng_ust_shm_handle *handle) |
852c2936 | 360 | { |
1d498196 | 361 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 362 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
363 | |
364 | if (config->wakeup != RING_BUFFER_WAKEUP_BY_TIMER | |
365 | || !chan->read_timer_interval | |
366 | || !buf->read_timer_enabled) | |
367 | return; | |
368 | ||
a6352fd4 MD |
369 | //TODO |
370 | //del_timer_sync(&buf->read_timer); | |
852c2936 MD |
371 | /* |
372 | * do one more check to catch data that has been written in the last | |
373 | * timer period. | |
374 | */ | |
1d498196 | 375 | if (lib_ring_buffer_poll_deliver(config, buf, chan, handle)) { |
a6352fd4 MD |
376 | //TODO |
377 | //wake_up_interruptible(&buf->read_wait); | |
378 | //wake_up_interruptible(&chan->read_wait); | |
852c2936 MD |
379 | } |
380 | buf->read_timer_enabled = 0; | |
381 | } | |
382 | ||
1d498196 | 383 | static void channel_unregister_notifiers(struct channel *chan, |
38fae1d3 | 384 | struct lttng_ust_shm_handle *handle) |
852c2936 | 385 | { |
4cfec15c | 386 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
387 | int cpu; |
388 | ||
852c2936 | 389 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
852c2936 | 390 | for_each_possible_cpu(cpu) { |
4cfec15c | 391 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
a6352fd4 | 392 | |
1d498196 MD |
393 | lib_ring_buffer_stop_switch_timer(buf, handle); |
394 | lib_ring_buffer_stop_read_timer(buf, handle); | |
852c2936 | 395 | } |
852c2936 | 396 | } else { |
4cfec15c | 397 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 398 | |
1d498196 MD |
399 | lib_ring_buffer_stop_switch_timer(buf, handle); |
400 | lib_ring_buffer_stop_read_timer(buf, handle); | |
852c2936 | 401 | } |
8d8a24c8 | 402 | //channel_backend_unregister_notifiers(&chan->backend); |
852c2936 MD |
403 | } |
404 | ||
74d81a6c | 405 | static void channel_free(struct channel *chan, struct lttng_ust_shm_handle *handle) |
852c2936 | 406 | { |
74d81a6c | 407 | channel_backend_free(&chan->backend, handle); |
431d5cf0 | 408 | /* chan is freed by shm teardown */ |
1d498196 MD |
409 | shm_object_table_destroy(handle->table); |
410 | free(handle); | |
852c2936 MD |
411 | } |
412 | ||
413 | /** | |
414 | * channel_create - Create channel. | |
415 | * @config: ring buffer instance configuration | |
416 | * @name: name of the channel | |
a3f61e7f MD |
417 | * @priv_data: ring buffer client private data area pointer (output) |
418 | * @priv_data_size: length, in bytes, of the private data area. | |
d028eddb | 419 | * @priv_data_init: initialization data for private data. |
852c2936 MD |
420 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous |
421 | * address mapping. It is used only by RING_BUFFER_STATIC | |
422 | * configuration. It can be set to NULL for other backends. | |
423 | * @subbuf_size: subbuffer size | |
424 | * @num_subbuf: number of subbuffers | |
425 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with | |
426 | * padding to let readers get those sub-buffers. | |
427 | * Used for live streaming. | |
428 | * @read_timer_interval: Time interval (in us) to wake up pending readers. | |
429 | * | |
430 | * Holds cpu hotplug. | |
431 | * Returns NULL on failure. | |
432 | */ | |
4cfec15c | 433 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
434 | const char *name, |
435 | void **priv_data, | |
436 | size_t priv_data_align, | |
437 | size_t priv_data_size, | |
d028eddb | 438 | void *priv_data_init, |
a3f61e7f | 439 | void *buf_addr, size_t subbuf_size, |
852c2936 | 440 | size_t num_subbuf, unsigned int switch_timer_interval, |
74d81a6c | 441 | unsigned int read_timer_interval) |
852c2936 | 442 | { |
1d498196 | 443 | int ret, cpu; |
a3f61e7f | 444 | size_t shmsize, chansize; |
852c2936 | 445 | struct channel *chan; |
38fae1d3 | 446 | struct lttng_ust_shm_handle *handle; |
1d498196 | 447 | struct shm_object *shmobj; |
74d81a6c MD |
448 | unsigned int nr_streams; |
449 | ||
450 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
451 | nr_streams = num_possible_cpus(); | |
452 | else | |
453 | nr_streams = 1; | |
852c2936 MD |
454 | |
455 | if (lib_ring_buffer_check_config(config, switch_timer_interval, | |
456 | read_timer_interval)) | |
457 | return NULL; | |
458 | ||
38fae1d3 | 459 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
431d5cf0 MD |
460 | if (!handle) |
461 | return NULL; | |
462 | ||
1d498196 MD |
463 | /* Allocate table for channel + per-cpu buffers */ |
464 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
465 | if (!handle->table) | |
466 | goto error_table_alloc; | |
852c2936 | 467 | |
1d498196 MD |
468 | /* Calculate the shm allocation layout */ |
469 | shmsize = sizeof(struct channel); | |
c1fca457 | 470 | shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_shmp)); |
74d81a6c | 471 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer_shmp) * nr_streams; |
a3f61e7f | 472 | chansize = shmsize; |
74d81a6c MD |
473 | if (priv_data_align) |
474 | shmsize += offset_align(shmsize, priv_data_align); | |
a3f61e7f | 475 | shmsize += priv_data_size; |
a6352fd4 | 476 | |
74d81a6c MD |
477 | /* Allocate normal memory for channel (not shared) */ |
478 | shmobj = shm_object_table_alloc(handle->table, shmsize, SHM_OBJECT_MEM); | |
b5a14697 MD |
479 | if (!shmobj) |
480 | goto error_append; | |
57773204 | 481 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
a3f61e7f | 482 | set_shmp(handle->chan, zalloc_shm(shmobj, chansize)); |
57773204 MD |
483 | assert(handle->chan._ref.index == 0); |
484 | assert(handle->chan._ref.offset == 0); | |
1d498196 | 485 | chan = shmp(handle, handle->chan); |
a6352fd4 | 486 | if (!chan) |
1d498196 | 487 | goto error_append; |
74d81a6c | 488 | chan->nr_streams = nr_streams; |
a6352fd4 | 489 | |
a3f61e7f MD |
490 | /* space for private data */ |
491 | if (priv_data_size) { | |
492 | DECLARE_SHMP(void, priv_data_alloc); | |
493 | ||
494 | align_shm(shmobj, priv_data_align); | |
495 | chan->priv_data_offset = shmobj->allocated_len; | |
496 | set_shmp(priv_data_alloc, zalloc_shm(shmobj, priv_data_size)); | |
497 | if (!shmp(handle, priv_data_alloc)) | |
498 | goto error_append; | |
499 | *priv_data = channel_get_private(chan); | |
d028eddb | 500 | memcpy(*priv_data, priv_data_init, priv_data_size); |
a3f61e7f MD |
501 | } else { |
502 | chan->priv_data_offset = -1; | |
74d81a6c MD |
503 | if (priv_data) |
504 | *priv_data = NULL; | |
a3f61e7f MD |
505 | } |
506 | ||
507 | ret = channel_backend_init(&chan->backend, name, config, | |
1d498196 | 508 | subbuf_size, num_subbuf, handle); |
852c2936 | 509 | if (ret) |
1d498196 | 510 | goto error_backend_init; |
852c2936 MD |
511 | |
512 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); | |
a6352fd4 MD |
513 | //TODO |
514 | //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); | |
515 | //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); | |
a6352fd4 MD |
516 | //TODO |
517 | //init_waitqueue_head(&chan->read_wait); | |
518 | //init_waitqueue_head(&chan->hp_wait); | |
852c2936 MD |
519 | |
520 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
852c2936 MD |
521 | /* |
522 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
523 | * in early initcall, it will not be notified of secondary cpus. | |
524 | * In that off case, we need to allocate for all possible cpus. | |
525 | */ | |
852c2936 | 526 | for_each_possible_cpu(cpu) { |
4cfec15c | 527 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
1d498196 MD |
528 | lib_ring_buffer_start_switch_timer(buf, handle); |
529 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 530 | } |
852c2936 | 531 | } else { |
4cfec15c | 532 | struct lttng_ust_lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 533 | |
1d498196 MD |
534 | lib_ring_buffer_start_switch_timer(buf, handle); |
535 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 536 | } |
431d5cf0 | 537 | return handle; |
852c2936 | 538 | |
1d498196 MD |
539 | error_backend_init: |
540 | error_append: | |
541 | shm_object_table_destroy(handle->table); | |
542 | error_table_alloc: | |
431d5cf0 | 543 | free(handle); |
852c2936 MD |
544 | return NULL; |
545 | } | |
852c2936 | 546 | |
74d81a6c | 547 | struct lttng_ust_shm_handle *channel_handle_create(void *data, |
193183fb MD |
548 | uint64_t memory_map_size) |
549 | { | |
38fae1d3 | 550 | struct lttng_ust_shm_handle *handle; |
193183fb MD |
551 | struct shm_object *object; |
552 | ||
38fae1d3 | 553 | handle = zmalloc(sizeof(struct lttng_ust_shm_handle)); |
193183fb MD |
554 | if (!handle) |
555 | return NULL; | |
556 | ||
557 | /* Allocate table for channel + per-cpu buffers */ | |
558 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
559 | if (!handle->table) | |
560 | goto error_table_alloc; | |
561 | /* Add channel object */ | |
74d81a6c MD |
562 | object = shm_object_table_append_mem(handle->table, data, |
563 | memory_map_size); | |
193183fb MD |
564 | if (!object) |
565 | goto error_table_object; | |
57773204 MD |
566 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
567 | handle->chan._ref.index = 0; | |
568 | handle->chan._ref.offset = 0; | |
193183fb MD |
569 | return handle; |
570 | ||
571 | error_table_object: | |
572 | shm_object_table_destroy(handle->table); | |
573 | error_table_alloc: | |
574 | free(handle); | |
575 | return NULL; | |
576 | } | |
577 | ||
38fae1d3 | 578 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
579 | int shm_fd, int wakeup_fd, uint32_t stream_nr, |
580 | uint64_t memory_map_size) | |
193183fb MD |
581 | { |
582 | struct shm_object *object; | |
583 | ||
584 | /* Add stream object */ | |
74d81a6c MD |
585 | object = shm_object_table_append_shm(handle->table, |
586 | shm_fd, wakeup_fd, stream_nr, | |
587 | memory_map_size); | |
193183fb | 588 | if (!object) |
74d81a6c | 589 | return -EINVAL; |
193183fb MD |
590 | return 0; |
591 | } | |
592 | ||
74d81a6c MD |
593 | unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle) |
594 | { | |
595 | assert(handle->table); | |
596 | return handle->table->allocated_len - 1; | |
597 | } | |
598 | ||
852c2936 | 599 | static |
74d81a6c | 600 | void channel_release(struct channel *chan, struct lttng_ust_shm_handle *handle) |
852c2936 | 601 | { |
74d81a6c | 602 | channel_free(chan, handle); |
852c2936 MD |
603 | } |
604 | ||
605 | /** | |
606 | * channel_destroy - Finalize, wait for q.s. and destroy channel. | |
607 | * @chan: channel to destroy | |
608 | * | |
609 | * Holds cpu hotplug. | |
431d5cf0 MD |
610 | * Call "destroy" callback, finalize channels, decrement the channel |
611 | * reference count. Note that when readers have completed data | |
612 | * consumption of finalized channels, get_subbuf() will return -ENODATA. | |
a3f61e7f | 613 | * They should release their handle at that point. |
852c2936 | 614 | */ |
a3f61e7f | 615 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
74d81a6c | 616 | int consumer) |
852c2936 | 617 | { |
74d81a6c MD |
618 | if (consumer) { |
619 | /* | |
620 | * Note: the consumer takes care of finalizing and | |
621 | * switching the buffers. | |
622 | */ | |
623 | channel_unregister_notifiers(chan, handle); | |
824f40b8 MD |
624 | } |
625 | ||
431d5cf0 MD |
626 | /* |
627 | * sessiond/consumer are keeping a reference on the shm file | |
628 | * descriptor directly. No need to refcount. | |
629 | */ | |
74d81a6c | 630 | channel_release(chan, handle); |
a3f61e7f | 631 | return; |
852c2936 | 632 | } |
852c2936 | 633 | |
4cfec15c MD |
634 | struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
635 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 636 | struct channel *chan, int cpu, |
38fae1d3 | 637 | struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
638 | int *shm_fd, int *wait_fd, |
639 | int *wakeup_fd, | |
640 | uint64_t *memory_map_size) | |
852c2936 | 641 | { |
381c0f1e MD |
642 | struct shm_ref *ref; |
643 | ||
644 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
74d81a6c | 645 | cpu = 0; |
381c0f1e | 646 | } else { |
e095d803 MD |
647 | if (cpu >= num_possible_cpus()) |
648 | return NULL; | |
381c0f1e | 649 | } |
74d81a6c MD |
650 | ref = &chan->backend.buf[cpu].shmp._ref; |
651 | *shm_fd = shm_get_shm_fd(handle, ref); | |
652 | *wait_fd = shm_get_wait_fd(handle, ref); | |
653 | *wakeup_fd = shm_get_wakeup_fd(handle, ref); | |
654 | if (shm_get_shm_size(handle, ref, memory_map_size)) | |
655 | return NULL; | |
656 | return shmp(handle, chan->backend.buf[cpu].shmp); | |
852c2936 | 657 | } |
852c2936 | 658 | |
74d81a6c MD |
659 | int ring_buffer_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
660 | struct channel *chan, | |
661 | struct lttng_ust_shm_handle *handle, | |
662 | int cpu) | |
852c2936 | 663 | { |
74d81a6c MD |
664 | struct shm_ref *ref; |
665 | ||
666 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
667 | cpu = 0; | |
668 | } else { | |
669 | if (cpu >= num_possible_cpus()) | |
670 | return -EINVAL; | |
824f40b8 | 671 | } |
74d81a6c MD |
672 | ref = &chan->backend.buf[cpu].shmp._ref; |
673 | return shm_close_wait_fd(handle, ref); | |
674 | } | |
675 | ||
676 | int ring_buffer_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
677 | struct channel *chan, | |
678 | struct lttng_ust_shm_handle *handle, | |
679 | int cpu) | |
680 | { | |
681 | struct shm_ref *ref; | |
682 | ||
683 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
684 | cpu = 0; | |
685 | } else { | |
686 | if (cpu >= num_possible_cpus()) | |
687 | return -EINVAL; | |
688 | } | |
689 | ref = &chan->backend.buf[cpu].shmp._ref; | |
690 | return shm_close_wakeup_fd(handle, ref); | |
691 | } | |
692 | ||
693 | int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, | |
694 | struct lttng_ust_shm_handle *handle) | |
695 | { | |
a6352fd4 | 696 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
852c2936 | 697 | return -EBUSY; |
a6352fd4 | 698 | cmm_smp_mb(); |
852c2936 MD |
699 | return 0; |
700 | } | |
852c2936 | 701 | |
4cfec15c | 702 | void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 703 | struct lttng_ust_shm_handle *handle) |
852c2936 | 704 | { |
1d498196 | 705 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 | 706 | |
a6352fd4 MD |
707 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
708 | cmm_smp_mb(); | |
709 | uatomic_dec(&buf->active_readers); | |
852c2936 MD |
710 | } |
711 | ||
712 | /** | |
713 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) | |
714 | * @buf: ring buffer | |
715 | * @consumed: consumed count indicating the position where to read | |
716 | * @produced: produced count, indicates position when to stop reading | |
717 | * | |
718 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
719 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 MD |
720 | */ |
721 | ||
4cfec15c | 722 | int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 723 | unsigned long *consumed, unsigned long *produced, |
38fae1d3 | 724 | struct lttng_ust_shm_handle *handle) |
852c2936 | 725 | { |
1d498196 | 726 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 727 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
728 | unsigned long consumed_cur, write_offset; |
729 | int finalized; | |
730 | ||
14641deb | 731 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
732 | /* |
733 | * Read finalized before counters. | |
734 | */ | |
a6352fd4 MD |
735 | cmm_smp_rmb(); |
736 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 MD |
737 | /* |
738 | * No need to issue a memory barrier between consumed count read and | |
739 | * write offset read, because consumed count can only change | |
740 | * concurrently in overwrite mode, and we keep a sequence counter | |
741 | * identifier derived from the write offset to check we are getting | |
742 | * the same sub-buffer we are expecting (the sub-buffers are atomically | |
743 | * "tagged" upon writes, tags are checked upon read). | |
744 | */ | |
745 | write_offset = v_read(config, &buf->offset); | |
746 | ||
747 | /* | |
748 | * Check that we are not about to read the same subbuffer in | |
749 | * which the writer head is. | |
750 | */ | |
751 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
752 | == 0) | |
753 | goto nodata; | |
754 | ||
755 | *consumed = consumed_cur; | |
756 | *produced = subbuf_trunc(write_offset, chan); | |
757 | ||
758 | return 0; | |
759 | ||
760 | nodata: | |
761 | /* | |
762 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
763 | * of "raw_spin_is_locked" memory ordering. | |
764 | */ | |
765 | if (finalized) | |
766 | return -ENODATA; | |
852c2936 MD |
767 | else |
768 | return -EAGAIN; | |
769 | } | |
852c2936 MD |
770 | |
771 | /** | |
772 | * lib_ring_buffer_put_snapshot - move consumed counter forward | |
773 | * @buf: ring buffer | |
774 | * @consumed_new: new consumed count value | |
775 | */ | |
4cfec15c | 776 | void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 777 | unsigned long consumed_new, |
38fae1d3 | 778 | struct lttng_ust_shm_handle *handle) |
852c2936 | 779 | { |
4cfec15c | 780 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
1d498196 | 781 | struct channel *chan = shmp(handle, bufb->chan); |
852c2936 MD |
782 | unsigned long consumed; |
783 | ||
74d81a6c | 784 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_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 | ||
74d81a6c | 907 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
852c2936 MD |
908 | |
909 | if (!buf->get_subbuf) { | |
910 | /* | |
911 | * Reader puts a subbuffer it did not get. | |
912 | */ | |
913 | CHAN_WARN_ON(chan, 1); | |
914 | return; | |
915 | } | |
916 | consumed = buf->get_subbuf_consumed; | |
917 | buf->get_subbuf = 0; | |
918 | ||
919 | /* | |
920 | * Clear the records_unread counter. (overruns counter) | |
921 | * Can still be non-zero if a file reader simply grabbed the data | |
922 | * without using iterators. | |
923 | * Can be below zero if an iterator is used on a snapshot more than | |
924 | * once. | |
925 | */ | |
926 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
927 | v_add(config, v_read(config, | |
4746ae29 | 928 | &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread), |
852c2936 | 929 | &bufb->records_read); |
4746ae29 | 930 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0); |
852c2936 MD |
931 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
932 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
933 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); | |
934 | ||
935 | /* | |
936 | * Exchange the reader subbuffer with the one we put in its place in the | |
937 | * writer subbuffer table. Expect the original consumed count. If | |
938 | * update_read_sb_index fails, this is because the writer updated the | |
939 | * subbuffer concurrently. We should therefore keep the subbuffer we | |
940 | * currently have: it has become invalid to try reading this sub-buffer | |
941 | * consumed count value anyway. | |
942 | */ | |
943 | consumed_idx = subbuf_index(consumed, chan); | |
944 | update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
945 | consumed_idx, buf_trunc_val(consumed, chan), |
946 | handle); | |
852c2936 MD |
947 | /* |
948 | * update_read_sb_index return value ignored. Don't exchange sub-buffer | |
949 | * if the writer concurrently updated it. | |
950 | */ | |
951 | } | |
852c2936 MD |
952 | |
953 | /* | |
954 | * cons_offset is an iterator on all subbuffer offsets between the reader | |
955 | * position and the writer position. (inclusive) | |
956 | */ | |
957 | static | |
4cfec15c | 958 | void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
959 | struct channel *chan, |
960 | unsigned long cons_offset, | |
1d498196 | 961 | int cpu, |
38fae1d3 | 962 | struct lttng_ust_shm_handle *handle) |
852c2936 | 963 | { |
4cfec15c | 964 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
965 | unsigned long cons_idx, commit_count, commit_count_sb; |
966 | ||
967 | cons_idx = subbuf_index(cons_offset, chan); | |
4746ae29 MD |
968 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc); |
969 | commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb); | |
852c2936 MD |
970 | |
971 | if (subbuf_offset(commit_count, chan) != 0) | |
4d3c9523 | 972 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
973 | "commit count in subbuffer %lu,\n" |
974 | "expecting multiples of %lu bytes\n" | |
975 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", | |
976 | chan->backend.name, cpu, cons_idx, | |
977 | chan->backend.subbuf_size, | |
978 | commit_count, commit_count_sb); | |
979 | ||
4d3c9523 | 980 | DBG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
852c2936 MD |
981 | chan->backend.name, cpu, commit_count); |
982 | } | |
983 | ||
984 | static | |
4cfec15c | 985 | void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 986 | struct channel *chan, |
1d498196 | 987 | void *priv, int cpu, |
38fae1d3 | 988 | struct lttng_ust_shm_handle *handle) |
852c2936 | 989 | { |
4cfec15c | 990 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
991 | unsigned long write_offset, cons_offset; |
992 | ||
852c2936 MD |
993 | /* |
994 | * No need to order commit_count, write_offset and cons_offset reads | |
995 | * because we execute at teardown when no more writer nor reader | |
996 | * references are left. | |
997 | */ | |
998 | write_offset = v_read(config, &buf->offset); | |
a6352fd4 | 999 | cons_offset = uatomic_read(&buf->consumed); |
852c2936 | 1000 | if (write_offset != cons_offset) |
4d3c9523 | 1001 | DBG("ring buffer %s, cpu %d: " |
852c2936 MD |
1002 | "non-consumed data\n" |
1003 | " [ %lu bytes written, %lu bytes read ]\n", | |
1004 | chan->backend.name, cpu, write_offset, cons_offset); | |
1005 | ||
a6352fd4 | 1006 | for (cons_offset = uatomic_read(&buf->consumed); |
852c2936 MD |
1007 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
1008 | chan) | |
1009 | - cons_offset) > 0; | |
1010 | cons_offset = subbuf_align(cons_offset, chan)) | |
1011 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, | |
1d498196 | 1012 | cpu, handle); |
852c2936 MD |
1013 | } |
1014 | ||
1015 | static | |
1016 | void lib_ring_buffer_print_errors(struct channel *chan, | |
4cfec15c | 1017 | struct lttng_ust_lib_ring_buffer *buf, int cpu, |
38fae1d3 | 1018 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1019 | { |
4cfec15c | 1020 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
a3f61e7f | 1021 | void *priv = channel_get_private(chan); |
852c2936 | 1022 | |
a1360615 MD |
1023 | if (!strcmp(chan->backend.name, "relay-metadata-mmap")) { |
1024 | DBG("ring buffer %s: %lu records written, " | |
1025 | "%lu records overrun\n", | |
1026 | chan->backend.name, | |
1027 | v_read(config, &buf->records_count), | |
1028 | v_read(config, &buf->records_overrun)); | |
1029 | } else { | |
1030 | DBG("ring buffer %s, cpu %d: %lu records written, " | |
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)) | |
1039 | DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n" | |
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, | |
2fed87ae | 1059 | uint64_t 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, | |
2fed87ae | 1097 | uint64_t 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, | |
2fed87ae | 1134 | uint64_t 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, | |
2fed87ae | 1170 | uint64_t 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, | |
2fed87ae | 1206 | uint64_t *tsc) |
852c2936 | 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)) { |
c352d3a4 MD |
1236 | /* |
1237 | * A final flush that encounters an empty | |
1238 | * sub-buffer cannot switch buffer if a | |
1239 | * reader is located within this sub-buffer. | |
1240 | * Anyway, the purpose of final flushing of a | |
1241 | * sub-buffer at offset 0 is to handle the case | |
1242 | * of entirely empty stream. | |
1243 | */ | |
1244 | if (caa_unlikely(subbuf_trunc(offsets->begin, chan) | |
1245 | - subbuf_trunc((unsigned long) | |
1246 | uatomic_read(&buf->consumed), chan) | |
1247 | >= chan->backend.buf_size)) | |
1248 | return -1; | |
852c2936 MD |
1249 | /* |
1250 | * The client does not save any header information. | |
1251 | * Don't switch empty subbuffer on finalize, because it | |
1252 | * is invalid to deliver a completely empty subbuffer. | |
1253 | */ | |
1254 | if (!config->cb.subbuffer_header_size()) | |
1255 | return -1; | |
1256 | /* | |
1257 | * Need to write the subbuffer start header on finalize. | |
1258 | */ | |
1259 | offsets->switch_old_start = 1; | |
1260 | } | |
1261 | offsets->begin = subbuf_align(offsets->begin, chan); | |
1262 | } else | |
1263 | return -1; /* we do not have to switch : buffer is empty */ | |
1264 | /* Note: old points to the next subbuf at offset 0 */ | |
1265 | offsets->end = offsets->begin; | |
1266 | return 0; | |
1267 | } | |
1268 | ||
1269 | /* | |
1270 | * Force a sub-buffer switch. This operation is completely reentrant : can be | |
1271 | * called while tracing is active with absolutely no lock held. | |
1272 | * | |
1273 | * Note, however, that as a v_cmpxchg is used for some atomic | |
1274 | * operations, this function must be called from the CPU which owns the buffer | |
1275 | * for a ACTIVE flush. | |
1276 | */ | |
4cfec15c | 1277 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, enum switch_mode mode, |
38fae1d3 | 1278 | struct lttng_ust_shm_handle *handle) |
852c2936 | 1279 | { |
1d498196 | 1280 | struct channel *chan = shmp(handle, buf->backend.chan); |
4cfec15c | 1281 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1282 | struct switch_offsets offsets; |
1283 | unsigned long oldidx; | |
2fed87ae | 1284 | uint64_t tsc; |
852c2936 MD |
1285 | |
1286 | offsets.size = 0; | |
1287 | ||
1288 | /* | |
1289 | * Perform retryable operations. | |
1290 | */ | |
1291 | do { | |
1292 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, | |
1293 | &tsc)) | |
1294 | return; /* Switch not needed */ | |
1295 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) | |
1296 | != offsets.old); | |
1297 | ||
1298 | /* | |
1299 | * Atomically update last_tsc. This update races against concurrent | |
1300 | * atomic updates, but the race will always cause supplementary full TSC | |
1301 | * records, never the opposite (missing a full TSC record when it would | |
1302 | * be needed). | |
1303 | */ | |
1304 | save_last_tsc(config, buf, tsc); | |
1305 | ||
1306 | /* | |
1307 | * Push the reader if necessary | |
1308 | */ | |
1309 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); | |
1310 | ||
1311 | oldidx = subbuf_index(offsets.old, chan); | |
1d498196 | 1312 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
852c2936 MD |
1313 | |
1314 | /* | |
1315 | * May need to populate header start on SWITCH_FLUSH. | |
1316 | */ | |
1317 | if (offsets.switch_old_start) { | |
1d498196 | 1318 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
852c2936 MD |
1319 | offsets.old += config->cb.subbuffer_header_size(); |
1320 | } | |
1321 | ||
1322 | /* | |
1323 | * Switch old subbuffer. | |
1324 | */ | |
1d498196 | 1325 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
852c2936 | 1326 | } |
852c2936 MD |
1327 | |
1328 | /* | |
1329 | * Returns : | |
1330 | * 0 if ok | |
1331 | * -ENOSPC if event size is too large for packet. | |
1332 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
1333 | * -EIO if data cannot be written into the buffer for any other reason. | |
1334 | */ | |
1335 | static | |
4cfec15c | 1336 | int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
1337 | struct channel *chan, |
1338 | struct switch_offsets *offsets, | |
4cfec15c | 1339 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 | 1340 | { |
4cfec15c | 1341 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
38fae1d3 | 1342 | struct lttng_ust_shm_handle *handle = ctx->handle; |
852c2936 MD |
1343 | unsigned long reserve_commit_diff; |
1344 | ||
1345 | offsets->begin = v_read(config, &buf->offset); | |
1346 | offsets->old = offsets->begin; | |
1347 | offsets->switch_new_start = 0; | |
1348 | offsets->switch_new_end = 0; | |
1349 | offsets->switch_old_end = 0; | |
1350 | offsets->pre_header_padding = 0; | |
1351 | ||
1352 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); | |
1353 | if ((int64_t) ctx->tsc == -EIO) | |
1354 | return -EIO; | |
1355 | ||
1356 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
1357 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; | |
1358 | ||
b5a3dfa5 | 1359 | if (caa_unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { |
852c2936 MD |
1360 | offsets->switch_new_start = 1; /* For offsets->begin */ |
1361 | } else { | |
1362 | offsets->size = config->cb.record_header_size(config, chan, | |
1363 | offsets->begin, | |
1364 | &offsets->pre_header_padding, | |
1365 | ctx); | |
1366 | offsets->size += | |
1367 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1368 | ctx->largest_align) | |
1369 | + ctx->data_size; | |
b5a3dfa5 | 1370 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) + |
852c2936 MD |
1371 | offsets->size > chan->backend.subbuf_size)) { |
1372 | offsets->switch_old_end = 1; /* For offsets->old */ | |
1373 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1374 | } | |
1375 | } | |
b5a3dfa5 | 1376 | if (caa_unlikely(offsets->switch_new_start)) { |
852c2936 MD |
1377 | unsigned long sb_index; |
1378 | ||
1379 | /* | |
1380 | * We are typically not filling the previous buffer completely. | |
1381 | */ | |
b5a3dfa5 | 1382 | if (caa_likely(offsets->switch_old_end)) |
852c2936 MD |
1383 | offsets->begin = subbuf_align(offsets->begin, chan); |
1384 | offsets->begin = offsets->begin | |
1385 | + config->cb.subbuffer_header_size(); | |
1386 | /* Test new buffer integrity */ | |
1387 | sb_index = subbuf_index(offsets->begin, chan); | |
1388 | reserve_commit_diff = | |
1389 | (buf_trunc(offsets->begin, chan) | |
1390 | >> chan->backend.num_subbuf_order) | |
1391 | - ((unsigned long) v_read(config, | |
4746ae29 | 1392 | &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb) |
852c2936 | 1393 | & chan->commit_count_mask); |
b5a3dfa5 | 1394 | if (caa_likely(reserve_commit_diff == 0)) { |
852c2936 | 1395 | /* Next subbuffer not being written to. */ |
b5a3dfa5 | 1396 | if (caa_unlikely(config->mode != RING_BUFFER_OVERWRITE && |
852c2936 MD |
1397 | subbuf_trunc(offsets->begin, chan) |
1398 | - subbuf_trunc((unsigned long) | |
a6352fd4 | 1399 | uatomic_read(&buf->consumed), chan) |
852c2936 | 1400 | >= chan->backend.buf_size)) { |
64493e4f MD |
1401 | unsigned long nr_lost; |
1402 | ||
852c2936 MD |
1403 | /* |
1404 | * We do not overwrite non consumed buffers | |
1405 | * and we are full : record is lost. | |
1406 | */ | |
64493e4f | 1407 | nr_lost = v_read(config, &buf->records_lost_full); |
852c2936 | 1408 | v_inc(config, &buf->records_lost_full); |
64493e4f MD |
1409 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1410 | DBG("%lu or more records lost in (%s:%d) (buffer full)\n", | |
1411 | nr_lost + 1, chan->backend.name, | |
1412 | buf->backend.cpu); | |
1413 | } | |
852c2936 MD |
1414 | return -ENOBUFS; |
1415 | } else { | |
1416 | /* | |
1417 | * Next subbuffer not being written to, and we | |
1418 | * are either in overwrite mode or the buffer is | |
1419 | * not full. It's safe to write in this new | |
1420 | * subbuffer. | |
1421 | */ | |
1422 | } | |
1423 | } else { | |
64493e4f MD |
1424 | unsigned long nr_lost; |
1425 | ||
852c2936 MD |
1426 | /* |
1427 | * Next subbuffer reserve offset does not match the | |
1428 | * commit offset. Drop record in producer-consumer and | |
1429 | * overwrite mode. Caused by either a writer OOPS or too | |
1430 | * many nested writes over a reserve/commit pair. | |
1431 | */ | |
64493e4f | 1432 | nr_lost = v_read(config, &buf->records_lost_wrap); |
852c2936 | 1433 | v_inc(config, &buf->records_lost_wrap); |
64493e4f MD |
1434 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1435 | DBG("%lu or more records lost in (%s:%d) (wrap-around)\n", | |
1436 | nr_lost + 1, chan->backend.name, | |
1437 | buf->backend.cpu); | |
1438 | } | |
852c2936 MD |
1439 | return -EIO; |
1440 | } | |
1441 | offsets->size = | |
1442 | config->cb.record_header_size(config, chan, | |
1443 | offsets->begin, | |
1444 | &offsets->pre_header_padding, | |
1445 | ctx); | |
1446 | offsets->size += | |
1447 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1448 | ctx->largest_align) | |
1449 | + ctx->data_size; | |
b5a3dfa5 | 1450 | if (caa_unlikely(subbuf_offset(offsets->begin, chan) |
852c2936 | 1451 | + offsets->size > chan->backend.subbuf_size)) { |
64493e4f MD |
1452 | unsigned long nr_lost; |
1453 | ||
852c2936 MD |
1454 | /* |
1455 | * Record too big for subbuffers, report error, don't | |
1456 | * complete the sub-buffer switch. | |
1457 | */ | |
64493e4f | 1458 | nr_lost = v_read(config, &buf->records_lost_big); |
852c2936 | 1459 | v_inc(config, &buf->records_lost_big); |
64493e4f MD |
1460 | if ((nr_lost & (DBG_PRINT_NR_LOST - 1)) == 0) { |
1461 | DBG("%lu or more records lost in (%s:%d) record size " | |
1462 | " of %zu bytes is too large for buffer\n", | |
1463 | nr_lost + 1, chan->backend.name, | |
1464 | buf->backend.cpu, offsets->size); | |
1465 | } | |
852c2936 MD |
1466 | return -ENOSPC; |
1467 | } else { | |
1468 | /* | |
1469 | * We just made a successful buffer switch and the | |
1470 | * record fits in the new subbuffer. Let's write. | |
1471 | */ | |
1472 | } | |
1473 | } else { | |
1474 | /* | |
1475 | * Record fits in the current buffer and we are not on a switch | |
1476 | * boundary. It's safe to write. | |
1477 | */ | |
1478 | } | |
1479 | offsets->end = offsets->begin + offsets->size; | |
1480 | ||
b5a3dfa5 | 1481 | if (caa_unlikely(subbuf_offset(offsets->end, chan) == 0)) { |
852c2936 MD |
1482 | /* |
1483 | * The offset_end will fall at the very beginning of the next | |
1484 | * subbuffer. | |
1485 | */ | |
1486 | offsets->switch_new_end = 1; /* For offsets->begin */ | |
1487 | } | |
1488 | return 0; | |
1489 | } | |
1490 | ||
1491 | /** | |
1492 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. | |
1493 | * @ctx: ring buffer context. | |
1494 | * | |
1495 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, | |
1496 | * -EIO for other errors, else returns 0. | |
1497 | * It will take care of sub-buffer switching. | |
1498 | */ | |
4cfec15c | 1499 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
852c2936 MD |
1500 | { |
1501 | struct channel *chan = ctx->chan; | |
38fae1d3 | 1502 | struct lttng_ust_shm_handle *handle = ctx->handle; |
4cfec15c MD |
1503 | const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config; |
1504 | struct lttng_ust_lib_ring_buffer *buf; | |
852c2936 MD |
1505 | struct switch_offsets offsets; |
1506 | int ret; | |
1507 | ||
1508 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1d498196 | 1509 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
852c2936 | 1510 | else |
1d498196 | 1511 | buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 MD |
1512 | ctx->buf = buf; |
1513 | ||
1514 | offsets.size = 0; | |
1515 | ||
1516 | do { | |
1517 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, | |
1518 | ctx); | |
b5a3dfa5 | 1519 | if (caa_unlikely(ret)) |
852c2936 | 1520 | return ret; |
b5a3dfa5 | 1521 | } while (caa_unlikely(v_cmpxchg(config, &buf->offset, offsets.old, |
852c2936 MD |
1522 | offsets.end) |
1523 | != offsets.old)); | |
1524 | ||
1525 | /* | |
1526 | * Atomically update last_tsc. This update races against concurrent | |
1527 | * atomic updates, but the race will always cause supplementary full TSC | |
1528 | * records, never the opposite (missing a full TSC record when it would | |
1529 | * be needed). | |
1530 | */ | |
1531 | save_last_tsc(config, buf, ctx->tsc); | |
1532 | ||
1533 | /* | |
1534 | * Push the reader if necessary | |
1535 | */ | |
1536 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); | |
1537 | ||
1538 | /* | |
1539 | * Clear noref flag for this subbuffer. | |
1540 | */ | |
1541 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1d498196 MD |
1542 | subbuf_index(offsets.end - 1, chan), |
1543 | handle); | |
852c2936 MD |
1544 | |
1545 | /* | |
1546 | * Switch old subbuffer if needed. | |
1547 | */ | |
b5a3dfa5 | 1548 | if (caa_unlikely(offsets.switch_old_end)) { |
852c2936 | 1549 | lib_ring_buffer_clear_noref(config, &buf->backend, |
1d498196 MD |
1550 | subbuf_index(offsets.old - 1, chan), |
1551 | handle); | |
1552 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); | |
852c2936 MD |
1553 | } |
1554 | ||
1555 | /* | |
1556 | * Populate new subbuffer. | |
1557 | */ | |
b5a3dfa5 | 1558 | if (caa_unlikely(offsets.switch_new_start)) |
1d498196 | 1559 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 | 1560 | |
b5a3dfa5 | 1561 | if (caa_unlikely(offsets.switch_new_end)) |
1d498196 | 1562 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 MD |
1563 | |
1564 | ctx->slot_size = offsets.size; | |
1565 | ctx->pre_offset = offsets.begin; | |
1566 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; | |
1567 | return 0; | |
1568 | } | |
f645cfa7 MD |
1569 | |
1570 | /* | |
1571 | * Force a read (imply TLS fixup for dlopen) of TLS variables. | |
1572 | */ | |
1573 | void lttng_fixup_ringbuffer_tls(void) | |
1574 | { | |
8c90a710 | 1575 | asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting))); |
f645cfa7 | 1576 | } |