2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring buffer backend (API).
8 * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
9 * the reader in flight recorder mode.
12 #ifndef _LTTNG_RING_BUFFER_BACKEND_H
13 #define _LTTNG_RING_BUFFER_BACKEND_H
18 /* Internal helpers */
19 #include "backend_internal.h"
20 #include "frontend_internal.h"
22 /* Ring buffer backend API */
24 /* Ring buffer backend access (read/write) */
26 extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
27 size_t offset
, void *dest
, size_t len
,
28 struct lttng_ust_shm_handle
*handle
)
29 __attribute__((visibility("hidden")));
31 extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
32 size_t offset
, void *dest
, size_t len
,
33 struct lttng_ust_shm_handle
*handle
)
34 __attribute__((visibility("hidden")));
37 * Return the address where a given offset is located.
38 * Should be used to get the current subbuffer header pointer. Given we know
39 * it's never on a page boundary, it's safe to write directly to this address,
40 * as long as the write is never bigger than a page size.
43 lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
45 struct lttng_ust_shm_handle
*handle
)
46 __attribute__((visibility("hidden")));
49 lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
51 struct lttng_ust_shm_handle
*handle
)
52 __attribute__((visibility("hidden")));
55 * lib_ring_buffer_write - write data to a buffer backend
56 * @config : ring buffer instance configuration
57 * @ctx: ring buffer context. (input arguments only)
58 * @src : source pointer to copy from
59 * @len : length of data to copy
61 * This function copies "len" bytes of data from a source pointer to a buffer
62 * backend, at the current context offset. This is more or less a buffer
63 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
64 * if copy is crossing a page boundary.
67 void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config
*config
,
68 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
69 const void *src
, size_t len
)
70 __attribute__((always_inline
));
72 void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config
*config
,
73 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
74 const void *src
, size_t len
)
76 struct lttng_ust_lib_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
77 struct channel_backend
*chanb
= &ctx_private
->chan
->backend
;
78 struct lttng_ust_shm_handle
*handle
= ctx_private
->chan
->handle
;
79 size_t offset
= ctx_private
->buf_offset
;
80 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
83 if (caa_unlikely(!len
))
86 * Underlying layer should never ask for writes across
89 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
90 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
91 if (caa_unlikely(!backend_pages
)) {
92 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
95 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
98 lib_ring_buffer_do_copy(config
, p
, src
, len
);
99 ctx_private
->buf_offset
+= len
;
103 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
104 * terminating character is found in @src. Returns the number of bytes
105 * copied. Does *not* terminate @dest with NULL terminating character.
108 size_t lib_ring_buffer_do_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
109 char *dest
, const char *src
, size_t len
)
110 __attribute__((always_inline
));
112 size_t lib_ring_buffer_do_strcpy(
113 const struct lttng_ust_lib_ring_buffer_config
*config
__attribute__((unused
)),
114 char *dest
, const char *src
, size_t len
)
118 for (count
= 0; count
< len
; count
++) {
122 * Only read source character once, in case it is
123 * modified concurrently.
125 c
= CMM_LOAD_SHARED(src
[count
]);
128 lib_ring_buffer_do_copy(config
, &dest
[count
], &c
, 1);
134 * lib_ring_buffer_strcpy - write string data to a buffer backend
135 * @config : ring buffer instance configuration
136 * @ctx: ring buffer context. (input arguments only)
137 * @src : source pointer to copy from
138 * @len : length of data to copy
139 * @pad : character to use for padding
141 * This function copies @len - 1 bytes of string data from a source
142 * pointer to a buffer backend, followed by a terminating '\0'
143 * character, at the current context offset. This is more or less a
144 * buffer backend-specific strncpy() operation. If a terminating '\0'
145 * character is found in @src before @len - 1 characters are copied, pad
146 * the buffer with @pad characters (e.g. '#').
149 void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
150 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
151 const char *src
, size_t len
, char pad
)
152 __attribute__((always_inline
));
154 void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
155 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
156 const char *src
, size_t len
, char pad
)
158 struct lttng_ust_lib_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
159 struct channel_backend
*chanb
= &ctx_private
->chan
->backend
;
160 struct lttng_ust_shm_handle
*handle
= ctx_private
->chan
->handle
;
162 size_t offset
= ctx_private
->buf_offset
;
163 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
166 if (caa_unlikely(!len
))
169 * Underlying layer should never ask for writes across
172 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
173 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
174 if (caa_unlikely(!backend_pages
)) {
175 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
178 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
179 if (caa_unlikely(!p
))
182 count
= lib_ring_buffer_do_strcpy(config
, p
, src
, len
- 1);
185 if (caa_unlikely(count
< len
- 1)) {
186 size_t pad_len
= len
- 1 - count
;
188 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
189 if (caa_unlikely(!p
))
191 lib_ring_buffer_do_memset(p
, pad
, pad_len
);
195 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
196 if (caa_unlikely(!p
))
198 lib_ring_buffer_do_memset(p
, '\0', 1);
199 ctx_private
->buf_offset
+= len
;
203 * lib_ring_buffer_pstrcpy - write to a buffer backend P-string
204 * @config : ring buffer instance configuration
205 * @ctx: ring buffer context. (input arguments only)
206 * @src : source pointer to copy from
207 * @len : length of data to copy
208 * @pad : character to use for padding
210 * This function copies up to @len bytes of data from a source pointer
211 * to a Pascal String into the buffer backend. If a terminating '\0'
212 * character is found in @src before @len characters are copied, pad the
213 * buffer with @pad characters (e.g. '\0').
215 * The length of the pascal strings in the ring buffer is explicit: it
216 * is either the array or sequence length.
219 void lib_ring_buffer_pstrcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
220 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
221 const char *src
, size_t len
, char pad
)
222 __attribute__((always_inline
));
224 void lib_ring_buffer_pstrcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
225 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
226 const char *src
, size_t len
, char pad
)
228 struct lttng_ust_lib_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
229 struct channel_backend
*chanb
= &ctx_private
->chan
->backend
;
230 struct lttng_ust_shm_handle
*handle
= ctx_private
->chan
->handle
;
232 size_t offset
= ctx_private
->buf_offset
;
233 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
236 if (caa_unlikely(!len
))
239 * Underlying layer should never ask for writes across
242 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
243 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
244 if (caa_unlikely(!backend_pages
)) {
245 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
248 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
249 if (caa_unlikely(!p
))
252 count
= lib_ring_buffer_do_strcpy(config
, p
, src
, len
);
255 if (caa_unlikely(count
< len
)) {
256 size_t pad_len
= len
- count
;
258 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
259 if (caa_unlikely(!p
))
261 lib_ring_buffer_do_memset(p
, pad
, pad_len
);
263 ctx_private
->buf_offset
+= len
;
267 * This accessor counts the number of unread records in a buffer.
268 * It only provides a consistent value if no reads not writes are performed
272 unsigned long lib_ring_buffer_get_records_unread(
273 const struct lttng_ust_lib_ring_buffer_config
*config
,
274 struct lttng_ust_lib_ring_buffer
*buf
,
275 struct lttng_ust_shm_handle
*handle
)
277 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
278 unsigned long records_unread
= 0, sb_bindex
;
280 struct lttng_ust_lib_ring_buffer_channel
*chan
;
282 chan
= shmp(handle
, bufb
->chan
);
285 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
286 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
287 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
288 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
290 wsb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
293 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
294 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
297 backend_pages
= shmp(handle
, rpages
->shmp
);
300 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
302 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
303 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
304 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
306 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
307 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
310 backend_pages
= shmp(handle
, rpages
->shmp
);
313 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
315 return records_unread
;
318 #endif /* _LTTNG_RING_BUFFER_BACKEND_H */