Update stream packet and event headers
[lttng-modules.git] / lib / ringbuffer / backend.h
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
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>
23 #include <linux/fs.h>
24 #include <linux/mm.h>
25
26 /* Internal helpers */
27 #include "../../wrapper/ringbuffer/backend_internal.h"
28 #include "../../wrapper/ringbuffer/frontend_internal.h"
29
30 /* Ring buffer backend API */
31
32 /* Ring buffer backend access (read/write) */
33
34 extern size_t lib_ring_buffer_read(struct lib_ring_buffer_backend *bufb,
35 size_t offset, void *dest, size_t len);
36
37 extern int __lib_ring_buffer_copy_to_user(struct lib_ring_buffer_backend *bufb,
38 size_t offset, void __user *dest,
39 size_t len);
40
41 extern int lib_ring_buffer_read_cstr(struct lib_ring_buffer_backend *bufb,
42 size_t offset, void *dest, size_t len);
43
44 extern struct page **
45 lib_ring_buffer_read_get_page(struct lib_ring_buffer_backend *bufb, size_t offset,
46 void ***virt);
47
48 /*
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.
53 */
54 extern void *
55 lib_ring_buffer_offset_address(struct lib_ring_buffer_backend *bufb,
56 size_t offset);
57 extern void *
58 lib_ring_buffer_read_offset_address(struct lib_ring_buffer_backend *bufb,
59 size_t offset);
60
61 /**
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
67 *
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.
72 */
73 static inline
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)
77 {
78 struct lib_ring_buffer_backend *bufb = &ctx->buf->backend;
79 struct channel_backend *chanb = &ctx->chan->backend;
80 size_t sbidx, index;
81 size_t offset = ctx->buf_offset;
82 ssize_t pagecpy;
83 struct lib_ring_buffer_backend_pages *rpages;
84 unsigned long sb_bindex, id;
85
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,
98 rpages->p[index].virt
99 + (offset & ~PAGE_MASK),
100 src, len);
101 else
102 _lib_ring_buffer_write(bufb, offset, src, len, 0);
103 ctx->buf_offset += len;
104 }
105
106 /*
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
109 * concurrently.
110 */
111 static inline
112 unsigned long lib_ring_buffer_get_records_unread(
113 const struct lib_ring_buffer_config *config,
114 struct lib_ring_buffer *buf)
115 {
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;
119 unsigned int i;
120
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);
126 }
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);
132 }
133 return records_unread;
134 }
135
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);
140
141 #endif /* _LINUX_RING_BUFFER_BACKEND_H */
This page took 0.036305 seconds and 4 git commands to generate.