1 #ifndef _LINUX_RING_BUFFER_BACKEND_H
2 #define _LINUX_RING_BUFFER_BACKEND_H
5 * linux/ringbuffer/backend.h
7 * Copyright (C) 2008-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Ring buffer backend (API).
11 * Dual LGPL v2.1/GPL v2 license.
13 * Credits to Steven Rostedt for proposing to use an extra-subbuffer owned by
14 * the reader in flight recorder mode.
17 #include <linux/types.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/wait.h>
21 #include <linux/poll.h>
22 #include <linux/list.h>
26 /* Internal helpers */
27 #include "../../wrapper/ringbuffer/backend_internal.h"
28 #include "../../wrapper/ringbuffer/frontend_internal.h"
30 /* Ring buffer backend API */
32 /* Ring buffer backend access (read/write) */
34 extern size_t lib_ring_buffer_read(struct lib_ring_buffer_backend
*bufb
,
35 size_t offset
, void *dest
, size_t len
);
37 extern int __lib_ring_buffer_copy_to_user(struct lib_ring_buffer_backend
*bufb
,
38 size_t offset
, void __user
*dest
,
41 extern int lib_ring_buffer_read_cstr(struct lib_ring_buffer_backend
*bufb
,
42 size_t offset
, void *dest
, size_t len
);
45 lib_ring_buffer_read_get_page(struct lib_ring_buffer_backend
*bufb
, size_t offset
,
49 * Return the address where a given offset is located.
50 * Should be used to get the current subbuffer header pointer. Given we know
51 * it's never on a page boundary, it's safe to write directly to this address,
52 * as long as the write is never bigger than a page size.
55 lib_ring_buffer_offset_address(struct lib_ring_buffer_backend
*bufb
,
58 lib_ring_buffer_read_offset_address(struct lib_ring_buffer_backend
*bufb
,
62 * lib_ring_buffer_write - write data to a buffer backend
63 * @config : ring buffer instance configuration
64 * @ctx: ring buffer context. (input arguments only)
65 * @src : source pointer to copy from
66 * @len : length of data to copy
68 * This function copies "len" bytes of data from a source pointer to a buffer
69 * backend, at the current context offset. This is more or less a buffer
70 * backend-specific memcpy() operation. Calls the slow path (_ring_buffer_write)
71 * if copy is crossing a page boundary.
74 void lib_ring_buffer_write(const struct lib_ring_buffer_config
*config
,
75 struct lib_ring_buffer_ctx
*ctx
,
76 const void *src
, size_t len
)
78 struct lib_ring_buffer_backend
*bufb
= &ctx
->buf
->backend
;
79 struct channel_backend
*chanb
= &ctx
->chan
->backend
;
81 size_t offset
= ctx
->buf_offset
;
83 struct lib_ring_buffer_backend_pages
*rpages
;
84 unsigned long sb_bindex
, id
;
86 offset
&= chanb
->buf_size
- 1;
87 sbidx
= offset
>> chanb
->subbuf_size_order
;
88 index
= (offset
& (chanb
->subbuf_size
- 1)) >> PAGE_SHIFT
;
89 pagecpy
= min_t(size_t, len
, (-offset
) & ~PAGE_MASK
);
90 id
= bufb
->buf_wsb
[sbidx
].id
;
91 sb_bindex
= subbuffer_id_get_index(config
, id
);
92 rpages
= bufb
->array
[sb_bindex
];
93 CHAN_WARN_ON(ctx
->chan
,
94 config
->mode
== RING_BUFFER_OVERWRITE
95 && subbuffer_id_is_noref(config
, id
));
96 if (likely(pagecpy
== len
))
97 lib_ring_buffer_do_copy(config
,
99 + (offset
& ~PAGE_MASK
),
102 _lib_ring_buffer_write(bufb
, offset
, src
, len
, 0);
103 ctx
->buf_offset
+= len
;
107 * This accessor counts the number of unread records in a buffer.
108 * It only provides a consistent value if no reads not writes are performed
112 unsigned long lib_ring_buffer_get_records_unread(
113 const struct lib_ring_buffer_config
*config
,
114 struct lib_ring_buffer
*buf
)
116 struct lib_ring_buffer_backend
*bufb
= &buf
->backend
;
117 struct lib_ring_buffer_backend_pages
*pages
;
118 unsigned long records_unread
= 0, sb_bindex
, id
;
121 for (i
= 0; i
< bufb
->chan
->backend
.num_subbuf
; i
++) {
122 id
= bufb
->buf_wsb
[i
].id
;
123 sb_bindex
= subbuffer_id_get_index(config
, id
);
124 pages
= bufb
->array
[sb_bindex
];
125 records_unread
+= v_read(config
, &pages
->records_unread
);
127 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
128 id
= bufb
->buf_rsb
.id
;
129 sb_bindex
= subbuffer_id_get_index(config
, id
);
130 pages
= bufb
->array
[sb_bindex
];
131 records_unread
+= v_read(config
, &pages
->records_unread
);
133 return records_unread
;
136 ssize_t
lib_ring_buffer_file_splice_read(struct file
*in
, loff_t
*ppos
,
137 struct pipe_inode_info
*pipe
,
138 size_t len
, unsigned int flags
);
139 loff_t
lib_ring_buffer_no_llseek(struct file
*file
, loff_t offset
, int origin
);
141 #endif /* _LINUX_RING_BUFFER_BACKEND_H */