Version 2.2.3
[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 char padding[RB_CHANNEL_PADDING];
74 /*
75 * Associated backend contains a variable-length array. Needs to
76 * be last member.
77 */
78 struct channel_backend backend; /* Associated backend */
79 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
80
81 /* Per-subbuffer commit counters used on the hot path */
82 #define RB_COMMIT_COUNT_HOT_PADDING 16
83 struct commit_counters_hot {
84 union v_atomic cc; /* Commit counter */
85 union v_atomic seq; /* Consecutive commits */
86 char padding[RB_COMMIT_COUNT_HOT_PADDING];
87 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
88
89 /* Per-subbuffer commit counters used only on cold paths */
90 #define RB_COMMIT_COUNT_COLD_PADDING 24
91 struct commit_counters_cold {
92 union v_atomic cc_sb; /* Incremented _once_ at sb switch */
93 char padding[RB_COMMIT_COUNT_COLD_PADDING];
94 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
95
96 /* ring buffer state */
97 #define RB_RING_BUFFER_PADDING 64
98 struct lttng_ust_lib_ring_buffer {
99 /* First 32 bytes cache-hot cacheline */
100 union v_atomic offset; /* Current offset in the buffer */
101 DECLARE_SHMP(struct commit_counters_hot, commit_hot);
102 /* Commit count per sub-buffer */
103 long consumed; /*
104 * Current offset in the buffer
105 * standard atomic access (shared)
106 */
107 int record_disabled;
108 /* End of first 32 bytes cacheline */
109 union v_atomic last_tsc; /*
110 * Last timestamp written in the buffer.
111 */
112
113 struct lttng_ust_lib_ring_buffer_backend backend; /* Associated backend */
114
115 DECLARE_SHMP(struct commit_counters_cold, commit_cold);
116 /* Commit count per sub-buffer */
117 long active_readers; /*
118 * Active readers count
119 * standard atomic access (shared)
120 */
121 /* Dropped records */
122 union v_atomic records_lost_full; /* Buffer full */
123 union v_atomic records_lost_wrap; /* Nested wrap-around */
124 union v_atomic records_lost_big; /* Events too big */
125 union v_atomic records_count; /* Number of records written */
126 union v_atomic records_overrun; /* Number of overwritten records */
127 //wait_queue_head_t read_wait; /* reader buffer-level wait queue */
128 int finalized; /* buffer has been finalized */
129 unsigned long get_subbuf_consumed; /* Read-side consumed */
130 unsigned long prod_snapshot; /* Producer count snapshot */
131 unsigned long cons_snapshot; /* Consumer count snapshot */
132 unsigned int get_subbuf:1; /* Sub-buffer being held by reader */
133 /* shmp pointer to self */
134 DECLARE_SHMP(struct lttng_ust_lib_ring_buffer, self);
135 char padding[RB_RING_BUFFER_PADDING];
136 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
137
138 static inline
139 void *channel_get_private(struct channel *chan)
140 {
141 return ((char *) chan) + chan->priv_data_offset;
142 }
143
144 #ifndef __rb_same_type
145 #define __rb_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
146 #endif
147
148 /*
149 * Issue warnings and disable channels upon internal error.
150 * Can receive struct lttng_ust_lib_ring_buffer or struct lttng_ust_lib_ring_buffer_backend
151 * parameters.
152 */
153 #define CHAN_WARN_ON(c, cond) \
154 ({ \
155 struct channel *__chan; \
156 int _____ret = caa_unlikely(cond); \
157 if (_____ret) { \
158 if (__rb_same_type(*(c), struct channel_backend)) \
159 __chan = caa_container_of((void *) (c), \
160 struct channel, \
161 backend); \
162 else if (__rb_same_type(*(c), struct channel)) \
163 __chan = (void *) (c); \
164 else \
165 BUG_ON(1); \
166 uatomic_inc(&__chan->record_disabled); \
167 WARN_ON(1); \
168 } \
169 _____ret = _____ret; /* For clang "unused result". */ \
170 })
171
172 #endif /* _LTTNG_RING_BUFFER_FRONTEND_TYPES_H */
This page took 0.033957 seconds and 4 git commands to generate.