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