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
);
30 extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
31 size_t offset
, void *dest
, size_t len
,
32 struct lttng_ust_shm_handle
*handle
);
35 * Return the address where a given offset is located.
36 * Should be used to get the current subbuffer header pointer. Given we know
37 * it's never on a page boundary, it's safe to write directly to this address,
38 * as long as the write is never bigger than a page size.
41 lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
43 struct lttng_ust_shm_handle
*handle
);
45 lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
47 struct lttng_ust_shm_handle
*handle
);
50 * lib_ring_buffer_write - write data to a buffer backend
51 * @config : ring buffer instance configuration
52 * @ctx: ring buffer context. (input arguments only)
53 * @src : source pointer to copy from
54 * @len : length of data to copy
56 * This function copies "len" bytes of data from a source pointer to a buffer
57 * backend, at the current context offset. This is more or less a buffer
58 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
59 * if copy is crossing a page boundary.
61 static inline __attribute__((always_inline
))
62 void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config
*config
,
63 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
64 const void *src
, size_t len
)
66 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
67 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
68 size_t offset
= ctx
->buf_offset
;
69 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
72 if (caa_unlikely(!len
))
75 * Underlying layer should never ask for writes across
78 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
79 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
80 if (caa_unlikely(!backend_pages
)) {
81 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
84 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
87 lib_ring_buffer_do_copy(config
, p
, src
, len
);
88 ctx
->buf_offset
+= len
;
92 * Copy up to @len string bytes from @src to @dest. Stop whenever a NULL
93 * terminating character is found in @src. Returns the number of bytes
94 * copied. Does *not* terminate @dest with NULL terminating character.
96 static inline __attribute__((always_inline
))
97 size_t lib_ring_buffer_do_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
98 char *dest
, const char *src
, size_t len
)
102 for (count
= 0; count
< len
; count
++) {
106 * Only read source character once, in case it is
107 * modified concurrently.
109 c
= CMM_LOAD_SHARED(src
[count
]);
112 lib_ring_buffer_do_copy(config
, &dest
[count
], &c
, 1);
118 * lib_ring_buffer_strcpy - write string data to a buffer backend
119 * @config : ring buffer instance configuration
120 * @ctx: ring buffer context. (input arguments only)
121 * @src : source pointer to copy from
122 * @len : length of data to copy
123 * @pad : character to use for padding
125 * This function copies @len - 1 bytes of string data from a source
126 * pointer to a buffer backend, followed by a terminating '\0'
127 * character, at the current context offset. This is more or less a
128 * buffer backend-specific strncpy() operation. If a terminating '\0'
129 * character is found in @src before @len - 1 characters are copied, pad
130 * the buffer with @pad characters (e.g. '#').
132 static inline __attribute__((always_inline
))
133 void lib_ring_buffer_strcpy(const struct lttng_ust_lib_ring_buffer_config
*config
,
134 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
135 const char *src
, size_t len
, int pad
)
137 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
138 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
140 size_t offset
= ctx
->buf_offset
;
141 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
144 if (caa_unlikely(!len
))
147 * Underlying layer should never ask for writes across
150 CHAN_WARN_ON(chanb
, (offset
& (chanb
->buf_size
- 1)) + len
> chanb
->buf_size
);
151 backend_pages
= lib_ring_buffer_get_backend_pages_from_ctx(config
, ctx
);
152 if (caa_unlikely(!backend_pages
)) {
153 if (lib_ring_buffer_backend_get_pages(config
, ctx
, &backend_pages
))
156 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
157 if (caa_unlikely(!p
))
160 count
= lib_ring_buffer_do_strcpy(config
, p
, src
, len
- 1);
163 if (caa_unlikely(count
< len
- 1)) {
164 size_t pad_len
= len
- 1 - count
;
166 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
167 if (caa_unlikely(!p
))
169 lib_ring_buffer_do_memset(p
, pad
, pad_len
);
173 p
= shmp_index(handle
, backend_pages
->p
, offset
& (chanb
->subbuf_size
- 1));
174 if (caa_unlikely(!p
))
176 lib_ring_buffer_do_memset(p
, '\0', 1);
177 ctx
->buf_offset
+= len
;
181 * This accessor counts the number of unread records in a buffer.
182 * It only provides a consistent value if no reads not writes are performed
186 unsigned long lib_ring_buffer_get_records_unread(
187 const struct lttng_ust_lib_ring_buffer_config
*config
,
188 struct lttng_ust_lib_ring_buffer
*buf
,
189 struct lttng_ust_shm_handle
*handle
)
191 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
192 unsigned long records_unread
= 0, sb_bindex
;
194 struct channel
*chan
;
196 chan
= shmp(handle
, bufb
->chan
);
199 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
200 struct lttng_ust_lib_ring_buffer_backend_subbuffer
*wsb
;
201 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
202 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
204 wsb
= shmp_index(handle
, bufb
->buf_wsb
, i
);
207 sb_bindex
= subbuffer_id_get_index(config
, wsb
->id
);
208 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
211 backend_pages
= shmp(handle
, rpages
->shmp
);
214 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
216 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
217 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*rpages
;
218 struct lttng_ust_lib_ring_buffer_backend_pages
*backend_pages
;
220 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
221 rpages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
224 backend_pages
= shmp(handle
, rpages
->shmp
);
227 records_unread
+= v_read(config
, &backend_pages
->records_unread
);
229 return records_unread
;
232 #endif /* _LTTNG_RING_BUFFER_BACKEND_H */