Cleanup: apply `include-what-you-use` guideline for `uint*_t`
[lttng-ust.git] / libringbuffer / frontend_types.h
CommitLineData
e92f3e28
MD
1#ifndef _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
2#define _LTTNG_RING_BUFFER_FRONTEND_TYPES_H
852c2936
MD
3
4/*
e92f3e28 5 * libringbuffer/frontend_types.h
852c2936
MD
6 *
7 * Ring Buffer Library Synchronization Header (types).
8 *
e92f3e28
MD
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 *
852c2936
MD
25 * Author:
26 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
27 *
28 * See ring_buffer_frontend.c for more information on wait-free algorithms.
852c2936
MD
29 */
30
fb31eb73 31#include <stdint.h>
a6352fd4 32#include <string.h>
03d2d293 33#include <time.h> /* for timer_t */
a6352fd4 34
14641deb
MD
35#include <urcu/list.h>
36#include <urcu/uatomic.h>
14641deb 37
4318ae1b 38#include <lttng/ringbuffer-config.h>
44c72f10 39#include <usterr-signal-safe.h>
4931a13e 40#include "backend_types.h"
1d498196 41#include "shm_internal.h"
03d2d293 42#include "shm_types.h"
a3bb4b27 43#include "vatomic.h"
852c2936
MD
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 */
49enum switch_mode { SWITCH_ACTIVE, SWITCH_FLUSH };
50
852c2936 51/* channel: collection of per-cpu ring buffers. */
74d81a6c 52#define RB_CHANNEL_PADDING 32
852c2936 53struct channel {
14641deb 54 int record_disabled;
852c2936
MD
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
03d2d293
MD
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) */
34a91bdb
MD
67 timer_t read_timer;
68 int read_timer_enabled;
69
852c2936 70 int finalized; /* Has channel been finalized */
a3f61e7f 71 size_t priv_data_offset;
74d81a6c 72 unsigned int nr_streams; /* Number of streams */
03d2d293 73 struct lttng_ust_shm_handle *handle;
b2c5f61a
MD
74 /* Extended options. */
75 union {
76 struct {
77 int32_t blocking_timeout_ms;
78 } s;
79 char padding[RB_CHANNEL_PADDING];
80 } u;
de85e7c3
MD
81 /*
82 * Associated backend contains a variable-length array. Needs to
83 * be last member.
84 */
85 struct channel_backend backend; /* Associated backend */
b728d87e 86} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
852c2936
MD
87
88/* Per-subbuffer commit counters used on the hot path */
3c8964ba 89#define RB_COMMIT_COUNT_HOT_PADDING 16
852c2936
MD
90struct commit_counters_hot {
91 union v_atomic cc; /* Commit counter */
92 union v_atomic seq; /* Consecutive commits */
3c8964ba 93 char padding[RB_COMMIT_COUNT_HOT_PADDING];
b728d87e 94} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
852c2936
MD
95
96/* Per-subbuffer commit counters used only on cold paths */
3c8964ba 97#define RB_COMMIT_COUNT_COLD_PADDING 24
852c2936
MD
98struct commit_counters_cold {
99 union v_atomic cc_sb; /* Incremented _once_ at sb switch */
3c8964ba 100 char padding[RB_COMMIT_COUNT_COLD_PADDING];
b728d87e 101} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
852c2936 102
852c2936 103/* ring buffer state */
a9ff648c
MD
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
129enum lttng_crash_type {
130 LTTNG_CRASH_TYPE_UST = 0,
131 LTTNG_CRASH_TYPE_KERNEL = 1,
132};
133
134struct 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
4cfec15c 181struct lttng_ust_lib_ring_buffer {
a9ff648c
MD
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 */
a6352fd4 188 DECLARE_SHMP(struct commit_counters_hot, commit_hot);
852c2936 189 /* Commit count per sub-buffer */
14641deb 190 long consumed; /*
852c2936
MD
191 * Current offset in the buffer
192 * standard atomic access (shared)
193 */
14641deb 194 int record_disabled;
a9ff648c
MD
195 /* End of cache-hot 32 bytes cacheline */
196
852c2936
MD
197 union v_atomic last_tsc; /*
198 * Last timestamp written in the buffer.
199 */
200
a9ff648c
MD
201 struct lttng_ust_lib_ring_buffer_backend backend;
202 /* Associated backend */
852c2936 203
a6352fd4 204 DECLARE_SHMP(struct commit_counters_cold, commit_cold);
852c2936 205 /* Commit count per sub-buffer */
6c737d05
MD
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 */
14641deb 220 long active_readers; /*
852c2936
MD
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 */
14641deb 230 //wait_queue_head_t read_wait; /* reader buffer-level wait queue */
852c2936 231 int finalized; /* buffer has been finalized */
852c2936
MD
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 */
34a91bdb 235 unsigned int get_subbuf:1; /* Sub-buffer being held by reader */
5d61a504 236 /* shmp pointer to self */
4cfec15c 237 DECLARE_SHMP(struct lttng_ust_lib_ring_buffer, self);
3c8964ba 238 char padding[RB_RING_BUFFER_PADDING];
b728d87e 239} __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
852c2936
MD
240
241static inline
242void *channel_get_private(struct channel *chan)
243{
a3f61e7f 244 return ((char *) chan) + chan->priv_data_offset;
852c2936
MD
245}
246
0d4aa2df
MD
247#ifndef __rb_same_type
248#define __rb_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
249#endif
250
852c2936
MD
251/*
252 * Issue warnings and disable channels upon internal error.
4cfec15c 253 * Can receive struct lttng_ust_lib_ring_buffer or struct lttng_ust_lib_ring_buffer_backend
852c2936
MD
254 * parameters.
255 */
256#define CHAN_WARN_ON(c, cond) \
257 ({ \
258 struct channel *__chan; \
b5a3dfa5 259 int _____ret = caa_unlikely(cond); \
852c2936 260 if (_____ret) { \
0d4aa2df 261 if (__rb_same_type(*(c), struct channel_backend)) \
14641deb 262 __chan = caa_container_of((void *) (c), \
852c2936
MD
263 struct channel, \
264 backend); \
0d4aa2df 265 else if (__rb_same_type(*(c), struct channel)) \
852c2936
MD
266 __chan = (void *) (c); \
267 else \
268 BUG_ON(1); \
14641deb 269 uatomic_inc(&__chan->record_disabled); \
852c2936
MD
270 WARN_ON(1); \
271 } \
257ecc17 272 _____ret = _____ret; /* For clang "unused result". */ \
852c2936
MD
273 })
274
e92f3e28 275#endif /* _LTTNG_RING_BUFFER_FRONTEND_TYPES_H */
This page took 0.040752 seconds and 4 git commands to generate.