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(const struct lttng_ust_lib_ring_buffer_config
*config
,
113 char *dest
, const char *src
, size_t len
)
117 for (count
= 0; count
< len
; count
++) {
121 * Only read source character once, in case it is
122 * modified concurrently.
124 c
= CMM_LOAD_SHARED(src
[count
]);
127 lib_ring_buffer_do_copy(config
, &dest
[count
], &c
, 1);
133 * lib_ring_buffer_strcpy - write string data to a buffer backend
134 * @config : ring buffer instance configuration
135 * @ctx: ring buffer context. (input arguments only)
136 * @src : source pointer to copy from
137 * @len : length of data to copy
138 * @pad : character to use for padding
140 * This function copies @len - 1 bytes of string data from a source
141 * pointer to a buffer backend, followed by a terminating '\0'
142 * character, at the current context offset. This is more or less a
143 * buffer backend-specific strncpy() operation. If a terminating '\0'
144 * character is found in @src before @len - 1 characters are copied, pad
145 * the buffer with @pad characters (e.g. '#').
148 void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
149 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
150 const char *src
, size_t len
, char pad
)
151 __attribute__((always_inline
));
153 void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
154 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
155 const char *src
, size_t len
, char pad
)
157 struct lttng_ust_lib_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
158 struct channel_backend
*chanb
= &ctx_private
->chan
->backend
;
159 struct lttng_ust_shm_handle
*handle
= ctx_private
->chan
->handle
;
161 size_t offset
= ctx_private
->buf_offset
;
162 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
165 if (caa_unlikely(!len
))
168 * Underlying layer should never ask for writes across
171 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
172 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
173 if (caa_unlikely(!backend_pages
)) {
174 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
177 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
178 if (caa_unlikely(!p
))
181 count
= lib_ring_buffer_do_strcpy(config
, p
, src
, len
- 1);
184 if (caa_unlikely(count
< len
- 1)) {
185 size_t pad_len
= len
- 1 - count
;
187 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
188 if (caa_unlikely(!p
))
190 lib_ring_buffer_do_memset(p
, pad
, pad_len
);
194 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
195 if (caa_unlikely(!p
))
197 lib_ring_buffer_do_memset(p
, '\0', 1);
198 ctx_private
->buf_offset
+= len
;
202 * lib_ring_buffer_pstrcpy - write to a buffer backend P-string
203 * @config : ring buffer instance configuration
204 * @ctx: ring buffer context. (input arguments only)
205 * @src : source pointer to copy from
206 * @len : length of data to copy
207 * @pad : character to use for padding
209 * This function copies up to @len bytes of data from a source pointer
210 * to a Pascal String into the buffer backend. If a terminating '\0'
211 * character is found in @src before @len characters are copied, pad the
212 * buffer with @pad characters (e.g. '\0').
214 * The length of the pascal strings in the ring buffer is explicit: it
215 * is either the array or sequence length.
218 void lib_ring_buffer_pstrcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
219 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
220 const char *src
, size_t len
, char pad
)
221 __attribute__((always_inline
));
223 void lib_ring_buffer_pstrcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
224 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
225 const char *src
, size_t len
, char pad
)
227 struct lttng_ust_lib_ring_buffer_ctx_private
*ctx_private
= ctx
->priv
;
228 struct channel_backend
*chanb
= &ctx_private
->chan
->backend
;
229 struct lttng_ust_shm_handle
*handle
= ctx_private
->chan
->handle
;
231 size_t offset
= ctx_private
->buf_offset
;
232 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
235 if (caa_unlikely(!len
))
238 * Underlying layer should never ask for writes across
241 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
242 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
243 if (caa_unlikely(!backend_pages
)) {
244 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
247 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
248 if (caa_unlikely(!p
))
251 count
= lib_ring_buffer_do_strcpy(config
, p
, src
, len
);
254 if (caa_unlikely(count
< len
)) {
255 size_t pad_len
= len
- count
;
257 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
258 if (caa_unlikely(!p
))
260 lib_ring_buffer_do_memset(p
, pad
, pad_len
);
262 ctx_private
->buf_offset
+= len
;
266 * This accessor counts the number of unread records in a buffer.
267 * It only provides a consistent value if no reads not writes are performed
271 unsigned long lib_ring_buffer_get_records_unread(
272 const struct lttng_ust_lib_ring_buffer_config
*config
,
273 struct lttng_ust_lib_ring_buffer
*buf
,
274 struct lttng_ust_shm_handle
*handle
)
276 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
277 unsigned long records_unread
= 0, sb_bindex
;
279 struct lttng_ust_lib_ring_buffer_channel
*chan
;
281 chan
= shmp(handle
, bufb
->chan
);
284 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
285 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
286 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
287 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
289 wsb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
292 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
293 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
296 backend_pages
= shmp(handle
, rpages
->shmp
);
299 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
301 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
302 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
303 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
305 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
306 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
309 backend_pages
= shmp(handle
, rpages
->shmp
);
312 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
314 return records_unread
;
317 #endif /* _LTTNG_RING_BUFFER_BACKEND_H */