2 * ring_buffer_backend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
23 #include <urcu/arch.h>
26 #include <lttng/ringbuffer-config.h>
34 * lib_ring_buffer_backend_allocate - allocate a channel buffer
35 * @config: ring buffer instance configuration
36 * @buf: the buffer struct
37 * @size: total size of the buffer
38 * @num_subbuf: number of subbuffers
39 * @extra_reader_sb: need extra subbuffer for reader
42 int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config
*config
,
43 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
44 size_t size
, size_t num_subbuf
,
46 struct lttng_ust_shm_handle
*handle
,
47 struct shm_object
*shmobj
)
49 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
50 unsigned long subbuf_size
, mmap_offset
= 0;
51 unsigned long num_subbuf_alloc
;
55 subbuf_size
= chanb
->subbuf_size
;
56 num_subbuf_alloc
= num_subbuf
;
61 page_size
= sysconf(_SC_PAGE_SIZE
);
66 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
67 set_shmp(bufb
->array
, zalloc_shm(shmobj
,
68 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
));
69 if (caa_unlikely(!shmp(handle
, bufb
->array
)))
73 * This is the largest element (the buffer pages) which needs to
74 * be aligned on page size.
76 align_shm(shmobj
, page_size
);
77 set_shmp(bufb
->memory_map
, zalloc_shm(shmobj
,
78 subbuf_size
* num_subbuf_alloc
));
79 if (caa_unlikely(!shmp(handle
, bufb
->memory_map
)))
80 goto memory_map_error
;
82 /* Allocate backend pages array elements */
83 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
84 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
85 set_shmp(shmp_index(handle
, bufb
->array
, i
)->shmp
,
87 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
)));
88 if (!shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
))
92 /* Allocate write-side subbuffer table */
93 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
94 set_shmp(bufb
->buf_wsb
, zalloc_shm(shmobj
,
95 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
)
97 if (caa_unlikely(!shmp(handle
, bufb
->buf_wsb
)))
100 for (i
= 0; i
< num_subbuf
; i
++)
101 shmp_index(handle
, bufb
->buf_wsb
, i
)->id
= subbuffer_id(config
, 0, 1, i
);
103 /* Assign read-side subbuffer table */
105 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
106 num_subbuf_alloc
- 1);
108 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
110 /* Assign pages to page index */
111 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
114 ref
.index
= bufb
->memory_map
._ref
.index
;
115 ref
.offset
= bufb
->memory_map
._ref
.offset
;
116 ref
.offset
+= i
* subbuf_size
;
118 set_shmp(shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->p
,
120 if (config
->output
== RING_BUFFER_MMAP
) {
121 shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->mmap_offset
= mmap_offset
;
122 mmap_offset
+= subbuf_size
;
128 /* bufb->array[i] will be freed by shm teardown */
130 /* bufb->array will be freed by shm teardown */
136 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
137 struct channel_backend
*chanb
, int cpu
,
138 struct lttng_ust_shm_handle
*handle
,
139 struct shm_object
*shmobj
)
141 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
143 set_shmp(bufb
->chan
, handle
->chan
._ref
);
146 return lib_ring_buffer_backend_allocate(config
, bufb
, chanb
->buf_size
,
148 chanb
->extra_reader_sb
,
152 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
153 struct lttng_ust_shm_handle
*handle
)
155 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
156 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
157 unsigned long num_subbuf_alloc
;
160 num_subbuf_alloc
= chanb
->num_subbuf
;
161 if (chanb
->extra_reader_sb
)
164 for (i
= 0; i
< chanb
->num_subbuf
; i
++)
165 shmp_index(handle
, bufb
->buf_wsb
, i
)->id
= subbuffer_id(config
, 0, 1, i
);
166 if (chanb
->extra_reader_sb
)
167 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
168 num_subbuf_alloc
- 1);
170 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
172 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
173 /* Don't reset mmap_offset */
174 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->records_commit
, 0);
175 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->records_unread
, 0);
176 shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->data_size
= 0;
177 /* Don't reset backend page and virt addresses */
179 /* Don't reset num_pages_per_subbuf, cpu, allocated */
180 v_set(config
, &bufb
->records_read
, 0);
184 * The frontend is responsible for also calling ring_buffer_backend_reset for
185 * each buffer when calling channel_backend_reset.
187 void channel_backend_reset(struct channel_backend
*chanb
)
189 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
190 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
193 * Don't reset buf_size, subbuf_size, subbuf_size_order,
194 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
195 * priv, notifiers, config, cpumask and name.
197 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
201 * channel_backend_init - initialize a channel backend
202 * @chanb: channel backend
203 * @name: channel name
204 * @config: client ring buffer configuration
205 * @parent: dentry of parent directory, %NULL for root directory
206 * @subbuf_size: size of sub-buffers (> page size, power of 2)
207 * @num_subbuf: number of sub-buffers (power of 2)
208 * @lttng_ust_shm_handle: shared memory handle
209 * @stream_fds: stream file descriptors.
211 * Returns channel pointer if successful, %NULL otherwise.
213 * Creates per-cpu channel buffers using the sizes and attributes
214 * specified. The created channel buffer files will be named
215 * name_0...name_N-1. File permissions will be %S_IRUSR.
217 * Called with CPU hotplug disabled.
219 int channel_backend_init(struct channel_backend
*chanb
,
221 const struct lttng_ust_lib_ring_buffer_config
*config
,
222 size_t subbuf_size
, size_t num_subbuf
,
223 struct lttng_ust_shm_handle
*handle
,
224 const int *stream_fds
)
226 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
229 size_t shmsize
= 0, num_subbuf_alloc
;
235 page_size
= sysconf(_SC_PAGE_SIZE
);
236 if (page_size
<= 0) {
239 /* Check that the subbuffer size is larger than a page. */
240 if (subbuf_size
< page_size
)
244 * Make sure the number of subbuffers and subbuffer size are
245 * power of 2, and nonzero.
247 if (!subbuf_size
|| (subbuf_size
& (subbuf_size
- 1)))
249 if (!num_subbuf
|| (num_subbuf
& (num_subbuf
- 1)))
252 * Overwrite mode buffers require at least 2 subbuffers per
255 if (config
->mode
== RING_BUFFER_OVERWRITE
&& num_subbuf
< 2)
258 ret
= subbuffer_id_check_index(config
, num_subbuf
);
262 chanb
->buf_size
= num_subbuf
* subbuf_size
;
263 chanb
->subbuf_size
= subbuf_size
;
264 chanb
->buf_size_order
= get_count_order(chanb
->buf_size
);
265 chanb
->subbuf_size_order
= get_count_order(subbuf_size
);
266 chanb
->num_subbuf_order
= get_count_order(num_subbuf
);
267 chanb
->extra_reader_sb
=
268 (config
->mode
== RING_BUFFER_OVERWRITE
) ? 1 : 0;
269 chanb
->num_subbuf
= num_subbuf
;
270 strncpy(chanb
->name
, name
, NAME_MAX
);
271 chanb
->name
[NAME_MAX
- 1] = '\0';
272 memcpy(&chanb
->config
, config
, sizeof(*config
));
274 /* Per-cpu buffer size: control (prior to backend) */
275 shmsize
= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer
));
276 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer
);
278 /* Per-cpu buffer size: backend */
279 /* num_subbuf + 1 is the worse case */
280 num_subbuf_alloc
= num_subbuf
+ 1;
281 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
282 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
;
283 shmsize
+= offset_align(shmsize
, page_size
);
284 shmsize
+= subbuf_size
* num_subbuf_alloc
;
285 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
286 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
) * num_subbuf_alloc
;
287 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
288 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
289 /* Per-cpu buffer size: control (after backend) */
290 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_hot
));
291 shmsize
+= sizeof(struct commit_counters_hot
) * num_subbuf
;
292 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_cold
));
293 shmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
295 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
296 struct lttng_ust_lib_ring_buffer
*buf
;
298 * We need to allocate for all possible cpus.
300 for_each_possible_cpu(i
) {
301 struct shm_object
*shmobj
;
303 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
304 SHM_OBJECT_SHM
, stream_fds
[i
]);
307 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
308 set_shmp(chanb
->buf
[i
].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
309 buf
= shmp(handle
, chanb
->buf
[i
].shmp
);
312 set_shmp(buf
->self
, chanb
->buf
[i
].shmp
._ref
);
313 ret
= lib_ring_buffer_create(buf
, chanb
, i
,
316 goto free_bufs
; /* cpu hotplug locked */
319 struct shm_object
*shmobj
;
320 struct lttng_ust_lib_ring_buffer
*buf
;
322 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
323 SHM_OBJECT_SHM
, stream_fds
[0]);
326 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
327 set_shmp(chanb
->buf
[0].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
328 buf
= shmp(handle
, chanb
->buf
[0].shmp
);
331 set_shmp(buf
->self
, chanb
->buf
[0].shmp
._ref
);
332 ret
= lib_ring_buffer_create(buf
, chanb
, -1,
337 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
342 /* We only free the buffer data upon shm teardown */
348 * channel_backend_free - destroy the channel
351 * Destroy all channel buffers and frees the channel.
353 void channel_backend_free(struct channel_backend
*chanb
,
354 struct lttng_ust_shm_handle
*handle
)
356 /* SHM teardown takes care of everything */
360 * lib_ring_buffer_read - read data from ring_buffer_buffer.
361 * @bufb : buffer backend
362 * @offset : offset within the buffer
363 * @dest : destination address
364 * @len : length to copy to destination
366 * Should be protected by get_subbuf/put_subbuf.
367 * Returns the length copied.
369 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
370 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
372 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
373 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
375 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
376 unsigned long sb_bindex
, id
;
380 offset
&= chanb
->buf_size
- 1;
382 if (caa_unlikely(!len
))
384 id
= bufb
->buf_rsb
.id
;
385 sb_bindex
= subbuffer_id_get_index(config
, id
);
386 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
388 * Underlying layer should never ask for reads across
391 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
392 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
393 && subbuffer_id_is_noref(config
, id
));
394 src
= shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
,
395 offset
& (chanb
->subbuf_size
- 1));
396 if (caa_unlikely(!src
))
398 memcpy(dest
, src
, len
);
403 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
404 * @bufb : buffer backend
405 * @offset : offset within the buffer
406 * @dest : destination address
407 * @len : destination's length
409 * Return string's length, or -EINVAL on error.
410 * Should be protected by get_subbuf/put_subbuf.
411 * Destination length should be at least 1 to hold '\0'.
413 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
414 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
416 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
417 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
418 ssize_t string_len
, orig_offset
;
420 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
421 unsigned long sb_bindex
, id
;
423 if (caa_unlikely(!len
))
425 offset
&= chanb
->buf_size
- 1;
426 orig_offset
= offset
;
427 id
= bufb
->buf_rsb
.id
;
428 sb_bindex
= subbuffer_id_get_index(config
, id
);
429 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
431 * Underlying layer should never ask for reads across
434 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
435 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
436 && subbuffer_id_is_noref(config
, id
));
437 str
= shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));
438 if (caa_unlikely(!str
))
440 string_len
= strnlen(str
, len
);
442 memcpy(dest
, str
, string_len
);
443 ((char *)dest
)[0] = 0;
445 return offset
- orig_offset
;
449 * lib_ring_buffer_read_offset_address - get address of a buffer location
450 * @bufb : buffer backend
451 * @offset : offset within the buffer.
453 * Return the address where a given offset is located (for read).
454 * Should be used to get the current subbuffer header pointer. Given we know
455 * it's never on a page boundary, it's safe to read/write directly
456 * from/to this address, as long as the read/write is never bigger than
459 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
461 struct lttng_ust_shm_handle
*handle
)
463 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
464 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
465 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
466 unsigned long sb_bindex
, id
;
468 offset
&= chanb
->buf_size
- 1;
469 id
= bufb
->buf_rsb
.id
;
470 sb_bindex
= subbuffer_id_get_index(config
, id
);
471 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
472 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
473 && subbuffer_id_is_noref(config
, id
));
474 return shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));
478 * lib_ring_buffer_offset_address - get address of a location within the buffer
479 * @bufb : buffer backend
480 * @offset : offset within the buffer.
482 * Return the address where a given offset is located.
483 * Should be used to get the current subbuffer header pointer. Given we know
484 * it's always at the beginning of a page, it's safe to write directly to this
485 * address, as long as the write is never bigger than a page size.
487 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
489 struct lttng_ust_shm_handle
*handle
)
492 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
493 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
494 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
495 unsigned long sb_bindex
, id
;
497 offset
&= chanb
->buf_size
- 1;
498 sbidx
= offset
>> chanb
->subbuf_size_order
;
499 id
= shmp_index(handle
, bufb
->buf_wsb
, sbidx
)->id
;
500 sb_bindex
= subbuffer_id_get_index(config
, id
);
501 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
502 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
503 && subbuffer_id_is_noref(config
, id
));
504 return shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));