d0890408eb9bfa4965fda6f1326d00693530a6d8
[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 <string.h>
32 #include <time.h> /* for timer_t */
33
34 #include <urcu/list.h>
35 #include <urcu/uatomic.h>
36
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"
42 #include "vatomic.h"
43
44 /*
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).
47 */
48 enum switch_mode { SWITCH_ACTIVE, SWITCH_FLUSH };
49
50 /* channel: collection of per-cpu ring buffers. */
51 #define RB_CHANNEL_PADDING 32
52 struct channel {
53 int record_disabled;
54 unsigned long commit_count_mask; /*
55 * Commit count mask, removing
56 * the MSBs corresponding to
57 * bits used to represent the
58 * subbuffer index.
59 */
60
61 unsigned long switch_timer_interval; /* Buffer flush (us) */
62 timer_t switch_timer;
63 int switch_timer_enabled;
64
65 unsigned long read_timer_interval; /* Reader wakeup (us) */
66 timer_t read_timer;
67 int read_timer_enabled;
68
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 /* Extended options. */
74 union {
75 struct {
76 int32_t blocking_timeout_ms;
77 } s;
78 char padding[RB_CHANNEL_PADDING];
79 } u;
80 /*
81 * Associated backend contains a variable-length array. Needs to
82 * be last member.
83 */
84 struct channel_backend backend; /* Associated backend */
85 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
86
87 /* Per-subbuffer commit counters used on the hot path */
88 #define RB_COMMIT_COUNT_HOT_PADDING 16
89 struct commit_counters_hot {
90 union v_atomic cc; /* Commit counter */
91 union v_atomic seq; /* Consecutive commits */
92 char padding[RB_COMMIT_COUNT_HOT_PADDING];
93 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
94
95 /* Per-subbuffer commit counters used only on cold paths */
96 #define RB_COMMIT_COUNT_COLD_PADDING 24
97 struct commit_counters_cold {
98 union v_atomic cc_sb; /* Incremented _once_ at sb switch */
99 char padding[RB_COMMIT_COUNT_COLD_PADDING];
100 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
101
102 /* ring buffer state */
103 #define RB_CRASH_DUMP_ABI_LEN 256
104 #define RB_RING_BUFFER_PADDING 60
105
106 #define RB_CRASH_DUMP_ABI_MAGIC_LEN 16
107
108 /*
109 * The 128-bit magic number is xor'd in the process data so it does not
110 * cause a false positive when searching for buffers by scanning memory.
111 * The actual magic number is:
112 * 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17, 0x7B, 0xF1,
113 * 0x77, 0xBF, 0x17, 0x7B, 0xF1, 0x77, 0xBF, 0x17,
114 */
115 #define RB_CRASH_DUMP_ABI_MAGIC_XOR \
116 { \
117 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, 0x77 ^ 0xFF, \
118 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, 0xF1 ^ 0xFF, \
119 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, 0x7B ^ 0xFF, \
120 0xF1 ^ 0xFF, 0x77 ^ 0xFF, 0xBF ^ 0xFF, 0x17 ^ 0xFF, \
121 }
122
123 #define RB_CRASH_ENDIAN 0x1234
124
125 #define RB_CRASH_DUMP_ABI_MAJOR 0
126 #define RB_CRASH_DUMP_ABI_MINOR 0
127
128 enum lttng_crash_type {
129 LTTNG_CRASH_TYPE_UST = 0,
130 LTTNG_CRASH_TYPE_KERNEL = 1,
131 };
132
133 struct lttng_crash_abi {
134 uint8_t magic[RB_CRASH_DUMP_ABI_MAGIC_LEN];
135 uint64_t mmap_length; /* Overall lenght of crash record */
136 uint16_t endian; /*
137 * { 0x12, 0x34 }: big endian
138 * { 0x34, 0x12 }: little endian
139 */
140 uint16_t major; /* Major number. */
141 uint16_t minor; /* Minor number. */
142 uint8_t word_size; /* Word size (bytes). */
143 uint8_t layout_type; /* enum lttng_crash_type */
144
145 struct {
146 uint32_t prod_offset;
147 uint32_t consumed_offset;
148 uint32_t commit_hot_array;
149 uint32_t commit_hot_seq;
150 uint32_t buf_wsb_array;
151 uint32_t buf_wsb_id;
152 uint32_t sb_array;
153 uint32_t sb_array_shmp_offset;
154 uint32_t sb_backend_p_offset;
155 uint32_t content_size;
156 uint32_t packet_size;
157 } __attribute__((packed)) offset;
158 struct {
159 uint8_t prod_offset;
160 uint8_t consumed_offset;
161 uint8_t commit_hot_seq;
162 uint8_t buf_wsb_id;
163 uint8_t sb_array_shmp_offset;
164 uint8_t sb_backend_p_offset;
165 uint8_t content_size;
166 uint8_t packet_size;
167 } __attribute__((packed)) length;
168 struct {
169 uint32_t commit_hot_array;
170 uint32_t buf_wsb_array;
171 uint32_t sb_array;
172 } __attribute__((packed)) stride;
173
174 uint64_t buf_size; /* Size of the buffer */
175 uint64_t subbuf_size; /* Sub-buffer size */
176 uint64_t num_subbuf; /* Number of sub-buffers for writer */
177 uint32_t mode; /* Buffer mode: 0: overwrite, 1: discard */
178 } __attribute__((packed));
179
180 struct lttng_ust_lib_ring_buffer {
181 /* First 32 bytes are for the buffer crash dump ABI */
182 struct lttng_crash_abi crash_abi;
183
184 /* 32 bytes cache-hot cacheline */
185 union v_atomic __attribute__((aligned(32))) offset;
186 /* Current offset in the buffer */
187 DECLARE_SHMP(struct commit_counters_hot, commit_hot);
188 /* Commit count per sub-buffer */
189 long consumed; /*
190 * Current offset in the buffer
191 * standard atomic access (shared)
192 */
193 int record_disabled;
194 /* End of cache-hot 32 bytes cacheline */
195
196 union v_atomic last_tsc; /*
197 * Last timestamp written in the buffer.
198 */
199
200 struct lttng_ust_lib_ring_buffer_backend backend;
201 /* Associated backend */
202
203 DECLARE_SHMP(struct commit_counters_cold, commit_cold);
204 /* Commit count per sub-buffer */
205 DECLARE_SHMP(uint64_t, ts_end); /*
206 * timestamp_end per sub-buffer.
207 * Time is sampled by the
208 * switch_*_end() callbacks
209 * which are the last space
210 * reservation performed in the
211 * sub-buffer before it can be
212 * fully committed and
213 * delivered. This time value is
214 * then read by the deliver
215 * callback, performed by the
216 * last commit before the buffer
217 * becomes readable.
218 */
219 long active_readers; /*
220 * Active readers count
221 * standard atomic access (shared)
222 */
223 /* Dropped records */
224 union v_atomic records_lost_full; /* Buffer full */
225 union v_atomic records_lost_wrap; /* Nested wrap-around */
226 union v_atomic records_lost_big; /* Events too big */
227 union v_atomic records_count; /* Number of records written */
228 union v_atomic records_overrun; /* Number of overwritten records */
229 //wait_queue_head_t read_wait; /* reader buffer-level wait queue */
230 int finalized; /* buffer has been finalized */
231 unsigned long get_subbuf_consumed; /* Read-side consumed */
232 unsigned long prod_snapshot; /* Producer count snapshot */
233 unsigned long cons_snapshot; /* Consumer count snapshot */
234 unsigned int get_subbuf:1; /* Sub-buffer being held by reader */
235 /* shmp pointer to self */
236 DECLARE_SHMP(struct lttng_ust_lib_ring_buffer, self);
237 char padding[RB_RING_BUFFER_PADDING];
238 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
239
240 static inline
241 void *channel_get_private(struct channel *chan)
242 {
243 return ((char *) chan) + chan->priv_data_offset;
244 }
245
246 #ifndef __rb_same_type
247 #define __rb_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
248 #endif
249
250 /*
251 * Issue warnings and disable channels upon internal error.
252 * Can receive struct lttng_ust_lib_ring_buffer or struct lttng_ust_lib_ring_buffer_backend
253 * parameters.
254 */
255 #define CHAN_WARN_ON(c, cond) \
256 ({ \
257 struct channel *__chan; \
258 int _____ret = caa_unlikely(cond); \
259 if (_____ret) { \
260 if (__rb_same_type(*(c), struct channel_backend)) \
261 __chan = caa_container_of((void *) (c), \
262 struct channel, \
263 backend); \
264 else if (__rb_same_type(*(c), struct channel)) \
265 __chan = (void *) (c); \
266 else \
267 BUG_ON(1); \
268 uatomic_inc(&__chan->record_disabled); \
269 WARN_ON(1); \
270 } \
271 _____ret = _____ret; /* For clang "unused result". */ \
272 })
273
274 #endif /* _LTTNG_RING_BUFFER_FRONTEND_TYPES_H */
This page took 0.041514 seconds and 3 git commands to generate.