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