43300f6a74ddec2324fbfb9f0d0c6dd8bcbb1786
[lttng-ust.git] / libringbuffer / frontend_types.h
1 #ifndef _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
2 #define _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
3
4 /*
5 * libringbuffer/frontend_types.h
6 *
7 * Ring Buffer Library Synchronization Header (types).
8 *
9 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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.
15 *
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.
20 *
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
24 *
25 * Author:
26 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
27 *
28 * See ring_buffer_frontend.c for more information on wait-free algorithms.
29 */
30
31 #include <stdint.h>
32 #include <string.h>
33 #include <time.h> /* for timer_t */
34
35 #include <urcu/list.h>
36 #include <urcu/uatomic.h>
37
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"
43 #include "vatomic.h"
44
45 /*
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).
48 */
49 enum switch_mode { SWITCH_ACTIVE, SWITCH_FLUSH };
50
51 /* channel: collection of per-cpu ring buffers. */
52 #define RB_CHANNEL_PADDING 32
53 struct channel {
54 int record_disabled;
55 unsigned long commit_count_mask; /*
56 * Commit count mask, removing
57 * the MSBs corresponding to
58 * bits used to represent the
59 * subbuffer index.
60 */
61
62 unsigned long switch_timer_interval; /* Buffer flush (us) */
63 timer_t switch_timer;
64 int switch_timer_enabled;
65
66 unsigned long read_timer_interval; /* Reader wakeup (us) */
67 timer_t read_timer;
68 int read_timer_enabled;
69
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. */
75 union {
76 struct {
77 int32_t blocking_timeout_ms;
78 } s;
79 char padding[RB_CHANNEL_PADDING];
80 } u;
81 /*
82 * Associated backend contains a variable-length array. Needs to
83 * be last member.
84 */
85 struct channel_backend backend; /* Associated backend */
86 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
87
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)));
95
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)));
102
103 /* ring buffer state */
104 #define RB_CRASH_DUMP_ABI_LEN 256
105 #define RB_RING_BUFFER_PADDING 60
106
107 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
108
109 /*
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,
115 */
116 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
117 { \
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, \
122 }
123
124 #define RB_CRASH_ENDIAN 0x1234
125
126 #define RB_CRASH_DUMP_ABI_MAJOR 0
127 #define RB_CRASH_DUMP_ABI_MINOR 0
128
129 enum lttng_crash_type {
130 LTTNG_CRASH_TYPE_UST = 0,
131 LTTNG_CRASH_TYPE_KERNEL = 1,
132 };
133
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 */
137 uint16_t endian; /*
138 * { 0x12, 0x34 }: big endian
139 * { 0x34, 0x12 }: little endian
140 */
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 */
145
146 struct {
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;
152 uint32_t buf_wsb_id;
153 uint32_t sb_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;
159 struct {
160 uint8_t prod_offset;
161 uint8_t consumed_offset;
162 uint8_t commit_hot_seq;
163 uint8_t buf_wsb_id;
164 uint8_t sb_array_shmp_offset;
165 uint8_t sb_backend_p_offset;
166 uint8_t content_size;
167 uint8_t packet_size;
168 } __attribute__((packed)) length;
169 struct {
170 uint32_t commit_hot_array;
171 uint32_t buf_wsb_array;
172 uint32_t sb_array;
173 } __attribute__((packed)) stride;
174
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));
180
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;
184
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 */
190 long consumed; /*
191 * Current offset in the buffer
192 * standard atomic access (shared)
193 */
194 int record_disabled;
195 /* End of cache-hot 32 bytes cacheline */
196
197 union v_atomic last_tsc; /*
198 * Last timestamp written in the buffer.
199 */
200
201 struct lttng_ust_lib_ring_buffer_backend backend;
202 /* Associated backend */
203
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
218 * becomes readable.
219 */
220 long active_readers; /*
221 * Active readers count
222 * standard atomic access (shared)
223 */
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)));
240
241 static inline
242 void *channel_get_private(struct channel *chan)
243 {
244 return ((char *) chan) + chan->priv_data_offset;
245 }
246
247 #ifndef __rb_same_type
248 #define __rb_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
249 #endif
250
251 /*
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
254 * parameters.
255 */
256 #define CHAN_WARN_ON(c, cond) \
257 ({ \
258 struct channel *__chan; \
259 int _____ret = caa_unlikely(cond); \
260 if (_____ret) { \
261 if (__rb_same_type(*(c), struct channel_backend)) \
262 __chan = caa_container_of((void *) (c), \
263 struct channel, \
264 backend); \
265 else if (__rb_same_type(*(c), struct channel)) \
266 __chan = (void *) (c); \
267 else \
268 BUG_ON(1); \
269 uatomic_inc(&__chan->record_disabled); \
270 WARN_ON(1); \
271 } \
272 _____ret = _____ret; /* For clang "unused result". */ \
273 })
274
275 #endif /* _LTTNG_RING_BUFFER_FRONTEND_TYPES_H */
This page took 0.034975 seconds and 3 git commands to generate.