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" |
8d8a24c8 | 49 | #include <ust/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, | |
1d498196 MD |
88 | struct lib_ring_buffer *buf, int cpu, |
89 | struct shm_handle *handle); | |
852c2936 MD |
90 | |
91 | /* | |
92 | * Must be called under cpu hotplug protection. | |
93 | */ | |
1d498196 MD |
94 | void lib_ring_buffer_free(struct lib_ring_buffer *buf, |
95 | struct 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 | */ | |
1d498196 MD |
115 | void lib_ring_buffer_reset(struct lib_ring_buffer *buf, |
116 | struct shm_handle *handle) | |
852c2936 | 117 | { |
1d498196 | 118 | struct channel *chan = shmp(handle, buf->backend.chan); |
193183fb | 119 | const struct 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 | */ | |
170 | int lib_ring_buffer_create(struct lib_ring_buffer *buf, | |
a6352fd4 | 171 | struct channel_backend *chanb, int cpu, |
1d498196 MD |
172 | struct shm_handle *handle, |
173 | struct shm_object *shmobj) | |
852c2936 | 174 | { |
193183fb | 175 | const struct lib_ring_buffer_config *config = &chanb->config; |
14641deb | 176 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
852c2936 MD |
177 | void *priv = chanb->priv; |
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 | { | |
245 | struct lib_ring_buffer *buf = (struct lib_ring_buffer *)data; | |
1d498196 | 246 | struct channel *chan = shmp(handle, buf->backend.chan); |
193183fb | 247 | const struct 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 | |
1d498196 MD |
265 | static void lib_ring_buffer_start_switch_timer(struct lib_ring_buffer *buf, |
266 | struct shm_handle *handle) | |
852c2936 | 267 | { |
1d498196 | 268 | struct channel *chan = shmp(handle, buf->backend.chan); |
d33db568 | 269 | //const struct 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 | ||
1d498196 MD |
285 | static void lib_ring_buffer_stop_switch_timer(struct lib_ring_buffer *buf, |
286 | struct 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 | { | |
304 | struct lib_ring_buffer *buf = (struct lib_ring_buffer *)data; | |
1d498196 | 305 | struct channel *chan = shmp(handle, buf->backend.chan); |
193183fb | 306 | const struct 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 | |
1d498196 MD |
327 | static void lib_ring_buffer_start_read_timer(struct lib_ring_buffer *buf, |
328 | struct shm_handle *handle) | |
852c2936 | 329 | { |
1d498196 | 330 | struct channel *chan = shmp(handle, buf->backend.chan); |
193183fb | 331 | const struct 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 | ||
1d498196 MD |
351 | static void lib_ring_buffer_stop_read_timer(struct lib_ring_buffer *buf, |
352 | struct shm_handle *handle) | |
852c2936 | 353 | { |
1d498196 | 354 | struct channel *chan = shmp(handle, buf->backend.chan); |
193183fb | 355 | const struct 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 MD |
376 | static void channel_unregister_notifiers(struct channel *chan, |
377 | struct shm_handle *handle) | |
852c2936 | 378 | { |
193183fb | 379 | const struct 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) { |
1d498196 | 384 | struct 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 { |
1d498196 | 390 | struct 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 | ||
824f40b8 MD |
398 | static void channel_free(struct channel *chan, struct shm_handle *handle, |
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 | |
412 | * @priv: ring buffer client private data | |
413 | * @buf_addr: pointer the the beginning of the preallocated buffer contiguous | |
414 | * address mapping. It is used only by RING_BUFFER_STATIC | |
415 | * configuration. It can be set to NULL for other backends. | |
416 | * @subbuf_size: subbuffer size | |
417 | * @num_subbuf: number of subbuffers | |
418 | * @switch_timer_interval: Time interval (in us) to fill sub-buffers with | |
419 | * padding to let readers get those sub-buffers. | |
420 | * Used for live streaming. | |
421 | * @read_timer_interval: Time interval (in us) to wake up pending readers. | |
422 | * | |
423 | * Holds cpu hotplug. | |
424 | * Returns NULL on failure. | |
425 | */ | |
431d5cf0 | 426 | struct shm_handle *channel_create(const struct lib_ring_buffer_config *config, |
852c2936 MD |
427 | const char *name, void *priv, void *buf_addr, |
428 | size_t subbuf_size, | |
429 | size_t num_subbuf, unsigned int switch_timer_interval, | |
193183fb MD |
430 | unsigned int read_timer_interval, |
431 | int *shm_fd, int *wait_fd, uint64_t *memory_map_size) | |
852c2936 | 432 | { |
1d498196 MD |
433 | int ret, cpu; |
434 | size_t shmsize; | |
852c2936 | 435 | struct channel *chan; |
431d5cf0 | 436 | struct shm_handle *handle; |
1d498196 | 437 | struct shm_object *shmobj; |
193183fb | 438 | struct shm_ref *ref; |
852c2936 MD |
439 | |
440 | if (lib_ring_buffer_check_config(config, switch_timer_interval, | |
441 | read_timer_interval)) | |
442 | return NULL; | |
443 | ||
431d5cf0 MD |
444 | handle = zmalloc(sizeof(struct shm_handle)); |
445 | if (!handle) | |
446 | return NULL; | |
447 | ||
1d498196 MD |
448 | /* Allocate table for channel + per-cpu buffers */ |
449 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
450 | if (!handle->table) | |
451 | goto error_table_alloc; | |
852c2936 | 452 | |
1d498196 MD |
453 | /* Calculate the shm allocation layout */ |
454 | shmsize = sizeof(struct channel); | |
455 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
456 | shmsize += sizeof(struct lib_ring_buffer_shmp) * num_possible_cpus(); | |
457 | else | |
458 | shmsize += sizeof(struct lib_ring_buffer_shmp); | |
a6352fd4 | 459 | |
1d498196 | 460 | shmobj = shm_object_table_append(handle->table, shmsize); |
b5a14697 MD |
461 | if (!shmobj) |
462 | goto error_append; | |
57773204 | 463 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
1d498196 | 464 | set_shmp(handle->chan, zalloc_shm(shmobj, sizeof(struct channel))); |
57773204 MD |
465 | assert(handle->chan._ref.index == 0); |
466 | assert(handle->chan._ref.offset == 0); | |
1d498196 | 467 | chan = shmp(handle, handle->chan); |
a6352fd4 | 468 | if (!chan) |
1d498196 | 469 | goto error_append; |
a6352fd4 MD |
470 | |
471 | ret = channel_backend_init(&chan->backend, name, config, priv, | |
1d498196 | 472 | subbuf_size, num_subbuf, handle); |
852c2936 | 473 | if (ret) |
1d498196 | 474 | goto error_backend_init; |
852c2936 MD |
475 | |
476 | chan->commit_count_mask = (~0UL >> chan->backend.num_subbuf_order); | |
a6352fd4 MD |
477 | //TODO |
478 | //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval); | |
479 | //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval); | |
a6352fd4 MD |
480 | //TODO |
481 | //init_waitqueue_head(&chan->read_wait); | |
482 | //init_waitqueue_head(&chan->hp_wait); | |
852c2936 MD |
483 | |
484 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
852c2936 MD |
485 | /* |
486 | * In case of non-hotplug cpu, if the ring-buffer is allocated | |
487 | * in early initcall, it will not be notified of secondary cpus. | |
488 | * In that off case, we need to allocate for all possible cpus. | |
489 | */ | |
852c2936 | 490 | for_each_possible_cpu(cpu) { |
1d498196 MD |
491 | struct lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
492 | lib_ring_buffer_start_switch_timer(buf, handle); | |
493 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 494 | } |
852c2936 | 495 | } else { |
1d498196 | 496 | struct lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 | 497 | |
1d498196 MD |
498 | lib_ring_buffer_start_switch_timer(buf, handle); |
499 | lib_ring_buffer_start_read_timer(buf, handle); | |
852c2936 | 500 | } |
193183fb MD |
501 | ref = &handle->chan._ref; |
502 | shm_get_object_data(handle, ref, shm_fd, wait_fd, memory_map_size); | |
431d5cf0 | 503 | return handle; |
852c2936 | 504 | |
1d498196 MD |
505 | error_backend_init: |
506 | error_append: | |
507 | shm_object_table_destroy(handle->table); | |
508 | error_table_alloc: | |
431d5cf0 | 509 | free(handle); |
852c2936 MD |
510 | return NULL; |
511 | } | |
852c2936 | 512 | |
193183fb MD |
513 | struct shm_handle *channel_handle_create(int shm_fd, int wait_fd, |
514 | uint64_t memory_map_size) | |
515 | { | |
516 | struct shm_handle *handle; | |
517 | struct shm_object *object; | |
518 | ||
519 | handle = zmalloc(sizeof(struct shm_handle)); | |
520 | if (!handle) | |
521 | return NULL; | |
522 | ||
523 | /* Allocate table for channel + per-cpu buffers */ | |
524 | handle->table = shm_object_table_create(1 + num_possible_cpus()); | |
525 | if (!handle->table) | |
526 | goto error_table_alloc; | |
527 | /* Add channel object */ | |
528 | object = shm_object_table_append_shadow(handle->table, | |
529 | shm_fd, wait_fd, memory_map_size); | |
530 | if (!object) | |
531 | goto error_table_object; | |
57773204 MD |
532 | /* struct channel is at object 0, offset 0 (hardcoded) */ |
533 | handle->chan._ref.index = 0; | |
534 | handle->chan._ref.offset = 0; | |
193183fb MD |
535 | return handle; |
536 | ||
537 | error_table_object: | |
538 | shm_object_table_destroy(handle->table); | |
539 | error_table_alloc: | |
540 | free(handle); | |
541 | return NULL; | |
542 | } | |
543 | ||
544 | int channel_handle_add_stream(struct shm_handle *handle, | |
545 | int shm_fd, int wait_fd, uint64_t memory_map_size) | |
546 | { | |
547 | struct shm_object *object; | |
548 | ||
549 | /* Add stream object */ | |
550 | object = shm_object_table_append_shadow(handle->table, | |
551 | shm_fd, wait_fd, memory_map_size); | |
552 | if (!object) | |
553 | return -1; | |
554 | return 0; | |
555 | } | |
556 | ||
852c2936 | 557 | static |
824f40b8 MD |
558 | void channel_release(struct channel *chan, struct shm_handle *handle, |
559 | int shadow) | |
852c2936 | 560 | { |
824f40b8 | 561 | channel_free(chan, handle, shadow); |
852c2936 MD |
562 | } |
563 | ||
564 | /** | |
565 | * channel_destroy - Finalize, wait for q.s. and destroy channel. | |
566 | * @chan: channel to destroy | |
567 | * | |
568 | * Holds cpu hotplug. | |
431d5cf0 MD |
569 | * Call "destroy" callback, finalize channels, decrement the channel |
570 | * reference count. Note that when readers have completed data | |
571 | * consumption of finalized channels, get_subbuf() will return -ENODATA. | |
572 | * They should release their handle at that point. Returns the private | |
573 | * data pointer. | |
852c2936 | 574 | */ |
824f40b8 MD |
575 | void *channel_destroy(struct channel *chan, struct shm_handle *handle, |
576 | int shadow) | |
852c2936 | 577 | { |
193183fb | 578 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 | 579 | void *priv; |
431d5cf0 | 580 | int cpu; |
852c2936 | 581 | |
824f40b8 MD |
582 | if (shadow) { |
583 | channel_release(chan, handle, shadow); | |
584 | return NULL; | |
585 | } | |
586 | ||
1d498196 | 587 | channel_unregister_notifiers(chan, handle); |
852c2936 MD |
588 | |
589 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { | |
852c2936 | 590 | for_each_channel_cpu(cpu, chan) { |
1d498196 | 591 | struct lib_ring_buffer *buf = shmp(handle, chan->backend.buf[cpu].shmp); |
852c2936 MD |
592 | |
593 | if (config->cb.buffer_finalize) | |
594 | config->cb.buffer_finalize(buf, | |
595 | chan->backend.priv, | |
1d498196 | 596 | cpu, handle); |
852c2936 | 597 | if (buf->backend.allocated) |
1d498196 MD |
598 | lib_ring_buffer_switch_slow(buf, SWITCH_FLUSH, |
599 | handle); | |
852c2936 MD |
600 | /* |
601 | * Perform flush before writing to finalized. | |
602 | */ | |
a6352fd4 | 603 | cmm_smp_wmb(); |
14641deb | 604 | CMM_ACCESS_ONCE(buf->finalized) = 1; |
a6352fd4 | 605 | //wake_up_interruptible(&buf->read_wait); |
852c2936 MD |
606 | } |
607 | } else { | |
1d498196 | 608 | struct lib_ring_buffer *buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 MD |
609 | |
610 | if (config->cb.buffer_finalize) | |
1d498196 | 611 | config->cb.buffer_finalize(buf, chan->backend.priv, -1, handle); |
852c2936 | 612 | if (buf->backend.allocated) |
1d498196 MD |
613 | lib_ring_buffer_switch_slow(buf, SWITCH_FLUSH, |
614 | handle); | |
852c2936 MD |
615 | /* |
616 | * Perform flush before writing to finalized. | |
617 | */ | |
a6352fd4 | 618 | cmm_smp_wmb(); |
14641deb | 619 | CMM_ACCESS_ONCE(buf->finalized) = 1; |
a6352fd4 | 620 | //wake_up_interruptible(&buf->read_wait); |
852c2936 | 621 | } |
14641deb | 622 | CMM_ACCESS_ONCE(chan->finalized) = 1; |
a6352fd4 MD |
623 | //wake_up_interruptible(&chan->hp_wait); |
624 | //wake_up_interruptible(&chan->read_wait); | |
431d5cf0 MD |
625 | /* |
626 | * sessiond/consumer are keeping a reference on the shm file | |
627 | * descriptor directly. No need to refcount. | |
628 | */ | |
852c2936 | 629 | priv = chan->backend.priv; |
824f40b8 | 630 | channel_release(chan, handle, shadow); |
852c2936 MD |
631 | return priv; |
632 | } | |
852c2936 MD |
633 | |
634 | struct lib_ring_buffer *channel_get_ring_buffer( | |
635 | const struct lib_ring_buffer_config *config, | |
1d498196 | 636 | struct channel *chan, int cpu, |
381c0f1e MD |
637 | struct shm_handle *handle, |
638 | int *shm_fd, int *wait_fd, | |
639 | uint64_t *memory_map_size) | |
852c2936 | 640 | { |
381c0f1e MD |
641 | struct shm_ref *ref; |
642 | ||
643 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL) { | |
644 | ref = &chan->backend.buf[0].shmp._ref; | |
645 | shm_get_object_data(handle, ref, shm_fd, wait_fd, | |
646 | memory_map_size); | |
1d498196 | 647 | return shmp(handle, chan->backend.buf[0].shmp); |
381c0f1e | 648 | } else { |
e095d803 MD |
649 | if (cpu >= num_possible_cpus()) |
650 | return NULL; | |
381c0f1e MD |
651 | ref = &chan->backend.buf[cpu].shmp._ref; |
652 | shm_get_object_data(handle, ref, shm_fd, wait_fd, | |
653 | memory_map_size); | |
1d498196 | 654 | return shmp(handle, chan->backend.buf[cpu].shmp); |
381c0f1e | 655 | } |
852c2936 | 656 | } |
852c2936 | 657 | |
1d498196 | 658 | int lib_ring_buffer_open_read(struct lib_ring_buffer *buf, |
824f40b8 MD |
659 | struct shm_handle *handle, |
660 | int shadow) | |
852c2936 | 661 | { |
824f40b8 MD |
662 | if (shadow) { |
663 | if (uatomic_cmpxchg(&buf->active_shadow_readers, 0, 1) != 0) | |
664 | return -EBUSY; | |
665 | cmm_smp_mb(); | |
666 | return 0; | |
667 | } | |
a6352fd4 | 668 | if (uatomic_cmpxchg(&buf->active_readers, 0, 1) != 0) |
852c2936 | 669 | return -EBUSY; |
a6352fd4 | 670 | cmm_smp_mb(); |
852c2936 MD |
671 | return 0; |
672 | } | |
852c2936 | 673 | |
1d498196 | 674 | void lib_ring_buffer_release_read(struct lib_ring_buffer *buf, |
824f40b8 MD |
675 | struct shm_handle *handle, |
676 | int shadow) | |
852c2936 | 677 | { |
1d498196 | 678 | struct channel *chan = shmp(handle, buf->backend.chan); |
852c2936 | 679 | |
824f40b8 MD |
680 | if (shadow) { |
681 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_shadow_readers) != 1); | |
682 | cmm_smp_mb(); | |
683 | uatomic_dec(&buf->active_shadow_readers); | |
684 | return; | |
685 | } | |
a6352fd4 MD |
686 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1); |
687 | cmm_smp_mb(); | |
688 | uatomic_dec(&buf->active_readers); | |
852c2936 MD |
689 | } |
690 | ||
691 | /** | |
692 | * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read) | |
693 | * @buf: ring buffer | |
694 | * @consumed: consumed count indicating the position where to read | |
695 | * @produced: produced count, indicates position when to stop reading | |
696 | * | |
697 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
698 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 MD |
699 | */ |
700 | ||
701 | int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf, | |
1d498196 MD |
702 | unsigned long *consumed, unsigned long *produced, |
703 | struct shm_handle *handle) | |
852c2936 | 704 | { |
1d498196 | 705 | struct channel *chan = shmp(handle, buf->backend.chan); |
193183fb | 706 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
707 | unsigned long consumed_cur, write_offset; |
708 | int finalized; | |
709 | ||
14641deb | 710 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
711 | /* |
712 | * Read finalized before counters. | |
713 | */ | |
a6352fd4 MD |
714 | cmm_smp_rmb(); |
715 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 MD |
716 | /* |
717 | * No need to issue a memory barrier between consumed count read and | |
718 | * write offset read, because consumed count can only change | |
719 | * concurrently in overwrite mode, and we keep a sequence counter | |
720 | * identifier derived from the write offset to check we are getting | |
721 | * the same sub-buffer we are expecting (the sub-buffers are atomically | |
722 | * "tagged" upon writes, tags are checked upon read). | |
723 | */ | |
724 | write_offset = v_read(config, &buf->offset); | |
725 | ||
726 | /* | |
727 | * Check that we are not about to read the same subbuffer in | |
728 | * which the writer head is. | |
729 | */ | |
730 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
731 | == 0) | |
732 | goto nodata; | |
733 | ||
734 | *consumed = consumed_cur; | |
735 | *produced = subbuf_trunc(write_offset, chan); | |
736 | ||
737 | return 0; | |
738 | ||
739 | nodata: | |
740 | /* | |
741 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
742 | * of "raw_spin_is_locked" memory ordering. | |
743 | */ | |
744 | if (finalized) | |
745 | return -ENODATA; | |
852c2936 MD |
746 | else |
747 | return -EAGAIN; | |
748 | } | |
852c2936 MD |
749 | |
750 | /** | |
751 | * lib_ring_buffer_put_snapshot - move consumed counter forward | |
752 | * @buf: ring buffer | |
753 | * @consumed_new: new consumed count value | |
754 | */ | |
755 | void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf, | |
1d498196 MD |
756 | unsigned long consumed_new, |
757 | struct shm_handle *handle) | |
852c2936 MD |
758 | { |
759 | struct lib_ring_buffer_backend *bufb = &buf->backend; | |
1d498196 | 760 | struct channel *chan = shmp(handle, bufb->chan); |
852c2936 MD |
761 | unsigned long consumed; |
762 | ||
824f40b8 MD |
763 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
764 | && uatomic_read(&buf->active_shadow_readers) != 1); | |
852c2936 MD |
765 | |
766 | /* | |
767 | * Only push the consumed value forward. | |
768 | * If the consumed cmpxchg fails, this is because we have been pushed by | |
769 | * the writer in flight recorder mode. | |
770 | */ | |
a6352fd4 | 771 | consumed = uatomic_read(&buf->consumed); |
852c2936 | 772 | while ((long) consumed - (long) consumed_new < 0) |
a6352fd4 MD |
773 | consumed = uatomic_cmpxchg(&buf->consumed, consumed, |
774 | consumed_new); | |
852c2936 | 775 | } |
852c2936 MD |
776 | |
777 | /** | |
778 | * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading | |
779 | * @buf: ring buffer | |
780 | * @consumed: consumed count indicating the position where to read | |
781 | * | |
782 | * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no | |
783 | * data to read at consumed position, or 0 if the get operation succeeds. | |
852c2936 MD |
784 | */ |
785 | int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf, | |
1d498196 MD |
786 | unsigned long consumed, |
787 | struct shm_handle *handle) | |
852c2936 | 788 | { |
1d498196 | 789 | struct channel *chan = shmp(handle, buf->backend.chan); |
193183fb | 790 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
791 | unsigned long consumed_cur, consumed_idx, commit_count, write_offset; |
792 | int ret; | |
793 | int finalized; | |
794 | ||
795 | retry: | |
14641deb | 796 | finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
797 | /* |
798 | * Read finalized before counters. | |
799 | */ | |
a6352fd4 MD |
800 | cmm_smp_rmb(); |
801 | consumed_cur = uatomic_read(&buf->consumed); | |
852c2936 | 802 | consumed_idx = subbuf_index(consumed, chan); |
4746ae29 | 803 | commit_count = v_read(config, &shmp_index(handle, buf->commit_cold, consumed_idx)->cc_sb); |
852c2936 MD |
804 | /* |
805 | * Make sure we read the commit count before reading the buffer | |
806 | * data and the write offset. Correct consumed offset ordering | |
807 | * wrt commit count is insured by the use of cmpxchg to update | |
808 | * the consumed offset. | |
852c2936 | 809 | */ |
a6352fd4 MD |
810 | /* |
811 | * Local rmb to match the remote wmb to read the commit count | |
812 | * before the buffer data and the write offset. | |
813 | */ | |
814 | cmm_smp_rmb(); | |
852c2936 MD |
815 | |
816 | write_offset = v_read(config, &buf->offset); | |
817 | ||
818 | /* | |
819 | * Check that the buffer we are getting is after or at consumed_cur | |
820 | * position. | |
821 | */ | |
822 | if ((long) subbuf_trunc(consumed, chan) | |
823 | - (long) subbuf_trunc(consumed_cur, chan) < 0) | |
824 | goto nodata; | |
825 | ||
826 | /* | |
827 | * Check that the subbuffer we are trying to consume has been | |
828 | * already fully committed. | |
829 | */ | |
830 | if (((commit_count - chan->backend.subbuf_size) | |
831 | & chan->commit_count_mask) | |
832 | - (buf_trunc(consumed_cur, chan) | |
833 | >> chan->backend.num_subbuf_order) | |
834 | != 0) | |
835 | goto nodata; | |
836 | ||
837 | /* | |
838 | * Check that we are not about to read the same subbuffer in | |
839 | * which the writer head is. | |
840 | */ | |
841 | if (subbuf_trunc(write_offset, chan) - subbuf_trunc(consumed_cur, chan) | |
842 | == 0) | |
843 | goto nodata; | |
844 | ||
845 | /* | |
846 | * Failure to get the subbuffer causes a busy-loop retry without going | |
847 | * to a wait queue. These are caused by short-lived race windows where | |
848 | * the writer is getting access to a subbuffer we were trying to get | |
849 | * access to. Also checks that the "consumed" buffer count we are | |
850 | * looking for matches the one contained in the subbuffer id. | |
851 | */ | |
852 | ret = update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
853 | consumed_idx, buf_trunc_val(consumed, chan), |
854 | handle); | |
852c2936 MD |
855 | if (ret) |
856 | goto retry; | |
857 | subbuffer_id_clear_noref(config, &buf->backend.buf_rsb.id); | |
858 | ||
859 | buf->get_subbuf_consumed = consumed; | |
860 | buf->get_subbuf = 1; | |
861 | ||
862 | return 0; | |
863 | ||
864 | nodata: | |
865 | /* | |
866 | * The memory barriers __wait_event()/wake_up_interruptible() take care | |
867 | * of "raw_spin_is_locked" memory ordering. | |
868 | */ | |
869 | if (finalized) | |
870 | return -ENODATA; | |
852c2936 MD |
871 | else |
872 | return -EAGAIN; | |
873 | } | |
852c2936 MD |
874 | |
875 | /** | |
876 | * lib_ring_buffer_put_subbuf - release exclusive subbuffer access | |
877 | * @buf: ring buffer | |
878 | */ | |
1d498196 MD |
879 | void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf, |
880 | struct shm_handle *handle) | |
852c2936 MD |
881 | { |
882 | struct lib_ring_buffer_backend *bufb = &buf->backend; | |
1d498196 | 883 | struct channel *chan = shmp(handle, bufb->chan); |
193183fb | 884 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
885 | unsigned long read_sb_bindex, consumed_idx, consumed; |
886 | ||
824f40b8 MD |
887 | CHAN_WARN_ON(chan, uatomic_read(&buf->active_readers) != 1 |
888 | && uatomic_read(&buf->active_shadow_readers) != 1); | |
852c2936 MD |
889 | |
890 | if (!buf->get_subbuf) { | |
891 | /* | |
892 | * Reader puts a subbuffer it did not get. | |
893 | */ | |
894 | CHAN_WARN_ON(chan, 1); | |
895 | return; | |
896 | } | |
897 | consumed = buf->get_subbuf_consumed; | |
898 | buf->get_subbuf = 0; | |
899 | ||
900 | /* | |
901 | * Clear the records_unread counter. (overruns counter) | |
902 | * Can still be non-zero if a file reader simply grabbed the data | |
903 | * without using iterators. | |
904 | * Can be below zero if an iterator is used on a snapshot more than | |
905 | * once. | |
906 | */ | |
907 | read_sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
908 | v_add(config, v_read(config, | |
4746ae29 | 909 | &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread), |
852c2936 | 910 | &bufb->records_read); |
4746ae29 | 911 | v_set(config, &shmp(handle, shmp_index(handle, bufb->array, read_sb_bindex)->shmp)->records_unread, 0); |
852c2936 MD |
912 | CHAN_WARN_ON(chan, config->mode == RING_BUFFER_OVERWRITE |
913 | && subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
914 | subbuffer_id_set_noref(config, &bufb->buf_rsb.id); | |
915 | ||
916 | /* | |
917 | * Exchange the reader subbuffer with the one we put in its place in the | |
918 | * writer subbuffer table. Expect the original consumed count. If | |
919 | * update_read_sb_index fails, this is because the writer updated the | |
920 | * subbuffer concurrently. We should therefore keep the subbuffer we | |
921 | * currently have: it has become invalid to try reading this sub-buffer | |
922 | * consumed count value anyway. | |
923 | */ | |
924 | consumed_idx = subbuf_index(consumed, chan); | |
925 | update_read_sb_index(config, &buf->backend, &chan->backend, | |
1d498196 MD |
926 | consumed_idx, buf_trunc_val(consumed, chan), |
927 | handle); | |
852c2936 MD |
928 | /* |
929 | * update_read_sb_index return value ignored. Don't exchange sub-buffer | |
930 | * if the writer concurrently updated it. | |
931 | */ | |
932 | } | |
852c2936 MD |
933 | |
934 | /* | |
935 | * cons_offset is an iterator on all subbuffer offsets between the reader | |
936 | * position and the writer position. (inclusive) | |
937 | */ | |
938 | static | |
939 | void lib_ring_buffer_print_subbuffer_errors(struct lib_ring_buffer *buf, | |
940 | struct channel *chan, | |
941 | unsigned long cons_offset, | |
1d498196 MD |
942 | int cpu, |
943 | struct shm_handle *handle) | |
852c2936 | 944 | { |
193183fb | 945 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
946 | unsigned long cons_idx, commit_count, commit_count_sb; |
947 | ||
948 | cons_idx = subbuf_index(cons_offset, chan); | |
4746ae29 MD |
949 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, cons_idx)->cc); |
950 | commit_count_sb = v_read(config, &shmp_index(handle, buf->commit_cold, cons_idx)->cc_sb); | |
852c2936 MD |
951 | |
952 | if (subbuf_offset(commit_count, chan) != 0) | |
a6352fd4 | 953 | ERRMSG("ring buffer %s, cpu %d: " |
852c2936 MD |
954 | "commit count in subbuffer %lu,\n" |
955 | "expecting multiples of %lu bytes\n" | |
956 | " [ %lu bytes committed, %lu bytes reader-visible ]\n", | |
957 | chan->backend.name, cpu, cons_idx, | |
958 | chan->backend.subbuf_size, | |
959 | commit_count, commit_count_sb); | |
960 | ||
a6352fd4 | 961 | ERRMSG("ring buffer: %s, cpu %d: %lu bytes committed\n", |
852c2936 MD |
962 | chan->backend.name, cpu, commit_count); |
963 | } | |
964 | ||
965 | static | |
966 | void lib_ring_buffer_print_buffer_errors(struct lib_ring_buffer *buf, | |
967 | struct channel *chan, | |
1d498196 MD |
968 | void *priv, int cpu, |
969 | struct shm_handle *handle) | |
852c2936 | 970 | { |
193183fb | 971 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
972 | unsigned long write_offset, cons_offset; |
973 | ||
974 | /* | |
975 | * Can be called in the error path of allocation when | |
976 | * trans_channel_data is not yet set. | |
977 | */ | |
978 | if (!chan) | |
979 | return; | |
980 | /* | |
981 | * No need to order commit_count, write_offset and cons_offset reads | |
982 | * because we execute at teardown when no more writer nor reader | |
983 | * references are left. | |
984 | */ | |
985 | write_offset = v_read(config, &buf->offset); | |
a6352fd4 | 986 | cons_offset = uatomic_read(&buf->consumed); |
852c2936 | 987 | if (write_offset != cons_offset) |
a6352fd4 | 988 | ERRMSG("ring buffer %s, cpu %d: " |
852c2936 MD |
989 | "non-consumed data\n" |
990 | " [ %lu bytes written, %lu bytes read ]\n", | |
991 | chan->backend.name, cpu, write_offset, cons_offset); | |
992 | ||
a6352fd4 | 993 | for (cons_offset = uatomic_read(&buf->consumed); |
852c2936 MD |
994 | (long) (subbuf_trunc((unsigned long) v_read(config, &buf->offset), |
995 | chan) | |
996 | - cons_offset) > 0; | |
997 | cons_offset = subbuf_align(cons_offset, chan)) | |
998 | lib_ring_buffer_print_subbuffer_errors(buf, chan, cons_offset, | |
1d498196 | 999 | cpu, handle); |
852c2936 MD |
1000 | } |
1001 | ||
1002 | static | |
1003 | void lib_ring_buffer_print_errors(struct channel *chan, | |
1d498196 MD |
1004 | struct lib_ring_buffer *buf, int cpu, |
1005 | struct shm_handle *handle) | |
852c2936 | 1006 | { |
193183fb | 1007 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1008 | void *priv = chan->backend.priv; |
1009 | ||
a6352fd4 | 1010 | ERRMSG("ring buffer %s, cpu %d: %lu records written, " |
852c2936 MD |
1011 | "%lu records overrun\n", |
1012 | chan->backend.name, cpu, | |
1013 | v_read(config, &buf->records_count), | |
1014 | v_read(config, &buf->records_overrun)); | |
1015 | ||
1016 | if (v_read(config, &buf->records_lost_full) | |
1017 | || v_read(config, &buf->records_lost_wrap) | |
1018 | || v_read(config, &buf->records_lost_big)) | |
a6352fd4 | 1019 | ERRMSG("ring buffer %s, cpu %d: records were lost. Caused by:\n" |
852c2936 MD |
1020 | " [ %lu buffer full, %lu nest buffer wrap-around, " |
1021 | "%lu event too big ]\n", | |
1022 | chan->backend.name, cpu, | |
1023 | v_read(config, &buf->records_lost_full), | |
1024 | v_read(config, &buf->records_lost_wrap), | |
1025 | v_read(config, &buf->records_lost_big)); | |
1026 | ||
1d498196 | 1027 | lib_ring_buffer_print_buffer_errors(buf, chan, priv, cpu, handle); |
852c2936 MD |
1028 | } |
1029 | ||
1030 | /* | |
1031 | * lib_ring_buffer_switch_old_start: Populate old subbuffer header. | |
1032 | * | |
1033 | * Only executed when the buffer is finalized, in SWITCH_FLUSH. | |
1034 | */ | |
1035 | static | |
1036 | void lib_ring_buffer_switch_old_start(struct lib_ring_buffer *buf, | |
1037 | struct channel *chan, | |
1038 | struct switch_offsets *offsets, | |
1d498196 MD |
1039 | u64 tsc, |
1040 | struct shm_handle *handle) | |
852c2936 | 1041 | { |
193183fb | 1042 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1043 | unsigned long oldidx = subbuf_index(offsets->old, chan); |
1044 | unsigned long commit_count; | |
1045 | ||
1d498196 | 1046 | config->cb.buffer_begin(buf, tsc, oldidx, handle); |
852c2936 MD |
1047 | |
1048 | /* | |
1049 | * Order all writes to buffer before the commit count update that will | |
1050 | * determine that the subbuffer is full. | |
1051 | */ | |
a6352fd4 | 1052 | cmm_smp_wmb(); |
852c2936 | 1053 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1054 | &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1055 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 MD |
1056 | /* Check if the written buffer has to be delivered */ |
1057 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old, | |
1d498196 | 1058 | commit_count, oldidx, handle); |
852c2936 MD |
1059 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1060 | offsets->old, commit_count, | |
1d498196 MD |
1061 | config->cb.subbuffer_header_size(), |
1062 | handle); | |
852c2936 MD |
1063 | } |
1064 | ||
1065 | /* | |
1066 | * lib_ring_buffer_switch_old_end: switch old subbuffer | |
1067 | * | |
1068 | * Note : offset_old should never be 0 here. It is ok, because we never perform | |
1069 | * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller | |
1070 | * increments the offset_old value when doing a SWITCH_FLUSH on an empty | |
1071 | * subbuffer. | |
1072 | */ | |
1073 | static | |
1074 | void lib_ring_buffer_switch_old_end(struct lib_ring_buffer *buf, | |
1075 | struct channel *chan, | |
1076 | struct switch_offsets *offsets, | |
1d498196 MD |
1077 | u64 tsc, |
1078 | struct shm_handle *handle) | |
852c2936 | 1079 | { |
193183fb | 1080 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1081 | unsigned long oldidx = subbuf_index(offsets->old - 1, chan); |
1082 | unsigned long commit_count, padding_size, data_size; | |
1083 | ||
1084 | data_size = subbuf_offset(offsets->old - 1, chan) + 1; | |
1085 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1086 | subbuffer_set_data_size(config, &buf->backend, oldidx, data_size, |
1087 | handle); | |
852c2936 MD |
1088 | |
1089 | /* | |
1090 | * Order all writes to buffer before the commit count update that will | |
1091 | * determine that the subbuffer is full. | |
1092 | */ | |
a6352fd4 | 1093 | cmm_smp_wmb(); |
4746ae29 MD |
1094 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, oldidx)->cc); |
1095 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, oldidx)->cc); | |
852c2936 | 1096 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->old - 1, |
1d498196 | 1097 | commit_count, oldidx, handle); |
852c2936 MD |
1098 | lib_ring_buffer_write_commit_counter(config, buf, chan, oldidx, |
1099 | offsets->old, commit_count, | |
1d498196 | 1100 | padding_size, handle); |
852c2936 MD |
1101 | } |
1102 | ||
1103 | /* | |
1104 | * lib_ring_buffer_switch_new_start: Populate new subbuffer. | |
1105 | * | |
1106 | * This code can be executed unordered : writers may already have written to the | |
1107 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
1108 | * that this code is executed before the deliver of this sub-buffer. | |
1109 | */ | |
1110 | static | |
1111 | void lib_ring_buffer_switch_new_start(struct lib_ring_buffer *buf, | |
1112 | struct channel *chan, | |
1113 | struct switch_offsets *offsets, | |
1d498196 MD |
1114 | u64 tsc, |
1115 | struct shm_handle *handle) | |
852c2936 | 1116 | { |
193183fb | 1117 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1118 | unsigned long beginidx = subbuf_index(offsets->begin, chan); |
1119 | unsigned long commit_count; | |
1120 | ||
1d498196 | 1121 | config->cb.buffer_begin(buf, tsc, beginidx, handle); |
852c2936 MD |
1122 | |
1123 | /* | |
1124 | * Order all writes to buffer before the commit count update that will | |
1125 | * determine that the subbuffer is full. | |
1126 | */ | |
a6352fd4 | 1127 | cmm_smp_wmb(); |
852c2936 | 1128 | v_add(config, config->cb.subbuffer_header_size(), |
4746ae29 MD |
1129 | &shmp_index(handle, buf->commit_hot, beginidx)->cc); |
1130 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, beginidx)->cc); | |
852c2936 MD |
1131 | /* Check if the written buffer has to be delivered */ |
1132 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->begin, | |
1d498196 | 1133 | commit_count, beginidx, handle); |
852c2936 MD |
1134 | lib_ring_buffer_write_commit_counter(config, buf, chan, beginidx, |
1135 | offsets->begin, commit_count, | |
1d498196 MD |
1136 | config->cb.subbuffer_header_size(), |
1137 | handle); | |
852c2936 MD |
1138 | } |
1139 | ||
1140 | /* | |
1141 | * lib_ring_buffer_switch_new_end: finish switching current subbuffer | |
1142 | * | |
1143 | * The only remaining threads could be the ones with pending commits. They will | |
1144 | * have to do the deliver themselves. | |
1145 | */ | |
1146 | static | |
1147 | void lib_ring_buffer_switch_new_end(struct lib_ring_buffer *buf, | |
1d498196 MD |
1148 | struct channel *chan, |
1149 | struct switch_offsets *offsets, | |
1150 | u64 tsc, | |
1151 | struct shm_handle *handle) | |
852c2936 | 1152 | { |
193183fb | 1153 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1154 | unsigned long endidx = subbuf_index(offsets->end - 1, chan); |
1155 | unsigned long commit_count, padding_size, data_size; | |
1156 | ||
1157 | data_size = subbuf_offset(offsets->end - 1, chan) + 1; | |
1158 | padding_size = chan->backend.subbuf_size - data_size; | |
1d498196 MD |
1159 | subbuffer_set_data_size(config, &buf->backend, endidx, data_size, |
1160 | handle); | |
852c2936 MD |
1161 | |
1162 | /* | |
1163 | * Order all writes to buffer before the commit count update that will | |
1164 | * determine that the subbuffer is full. | |
1165 | */ | |
a6352fd4 | 1166 | cmm_smp_wmb(); |
4746ae29 MD |
1167 | v_add(config, padding_size, &shmp_index(handle, buf->commit_hot, endidx)->cc); |
1168 | commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc); | |
852c2936 | 1169 | lib_ring_buffer_check_deliver(config, buf, chan, offsets->end - 1, |
1d498196 | 1170 | commit_count, endidx, handle); |
852c2936 MD |
1171 | lib_ring_buffer_write_commit_counter(config, buf, chan, endidx, |
1172 | offsets->end, commit_count, | |
1d498196 | 1173 | padding_size, handle); |
852c2936 MD |
1174 | } |
1175 | ||
1176 | /* | |
1177 | * Returns : | |
1178 | * 0 if ok | |
1179 | * !0 if execution must be aborted. | |
1180 | */ | |
1181 | static | |
1182 | int lib_ring_buffer_try_switch_slow(enum switch_mode mode, | |
1183 | struct lib_ring_buffer *buf, | |
1184 | struct channel *chan, | |
1185 | struct switch_offsets *offsets, | |
1186 | u64 *tsc) | |
1187 | { | |
193183fb | 1188 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1189 | unsigned long off; |
1190 | ||
1191 | offsets->begin = v_read(config, &buf->offset); | |
1192 | offsets->old = offsets->begin; | |
1193 | offsets->switch_old_start = 0; | |
1194 | off = subbuf_offset(offsets->begin, chan); | |
1195 | ||
1196 | *tsc = config->cb.ring_buffer_clock_read(chan); | |
1197 | ||
1198 | /* | |
1199 | * Ensure we flush the header of an empty subbuffer when doing the | |
1200 | * finalize (SWITCH_FLUSH). This ensures that we end up knowing the | |
1201 | * total data gathering duration even if there were no records saved | |
1202 | * after the last buffer switch. | |
1203 | * In SWITCH_ACTIVE mode, switch the buffer when it contains events. | |
1204 | * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of | |
1205 | * subbuffer header as appropriate. | |
1206 | * The next record that reserves space will be responsible for | |
1207 | * populating the following subbuffer header. We choose not to populate | |
1208 | * the next subbuffer header here because we want to be able to use | |
a6352fd4 MD |
1209 | * SWITCH_ACTIVE for periodical buffer flush, which must |
1210 | * guarantee that all the buffer content (records and header | |
1211 | * timestamps) are visible to the reader. This is required for | |
1212 | * quiescence guarantees for the fusion merge. | |
852c2936 MD |
1213 | */ |
1214 | if (mode == SWITCH_FLUSH || off > 0) { | |
1215 | if (unlikely(off == 0)) { | |
1216 | /* | |
1217 | * The client does not save any header information. | |
1218 | * Don't switch empty subbuffer on finalize, because it | |
1219 | * is invalid to deliver a completely empty subbuffer. | |
1220 | */ | |
1221 | if (!config->cb.subbuffer_header_size()) | |
1222 | return -1; | |
1223 | /* | |
1224 | * Need to write the subbuffer start header on finalize. | |
1225 | */ | |
1226 | offsets->switch_old_start = 1; | |
1227 | } | |
1228 | offsets->begin = subbuf_align(offsets->begin, chan); | |
1229 | } else | |
1230 | return -1; /* we do not have to switch : buffer is empty */ | |
1231 | /* Note: old points to the next subbuf at offset 0 */ | |
1232 | offsets->end = offsets->begin; | |
1233 | return 0; | |
1234 | } | |
1235 | ||
1236 | /* | |
1237 | * Force a sub-buffer switch. This operation is completely reentrant : can be | |
1238 | * called while tracing is active with absolutely no lock held. | |
1239 | * | |
1240 | * Note, however, that as a v_cmpxchg is used for some atomic | |
1241 | * operations, this function must be called from the CPU which owns the buffer | |
1242 | * for a ACTIVE flush. | |
1243 | */ | |
1d498196 MD |
1244 | void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, enum switch_mode mode, |
1245 | struct shm_handle *handle) | |
852c2936 | 1246 | { |
1d498196 | 1247 | struct channel *chan = shmp(handle, buf->backend.chan); |
193183fb | 1248 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1249 | struct switch_offsets offsets; |
1250 | unsigned long oldidx; | |
1251 | u64 tsc; | |
1252 | ||
1253 | offsets.size = 0; | |
1254 | ||
1255 | /* | |
1256 | * Perform retryable operations. | |
1257 | */ | |
1258 | do { | |
1259 | if (lib_ring_buffer_try_switch_slow(mode, buf, chan, &offsets, | |
1260 | &tsc)) | |
1261 | return; /* Switch not needed */ | |
1262 | } while (v_cmpxchg(config, &buf->offset, offsets.old, offsets.end) | |
1263 | != offsets.old); | |
1264 | ||
1265 | /* | |
1266 | * Atomically update last_tsc. This update races against concurrent | |
1267 | * atomic updates, but the race will always cause supplementary full TSC | |
1268 | * records, never the opposite (missing a full TSC record when it would | |
1269 | * be needed). | |
1270 | */ | |
1271 | save_last_tsc(config, buf, tsc); | |
1272 | ||
1273 | /* | |
1274 | * Push the reader if necessary | |
1275 | */ | |
1276 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.old); | |
1277 | ||
1278 | oldidx = subbuf_index(offsets.old, chan); | |
1d498196 | 1279 | lib_ring_buffer_clear_noref(config, &buf->backend, oldidx, handle); |
852c2936 MD |
1280 | |
1281 | /* | |
1282 | * May need to populate header start on SWITCH_FLUSH. | |
1283 | */ | |
1284 | if (offsets.switch_old_start) { | |
1d498196 | 1285 | lib_ring_buffer_switch_old_start(buf, chan, &offsets, tsc, handle); |
852c2936 MD |
1286 | offsets.old += config->cb.subbuffer_header_size(); |
1287 | } | |
1288 | ||
1289 | /* | |
1290 | * Switch old subbuffer. | |
1291 | */ | |
1d498196 | 1292 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, tsc, handle); |
852c2936 | 1293 | } |
852c2936 MD |
1294 | |
1295 | /* | |
1296 | * Returns : | |
1297 | * 0 if ok | |
1298 | * -ENOSPC if event size is too large for packet. | |
1299 | * -ENOBUFS if there is currently not enough space in buffer for the event. | |
1300 | * -EIO if data cannot be written into the buffer for any other reason. | |
1301 | */ | |
1302 | static | |
1303 | int lib_ring_buffer_try_reserve_slow(struct lib_ring_buffer *buf, | |
1304 | struct channel *chan, | |
1305 | struct switch_offsets *offsets, | |
1306 | struct lib_ring_buffer_ctx *ctx) | |
1307 | { | |
193183fb | 1308 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
1d498196 | 1309 | struct shm_handle *handle = ctx->handle; |
852c2936 MD |
1310 | unsigned long reserve_commit_diff; |
1311 | ||
1312 | offsets->begin = v_read(config, &buf->offset); | |
1313 | offsets->old = offsets->begin; | |
1314 | offsets->switch_new_start = 0; | |
1315 | offsets->switch_new_end = 0; | |
1316 | offsets->switch_old_end = 0; | |
1317 | offsets->pre_header_padding = 0; | |
1318 | ||
1319 | ctx->tsc = config->cb.ring_buffer_clock_read(chan); | |
1320 | if ((int64_t) ctx->tsc == -EIO) | |
1321 | return -EIO; | |
1322 | ||
1323 | if (last_tsc_overflow(config, buf, ctx->tsc)) | |
1324 | ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC; | |
1325 | ||
1326 | if (unlikely(subbuf_offset(offsets->begin, ctx->chan) == 0)) { | |
1327 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1328 | } else { | |
1329 | offsets->size = config->cb.record_header_size(config, chan, | |
1330 | offsets->begin, | |
1331 | &offsets->pre_header_padding, | |
1332 | ctx); | |
1333 | offsets->size += | |
1334 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1335 | ctx->largest_align) | |
1336 | + ctx->data_size; | |
1337 | if (unlikely(subbuf_offset(offsets->begin, chan) + | |
1338 | offsets->size > chan->backend.subbuf_size)) { | |
1339 | offsets->switch_old_end = 1; /* For offsets->old */ | |
1340 | offsets->switch_new_start = 1; /* For offsets->begin */ | |
1341 | } | |
1342 | } | |
1343 | if (unlikely(offsets->switch_new_start)) { | |
1344 | unsigned long sb_index; | |
1345 | ||
1346 | /* | |
1347 | * We are typically not filling the previous buffer completely. | |
1348 | */ | |
1349 | if (likely(offsets->switch_old_end)) | |
1350 | offsets->begin = subbuf_align(offsets->begin, chan); | |
1351 | offsets->begin = offsets->begin | |
1352 | + config->cb.subbuffer_header_size(); | |
1353 | /* Test new buffer integrity */ | |
1354 | sb_index = subbuf_index(offsets->begin, chan); | |
1355 | reserve_commit_diff = | |
1356 | (buf_trunc(offsets->begin, chan) | |
1357 | >> chan->backend.num_subbuf_order) | |
1358 | - ((unsigned long) v_read(config, | |
4746ae29 | 1359 | &shmp_index(handle, buf->commit_cold, sb_index)->cc_sb) |
852c2936 MD |
1360 | & chan->commit_count_mask); |
1361 | if (likely(reserve_commit_diff == 0)) { | |
1362 | /* Next subbuffer not being written to. */ | |
1363 | if (unlikely(config->mode != RING_BUFFER_OVERWRITE && | |
1364 | subbuf_trunc(offsets->begin, chan) | |
1365 | - subbuf_trunc((unsigned long) | |
a6352fd4 | 1366 | uatomic_read(&buf->consumed), chan) |
852c2936 MD |
1367 | >= chan->backend.buf_size)) { |
1368 | /* | |
1369 | * We do not overwrite non consumed buffers | |
1370 | * and we are full : record is lost. | |
1371 | */ | |
1372 | v_inc(config, &buf->records_lost_full); | |
1373 | return -ENOBUFS; | |
1374 | } else { | |
1375 | /* | |
1376 | * Next subbuffer not being written to, and we | |
1377 | * are either in overwrite mode or the buffer is | |
1378 | * not full. It's safe to write in this new | |
1379 | * subbuffer. | |
1380 | */ | |
1381 | } | |
1382 | } else { | |
1383 | /* | |
1384 | * Next subbuffer reserve offset does not match the | |
1385 | * commit offset. Drop record in producer-consumer and | |
1386 | * overwrite mode. Caused by either a writer OOPS or too | |
1387 | * many nested writes over a reserve/commit pair. | |
1388 | */ | |
1389 | v_inc(config, &buf->records_lost_wrap); | |
1390 | return -EIO; | |
1391 | } | |
1392 | offsets->size = | |
1393 | config->cb.record_header_size(config, chan, | |
1394 | offsets->begin, | |
1395 | &offsets->pre_header_padding, | |
1396 | ctx); | |
1397 | offsets->size += | |
1398 | lib_ring_buffer_align(offsets->begin + offsets->size, | |
1399 | ctx->largest_align) | |
1400 | + ctx->data_size; | |
1401 | if (unlikely(subbuf_offset(offsets->begin, chan) | |
1402 | + offsets->size > chan->backend.subbuf_size)) { | |
1403 | /* | |
1404 | * Record too big for subbuffers, report error, don't | |
1405 | * complete the sub-buffer switch. | |
1406 | */ | |
1407 | v_inc(config, &buf->records_lost_big); | |
1408 | return -ENOSPC; | |
1409 | } else { | |
1410 | /* | |
1411 | * We just made a successful buffer switch and the | |
1412 | * record fits in the new subbuffer. Let's write. | |
1413 | */ | |
1414 | } | |
1415 | } else { | |
1416 | /* | |
1417 | * Record fits in the current buffer and we are not on a switch | |
1418 | * boundary. It's safe to write. | |
1419 | */ | |
1420 | } | |
1421 | offsets->end = offsets->begin + offsets->size; | |
1422 | ||
1423 | if (unlikely(subbuf_offset(offsets->end, chan) == 0)) { | |
1424 | /* | |
1425 | * The offset_end will fall at the very beginning of the next | |
1426 | * subbuffer. | |
1427 | */ | |
1428 | offsets->switch_new_end = 1; /* For offsets->begin */ | |
1429 | } | |
1430 | return 0; | |
1431 | } | |
1432 | ||
1433 | /** | |
1434 | * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer. | |
1435 | * @ctx: ring buffer context. | |
1436 | * | |
1437 | * Return : -NOBUFS if not enough space, -ENOSPC if event size too large, | |
1438 | * -EIO for other errors, else returns 0. | |
1439 | * It will take care of sub-buffer switching. | |
1440 | */ | |
1441 | int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx) | |
1442 | { | |
1443 | struct channel *chan = ctx->chan; | |
1d498196 | 1444 | struct shm_handle *handle = ctx->handle; |
193183fb | 1445 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
852c2936 MD |
1446 | struct lib_ring_buffer *buf; |
1447 | struct switch_offsets offsets; | |
1448 | int ret; | |
1449 | ||
1450 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) | |
1d498196 | 1451 | buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp); |
852c2936 | 1452 | else |
1d498196 | 1453 | buf = shmp(handle, chan->backend.buf[0].shmp); |
852c2936 MD |
1454 | ctx->buf = buf; |
1455 | ||
1456 | offsets.size = 0; | |
1457 | ||
1458 | do { | |
1459 | ret = lib_ring_buffer_try_reserve_slow(buf, chan, &offsets, | |
1460 | ctx); | |
1461 | if (unlikely(ret)) | |
1462 | return ret; | |
1463 | } while (unlikely(v_cmpxchg(config, &buf->offset, offsets.old, | |
1464 | offsets.end) | |
1465 | != offsets.old)); | |
1466 | ||
1467 | /* | |
1468 | * Atomically update last_tsc. This update races against concurrent | |
1469 | * atomic updates, but the race will always cause supplementary full TSC | |
1470 | * records, never the opposite (missing a full TSC record when it would | |
1471 | * be needed). | |
1472 | */ | |
1473 | save_last_tsc(config, buf, ctx->tsc); | |
1474 | ||
1475 | /* | |
1476 | * Push the reader if necessary | |
1477 | */ | |
1478 | lib_ring_buffer_reserve_push_reader(buf, chan, offsets.end - 1); | |
1479 | ||
1480 | /* | |
1481 | * Clear noref flag for this subbuffer. | |
1482 | */ | |
1483 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1d498196 MD |
1484 | subbuf_index(offsets.end - 1, chan), |
1485 | handle); | |
852c2936 MD |
1486 | |
1487 | /* | |
1488 | * Switch old subbuffer if needed. | |
1489 | */ | |
1490 | if (unlikely(offsets.switch_old_end)) { | |
1491 | lib_ring_buffer_clear_noref(config, &buf->backend, | |
1d498196 MD |
1492 | subbuf_index(offsets.old - 1, chan), |
1493 | handle); | |
1494 | lib_ring_buffer_switch_old_end(buf, chan, &offsets, ctx->tsc, handle); | |
852c2936 MD |
1495 | } |
1496 | ||
1497 | /* | |
1498 | * Populate new subbuffer. | |
1499 | */ | |
1500 | if (unlikely(offsets.switch_new_start)) | |
1d498196 | 1501 | lib_ring_buffer_switch_new_start(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 MD |
1502 | |
1503 | if (unlikely(offsets.switch_new_end)) | |
1d498196 | 1504 | lib_ring_buffer_switch_new_end(buf, chan, &offsets, ctx->tsc, handle); |
852c2936 MD |
1505 | |
1506 | ctx->slot_size = offsets.size; | |
1507 | ctx->pre_offset = offsets.begin; | |
1508 | ctx->buf_offset = offsets.begin + offsets.pre_header_padding; | |
1509 | return 0; | |
1510 | } |