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 __attribute__((visibility("hidden")))
27 extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
28 size_t offset
, void *dest
, size_t len
,
29 struct lttng_ust_shm_handle
*handle
);
31 __attribute__((visibility("hidden")))
32 extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
33 size_t offset
, void *dest
, size_t len
,
34 struct lttng_ust_shm_handle
*handle
);
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.
42 __attribute__((visibility("hidden")))
44 lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
46 struct lttng_ust_shm_handle
*handle
);
48 __attribute__((visibility("hidden")))
50 lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
52 struct lttng_ust_shm_handle
*handle
);
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.
66 static inline __attribute__((always_inline
))
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
)
71 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
72 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
73 size_t offset
= ctx
->buf_offset
;
74 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
77 if (caa_unlikely(!len
))
80 * Underlying layer should never ask for writes across
83 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
84 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
85 if (caa_unlikely(!backend_pages
)) {
86 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
89 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
92 lib_ring_buffer_do_copy(config
, p
, src
, len
);
93 ctx
->buf_offset
+= len
;
97 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
98 * terminating character is found in @src. Returns the number of bytes
99 * copied. Does *not* terminate @dest with NULL terminating character.
101 static inline __attribute__((always_inline
))
102 size_t lib_ring_buffer_do_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
103 char *dest
, const char *src
, size_t len
)
107 for (count
= 0; count
< len
; count
++) {
111 * Only read source character once, in case it is
112 * modified concurrently.
114 c
= CMM_LOAD_SHARED(src
[count
]);
117 lib_ring_buffer_do_copy(config
, &dest
[count
], &c
, 1);
123 * lib_ring_buffer_strcpy - write string data to a buffer backend
124 * @config : ring buffer instance configuration
125 * @ctx: ring buffer context. (input arguments only)
126 * @src : source pointer to copy from
127 * @len : length of data to copy
128 * @pad : character to use for padding
130 * This function copies @len - 1 bytes of string data from a source
131 * pointer to a buffer backend, followed by a terminating '\0'
132 * character, at the current context offset. This is more or less a
133 * buffer backend-specific strncpy() operation. If a terminating '\0'
134 * character is found in @src before @len - 1 characters are copied, pad
135 * the buffer with @pad characters (e.g. '#').
137 static inline __attribute__((always_inline
))
138 void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
139 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
140 const char *src
, size_t len
, int pad
)
142 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
143 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
145 size_t offset
= ctx
->buf_offset
;
146 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
149 if (caa_unlikely(!len
))
152 * Underlying layer should never ask for writes across
155 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
156 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
157 if (caa_unlikely(!backend_pages
)) {
158 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
161 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
162 if (caa_unlikely(!p
))
165 count
= lib_ring_buffer_do_strcpy(config
, p
, src
, len
- 1);
168 if (caa_unlikely(count
< len
- 1)) {
169 size_t pad_len
= len
- 1 - count
;
171 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
172 if (caa_unlikely(!p
))
174 lib_ring_buffer_do_memset(p
, pad
, pad_len
);
178 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
179 if (caa_unlikely(!p
))
181 lib_ring_buffer_do_memset(p
, '\0', 1);
182 ctx
->buf_offset
+= len
;
186 * This accessor counts the number of unread records in a buffer.
187 * It only provides a consistent value if no reads not writes are performed
191 unsigned long lib_ring_buffer_get_records_unread(
192 const struct lttng_ust_lib_ring_buffer_config
*config
,
193 struct lttng_ust_lib_ring_buffer
*buf
,
194 struct lttng_ust_shm_handle
*handle
)
196 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
197 unsigned long records_unread
= 0, sb_bindex
;
199 struct lttng_ust_lib_ring_buffer_channel
*chan
;
201 chan
= shmp(handle
, bufb
->chan
);
204 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
205 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
206 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
207 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
209 wsb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
212 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
213 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
216 backend_pages
= shmp(handle
, rpages
->shmp
);
219 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
221 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
222 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
223 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
225 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
226 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
229 backend_pages
= shmp(handle
, rpages
->shmp
);
232 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
234 return records_unread
;
237 #endif /* _LTTNG_RING_BUFFER_BACKEND_H */