1 #ifndef _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
2 #define _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
5 * libringbuffer/frontend_types.h
7 * Ring Buffer Library Synchronization Header (types).
9 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; only
14 * version 2.1 of the License.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
28 * See ring_buffer_frontend.c for more information on wait-free algorithms.
33 #include <time.h> /* for timer_t */
35 #include <urcu/list.h>
36 #include <urcu/uatomic.h>
38 #include <lttng/ringbuffer-config.h>
39 #include <usterr-signal-safe.h>
40 #include "backend_types.h"
41 #include "shm_internal.h"
42 #include "shm_types.h"
46 * A switch is done during tracing or as a final flush after tracing (so it
47 * won't write in the new sub-buffer).
49 enum switch_mode
{ SWITCH_ACTIVE
, SWITCH_FLUSH
};
51 /* channel: collection of per-cpu ring buffers. */
52 #define RB_CHANNEL_PADDING 32
55 unsigned long commit_count_mask
; /*
56 * Commit count mask, removing
57 * the MSBs corresponding to
58 * bits used to represent the
62 unsigned long switch_timer_interval
; /* Buffer flush (us) */
64 int switch_timer_enabled
;
66 unsigned long read_timer_interval
; /* Reader wakeup (us) */
68 int read_timer_enabled
;
70 int finalized
; /* Has channel been finalized */
71 size_t priv_data_offset
;
72 unsigned int nr_streams
; /* Number of streams */
73 struct lttng_ust_shm_handle
*handle
;
74 /* Extended options. */
77 int32_t blocking_timeout_ms
;
79 char padding
[RB_CHANNEL_PADDING
];
82 * Associated backend contains a variable-length array. Needs to
85 struct channel_backend backend
; /* Associated backend */
86 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
88 /* Per-subbuffer commit counters used on the hot path */
89 #define RB_COMMIT_COUNT_HOT_PADDING 16
90 struct commit_counters_hot
{
91 union v_atomic cc
; /* Commit counter */
92 union v_atomic seq
; /* Consecutive commits */
93 char padding
[RB_COMMIT_COUNT_HOT_PADDING
];
94 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
96 /* Per-subbuffer commit counters used only on cold paths */
97 #define RB_COMMIT_COUNT_COLD_PADDING 24
98 struct commit_counters_cold
{
99 union v_atomic cc_sb
; /* Incremented _once_ at sb switch */
100 char padding
[RB_COMMIT_COUNT_COLD_PADDING
];
101 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
103 /* ring buffer state */
104 #define RB_CRASH_DUMP_ABI_LEN 256
105 #define RB_RING_BUFFER_PADDING 60
107 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
110 * The 128-bit magic number is xor'd in the process data so it does not
111 * cause a false positive when searching for buffers by scanning memory.
112 * The actual magic number is:
113 * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1,
114 * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17,
116 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
118 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, \
119 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, \
120 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, \
121 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
124 #define RB_CRASH_ENDIAN 0x1234
126 #define RB_CRASH_DUMP_ABI_MAJOR 0
127 #define RB_CRASH_DUMP_ABI_MINOR 0
129 enum lttng_crash_type
{
130 LTTNG_CRASH_TYPE_UST
= 0,
131 LTTNG_CRASH_TYPE_KERNEL
= 1,
134 struct lttng_crash_abi
{
135 uint8_t magic
[RB_CRASH_DUMP_ABI_MAGIC_LEN
];
136 uint64_t mmap_length
; /* Overall lenght of crash record */
138 * { 0x12, 0x34 }: big endian
139 * { 0x34, 0x12 }: little endian
141 uint16_t major
; /* Major number. */
142 uint16_t minor
; /* Minor number. */
143 uint8_t word_size
; /* Word size (bytes). */
144 uint8_t layout_type
; /* enum lttng_crash_type */
147 uint32_t prod_offset
;
148 uint32_t consumed_offset
;
149 uint32_t commit_hot_array
;
150 uint32_t commit_hot_seq
;
151 uint32_t buf_wsb_array
;
154 uint32_t sb_array_shmp_offset
;
155 uint32_t sb_backend_p_offset
;
156 uint32_t content_size
;
157 uint32_t packet_size
;
158 } __attribute__((packed
)) offset
;
161 uint8_t consumed_offset
;
162 uint8_t commit_hot_seq
;
164 uint8_t sb_array_shmp_offset
;
165 uint8_t sb_backend_p_offset
;
166 uint8_t content_size
;
168 } __attribute__((packed
)) length
;
170 uint32_t commit_hot_array
;
171 uint32_t buf_wsb_array
;
173 } __attribute__((packed
)) stride
;
175 uint64_t buf_size
; /* Size of the buffer */
176 uint64_t subbuf_size
; /* Sub-buffer size */
177 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
178 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
179 } __attribute__((packed
));
181 struct lttng_ust_lib_ring_buffer
{
182 /* First 32 bytes are for the buffer crash dump ABI */
183 struct lttng_crash_abi crash_abi
;
185 /* 32 bytes cache-hot cacheline */
186 union v_atomic
__attribute__((aligned(32))) offset
;
187 /* Current offset in the buffer */
188 DECLARE_SHMP(struct commit_counters_hot
, commit_hot
);
189 /* Commit count per sub-buffer */
191 * Current offset in the buffer
192 * standard atomic access (shared)
195 /* End of cache-hot 32 bytes cacheline */
197 union v_atomic last_tsc
; /*
198 * Last timestamp written in the buffer.
201 struct lttng_ust_lib_ring_buffer_backend backend
;
202 /* Associated backend */
204 DECLARE_SHMP(struct commit_counters_cold
, commit_cold
);
205 /* Commit count per sub-buffer */
206 DECLARE_SHMP(uint64_t, ts_end
); /*
207 * timestamp_end per sub-buffer.
208 * Time is sampled by the
209 * switch_*_end() callbacks
210 * which are the last space
211 * reservation performed in the
212 * sub-buffer before it can be
213 * fully committed and
214 * delivered. This time value is
215 * then read by the deliver
216 * callback, performed by the
217 * last commit before the buffer
220 long active_readers
; /*
221 * Active readers count
222 * standard atomic access (shared)
224 /* Dropped records */
225 union v_atomic records_lost_full
; /* Buffer full */
226 union v_atomic records_lost_wrap
; /* Nested wrap-around */
227 union v_atomic records_lost_big
; /* Events too big */
228 union v_atomic records_count
; /* Number of records written */
229 union v_atomic records_overrun
; /* Number of overwritten records */
230 //wait_queue_head_t read_wait; /* reader buffer-level wait queue */
231 int finalized
; /* buffer has been finalized */
232 unsigned long get_subbuf_consumed
; /* Read-side consumed */
233 unsigned long prod_snapshot
; /* Producer count snapshot */
234 unsigned long cons_snapshot
; /* Consumer count snapshot */
235 unsigned int get_subbuf
:1; /* Sub-buffer being held by reader */
236 /* shmp pointer to self */
237 DECLARE_SHMP(struct lttng_ust_lib_ring_buffer
, self
);
238 char padding
[RB_RING_BUFFER_PADDING
];
239 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
242 void *channel_get_private(struct channel
*chan
)
244 return ((char *) chan
) + chan
->priv_data_offset
;
247 #ifndef __rb_same_type
248 #define __rb_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
252 * Issue warnings and disable channels upon internal error.
253 * Can receive struct lttng_ust_lib_ring_buffer or struct lttng_ust_lib_ring_buffer_backend
256 #define CHAN_WARN_ON(c, cond) \
258 struct channel *__chan; \
259 int _____ret = caa_unlikely(cond); \
261 if (__rb_same_type(*(c), struct channel_backend)) \
262 __chan = caa_container_of((void *) (c), \
265 else if (__rb_same_type(*(c), struct channel)) \
266 __chan = (void *) (c); \
269 uatomic_inc(&__chan->record_disabled); \
272 _____ret = _____ret; /* For clang "unused result". */ \
275 #endif /* _LTTNG_RING_BUFFER_FRONTEND_TYPES_H */