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
;
129 /* bufb->array[i] will be freed by shm teardown */
131 /* bufb->array will be freed by shm teardown */
137 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
138 struct channel_backend
*chanb
, int cpu
,
139 struct lttng_ust_shm_handle
*handle
,
140 struct shm_object
*shmobj
)
142 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
144 set_shmp(bufb
->chan
, handle
->chan
._ref
);
147 return lib_ring_buffer_backend_allocate(config
, bufb
, chanb
->buf_size
,
149 chanb
->extra_reader_sb
,
153 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
154 struct lttng_ust_shm_handle
*handle
)
156 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
157 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
158 unsigned long num_subbuf_alloc
;
161 num_subbuf_alloc
= chanb
->num_subbuf
;
162 if (chanb
->extra_reader_sb
)
165 for (i
= 0; i
< chanb
->num_subbuf
; i
++)
166 shmp_index(handle
, bufb
->buf_wsb
, i
)->id
= subbuffer_id(config
, 0, 1, i
);
167 if (chanb
->extra_reader_sb
)
168 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1,
169 num_subbuf_alloc
- 1);
171 bufb
->buf_rsb
.id
= subbuffer_id(config
, 0, 1, 0);
173 for (i
= 0; i
< num_subbuf_alloc
; i
++) {
174 /* Don't reset mmap_offset */
175 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->records_commit
, 0);
176 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->records_unread
, 0);
177 shmp(handle
, shmp_index(handle
, bufb
->array
, i
)->shmp
)->data_size
= 0;
178 /* Don't reset backend page and virt addresses */
180 /* Don't reset num_pages_per_subbuf, cpu, allocated */
181 v_set(config
, &bufb
->records_read
, 0);
185 * The frontend is responsible for also calling ring_buffer_backend_reset for
186 * each buffer when calling channel_backend_reset.
188 void channel_backend_reset(struct channel_backend
*chanb
)
190 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
191 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
194 * Don't reset buf_size, subbuf_size, subbuf_size_order,
195 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
196 * priv, notifiers, config, cpumask and name.
198 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
202 * channel_backend_init - initialize a channel backend
203 * @chanb: channel backend
204 * @name: channel name
205 * @config: client ring buffer configuration
206 * @parent: dentry of parent directory, %NULL for root directory
207 * @subbuf_size: size of sub-buffers (> page size, power of 2)
208 * @num_subbuf: number of sub-buffers (power of 2)
209 * @lttng_ust_shm_handle: shared memory handle
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
)
225 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
228 size_t shmsize
= 0, num_subbuf_alloc
;
234 page_size
= sysconf(_SC_PAGE_SIZE
);
235 if (page_size
<= 0) {
238 /* Check that the subbuffer size is larger than a page. */
239 if (subbuf_size
< page_size
)
243 * Make sure the number of subbuffers and subbuffer size are
244 * power of 2, and nonzero.
246 if (!subbuf_size
|| (subbuf_size
& (subbuf_size
- 1)))
248 if (!num_subbuf
|| (num_subbuf
& (num_subbuf
- 1)))
251 * Overwrite mode buffers require at least 2 subbuffers per
254 if (config
->mode
== RING_BUFFER_OVERWRITE
&& num_subbuf
< 2)
257 ret
= subbuffer_id_check_index(config
, num_subbuf
);
261 chanb
->buf_size
= num_subbuf
* subbuf_size
;
262 chanb
->subbuf_size
= subbuf_size
;
263 chanb
->buf_size_order
= get_count_order(chanb
->buf_size
);
264 chanb
->subbuf_size_order
= get_count_order(subbuf_size
);
265 chanb
->num_subbuf_order
= get_count_order(num_subbuf
);
266 chanb
->extra_reader_sb
=
267 (config
->mode
== RING_BUFFER_OVERWRITE
) ? 1 : 0;
268 chanb
->num_subbuf
= num_subbuf
;
269 strncpy(chanb
->name
, name
, NAME_MAX
);
270 chanb
->name
[NAME_MAX
- 1] = '\0';
271 memcpy(&chanb
->config
, config
, sizeof(*config
));
273 /* Per-cpu buffer size: control (prior to backend) */
274 shmsize
= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer
));
275 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer
);
277 /* Per-cpu buffer size: backend */
278 /* num_subbuf + 1 is the worse case */
279 num_subbuf_alloc
= num_subbuf
+ 1;
280 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
));
281 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp
) * num_subbuf_alloc
;
282 shmsize
+= offset_align(shmsize
, page_size
);
283 shmsize
+= subbuf_size
* num_subbuf_alloc
;
284 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages
));
285 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_pages
) * num_subbuf_alloc
;
286 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer
));
287 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer
) * num_subbuf
;
288 /* Per-cpu buffer size: control (after backend) */
289 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_hot
));
290 shmsize
+= sizeof(struct commit_counters_hot
) * num_subbuf
;
291 shmsize
+= offset_align(shmsize
, __alignof__(struct commit_counters_cold
));
292 shmsize
+= sizeof(struct commit_counters_cold
) * num_subbuf
;
294 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
295 struct lttng_ust_lib_ring_buffer
*buf
;
297 * We need to allocate for all possible cpus.
299 for_each_possible_cpu(i
) {
300 struct shm_object
*shmobj
;
302 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
306 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
307 set_shmp(chanb
->buf
[i
].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
308 buf
= shmp(handle
, chanb
->buf
[i
].shmp
);
311 set_shmp(buf
->self
, chanb
->buf
[i
].shmp
._ref
);
312 ret
= lib_ring_buffer_create(buf
, chanb
, i
,
315 goto free_bufs
; /* cpu hotplug locked */
318 struct shm_object
*shmobj
;
319 struct lttng_ust_lib_ring_buffer
*buf
;
321 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
,
325 align_shm(shmobj
, __alignof__(struct lttng_ust_lib_ring_buffer
));
326 set_shmp(chanb
->buf
[0].shmp
, zalloc_shm(shmobj
, sizeof(struct lttng_ust_lib_ring_buffer
)));
327 buf
= shmp(handle
, chanb
->buf
[0].shmp
);
330 set_shmp(buf
->self
, chanb
->buf
[0].shmp
._ref
);
331 ret
= lib_ring_buffer_create(buf
, chanb
, -1,
336 chanb
->start_tsc
= config
->cb
.ring_buffer_clock_read(chan
);
341 /* We only free the buffer data upon shm teardown */
347 * channel_backend_free - destroy the channel
350 * Destroy all channel buffers and frees the channel.
352 void channel_backend_free(struct channel_backend
*chanb
,
353 struct lttng_ust_shm_handle
*handle
)
355 /* SHM teardown takes care of everything */
359 * lib_ring_buffer_read - read data from ring_buffer_buffer.
360 * @bufb : buffer backend
361 * @offset : offset within the buffer
362 * @dest : destination address
363 * @len : length to copy to destination
365 * Should be protected by get_subbuf/put_subbuf.
366 * Returns the length copied.
368 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
369 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
371 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
372 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
374 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
375 unsigned long sb_bindex
, id
;
378 offset
&= chanb
->buf_size
- 1;
380 if (caa_unlikely(!len
))
382 id
= bufb
->buf_rsb
.id
;
383 sb_bindex
= subbuffer_id_get_index(config
, id
);
384 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
386 * Underlying layer should never ask for reads across
389 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
390 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
391 && subbuffer_id_is_noref(config
, id
));
392 memcpy(dest
, shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1)), len
);
397 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
398 * @bufb : buffer backend
399 * @offset : offset within the buffer
400 * @dest : destination address
401 * @len : destination's length
403 * Return string's length, or -EINVAL on error.
404 * Should be protected by get_subbuf/put_subbuf.
405 * Destination length should be at least 1 to hold '\0'.
407 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
, size_t offset
,
408 void *dest
, size_t len
, struct lttng_ust_shm_handle
*handle
)
410 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
411 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
412 ssize_t string_len
, orig_offset
;
414 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
415 unsigned long sb_bindex
, id
;
417 if (caa_unlikely(!len
))
419 offset
&= chanb
->buf_size
- 1;
420 orig_offset
= offset
;
421 id
= bufb
->buf_rsb
.id
;
422 sb_bindex
= subbuffer_id_get_index(config
, id
);
423 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
425 * Underlying layer should never ask for reads across
428 CHAN_WARN_ON(chanb
, offset
>= chanb
->buf_size
);
429 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
430 && subbuffer_id_is_noref(config
, id
));
431 str
= shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));
432 string_len
= strnlen(str
, len
);
434 memcpy(dest
, str
, string_len
);
435 ((char *)dest
)[0] = 0;
437 return offset
- orig_offset
;
441 * lib_ring_buffer_read_offset_address - get address of a buffer location
442 * @bufb : buffer backend
443 * @offset : offset within the buffer.
445 * Return the address where a given offset is located (for read).
446 * Should be used to get the current subbuffer header pointer. Given we know
447 * it's never on a page boundary, it's safe to read/write directly
448 * from/to this address, as long as the read/write is never bigger than
451 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
453 struct lttng_ust_shm_handle
*handle
)
455 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
456 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
457 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
458 unsigned long sb_bindex
, id
;
460 offset
&= chanb
->buf_size
- 1;
461 id
= bufb
->buf_rsb
.id
;
462 sb_bindex
= subbuffer_id_get_index(config
, id
);
463 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
464 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
465 && subbuffer_id_is_noref(config
, id
));
466 return shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));
470 * lib_ring_buffer_offset_address - get address of a location within the buffer
471 * @bufb : buffer backend
472 * @offset : offset within the buffer.
474 * Return the address where a given offset is located.
475 * Should be used to get the current subbuffer header pointer. Given we know
476 * it's always at the beginning of a page, it's safe to write directly to this
477 * address, as long as the write is never bigger than a page size.
479 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
481 struct lttng_ust_shm_handle
*handle
)
484 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
485 struct channel_backend
*chanb
= &shmp(handle
, bufb
->chan
)->backend
;
486 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
487 unsigned long sb_bindex
, id
;
489 offset
&= chanb
->buf_size
- 1;
490 sbidx
= offset
>> chanb
->subbuf_size_order
;
491 id
= shmp_index(handle
, bufb
->buf_wsb
, sbidx
)->id
;
492 sb_bindex
= subbuffer_id_get_index(config
, id
);
493 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
494 CHAN_WARN_ON(chanb
, config
->mode
== RING_BUFFER_OVERWRITE
495 && subbuffer_id_is_noref(config
, id
));
496 return shmp_index(handle
, shmp(handle
, rpages
->shmp
)->p
, offset
& (chanb
->subbuf_size
- 1));