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