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