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 #include "ust-helper.h"
20 /* Internal helpers */
21 #include "backend_internal.h"
22 #include "frontend_internal.h"
24 /* Ring buffer backend API */
26 /* Ring buffer backend access (read/write) */
29 extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
30 size_t offset
, void *dest
, size_t len
,
31 struct lttng_ust_shm_handle
*handle
);
34 extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
35 size_t offset
, void *dest
, size_t len
,
36 struct lttng_ust_shm_handle
*handle
);
39 * Return the address where a given offset is located.
40 * Should be used to get the current subbuffer header pointer. Given we know
41 * it's never on a page boundary, it's safe to write directly to this address,
42 * as long as the write is never bigger than a page size.
46 lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
48 struct lttng_ust_shm_handle
*handle
);
51 lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
53 struct lttng_ust_shm_handle
*handle
);
56 * lib_ring_buffer_write - write data to a buffer backend
57 * @config : ring buffer instance configuration
58 * @ctx: ring buffer context. (input arguments only)
59 * @src : source pointer to copy from
60 * @len : length of data to copy
62 * This function copies "len" bytes of data from a source pointer to a buffer
63 * backend, at the current context offset. This is more or less a buffer
64 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
65 * if copy is crossing a page boundary.
67 static inline __attribute__((always_inline
))
68 void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config
*config
,
69 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
70 const void *src
, size_t len
)
72 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
73 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
74 size_t offset
= ctx
->buf_offset
;
75 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
78 if (caa_unlikely(!len
))
81 * Underlying layer should never ask for writes across
84 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
85 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
86 if (caa_unlikely(!backend_pages
)) {
87 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
90 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
93 lib_ring_buffer_do_copy(config
, p
, src
, len
);
94 ctx
->buf_offset
+= len
;
98 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
99 * terminating character is found in @src. Returns the number of bytes
100 * copied. Does *not* terminate @dest with NULL terminating character.
102 static inline __attribute__((always_inline
))
103 size_t lib_ring_buffer_do_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
104 char *dest
, const char *src
, size_t len
)
108 for (count
= 0; count
< len
; count
++) {
112 * Only read source character once, in case it is
113 * modified concurrently.
115 c
= CMM_LOAD_SHARED(src
[count
]);
118 lib_ring_buffer_do_copy(config
, &dest
[count
], &c
, 1);
124 * lib_ring_buffer_strcpy - write string data to a buffer backend
125 * @config : ring buffer instance configuration
126 * @ctx: ring buffer context. (input arguments only)
127 * @src : source pointer to copy from
128 * @len : length of data to copy
129 * @pad : character to use for padding
131 * This function copies @len - 1 bytes of string data from a source
132 * pointer to a buffer backend, followed by a terminating '\0'
133 * character, at the current context offset. This is more or less a
134 * buffer backend-specific strncpy() operation. If a terminating '\0'
135 * character is found in @src before @len - 1 characters are copied, pad
136 * the buffer with @pad characters (e.g. '#').
138 static inline __attribute__((always_inline
))
139 void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
140 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
141 const char *src
, size_t len
, int pad
)
143 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
144 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
146 size_t offset
= ctx
->buf_offset
;
147 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
150 if (caa_unlikely(!len
))
153 * Underlying layer should never ask for writes across
156 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
157 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
158 if (caa_unlikely(!backend_pages
)) {
159 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
162 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
163 if (caa_unlikely(!p
))
166 count
= lib_ring_buffer_do_strcpy(config
, p
, src
, len
- 1);
169 if (caa_unlikely(count
< len
- 1)) {
170 size_t pad_len
= len
- 1 - count
;
172 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
173 if (caa_unlikely(!p
))
175 lib_ring_buffer_do_memset(p
, pad
, pad_len
);
179 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
180 if (caa_unlikely(!p
))
182 lib_ring_buffer_do_memset(p
, '\0', 1);
183 ctx
->buf_offset
+= len
;
187 * This accessor counts the number of unread records in a buffer.
188 * It only provides a consistent value if no reads not writes are performed
192 unsigned long lib_ring_buffer_get_records_unread(
193 const struct lttng_ust_lib_ring_buffer_config
*config
,
194 struct lttng_ust_lib_ring_buffer
*buf
,
195 struct lttng_ust_shm_handle
*handle
)
197 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
198 unsigned long records_unread
= 0, sb_bindex
;
200 struct channel
*chan
;
202 chan
= shmp(handle
, bufb
->chan
);
205 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
206 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
207 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
208 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
210 wsb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
213 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
214 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
217 backend_pages
= shmp(handle
, rpages
->shmp
);
220 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
222 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
223 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
224 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
226 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
227 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
230 backend_pages
= shmp(handle
, rpages
->shmp
);
233 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
235 return records_unread
;
238 #endif /* _LTTNG_RING_BUFFER_BACKEND_H */