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.
32 #include <time.h> /* for timer_t */
34 #include <urcu/list.h>
35 #include <urcu/uatomic.h>
37 #include <lttng/ringbuffer-config.h>
38 #include <usterr-signal-safe.h>
39 #include "backend_types.h"
40 #include "shm_internal.h"
41 #include "shm_types.h"
45 * A switch is done during tracing or as a final flush after tracing (so it
46 * won't write in the new sub-buffer).
48 enum switch_mode
{ SWITCH_ACTIVE
, SWITCH_FLUSH
};
50 /* channel: collection of per-cpu ring buffers. */
51 #define RB_CHANNEL_PADDING 32
54 unsigned long commit_count_mask
; /*
55 * Commit count mask, removing
56 * the MSBs corresponding to
57 * bits used to represent the
61 unsigned long switch_timer_interval
; /* Buffer flush (us) */
63 int switch_timer_enabled
;
65 unsigned long read_timer_interval
; /* Reader wakeup (us) */
67 int read_timer_enabled
;
69 int finalized
; /* Has channel been finalized */
70 size_t priv_data_offset
;
71 unsigned int nr_streams
; /* Number of streams */
72 struct lttng_ust_shm_handle
*handle
;
73 char padding
[RB_CHANNEL_PADDING
];
75 * Associated backend contains a variable-length array. Needs to
78 struct channel_backend backend
; /* Associated backend */
79 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
81 /* Per-subbuffer commit counters used on the hot path */
82 #define RB_COMMIT_COUNT_HOT_PADDING 16
83 struct commit_counters_hot
{
84 union v_atomic cc
; /* Commit counter */
85 union v_atomic seq
; /* Consecutive commits */
86 char padding
[RB_COMMIT_COUNT_HOT_PADDING
];
87 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
89 /* Per-subbuffer commit counters used only on cold paths */
90 #define RB_COMMIT_COUNT_COLD_PADDING 24
91 struct commit_counters_cold
{
92 union v_atomic cc_sb
; /* Incremented _once_ at sb switch */
93 char padding
[RB_COMMIT_COUNT_COLD_PADDING
];
94 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
96 /* ring buffer state */
97 #define RB_CRASH_DUMP_ABI_LEN 256
98 #define RB_RING_BUFFER_PADDING 60
100 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
103 * The 128-bit magic number is xor'd in the process data so it does not
104 * cause a false positive when searching for buffers by scanning memory.
105 * The actual magic number is:
106 * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1,
107 * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17,
109 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
111 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, \
112 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, \
113 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, \
114 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
117 #define RB_CRASH_ENDIAN 0x1234
119 #define RB_CRASH_DUMP_ABI_MAJOR 0
120 #define RB_CRASH_DUMP_ABI_MINOR 0
122 enum lttng_crash_type
{
123 LTTNG_CRASH_TYPE_UST
= 0,
124 LTTNG_CRASH_TYPE_KERNEL
= 1,
127 struct lttng_crash_abi
{
128 uint8_t magic
[RB_CRASH_DUMP_ABI_MAGIC_LEN
];
129 uint64_t mmap_length
; /* Overall lenght of crash record */
131 * { 0x12, 0x34 }: big endian
132 * { 0x34, 0x12 }: little endian
134 uint16_t major
; /* Major number. */
135 uint16_t minor
; /* Minor number. */
136 uint8_t word_size
; /* Word size (bytes). */
137 uint8_t layout_type
; /* enum lttng_crash_type */
140 uint32_t prod_offset
;
141 uint32_t consumed_offset
;
142 uint32_t commit_hot_array
;
143 uint32_t commit_hot_seq
;
144 uint32_t buf_wsb_array
;
147 uint32_t sb_array_shmp_offset
;
148 uint32_t sb_backend_p_offset
;
149 uint32_t content_size
;
150 uint32_t packet_size
;
151 } __attribute__((packed
)) offset
;
154 uint8_t consumed_offset
;
155 uint8_t commit_hot_seq
;
157 uint8_t sb_array_shmp_offset
;
158 uint8_t sb_backend_p_offset
;
159 uint8_t content_size
;
161 } __attribute__((packed
)) length
;
163 uint32_t commit_hot_array
;
164 uint32_t buf_wsb_array
;
166 } __attribute__((packed
)) stride
;
168 uint64_t buf_size
; /* Size of the buffer */
169 uint64_t subbuf_size
; /* Sub-buffer size */
170 uint64_t num_subbuf
; /* Number of sub-buffers for writer */
171 uint32_t mode
; /* Buffer mode: 0: overwrite, 1: discard */
172 } __attribute__((packed
));
174 struct lttng_ust_lib_ring_buffer
{
175 /* First 32 bytes are for the buffer crash dump ABI */
176 struct lttng_crash_abi crash_abi
;
178 /* 32 bytes cache-hot cacheline */
179 union v_atomic
__attribute__((aligned(32))) offset
;
180 /* Current offset in the buffer */
181 DECLARE_SHMP(struct commit_counters_hot
, commit_hot
);
182 /* Commit count per sub-buffer */
184 * Current offset in the buffer
185 * standard atomic access (shared)
188 /* End of cache-hot 32 bytes cacheline */
190 union v_atomic last_tsc
; /*
191 * Last timestamp written in the buffer.
194 struct lttng_ust_lib_ring_buffer_backend backend
;
195 /* Associated backend */
197 DECLARE_SHMP(struct commit_counters_cold
, commit_cold
);
198 /* Commit count per sub-buffer */
199 long active_readers
; /*
200 * Active readers count
201 * standard atomic access (shared)
203 /* Dropped records */
204 union v_atomic records_lost_full
; /* Buffer full */
205 union v_atomic records_lost_wrap
; /* Nested wrap-around */
206 union v_atomic records_lost_big
; /* Events too big */
207 union v_atomic records_count
; /* Number of records written */
208 union v_atomic records_overrun
; /* Number of overwritten records */
209 //wait_queue_head_t read_wait; /* reader buffer-level wait queue */
210 int finalized
; /* buffer has been finalized */
211 unsigned long get_subbuf_consumed
; /* Read-side consumed */
212 unsigned long prod_snapshot
; /* Producer count snapshot */
213 unsigned long cons_snapshot
; /* Consumer count snapshot */
214 unsigned int get_subbuf
:1; /* Sub-buffer being held by reader */
215 /* shmp pointer to self */
216 DECLARE_SHMP(struct lttng_ust_lib_ring_buffer
, self
);
217 char padding
[RB_RING_BUFFER_PADDING
];
218 } __attribute__((aligned(CAA_CACHE_LINE_SIZE
)));
221 void *channel_get_private(struct channel
*chan
)
223 return ((char *) chan
) + chan
->priv_data_offset
;
226 #ifndef __rb_same_type
227 #define __rb_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
231 * Issue warnings and disable channels upon internal error.
232 * Can receive struct lttng_ust_lib_ring_buffer or struct lttng_ust_lib_ring_buffer_backend
235 #define CHAN_WARN_ON(c, cond) \
237 struct channel *__chan; \
238 int _____ret = caa_unlikely(cond); \
240 if (__rb_same_type(*(c), struct channel_backend)) \
241 __chan = caa_container_of((void *) (c), \
244 else if (__rb_same_type(*(c), struct channel)) \
245 __chan = (void *) (c); \
248 uatomic_inc(&__chan->record_disabled); \
251 _____ret = _____ret; /* For clang "unused result". */ \
254 #endif /* _LTTNG_RING_BUFFER_FRONTEND_TYPES_H */