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