Commit | Line | Data |
---|---|---|
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 |
7fb51704 | 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 | */ | |
41 | static | |
4cfec15c MD |
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, | |
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 | { |
1d498196 | 49 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
852c2936 MD |
50 | unsigned long subbuf_size, mmap_offset = 0; |
51 | unsigned long num_subbuf_alloc; | |
852c2936 | 52 | unsigned long i; |
7fb51704 | 53 | long page_size; |
852c2936 | 54 | |
852c2936 MD |
55 | subbuf_size = chanb->subbuf_size; |
56 | num_subbuf_alloc = num_subbuf; | |
57 | ||
a6352fd4 | 58 | if (extra_reader_sb) |
852c2936 | 59 | num_subbuf_alloc++; |
852c2936 | 60 | |
7fb51704 MD |
61 | page_size = sysconf(_SC_PAGE_SIZE); |
62 | if (page_size <= 0) { | |
63 | goto page_size_error; | |
64 | } | |
65 | ||
4cfec15c | 66 | align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages_shmp)); |
1d498196 | 67 | set_shmp(bufb->array, zalloc_shm(shmobj, |
4cfec15c | 68 | sizeof(struct lttng_ust_lib_ring_buffer_backend_pages_shmp) * num_subbuf_alloc)); |
b5a3dfa5 | 69 | if (caa_unlikely(!shmp(handle, bufb->array))) |
852c2936 MD |
70 | goto array_error; |
71 | ||
431d5cf0 MD |
72 | /* |
73 | * This is the largest element (the buffer pages) which needs to | |
7fb51704 | 74 | * be aligned on page size. |
431d5cf0 | 75 | */ |
7fb51704 | 76 | align_shm(shmobj, page_size); |
1d498196 | 77 | set_shmp(bufb->memory_map, zalloc_shm(shmobj, |
a6352fd4 | 78 | subbuf_size * num_subbuf_alloc)); |
b5a3dfa5 | 79 | if (caa_unlikely(!shmp(handle, bufb->memory_map))) |
a6352fd4 | 80 | goto memory_map_error; |
852c2936 MD |
81 | |
82 | /* Allocate backend pages array elements */ | |
83 | for (i = 0; i < num_subbuf_alloc; i++) { | |
4cfec15c | 84 | align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_pages)); |
aead2025 | 85 | set_shmp(shmp_index(handle, bufb->array, i)->shmp, |
1d498196 | 86 | zalloc_shm(shmobj, |
4cfec15c | 87 | sizeof(struct lttng_ust_lib_ring_buffer_backend_pages))); |
4746ae29 | 88 | if (!shmp(handle, shmp_index(handle, bufb->array, i)->shmp)) |
852c2936 MD |
89 | goto free_array; |
90 | } | |
91 | ||
92 | /* Allocate write-side subbuffer table */ | |
4cfec15c | 93 | align_shm(shmobj, __alignof__(struct lttng_ust_lib_ring_buffer_backend_subbuffer)); |
1d498196 | 94 | set_shmp(bufb->buf_wsb, zalloc_shm(shmobj, |
4cfec15c | 95 | sizeof(struct lttng_ust_lib_ring_buffer_backend_subbuffer) |
1d498196 | 96 | * num_subbuf)); |
b5a3dfa5 | 97 | if (caa_unlikely(!shmp(handle, bufb->buf_wsb))) |
852c2936 MD |
98 | goto free_array; |
99 | ||
100 | for (i = 0; i < num_subbuf; i++) | |
4746ae29 | 101 | shmp_index(handle, bufb->buf_wsb, i)->id = subbuffer_id(config, 0, 1, i); |
852c2936 MD |
102 | |
103 | /* Assign read-side subbuffer table */ | |
104 | if (extra_reader_sb) | |
105 | bufb->buf_rsb.id = subbuffer_id(config, 0, 1, | |
106 | num_subbuf_alloc - 1); | |
107 | else | |
108 | bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0); | |
109 | ||
110 | /* Assign pages to page index */ | |
111 | for (i = 0; i < num_subbuf_alloc; i++) { | |
1d498196 MD |
112 | struct shm_ref ref; |
113 | ||
114 | ref.index = bufb->memory_map._ref.index; | |
115 | ref.offset = bufb->memory_map._ref.offset; | |
116 | ref.offset += i * subbuf_size; | |
117 | ||
4746ae29 | 118 | set_shmp(shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->p, |
1d498196 | 119 | ref); |
852c2936 | 120 | if (config->output == RING_BUFFER_MMAP) { |
4746ae29 | 121 | shmp(handle, shmp_index(handle, bufb->array, i)->shmp)->mmap_offset = mmap_offset; |
852c2936 MD |
122 | mmap_offset += subbuf_size; |
123 | } | |
124 | } | |
852c2936 MD |
125 | return 0; |
126 | ||
127 | free_array: | |
a6352fd4 MD |
128 | /* bufb->array[i] will be freed by shm teardown */ |
129 | memory_map_error: | |
130 | /* bufb->array will be freed by shm teardown */ | |
852c2936 | 131 | array_error: |
7fb51704 | 132 | page_size_error: |
852c2936 MD |
133 | return -ENOMEM; |
134 | } | |
135 | ||
4cfec15c | 136 | int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb, |
a6352fd4 | 137 | struct channel_backend *chanb, int cpu, |
38fae1d3 | 138 | struct lttng_ust_shm_handle *handle, |
1d498196 | 139 | struct shm_object *shmobj) |
852c2936 | 140 | { |
4cfec15c | 141 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 | 142 | |
1d498196 | 143 | set_shmp(bufb->chan, handle->chan._ref); |
852c2936 MD |
144 | bufb->cpu = cpu; |
145 | ||
146 | return lib_ring_buffer_backend_allocate(config, bufb, chanb->buf_size, | |
147 | chanb->num_subbuf, | |
a6352fd4 | 148 | chanb->extra_reader_sb, |
1d498196 | 149 | handle, shmobj); |
852c2936 MD |
150 | } |
151 | ||
4cfec15c | 152 | void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb, |
38fae1d3 | 153 | struct lttng_ust_shm_handle *handle) |
852c2936 | 154 | { |
1d498196 | 155 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 156 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
157 | unsigned long num_subbuf_alloc; |
158 | unsigned int i; | |
159 | ||
160 | num_subbuf_alloc = chanb->num_subbuf; | |
161 | if (chanb->extra_reader_sb) | |
162 | num_subbuf_alloc++; | |
163 | ||
164 | for (i = 0; i < chanb->num_subbuf; i++) | |
4746ae29 | 165 | shmp_index(handle, bufb->buf_wsb, i)->id = subbuffer_id(config, 0, 1, i); |
852c2936 MD |
166 | if (chanb->extra_reader_sb) |
167 | bufb->buf_rsb.id = subbuffer_id(config, 0, 1, | |
168 | num_subbuf_alloc - 1); | |
169 | else | |
170 | bufb->buf_rsb.id = subbuffer_id(config, 0, 1, 0); | |
171 | ||
172 | for (i = 0; i < num_subbuf_alloc; i++) { | |
173 | /* Don't reset mmap_offset */ | |
4746ae29 MD |
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; | |
852c2936 MD |
177 | /* Don't reset backend page and virt addresses */ |
178 | } | |
179 | /* Don't reset num_pages_per_subbuf, cpu, allocated */ | |
180 | v_set(config, &bufb->records_read, 0); | |
181 | } | |
182 | ||
183 | /* | |
184 | * The frontend is responsible for also calling ring_buffer_backend_reset for | |
185 | * each buffer when calling channel_backend_reset. | |
186 | */ | |
187 | void channel_backend_reset(struct channel_backend *chanb) | |
188 | { | |
14641deb | 189 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
4cfec15c | 190 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
191 | |
192 | /* | |
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. | |
196 | */ | |
197 | chanb->start_tsc = config->cb.ring_buffer_clock_read(chan); | |
198 | } | |
199 | ||
852c2936 MD |
200 | /** |
201 | * channel_backend_init - initialize a channel backend | |
202 | * @chanb: channel backend | |
203 | * @name: channel name | |
204 | * @config: client ring buffer configuration | |
852c2936 | 205 | * @parent: dentry of parent directory, %NULL for root directory |
7fb51704 | 206 | * @subbuf_size: size of sub-buffers (> page size, power of 2) |
852c2936 | 207 | * @num_subbuf: number of sub-buffers (power of 2) |
38fae1d3 | 208 | * @lttng_ust_shm_handle: shared memory handle |
5ea386c3 | 209 | * @stream_fds: stream file descriptors. |
852c2936 MD |
210 | * |
211 | * Returns channel pointer if successful, %NULL otherwise. | |
212 | * | |
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. | |
216 | * | |
217 | * Called with CPU hotplug disabled. | |
218 | */ | |
219 | int channel_backend_init(struct channel_backend *chanb, | |
220 | const char *name, | |
4cfec15c | 221 | const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f | 222 | size_t subbuf_size, size_t num_subbuf, |
a9ff648c | 223 | struct lttng_ust_shm_handle *handle, |
5ea386c3 | 224 | const int *stream_fds) |
852c2936 | 225 | { |
14641deb | 226 | struct channel *chan = caa_container_of(chanb, struct channel, backend); |
852c2936 MD |
227 | unsigned int i; |
228 | int ret; | |
6c1f7d8b | 229 | size_t shmsize = 0, num_subbuf_alloc; |
7fb51704 | 230 | long page_size; |
852c2936 MD |
231 | |
232 | if (!name) | |
233 | return -EPERM; | |
234 | ||
7fb51704 MD |
235 | page_size = sysconf(_SC_PAGE_SIZE); |
236 | if (page_size <= 0) { | |
237 | return -ENOMEM; | |
238 | } | |
852c2936 | 239 | /* Check that the subbuffer size is larger than a page. */ |
7fb51704 | 240 | if (subbuf_size < page_size) |
852c2936 MD |
241 | return -EINVAL; |
242 | ||
243 | /* | |
12bcbbdb MD |
244 | * Make sure the number of subbuffers and subbuffer size are |
245 | * power of 2, and nonzero. | |
852c2936 | 246 | */ |
12bcbbdb | 247 | if (!subbuf_size || (subbuf_size & (subbuf_size - 1))) |
e52b0723 | 248 | return -EINVAL; |
12bcbbdb | 249 | if (!num_subbuf || (num_subbuf & (num_subbuf - 1))) |
e52b0723 | 250 | return -EINVAL; |
3d8e9399 MD |
251 | /* |
252 | * Overwrite mode buffers require at least 2 subbuffers per | |
253 | * buffer. | |
254 | */ | |
255 | if (config->mode == RING_BUFFER_OVERWRITE && num_subbuf < 2) | |
256 | return -EINVAL; | |
852c2936 MD |
257 | |
258 | ret = subbuffer_id_check_index(config, num_subbuf); | |
259 | if (ret) | |
260 | return ret; | |
261 | ||
852c2936 MD |
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; | |
a6352fd4 MD |
270 | strncpy(chanb->name, name, NAME_MAX); |
271 | chanb->name[NAME_MAX - 1] = '\0'; | |
193183fb | 272 | memcpy(&chanb->config, config, sizeof(*config)); |
852c2936 | 273 | |
1d498196 | 274 | /* Per-cpu buffer size: control (prior to backend) */ |
4cfec15c MD |
275 | shmsize = offset_align(shmsize, __alignof__(struct lttng_ust_lib_ring_buffer)); |
276 | shmsize += sizeof(struct lttng_ust_lib_ring_buffer); | |
1d498196 MD |
277 | |
278 | /* Per-cpu buffer size: backend */ | |
279 | /* num_subbuf + 1 is the worse case */ | |
280 | num_subbuf_alloc = num_subbuf + 1; | |
4cfec15c MD |
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; | |
7fb51704 | 283 | shmsize += offset_align(shmsize, page_size); |
1d498196 | 284 | shmsize += subbuf_size * num_subbuf_alloc; |
4cfec15c MD |
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; | |
1d498196 MD |
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; | |
294 | ||
852c2936 | 295 | if (config->alloc == RING_BUFFER_ALLOC_PER_CPU) { |
4cfec15c | 296 | struct lttng_ust_lib_ring_buffer *buf; |
852c2936 | 297 | /* |
a6352fd4 | 298 | * We need to allocate for all possible cpus. |
852c2936 | 299 | */ |
852c2936 | 300 | for_each_possible_cpu(i) { |
1d498196 | 301 | struct shm_object *shmobj; |
5ea386c3 | 302 | |
74d81a6c | 303 | shmobj = shm_object_table_alloc(handle->table, shmsize, |
5ea386c3 | 304 | SHM_OBJECT_SHM, stream_fds[i]); |
afdf9825 MD |
305 | if (!shmobj) |
306 | goto end; | |
4cfec15c MD |
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))); | |
1d498196 MD |
309 | buf = shmp(handle, chanb->buf[i].shmp); |
310 | if (!buf) | |
311 | goto end; | |
5d61a504 | 312 | set_shmp(buf->self, chanb->buf[i].shmp._ref); |
1d498196 MD |
313 | ret = lib_ring_buffer_create(buf, chanb, i, |
314 | handle, shmobj); | |
852c2936 MD |
315 | if (ret) |
316 | goto free_bufs; /* cpu hotplug locked */ | |
317 | } | |
852c2936 | 318 | } else { |
1d498196 | 319 | struct shm_object *shmobj; |
4cfec15c | 320 | struct lttng_ust_lib_ring_buffer *buf; |
a6352fd4 | 321 | |
74d81a6c | 322 | shmobj = shm_object_table_alloc(handle->table, shmsize, |
5ea386c3 | 323 | SHM_OBJECT_SHM, stream_fds[0]); |
afdf9825 MD |
324 | if (!shmobj) |
325 | goto end; | |
4cfec15c MD |
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))); | |
1d498196 | 328 | buf = shmp(handle, chanb->buf[0].shmp); |
a6352fd4 MD |
329 | if (!buf) |
330 | goto end; | |
cb14bae9 | 331 | set_shmp(buf->self, chanb->buf[0].shmp._ref); |
1d498196 MD |
332 | ret = lib_ring_buffer_create(buf, chanb, -1, |
333 | handle, shmobj); | |
852c2936 MD |
334 | if (ret) |
335 | goto free_bufs; | |
336 | } | |
337 | chanb->start_tsc = config->cb.ring_buffer_clock_read(chan); | |
338 | ||
339 | return 0; | |
340 | ||
341 | free_bufs: | |
a6352fd4 MD |
342 | /* We only free the buffer data upon shm teardown */ |
343 | end: | |
852c2936 MD |
344 | return -ENOMEM; |
345 | } | |
346 | ||
852c2936 MD |
347 | /** |
348 | * channel_backend_free - destroy the channel | |
349 | * @chan: the channel | |
350 | * | |
351 | * Destroy all channel buffers and frees the channel. | |
352 | */ | |
1d498196 | 353 | void channel_backend_free(struct channel_backend *chanb, |
38fae1d3 | 354 | struct lttng_ust_shm_handle *handle) |
852c2936 | 355 | { |
45e9e699 | 356 | /* SHM teardown takes care of everything */ |
852c2936 MD |
357 | } |
358 | ||
852c2936 MD |
359 | /** |
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 | |
365 | * | |
366 | * Should be protected by get_subbuf/put_subbuf. | |
367 | * Returns the length copied. | |
368 | */ | |
4cfec15c | 369 | size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset, |
38fae1d3 | 370 | void *dest, size_t len, struct lttng_ust_shm_handle *handle) |
852c2936 | 371 | { |
1d498196 | 372 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 373 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
a6352fd4 | 374 | ssize_t orig_len; |
4cfec15c | 375 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
852c2936 | 376 | unsigned long sb_bindex, id; |
c6a9f474 | 377 | void *src; |
852c2936 MD |
378 | |
379 | orig_len = len; | |
380 | offset &= chanb->buf_size - 1; | |
a6352fd4 | 381 | |
b5a3dfa5 | 382 | if (caa_unlikely(!len)) |
852c2936 | 383 | return 0; |
a6352fd4 MD |
384 | id = bufb->buf_rsb.id; |
385 | sb_bindex = subbuffer_id_get_index(config, id); | |
4746ae29 | 386 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
a6352fd4 MD |
387 | /* |
388 | * Underlying layer should never ask for reads across | |
389 | * subbuffers. | |
390 | */ | |
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)); | |
c6a9f474 MD |
394 | src = shmp_index(handle, shmp(handle, rpages->shmp)->p, |
395 | offset & (chanb->subbuf_size - 1)); | |
396 | if (caa_unlikely(!src)) | |
397 | return 0; | |
398 | memcpy(dest, src, len); | |
852c2936 MD |
399 | return orig_len; |
400 | } | |
852c2936 | 401 | |
852c2936 MD |
402 | /** |
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 | |
408 | * | |
0bf3c920 | 409 | * Return string's length, or -EINVAL on error. |
852c2936 | 410 | * Should be protected by get_subbuf/put_subbuf. |
0bf3c920 | 411 | * Destination length should be at least 1 to hold '\0'. |
852c2936 | 412 | */ |
4cfec15c | 413 | int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb, size_t offset, |
38fae1d3 | 414 | void *dest, size_t len, struct lttng_ust_shm_handle *handle) |
852c2936 | 415 | { |
1d498196 | 416 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 417 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
a6352fd4 | 418 | ssize_t string_len, orig_offset; |
852c2936 | 419 | char *str; |
4cfec15c | 420 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
852c2936 MD |
421 | unsigned long sb_bindex, id; |
422 | ||
0bf3c920 MD |
423 | if (caa_unlikely(!len)) |
424 | return -EINVAL; | |
852c2936 | 425 | offset &= chanb->buf_size - 1; |
852c2936 | 426 | orig_offset = offset; |
852c2936 MD |
427 | id = bufb->buf_rsb.id; |
428 | sb_bindex = subbuffer_id_get_index(config, id); | |
4746ae29 | 429 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
a6352fd4 MD |
430 | /* |
431 | * Underlying layer should never ask for reads across | |
432 | * subbuffers. | |
433 | */ | |
434 | CHAN_WARN_ON(chanb, offset >= chanb->buf_size); | |
852c2936 MD |
435 | CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE |
436 | && subbuffer_id_is_noref(config, id)); | |
aead2025 | 437 | str = shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)); |
c6a9f474 MD |
438 | if (caa_unlikely(!str)) |
439 | return -EINVAL; | |
a6352fd4 MD |
440 | string_len = strnlen(str, len); |
441 | if (dest && len) { | |
442 | memcpy(dest, str, string_len); | |
443 | ((char *)dest)[0] = 0; | |
444 | } | |
445 | return offset - orig_offset; | |
852c2936 | 446 | } |
852c2936 MD |
447 | |
448 | /** | |
449 | * lib_ring_buffer_read_offset_address - get address of a buffer location | |
450 | * @bufb : buffer backend | |
451 | * @offset : offset within the buffer. | |
452 | * | |
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 | |
22b22ce9 MD |
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 | |
457 | * a page size. | |
852c2936 | 458 | */ |
4cfec15c | 459 | void *lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb, |
1d498196 | 460 | size_t offset, |
38fae1d3 | 461 | struct lttng_ust_shm_handle *handle) |
852c2936 | 462 | { |
4cfec15c | 463 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
1d498196 | 464 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 465 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
466 | unsigned long sb_bindex, id; |
467 | ||
468 | offset &= chanb->buf_size - 1; | |
852c2936 MD |
469 | id = bufb->buf_rsb.id; |
470 | sb_bindex = subbuffer_id_get_index(config, id); | |
4746ae29 | 471 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
852c2936 MD |
472 | CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE |
473 | && subbuffer_id_is_noref(config, id)); | |
aead2025 | 474 | return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)); |
852c2936 | 475 | } |
852c2936 MD |
476 | |
477 | /** | |
478 | * lib_ring_buffer_offset_address - get address of a location within the buffer | |
479 | * @bufb : buffer backend | |
480 | * @offset : offset within the buffer. | |
481 | * | |
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. | |
486 | */ | |
4cfec15c | 487 | void *lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb, |
1d498196 | 488 | size_t offset, |
38fae1d3 | 489 | struct lttng_ust_shm_handle *handle) |
852c2936 | 490 | { |
a6352fd4 | 491 | size_t sbidx; |
4cfec15c | 492 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
1d498196 | 493 | struct channel_backend *chanb = &shmp(handle, bufb->chan)->backend; |
4cfec15c | 494 | const struct lttng_ust_lib_ring_buffer_config *config = &chanb->config; |
852c2936 MD |
495 | unsigned long sb_bindex, id; |
496 | ||
497 | offset &= chanb->buf_size - 1; | |
498 | sbidx = offset >> chanb->subbuf_size_order; | |
4746ae29 | 499 | id = shmp_index(handle, bufb->buf_wsb, sbidx)->id; |
852c2936 | 500 | sb_bindex = subbuffer_id_get_index(config, id); |
4746ae29 | 501 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
852c2936 MD |
502 | CHAN_WARN_ON(chanb, config->mode == RING_BUFFER_OVERWRITE |
503 | && subbuffer_id_is_noref(config, id)); | |
aead2025 | 504 | return shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)); |
852c2936 | 505 | } |