1 #ifndef _LINUX_RING_BUFFER_FRONTEND_H
2 #define _LINUX_RING_BUFFER_FRONTEND_H
5 * linux/ringbuffer/frontend.h
7 * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Ring Buffer Library Synchronization Header (API).
12 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 * See ring_buffer_frontend.c for more information on wait-free algorithms.
16 * Dual LGPL v2.1/GPL v2 license.
19 #include <urcu/compiler.h>
20 #include <urcu/uatomic.h>
23 /* Internal helpers */
24 #include "frontend_internal.h"
26 /* Buffer creation/removal and setup operations */
29 * switch_timer_interval is the time interval (in us) to fill sub-buffers with
30 * padding to let readers get those sub-buffers. Used for live streaming.
32 * read_timer_interval is the time interval (in us) to wake up pending readers.
34 * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
35 * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
36 * be set to NULL for other backends.
40 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
41 const char *name
, void *priv
,
43 size_t subbuf_size
, size_t num_subbuf
,
44 unsigned int switch_timer_interval
,
45 unsigned int read_timer_interval
,
46 int *shm_fd
, int *wait_fd
,
47 uint64_t *memory_map_size
);
49 /* channel_handle_create - for consumer. */
51 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
52 uint64_t memory_map_size
);
54 /* channel_handle_add_stream - for consumer. */
56 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
57 int shm_fd
, int wait_fd
, uint64_t memory_map_size
);
60 * channel_destroy returns the private data pointer. It finalizes all channel's
61 * buffers, waits for readers to release all references, and destroys the
65 void *channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
69 /* Buffer read operations */
72 * Iteration on channel cpumask needs to issue a read barrier to match the write
73 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
74 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
75 * only performed at channel destruction.
77 #define for_each_channel_cpu(cpu, chan) \
78 for_each_possible_cpu(cpu)
80 extern struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
81 const struct lttng_ust_lib_ring_buffer_config
*config
,
82 struct channel
*chan
, int cpu
,
83 struct lttng_ust_shm_handle
*handle
,
84 int *shm_fd
, int *wait_fd
,
85 uint64_t *memory_map_size
);
86 extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
87 struct lttng_ust_shm_handle
*handle
,
89 extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
90 struct lttng_ust_shm_handle
*handle
,
94 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
96 extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
97 unsigned long *consumed
,
98 unsigned long *produced
,
99 struct lttng_ust_shm_handle
*handle
);
100 extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
101 unsigned long consumed_new
,
102 struct lttng_ust_shm_handle
*handle
);
104 extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
105 unsigned long consumed
,
106 struct lttng_ust_shm_handle
*handle
);
107 extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
108 struct lttng_ust_shm_handle
*handle
);
111 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
112 * to read sub-buffers sequentially.
114 static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
115 struct lttng_ust_shm_handle
*handle
)
119 ret
= lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
120 &buf
->prod_snapshot
, handle
);
123 ret
= lib_ring_buffer_get_subbuf(buf
, buf
->cons_snapshot
, handle
);
128 void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
129 struct lttng_ust_shm_handle
*handle
)
131 lib_ring_buffer_put_subbuf(buf
, handle
);
132 lib_ring_buffer_move_consumer(buf
, subbuf_align(buf
->cons_snapshot
,
133 shmp(handle
, buf
->backend
.chan
)), handle
);
136 extern void channel_reset(struct channel
*chan
);
137 extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
138 struct lttng_ust_shm_handle
*handle
);
141 unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
142 struct lttng_ust_lib_ring_buffer
*buf
)
144 return v_read(config
, &buf
->offset
);
148 unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config
*config
,
149 struct lttng_ust_lib_ring_buffer
*buf
)
151 return uatomic_read(&buf
->consumed
);
155 * Must call lib_ring_buffer_is_finalized before reading counters (memory
156 * ordering enforced with respect to trace teardown).
159 int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config
*config
,
160 struct lttng_ust_lib_ring_buffer
*buf
)
162 int finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
164 * Read finalized before counters.
171 int lib_ring_buffer_channel_is_finalized(const struct channel
*chan
)
173 return chan
->finalized
;
177 int lib_ring_buffer_channel_is_disabled(const struct channel
*chan
)
179 return uatomic_read(&chan
->record_disabled
);
183 unsigned long lib_ring_buffer_get_read_data_size(
184 const struct lttng_ust_lib_ring_buffer_config
*config
,
185 struct lttng_ust_lib_ring_buffer
*buf
,
186 struct lttng_ust_shm_handle
*handle
)
188 return subbuffer_get_read_data_size(config
, &buf
->backend
, handle
);
192 unsigned long lib_ring_buffer_get_records_count(
193 const struct lttng_ust_lib_ring_buffer_config
*config
,
194 struct lttng_ust_lib_ring_buffer
*buf
)
196 return v_read(config
, &buf
->records_count
);
200 unsigned long lib_ring_buffer_get_records_overrun(
201 const struct lttng_ust_lib_ring_buffer_config
*config
,
202 struct lttng_ust_lib_ring_buffer
*buf
)
204 return v_read(config
, &buf
->records_overrun
);
208 unsigned long lib_ring_buffer_get_records_lost_full(
209 const struct lttng_ust_lib_ring_buffer_config
*config
,
210 struct lttng_ust_lib_ring_buffer
*buf
)
212 return v_read(config
, &buf
->records_lost_full
);
216 unsigned long lib_ring_buffer_get_records_lost_wrap(
217 const struct lttng_ust_lib_ring_buffer_config
*config
,
218 struct lttng_ust_lib_ring_buffer
*buf
)
220 return v_read(config
, &buf
->records_lost_wrap
);
224 unsigned long lib_ring_buffer_get_records_lost_big(
225 const struct lttng_ust_lib_ring_buffer_config
*config
,
226 struct lttng_ust_lib_ring_buffer
*buf
)
228 return v_read(config
, &buf
->records_lost_big
);
232 unsigned long lib_ring_buffer_get_records_read(
233 const struct lttng_ust_lib_ring_buffer_config
*config
,
234 struct lttng_ust_lib_ring_buffer
*buf
)
236 return v_read(config
, &buf
->backend
.records_read
);
239 #endif /* _LINUX_RING_BUFFER_FRONTEND_H */