Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | #ifndef _LINUX_RING_BUFFER_BACKEND_H |
2 | #define _LINUX_RING_BUFFER_BACKEND_H | |
3 | ||
4 | /* | |
5 | * linux/ringbuffer/backend.h | |
6 | * | |
7 | * Copyright (C) 2008-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * | |
9 | * Ring buffer backend (API). | |
10 | * | |
11 | * Dual LGPL v2.1/GPL v2 license. | |
12 | * | |
13 | * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by | |
14 | * the reader in flight recorder mode. | |
15 | */ | |
16 | ||
14641deb MD |
17 | #include <unistd.h> |
18 | ||
852c2936 | 19 | /* Internal helpers */ |
4931a13e MD |
20 | #include "backend_internal.h" |
21 | #include "frontend_internal.h" | |
852c2936 MD |
22 | |
23 | /* Ring buffer backend API */ | |
24 | ||
25 | /* Ring buffer backend access (read/write) */ | |
26 | ||
4cfec15c | 27 | extern size_t lib_ring_buffer_read(struct lttng_ust_lib_ring_buffer_backend *bufb, |
1d498196 | 28 | size_t offset, void *dest, size_t len, |
38fae1d3 | 29 | struct lttng_ust_shm_handle *handle); |
852c2936 | 30 | |
4cfec15c | 31 | extern int lib_ring_buffer_read_cstr(struct lttng_ust_lib_ring_buffer_backend *bufb, |
1d498196 | 32 | size_t offset, void *dest, size_t len, |
38fae1d3 | 33 | struct lttng_ust_shm_handle *handle); |
852c2936 | 34 | |
852c2936 MD |
35 | /* |
36 | * Return the address where a given offset is located. | |
37 | * Should be used to get the current subbuffer header pointer. Given we know | |
38 | * it's never on a page boundary, it's safe to write directly to this address, | |
39 | * as long as the write is never bigger than a page size. | |
40 | */ | |
41 | extern void * | |
4cfec15c | 42 | lib_ring_buffer_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb, |
1d498196 | 43 | size_t offset, |
38fae1d3 | 44 | struct lttng_ust_shm_handle *handle); |
852c2936 | 45 | extern void * |
4cfec15c | 46 | lib_ring_buffer_read_offset_address(struct lttng_ust_lib_ring_buffer_backend *bufb, |
1d498196 | 47 | size_t offset, |
38fae1d3 | 48 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
49 | |
50 | /** | |
51 | * lib_ring_buffer_write - write data to a buffer backend | |
52 | * @config : ring buffer instance configuration | |
53 | * @ctx: ring buffer context. (input arguments only) | |
54 | * @src : source pointer to copy from | |
55 | * @len : length of data to copy | |
56 | * | |
57 | * This function copies "len" bytes of data from a source pointer to a buffer | |
58 | * backend, at the current context offset. This is more or less a buffer | |
59 | * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write) | |
60 | * if copy is crossing a page boundary. | |
61 | */ | |
62 | static inline | |
4cfec15c MD |
63 | void lib_ring_buffer_write(const struct lttng_ust_lib_ring_buffer_config *config, |
64 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
852c2936 MD |
65 | const void *src, size_t len) |
66 | { | |
4cfec15c | 67 | struct lttng_ust_lib_ring_buffer_backend *bufb = &ctx->buf->backend; |
852c2936 | 68 | struct channel_backend *chanb = &ctx->chan->backend; |
38fae1d3 | 69 | struct lttng_ust_shm_handle *handle = ctx->handle; |
a6352fd4 | 70 | size_t sbidx; |
852c2936 | 71 | size_t offset = ctx->buf_offset; |
4cfec15c | 72 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; |
852c2936 MD |
73 | unsigned long sb_bindex, id; |
74 | ||
75 | offset &= chanb->buf_size - 1; | |
76 | sbidx = offset >> chanb->subbuf_size_order; | |
4746ae29 | 77 | id = shmp_index(handle, bufb->buf_wsb, sbidx)->id; |
852c2936 | 78 | sb_bindex = subbuffer_id_get_index(config, id); |
4746ae29 | 79 | rpages = shmp_index(handle, bufb->array, sb_bindex); |
852c2936 MD |
80 | CHAN_WARN_ON(ctx->chan, |
81 | config->mode == RING_BUFFER_OVERWRITE | |
82 | && subbuffer_id_is_noref(config, id)); | |
a6352fd4 MD |
83 | /* |
84 | * Underlying layer should never ask for writes across | |
85 | * subbuffers. | |
86 | */ | |
87 | CHAN_WARN_ON(chanb, offset >= chanb->buf_size); | |
88 | lib_ring_buffer_do_copy(config, | |
aead2025 | 89 | shmp_index(handle, shmp(handle, rpages->shmp)->p, offset & (chanb->subbuf_size - 1)), |
a6352fd4 | 90 | src, len); |
852c2936 MD |
91 | ctx->buf_offset += len; |
92 | } | |
93 | ||
94 | /* | |
95 | * This accessor counts the number of unread records in a buffer. | |
96 | * It only provides a consistent value if no reads not writes are performed | |
97 | * concurrently. | |
98 | */ | |
99 | static inline | |
100 | unsigned long lib_ring_buffer_get_records_unread( | |
4cfec15c MD |
101 | const struct lttng_ust_lib_ring_buffer_config *config, |
102 | struct lttng_ust_lib_ring_buffer *buf, | |
38fae1d3 | 103 | struct lttng_ust_shm_handle *handle) |
852c2936 | 104 | { |
4cfec15c MD |
105 | struct lttng_ust_lib_ring_buffer_backend *bufb = &buf->backend; |
106 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages; | |
852c2936 MD |
107 | unsigned long records_unread = 0, sb_bindex, id; |
108 | unsigned int i; | |
109 | ||
1d498196 | 110 | for (i = 0; i < shmp(handle, bufb->chan)->backend.num_subbuf; i++) { |
4746ae29 | 111 | id = shmp_index(handle, bufb->buf_wsb, i)->id; |
852c2936 | 112 | sb_bindex = subbuffer_id_get_index(config, id); |
4746ae29 | 113 | pages = shmp_index(handle, bufb->array, sb_bindex); |
1d498196 | 114 | records_unread += v_read(config, &shmp(handle, pages->shmp)->records_unread); |
852c2936 MD |
115 | } |
116 | if (config->mode == RING_BUFFER_OVERWRITE) { | |
117 | id = bufb->buf_rsb.id; | |
118 | sb_bindex = subbuffer_id_get_index(config, id); | |
4746ae29 | 119 | pages = shmp_index(handle, bufb->array, sb_bindex); |
1d498196 | 120 | records_unread += v_read(config, &shmp(handle, pages->shmp)->records_unread); |
852c2936 MD |
121 | } |
122 | return records_unread; | |
123 | } | |
124 | ||
852c2936 | 125 | #endif /* _LINUX_RING_BUFFER_BACKEND_H */ |