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