Performance: define _LGPL_SOURCE in LGPL c files
[lttng-ust.git] / libringbuffer / ring_buffer_backend.c
1 /*
2 * ring_buffer_backend.c
3 *
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #define _GNU_SOURCE
22 #define _LGPL_SOURCE
23 #include <unistd.h>
24 #include <urcu/arch.h>
25 #include <limits.h>
26
27 #include <lttng/ringbuffer-config.h>
28 #include "vatomic.h"
29 #include "backend.h"
30 #include "frontend.h"
31 #include "smp.h"
32 #include "shm.h"
33
34 /**
35 * lib_ring_buffer_backend_allocate - allocate a channel buffer
36 * @config: ring buffer instance configuration
37 * @buf: the buffer struct
38 * @size: total size of the buffer
39 * @num_subbuf: number of subbuffers
40 * @extra_reader_sb: need extra subbuffer for reader
41 */
42 static
43 int lib_ring_buffer_backend_allocate(const struct lttng_ust_lib_ring_buffer_config *config,
44 struct lttng_ust_lib_ring_buffer_backend *bufb,
45 size_t size, size_t num_subbuf,
46 int extra_reader_sb,
47 struct lttng_ust_shm_handle *handle,
48 struct shm_object *shmobj)
49 {
50 struct channel_backend *chanb;
51 unsigned long subbuf_size, mmap_offset = 0;
52 unsigned long num_subbuf_alloc;
53 unsigned long i;
54 long page_size;
55
56 chanb = &shmp(handle, bufb->chan)->backend;
57 if (!chanb)
58 return -EINVAL;
59
60 subbuf_size = chanb->subbuf_size;
61 num_subbuf_alloc = num_subbuf;
62
63 if (extra_reader_sb)
64 num_subbuf_alloc++;
65
66 page_size = sysconf(_SC_PAGE_SIZE);
67 if (page_size <= 0) {
68 goto page_size_error;
69 }
70
71 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp));
72 set_shmp(bufb->array, zalloc_shm(shmobj,
73 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc));
74 if (caa_unlikely(!shmp(handle, bufb->array)))
75 goto array_error;
76
77 /*
78 * This is the largest element (the buffer pages) which needs to
79 * be aligned on page size.
80 */
81 align_shm(shmobj, page_size);
82 set_shmp(bufb->memory_map, zalloc_shm(shmobj,
83 subbuf_size * num_subbuf_alloc));
84 if (caa_unlikely(!shmp(handle, bufb->memory_map)))
85 goto memory_map_error;
86
87 /* Allocate backend pages array elements */
88 for (i = 0; i < num_subbuf_alloc; i++) {
89 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages));
90 set_shmp(shmp_index(handle, bufb->array, i)->shmp,
91 zalloc_shm(shmobj,
92 sizeof(struct lttng_ust_lib_ring_buffer_backend_pages)));
93 if (!shmp(handle, shmp_index(handle, bufb->array, i)->shmp))
94 goto free_array;
95 }
96
97 /* Allocate write-side subbuffer table */
98 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer));
99 set_shmp(bufb->buf_wsb, zalloc_shm(shmobj,
100 sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer)
101 * num_subbuf));
102 if (caa_unlikely(!shmp(handle, bufb->buf_wsb)))
103 goto free_array;
104
105 for (i = 0; i < num_subbuf; i++) {
106 struct lttng_ust_lib_ring_buffer_backend_subbuffer *sb;
107
108 sb = shmp_index(handle, bufb->buf_wsb, i);
109 if (!sb)
110 goto free_array;
111 sb->id = subbuffer_id(config, 0, 1, i);
112 }
113
114 /* Assign read-side subbuffer table */
115 if (extra_reader_sb)
116 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
117 num_subbuf_alloc - 1);
118 else
119 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
120
121 /* Allocate subbuffer packet counter table */
122 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer));
123 set_shmp(bufb->buf_cnt, zalloc_shm(shmobj,
124 sizeof(struct lttng_ust_lib_ring_buffer_backend_counts)
125 * num_subbuf));
126 if (caa_unlikely(!shmp(handle, bufb->buf_cnt)))
127 goto free_wsb;
128
129 /* Assign pages to page index */
130 for (i = 0; i < num_subbuf_alloc; i++) {
131 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *sbp;
132 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
133 struct shm_ref ref;
134
135 ref.index = bufb->memory_map._ref.index;
136 ref.offset = bufb->memory_map._ref.offset;
137 ref.offset += i * subbuf_size;
138
139 sbp = shmp_index(handle, bufb->array, i);
140 if (!sbp)
141 goto free_array;
142 pages = shmp(handle, sbp->shmp);
143 if (!pages)
144 goto free_array;
145 set_shmp(pages->p, ref);
146 if (config->output == RING_BUFFER_MMAP) {
147 pages->mmap_offset = mmap_offset;
148 mmap_offset += subbuf_size;
149 }
150 }
151 return 0;
152
153 free_wsb:
154 /* bufb->buf_wsb will be freed by shm teardown */
155 free_array:
156 /* bufb->array[i] will be freed by shm teardown */
157 memory_map_error:
158 /* bufb->array will be freed by shm teardown */
159 array_error:
160 page_size_error:
161 return -ENOMEM;
162 }
163
164 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb,
165 struct channel_backend *chanb, int cpu,
166 struct lttng_ust_shm_handle *handle,
167 struct shm_object *shmobj)
168 {
169 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
170
171 set_shmp(bufb->chan, handle->chan._ref);
172 bufb->cpu = cpu;
173
174 return lib_ring_buffer_backend_allocate(config, bufb, chanb->buf_size,
175 chanb->num_subbuf,
176 chanb->extra_reader_sb,
177 handle, shmobj);
178 }
179
180 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb,
181 struct lttng_ust_shm_handle *handle)
182 {
183 struct channel_backend *chanb;
184 const struct lttng_ust_lib_ring_buffer_config *config;
185 unsigned long num_subbuf_alloc;
186 unsigned int i;
187
188 chanb = &shmp(handle, bufb->chan)->backend;
189 if (!chanb)
190 abort();
191 config = &chanb->config;
192
193 num_subbuf_alloc = chanb->num_subbuf;
194 if (chanb->extra_reader_sb)
195 num_subbuf_alloc++;
196
197 for (i = 0; i < chanb->num_subbuf; i++) {
198 struct lttng_ust_lib_ring_buffer_backend_subbuffer *sb;
199
200 sb = shmp_index(handle, bufb->buf_wsb, i);
201 if (!sb)
202 abort();
203 sb->id = subbuffer_id(config, 0, 1, i);
204 }
205 if (chanb->extra_reader_sb)
206 bufb->buf_rsb.id = subbuffer_id(config, 0, 1,
207 num_subbuf_alloc - 1);
208 else
209 bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0);
210
211 for (i = 0; i < num_subbuf_alloc; i++) {
212 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *sbp;
213 struct lttng_ust_lib_ring_buffer_backend_pages *pages;
214
215 sbp = shmp_index(handle, bufb->array, i);
216 if (!sbp)
217 abort();
218 pages = shmp(handle, sbp->shmp);
219 if (!pages)
220 abort();
221 /* Don't reset mmap_offset */
222 v_set(config, &pages->records_commit, 0);
223 v_set(config, &pages->records_unread, 0);
224 pages->data_size = 0;
225 /* Don't reset backend page and virt addresses */
226 }
227 /* Don't reset num_pages_per_subbuf, cpu, allocated */
228 v_set(config, &bufb->records_read, 0);
229 }
230
231 /*
232 * The frontend is responsible for also calling ring_buffer_backend_reset for
233 * each buffer when calling channel_backend_reset.
234 */
235 void channel_backend_reset(struct channel_backend *chanb)
236 {
237 struct channel *chan = caa_container_of(chanb, struct channel, backend);
238 const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config;
239
240 /*
241 * Don't reset buf_size, subbuf_size, subbuf_size_order,
242 * num_subbuf_order, buf_size_order, extra_reader_sb, num_subbuf,
243 * priv, notifiers, config, cpumask and name.
244 */
245 chanb->start_tsc = config->cb.ring_buffer_clock_read(chan);
246 }
247
248 /**
249 * channel_backend_init - initialize a channel backend
250 * @chanb: channel backend
251 * @name: channel name
252 * @config: client ring buffer configuration
253 * @parent: dentry of parent directory, %NULL for root directory
254 * @subbuf_size: size of sub-buffers (> page size, power of 2)
255 * @num_subbuf: number of sub-buffers (power of 2)
256 * @lttng_ust_shm_handle: shared memory handle
257 * @stream_fds: stream file descriptors.
258 *
259 * Returns channel pointer if successful, %NULL otherwise.
260 *
261 * Creates per-cpu channel buffers using the sizes and attributes
262 * specified. The created channel buffer files will be named
263 * name_0...name_N-1. File permissions will be %S_IRUSR.
264 *
265 * Called with CPU hotplug disabled.
266 */
267 int channel_backend_init(struct channel_backend *chanb,
268 const char *name,
269 const struct lttng_ust_lib_ring_buffer_config *config,
270 size_t subbuf_size, size_t num_subbuf,
271 struct lttng_ust_shm_handle *handle,
272 const int *stream_fds)
273 {
274 struct channel *chan = caa_container_of(chanb, struct channel, backend);
275 unsigned int i;
276 int ret;
277 size_t shmsize = 0, num_subbuf_alloc;
278 long page_size;
279
280 if (!name)
281 return -EPERM;
282
283 page_size = sysconf(_SC_PAGE_SIZE);
284 if (page_size <= 0) {
285 return -ENOMEM;
286 }
287 /* Check that the subbuffer size is larger than a page. */
288 if (subbuf_size < page_size)
289 return -EINVAL;
290
291 /*
292 * Make sure the number of subbuffers and subbuffer size are
293 * power of 2, and nonzero.
294 */
295 if (!subbuf_size || (subbuf_size & (subbuf_size - 1)))
296 return -EINVAL;
297 if (!num_subbuf || (num_subbuf & (num_subbuf - 1)))
298 return -EINVAL;
299 /*
300 * Overwrite mode buffers require at least 2 subbuffers per
301 * buffer.
302 */
303 if (config->mode == RING_BUFFER_OVERWRITE && num_subbuf < 2)
304 return -EINVAL;
305
306 ret = subbuffer_id_check_index(config, num_subbuf);
307 if (ret)
308 return ret;
309
310 chanb->buf_size = num_subbuf * subbuf_size;
311 chanb->subbuf_size = subbuf_size;
312 chanb->buf_size_order = get_count_order(chanb->buf_size);
313 chanb->subbuf_size_order = get_count_order(subbuf_size);
314 chanb->num_subbuf_order = get_count_order(num_subbuf);
315 chanb->extra_reader_sb =
316 (config->mode == RING_BUFFER_OVERWRITE) ? 1 : 0;
317 chanb->num_subbuf = num_subbuf;
318 strncpy(chanb->name, name, NAME_MAX);
319 chanb->name[NAME_MAX - 1] = '\0';
320 memcpy(&chanb->config, config, sizeof(*config));
321
322 /* Per-cpu buffer size: control (prior to backend) */
323 shmsize = offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer));
324 shmsize += sizeof(struct lttng_ust_lib_ring_buffer);
325
326 /* Per-cpu buffer size: backend */
327 /* num_subbuf + 1 is the worse case */
328 num_subbuf_alloc = num_subbuf + 1;
329 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp));
330 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc;
331 shmsize += offset_align(shmsize, page_size);
332 shmsize += subbuf_size * num_subbuf_alloc;
333 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages));
334 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_pages) * num_subbuf_alloc;
335 shmsize += offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer));
336 shmsize += sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer) * num_subbuf;
337 /* Per-cpu buffer size: control (after backend) */
338 shmsize += offset_align(shmsize, __alignof__(struct commit_counters_hot));
339 shmsize += sizeof(struct commit_counters_hot) * num_subbuf;
340 shmsize += offset_align(shmsize, __alignof__(struct commit_counters_cold));
341 shmsize += sizeof(struct commit_counters_cold) * num_subbuf;
342
343 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) {
344 struct lttng_ust_lib_ring_buffer *buf;
345 /*
346 * We need to allocate for all possible cpus.
347 */
348 for_each_possible_cpu(i) {
349 struct shm_object *shmobj;
350
351 shmobj = shm_object_table_alloc(handle->table, shmsize,
352 SHM_OBJECT_SHM, stream_fds[i]);
353 if (!shmobj)
354 goto end;
355 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer));
356 set_shmp(chanb->buf[i].shmp, zalloc_shm(shmobj, sizeof(struct lttng_ust_lib_ring_buffer)));
357 buf = shmp(handle, chanb->buf[i].shmp);
358 if (!buf)
359 goto end;
360 set_shmp(buf->self, chanb->buf[i].shmp._ref);
361 ret = lib_ring_buffer_create(buf, chanb, i,
362 handle, shmobj);
363 if (ret)
364 goto free_bufs; /* cpu hotplug locked */
365 }
366 } else {
367 struct shm_object *shmobj;
368 struct lttng_ust_lib_ring_buffer *buf;
369
370 shmobj = shm_object_table_alloc(handle->table, shmsize,
371 SHM_OBJECT_SHM, stream_fds[0]);
372 if (!shmobj)
373 goto end;
374 align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer));
375 set_shmp(chanb->buf[0].shmp, zalloc_shm(shmobj, sizeof(struct lttng_ust_lib_ring_buffer)));
376 buf = shmp(handle, chanb->buf[0].shmp);
377 if (!buf)
378 goto end;
379 set_shmp(buf->self, chanb->buf[0].shmp._ref);
380 ret = lib_ring_buffer_create(buf, chanb, -1,
381 handle, shmobj);
382 if (ret)
383 goto free_bufs;
384 }
385 chanb->start_tsc = config->cb.ring_buffer_clock_read(chan);
386
387 return 0;
388
389 free_bufs:
390 /* We only free the buffer data upon shm teardown */
391 end:
392 return -ENOMEM;
393 }
394
395 /**
396 * channel_backend_free - destroy the channel
397 * @chan: the channel
398 *
399 * Destroy all channel buffers and frees the channel.
400 */
401 void channel_backend_free(struct channel_backend *chanb,
402 struct lttng_ust_shm_handle *handle)
403 {
404 /* SHM teardown takes care of everything */
405 }
406
407 /**
408 * lib_ring_buffer_read - read data from ring_buffer_buffer.
409 * @bufb : buffer backend
410 * @offset : offset within the buffer
411 * @dest : destination address
412 * @len : length to copy to destination
413 *
414 * Should be protected by get_subbuf/put_subbuf.
415 * Returns the length copied.
416 */
417 size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset,
418 void *dest, size_t len, struct lttng_ust_shm_handle *handle)
419 {
420 struct channel_backend *chanb;
421 const struct lttng_ust_lib_ring_buffer_config *config;
422 ssize_t orig_len;
423 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
424 unsigned long sb_bindex, id;
425 void *src;
426
427 chanb = &shmp(handle, bufb->chan)->backend;
428 if (!chanb)
429 return 0;
430 config = &chanb->config;
431 orig_len = len;
432 offset &= chanb->buf_size - 1;
433
434 if (caa_unlikely(!len))
435 return 0;
436 id = bufb->buf_rsb.id;
437 sb_bindex = subbuffer_id_get_index(config, id);
438 rpages = shmp_index(handle, bufb->array, sb_bindex);
439 /*
440 * Underlying layer should never ask for reads across
441 * subbuffers.
442 */
443 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
444 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
445 && subbuffer_id_is_noref(config, id));
446 src = shmp_index(handle, shmp(handle, rpages->shmp)->p,
447 offset & (chanb->subbuf_size - 1));
448 if (caa_unlikely(!src))
449 return 0;
450 memcpy(dest, src, len);
451 return orig_len;
452 }
453
454 /**
455 * lib_ring_buffer_read_cstr - read a C-style string from ring_buffer.
456 * @bufb : buffer backend
457 * @offset : offset within the buffer
458 * @dest : destination address
459 * @len : destination's length
460 *
461 * Return string's length, or -EINVAL on error.
462 * Should be protected by get_subbuf/put_subbuf.
463 * Destination length should be at least 1 to hold '\0'.
464 */
465 int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset,
466 void *dest, size_t len, struct lttng_ust_shm_handle *handle)
467 {
468 struct channel_backend *chanb;
469 const struct lttng_ust_lib_ring_buffer_config *config;
470 ssize_t string_len, orig_offset;
471 char *str;
472 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
473 unsigned long sb_bindex, id;
474
475 chanb = &shmp(handle, bufb->chan)->backend;
476 if (!chanb)
477 return -EINVAL;
478 config = &chanb->config;
479 if (caa_unlikely(!len))
480 return -EINVAL;
481 offset &= chanb->buf_size - 1;
482 orig_offset = offset;
483 id = bufb->buf_rsb.id;
484 sb_bindex = subbuffer_id_get_index(config, id);
485 rpages = shmp_index(handle, bufb->array, sb_bindex);
486 /*
487 * Underlying layer should never ask for reads across
488 * subbuffers.
489 */
490 CHAN_WARN_ON(chanb, offset >= chanb->buf_size);
491 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
492 && subbuffer_id_is_noref(config, id));
493 str = shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
494 if (caa_unlikely(!str))
495 return -EINVAL;
496 string_len = strnlen(str, len);
497 if (dest && len) {
498 memcpy(dest, str, string_len);
499 ((char *)dest)[0] = 0;
500 }
501 return offset - orig_offset;
502 }
503
504 /**
505 * lib_ring_buffer_read_offset_address - get address of a buffer location
506 * @bufb : buffer backend
507 * @offset : offset within the buffer.
508 *
509 * Return the address where a given offset is located (for read).
510 * Should be used to get the current subbuffer header pointer. Given we know
511 * it's never on a page boundary, it's safe to read/write directly
512 * from/to this address, as long as the read/write is never bigger than
513 * a page size.
514 */
515 void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
516 size_t offset,
517 struct lttng_ust_shm_handle *handle)
518 {
519 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
520 struct channel_backend *chanb;
521 const struct lttng_ust_lib_ring_buffer_config *config;
522 unsigned long sb_bindex, id;
523
524 chanb = &shmp(handle, bufb->chan)->backend;
525 if (!chanb)
526 return NULL;
527 config = &chanb->config;
528 offset &= chanb->buf_size - 1;
529 id = bufb->buf_rsb.id;
530 sb_bindex = subbuffer_id_get_index(config, id);
531 rpages = shmp_index(handle, bufb->array, sb_bindex);
532 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
533 && subbuffer_id_is_noref(config, id));
534 return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
535 }
536
537 /**
538 * lib_ring_buffer_offset_address - get address of a location within the buffer
539 * @bufb : buffer backend
540 * @offset : offset within the buffer.
541 *
542 * Return the address where a given offset is located.
543 * Should be used to get the current subbuffer header pointer. Given we know
544 * it's always at the beginning of a page, it's safe to write directly to this
545 * address, as long as the write is never bigger than a page size.
546 */
547 void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb,
548 size_t offset,
549 struct lttng_ust_shm_handle *handle)
550 {
551 size_t sbidx;
552 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages;
553 struct channel_backend *chanb;
554 const struct lttng_ust_lib_ring_buffer_config *config;
555 unsigned long sb_bindex, id;
556 struct lttng_ust_lib_ring_buffer_backend_subbuffer *sb;
557
558 chanb = &shmp(handle, bufb->chan)->backend;
559 if (!chanb)
560 return NULL;
561 config = &chanb->config;
562 offset &= chanb->buf_size - 1;
563 sbidx = offset >> chanb->subbuf_size_order;
564 sb = shmp_index(handle, bufb->buf_wsb, sbidx);
565 if (!sb)
566 return NULL;
567 id = sb->id;
568 sb_bindex = subbuffer_id_get_index(config, id);
569 rpages = shmp_index(handle, bufb->array, sb_bindex);
570 CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE
571 && subbuffer_id_is_noref(config, id));
572 return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1));
573 }
This page took 0.040007 seconds and 4 git commands to generate.