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