| 1 | #ifndef _LINUX_RING_BUFFER_FRONTEND_H |
| 2 | #define _LINUX_RING_BUFFER_FRONTEND_H |
| 3 | |
| 4 | /* |
| 5 | * linux/ringbuffer/frontend.h |
| 6 | * |
| 7 | * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * Ring Buffer Library Synchronization Header (API). |
| 10 | * |
| 11 | * Author: |
| 12 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 13 | * |
| 14 | * See ring_buffer_frontend.c for more information on wait-free algorithms. |
| 15 | * |
| 16 | * Dual LGPL v2.1/GPL v2 license. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/pipe_fs_i.h> |
| 20 | #include <linux/rcupdate.h> |
| 21 | #include <linux/cpumask.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/bitops.h> |
| 24 | #include <linux/splice.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/timer.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/cache.h> |
| 29 | #include <linux/time.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/stat.h> |
| 33 | #include <linux/cpu.h> |
| 34 | #include <linux/fs.h> |
| 35 | |
| 36 | #include <asm/atomic.h> |
| 37 | #include <asm/local.h> |
| 38 | |
| 39 | /* Internal helpers */ |
| 40 | #include "../../wrapper/ringbuffer/frontend_internal.h" |
| 41 | |
| 42 | /* Buffer creation/removal and setup operations */ |
| 43 | |
| 44 | /* |
| 45 | * switch_timer_interval is the time interval (in us) to fill sub-buffers with |
| 46 | * padding to let readers get those sub-buffers. Used for live streaming. |
| 47 | * |
| 48 | * read_timer_interval is the time interval (in us) to wake up pending readers. |
| 49 | * |
| 50 | * buf_addr is a pointer the the beginning of the preallocated buffer contiguous |
| 51 | * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can |
| 52 | * be set to NULL for other backends. |
| 53 | */ |
| 54 | |
| 55 | extern |
| 56 | struct channel *channel_create(const struct lib_ring_buffer_config *config, |
| 57 | const char *name, void *priv, |
| 58 | void *buf_addr, |
| 59 | size_t subbuf_size, size_t num_subbuf, |
| 60 | unsigned int switch_timer_interval, |
| 61 | unsigned int read_timer_interval); |
| 62 | |
| 63 | /* |
| 64 | * channel_destroy returns the private data pointer. It finalizes all channel's |
| 65 | * buffers, waits for readers to release all references, and destroys the |
| 66 | * channel. |
| 67 | */ |
| 68 | extern |
| 69 | void *channel_destroy(struct channel *chan); |
| 70 | |
| 71 | |
| 72 | /* Buffer read operations */ |
| 73 | |
| 74 | /* |
| 75 | * Iteration on channel cpumask needs to issue a read barrier to match the write |
| 76 | * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu |
| 77 | * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is |
| 78 | * only performed at channel destruction. |
| 79 | */ |
| 80 | #define for_each_channel_cpu(cpu, chan) \ |
| 81 | for ((cpu) = -1; \ |
| 82 | ({ (cpu) = cpumask_next(cpu, (chan)->backend.cpumask); \ |
| 83 | smp_read_barrier_depends(); (cpu) < nr_cpu_ids; });) |
| 84 | |
| 85 | extern struct lib_ring_buffer *channel_get_ring_buffer( |
| 86 | const struct lib_ring_buffer_config *config, |
| 87 | struct channel *chan, int cpu); |
| 88 | extern int lib_ring_buffer_open_read(struct lib_ring_buffer *buf); |
| 89 | extern void lib_ring_buffer_release_read(struct lib_ring_buffer *buf); |
| 90 | |
| 91 | /* |
| 92 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. |
| 93 | */ |
| 94 | extern int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf, |
| 95 | unsigned long *consumed, |
| 96 | unsigned long *produced); |
| 97 | extern void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf, |
| 98 | unsigned long consumed_new); |
| 99 | |
| 100 | extern int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf, |
| 101 | unsigned long consumed); |
| 102 | extern void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf); |
| 103 | |
| 104 | /* |
| 105 | * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers |
| 106 | * to read sub-buffers sequentially. |
| 107 | */ |
| 108 | static inline int lib_ring_buffer_get_next_subbuf(struct lib_ring_buffer *buf) |
| 109 | { |
| 110 | int ret; |
| 111 | |
| 112 | ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, |
| 113 | &buf->prod_snapshot); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static inline void lib_ring_buffer_put_next_subbuf(struct lib_ring_buffer *buf) |
| 121 | { |
| 122 | lib_ring_buffer_put_subbuf(buf); |
| 123 | lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, |
| 124 | buf->backend.chan)); |
| 125 | } |
| 126 | |
| 127 | extern void channel_reset(struct channel *chan); |
| 128 | extern void lib_ring_buffer_reset(struct lib_ring_buffer *buf); |
| 129 | |
| 130 | static inline |
| 131 | unsigned long lib_ring_buffer_get_offset(const struct lib_ring_buffer_config *config, |
| 132 | struct lib_ring_buffer *buf) |
| 133 | { |
| 134 | return v_read(config, &buf->offset); |
| 135 | } |
| 136 | |
| 137 | static inline |
| 138 | unsigned long lib_ring_buffer_get_consumed(const struct lib_ring_buffer_config *config, |
| 139 | struct lib_ring_buffer *buf) |
| 140 | { |
| 141 | return atomic_long_read(&buf->consumed); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Must call lib_ring_buffer_is_finalized before reading counters (memory |
| 146 | * ordering enforced with respect to trace teardown). |
| 147 | */ |
| 148 | static inline |
| 149 | int lib_ring_buffer_is_finalized(const struct lib_ring_buffer_config *config, |
| 150 | struct lib_ring_buffer *buf) |
| 151 | { |
| 152 | int finalized = ACCESS_ONCE(buf->finalized); |
| 153 | /* |
| 154 | * Read finalized before counters. |
| 155 | */ |
| 156 | smp_rmb(); |
| 157 | return finalized; |
| 158 | } |
| 159 | |
| 160 | static inline |
| 161 | int lib_ring_buffer_channel_is_finalized(const struct channel *chan) |
| 162 | { |
| 163 | return chan->finalized; |
| 164 | } |
| 165 | |
| 166 | static inline |
| 167 | int lib_ring_buffer_channel_is_disabled(const struct channel *chan) |
| 168 | { |
| 169 | return atomic_read(&chan->record_disabled); |
| 170 | } |
| 171 | |
| 172 | static inline |
| 173 | unsigned long lib_ring_buffer_get_read_data_size( |
| 174 | const struct lib_ring_buffer_config *config, |
| 175 | struct lib_ring_buffer *buf) |
| 176 | { |
| 177 | return subbuffer_get_read_data_size(config, &buf->backend); |
| 178 | } |
| 179 | |
| 180 | static inline |
| 181 | unsigned long lib_ring_buffer_get_records_count( |
| 182 | const struct lib_ring_buffer_config *config, |
| 183 | struct lib_ring_buffer *buf) |
| 184 | { |
| 185 | return v_read(config, &buf->records_count); |
| 186 | } |
| 187 | |
| 188 | static inline |
| 189 | unsigned long lib_ring_buffer_get_records_overrun( |
| 190 | const struct lib_ring_buffer_config *config, |
| 191 | struct lib_ring_buffer *buf) |
| 192 | { |
| 193 | return v_read(config, &buf->records_overrun); |
| 194 | } |
| 195 | |
| 196 | static inline |
| 197 | unsigned long lib_ring_buffer_get_records_lost_full( |
| 198 | const struct lib_ring_buffer_config *config, |
| 199 | struct lib_ring_buffer *buf) |
| 200 | { |
| 201 | return v_read(config, &buf->records_lost_full); |
| 202 | } |
| 203 | |
| 204 | static inline |
| 205 | unsigned long lib_ring_buffer_get_records_lost_wrap( |
| 206 | const struct lib_ring_buffer_config *config, |
| 207 | struct lib_ring_buffer *buf) |
| 208 | { |
| 209 | return v_read(config, &buf->records_lost_wrap); |
| 210 | } |
| 211 | |
| 212 | static inline |
| 213 | unsigned long lib_ring_buffer_get_records_lost_big( |
| 214 | const struct lib_ring_buffer_config *config, |
| 215 | struct lib_ring_buffer *buf) |
| 216 | { |
| 217 | return v_read(config, &buf->records_lost_big); |
| 218 | } |
| 219 | |
| 220 | static inline |
| 221 | unsigned long lib_ring_buffer_get_records_read( |
| 222 | const struct lib_ring_buffer_config *config, |
| 223 | struct lib_ring_buffer *buf) |
| 224 | { |
| 225 | return v_read(config, &buf->backend.records_read); |
| 226 | } |
| 227 | |
| 228 | #endif /* _LINUX_RING_BUFFER_FRONTEND_H */ |