Commit | Line | Data |
---|---|---|
9f36eaed MJ |
1 | /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1) |
2 | * | |
886d51a3 | 3 | * lib/ringbuffer/frontend_types.h |
f3bc08c5 MD |
4 | * |
5 | * Ring Buffer Library Synchronization Header (types). | |
6 | * | |
886d51a3 MD |
7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
8 | * | |
f3bc08c5 | 9 | * See ring_buffer_frontend.c for more information on wait-free algorithms. |
f3bc08c5 MD |
10 | */ |
11 | ||
9f36eaed MJ |
12 | #ifndef _LIB_RING_BUFFER_FRONTEND_TYPES_H |
13 | #define _LIB_RING_BUFFER_FRONTEND_TYPES_H | |
14 | ||
f40270ad | 15 | #include <linux/kref.h> |
5671a661 MD |
16 | #include <wrapper/ringbuffer/config.h> |
17 | #include <wrapper/ringbuffer/backend_types.h> | |
18 | #include <wrapper/spinlock.h> | |
19 | #include <lib/prio_heap/lttng_prio_heap.h> /* For per-CPU read-side iterator */ | |
1e367326 | 20 | #include <lttng-cpuhotplug.h> |
f3bc08c5 MD |
21 | |
22 | /* | |
23 | * A switch is done during tracing or as a final flush after tracing (so it | |
24 | * won't write in the new sub-buffer). | |
25 | */ | |
26 | enum switch_mode { SWITCH_ACTIVE, SWITCH_FLUSH }; | |
27 | ||
28 | /* channel-level read-side iterator */ | |
29 | struct channel_iter { | |
30 | /* Prio heap of buffers. Lowest timestamps at the top. */ | |
a88db018 | 31 | struct lttng_ptr_heap heap; /* Heap of struct lib_ring_buffer ptrs */ |
f3bc08c5 MD |
32 | struct list_head empty_head; /* Empty buffers linked-list head */ |
33 | int read_open; /* Opened for reading ? */ | |
34 | u64 last_qs; /* Last quiescent state timestamp */ | |
35 | u64 last_timestamp; /* Last timestamp (for WARN_ON) */ | |
36 | int last_cpu; /* Last timestamp cpu */ | |
37 | /* | |
38 | * read() file operation state. | |
39 | */ | |
40 | unsigned long len_left; | |
41 | }; | |
42 | ||
43 | /* channel: collection of per-cpu ring buffers. */ | |
44 | struct channel { | |
45 | atomic_t record_disabled; | |
46 | unsigned long commit_count_mask; /* | |
47 | * Commit count mask, removing | |
48 | * the MSBs corresponding to | |
49 | * bits used to represent the | |
50 | * subbuffer index. | |
51 | */ | |
52 | ||
53 | struct channel_backend backend; /* Associated backend */ | |
54 | ||
55 | unsigned long switch_timer_interval; /* Buffer flush (jiffies) */ | |
56 | unsigned long read_timer_interval; /* Reader wakeup (jiffies) */ | |
1e367326 MD |
57 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) |
58 | struct lttng_cpuhp_node cpuhp_prepare; | |
59 | struct lttng_cpuhp_node cpuhp_online; | |
60 | struct lttng_cpuhp_node cpuhp_iter_online; | |
61 | #else | |
f3bc08c5 | 62 | struct notifier_block cpu_hp_notifier; /* CPU hotplug notifier */ |
f3bc08c5 | 63 | struct notifier_block hp_iter_notifier; /* hotplug iterator notifier */ |
9cccf98a MD |
64 | unsigned int cpu_hp_enable:1; /* Enable CPU hotplug notif. */ |
65 | unsigned int hp_iter_enable:1; /* Enable hp iter notif. */ | |
1e367326 MD |
66 | #endif |
67 | struct notifier_block tick_nohz_notifier; /* CPU nohz notifier */ | |
f3bc08c5 | 68 | wait_queue_head_t read_wait; /* reader wait queue */ |
24cedcfe MD |
69 | wait_queue_head_t hp_wait; /* CPU hotplug wait queue */ |
70 | int finalized; /* Has channel been finalized */ | |
f3bc08c5 | 71 | struct channel_iter iter; /* Channel read-side iterator */ |
f40270ad | 72 | struct kref ref; /* Reference count */ |
f3bc08c5 MD |
73 | }; |
74 | ||
75 | /* Per-subbuffer commit counters used on the hot path */ | |
76 | struct commit_counters_hot { | |
77 | union v_atomic cc; /* Commit counter */ | |
78 | union v_atomic seq; /* Consecutive commits */ | |
79 | }; | |
80 | ||
81 | /* Per-subbuffer commit counters used only on cold paths */ | |
82 | struct commit_counters_cold { | |
83 | union v_atomic cc_sb; /* Incremented _once_ at sb switch */ | |
84 | }; | |
85 | ||
86 | /* Per-buffer read iterator */ | |
87 | struct lib_ring_buffer_iter { | |
88 | u64 timestamp; /* Current record timestamp */ | |
89 | size_t header_len; /* Current record header length */ | |
90 | size_t payload_len; /* Current record payload length */ | |
91 | ||
92 | struct list_head empty_node; /* Linked list of empty buffers */ | |
93 | unsigned long consumed, read_offset, data_size; | |
94 | enum { | |
95 | ITER_GET_SUBBUF = 0, | |
96 | ITER_TEST_RECORD, | |
97 | ITER_NEXT_RECORD, | |
98 | ITER_PUT_SUBBUF, | |
99 | } state; | |
9cccf98a MD |
100 | unsigned int allocated:1; |
101 | unsigned int read_open:1; /* Opened for reading ? */ | |
f3bc08c5 MD |
102 | }; |
103 | ||
104 | /* ring buffer state */ | |
105 | struct lib_ring_buffer { | |
106 | /* First 32 bytes cache-hot cacheline */ | |
107 | union v_atomic offset; /* Current offset in the buffer */ | |
108 | struct commit_counters_hot *commit_hot; | |
109 | /* Commit count per sub-buffer */ | |
110 | atomic_long_t consumed; /* | |
111 | * Current offset in the buffer | |
112 | * standard atomic access (shared) | |
113 | */ | |
114 | atomic_t record_disabled; | |
115 | /* End of first 32 bytes cacheline */ | |
116 | union v_atomic last_tsc; /* | |
117 | * Last timestamp written in the buffer. | |
118 | */ | |
119 | ||
120 | struct lib_ring_buffer_backend backend; /* Associated backend */ | |
121 | ||
122 | struct commit_counters_cold *commit_cold; | |
123 | /* Commit count per sub-buffer */ | |
124 | atomic_long_t active_readers; /* | |
125 | * Active readers count | |
126 | * standard atomic access (shared) | |
127 | */ | |
128 | /* Dropped records */ | |
129 | union v_atomic records_lost_full; /* Buffer full */ | |
130 | union v_atomic records_lost_wrap; /* Nested wrap-around */ | |
131 | union v_atomic records_lost_big; /* Events too big */ | |
132 | union v_atomic records_count; /* Number of records written */ | |
133 | union v_atomic records_overrun; /* Number of overwritten records */ | |
134 | wait_queue_head_t read_wait; /* reader buffer-level wait queue */ | |
71c1d843 | 135 | wait_queue_head_t write_wait; /* writer buffer-level wait queue (for metadata only) */ |
f3bc08c5 MD |
136 | int finalized; /* buffer has been finalized */ |
137 | struct timer_list switch_timer; /* timer for periodical switch */ | |
138 | struct timer_list read_timer; /* timer for read poll */ | |
139 | raw_spinlock_t raw_tick_nohz_spinlock; /* nohz entry lock/trylock */ | |
140 | struct lib_ring_buffer_iter iter; /* read-side iterator */ | |
141 | unsigned long get_subbuf_consumed; /* Read-side consumed */ | |
142 | unsigned long prod_snapshot; /* Producer count snapshot */ | |
143 | unsigned long cons_snapshot; /* Consumer count snapshot */ | |
9cccf98a MD |
144 | unsigned int get_subbuf:1, /* Sub-buffer being held by reader */ |
145 | switch_timer_enabled:1, /* Protected by ring_buffer_nohz_lock */ | |
64af2437 MD |
146 | read_timer_enabled:1, /* Protected by ring_buffer_nohz_lock */ |
147 | quiescent:1; | |
f3bc08c5 MD |
148 | }; |
149 | ||
9115fbdc MD |
150 | static inline |
151 | void *channel_get_private(struct channel *chan) | |
152 | { | |
153 | return chan->backend.priv; | |
154 | } | |
155 | ||
d7e74017 MD |
156 | void lib_ring_buffer_lost_event_too_big(struct channel *chan); |
157 | ||
f3bc08c5 MD |
158 | /* |
159 | * Issue warnings and disable channels upon internal error. | |
160 | * Can receive struct lib_ring_buffer or struct lib_ring_buffer_backend | |
161 | * parameters. | |
162 | */ | |
163 | #define CHAN_WARN_ON(c, cond) \ | |
164 | ({ \ | |
165 | struct channel *__chan; \ | |
166 | int _____ret = unlikely(cond); \ | |
167 | if (_____ret) { \ | |
168 | if (__same_type(*(c), struct channel_backend)) \ | |
169 | __chan = container_of((void *) (c), \ | |
170 | struct channel, \ | |
171 | backend); \ | |
172 | else if (__same_type(*(c), struct channel)) \ | |
173 | __chan = (void *) (c); \ | |
174 | else \ | |
175 | BUG_ON(1); \ | |
176 | atomic_inc(&__chan->record_disabled); \ | |
177 | WARN_ON(1); \ | |
178 | } \ | |
179 | _____ret; \ | |
180 | }) | |
181 | ||
886d51a3 | 182 | #endif /* _LIB_RING_BUFFER_FRONTEND_TYPES_H */ |