7d582a8802e13c70cced7ea3e1bfbbe93e56c005
[lttng-ust.git] / libust / relay.h
1 /*
2 * linux/include/linux/ltt-relay.h
3 *
4 * Copyright (C) 2002, 2003 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
5 * Copyright (C) 1999, 2000, 2001, 2002 - Karim Yaghmour (karim@opersys.com)
6 * Copyright (C) 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
7 *
8 * CONFIG_RELAY definitions and declarations
9 */
10
11 #ifndef _LINUX_LTT_RELAY_H
12 #define _LINUX_LTT_RELAY_H
13
14 //ust// #include <linux/types.h>
15 //ust// #include <linux/sched.h>
16 //ust// #include <linux/timer.h>
17 //ust// #include <linux/wait.h>
18 //ust// #include <linux/list.h>
19 //ust// #include <linux/fs.h>
20 //ust// #include <linux/poll.h>
21 //ust// #include <linux/kref.h>
22 //ust// #include <linux/mm.h>
23 //ust// #include <linux/ltt-core.h>
24 #include "kref.h"
25 //#include "list.h"
26 #include "channels.h"
27
28 /* Needs a _much_ better name... */
29 #define FIX_SIZE(x) ((((x) - 1) & PAGE_MASK) + PAGE_SIZE)
30
31 /*
32 * Tracks changes to rchan/rchan_buf structs
33 */
34 #define LTT_RELAY_CHANNEL_VERSION 8
35
36 struct rchan_buf;
37
38 struct buf_page {
39 struct page *page;
40 struct rchan_buf *buf; /* buffer the page belongs to */
41 size_t offset; /* page offset in the buffer */
42 struct list_head list; /* buffer linked list */
43 };
44
45 /*
46 * Per-cpu relay channel buffer
47 */
48 struct rchan_buf {
49 struct rchan *chan; /* associated channel */
50 //ust// wait_queue_head_t read_wait; /* reader wait queue */
51 //ust// struct timer_list timer; /* reader wake-up timer */
52 //ust// struct dentry *dentry; /* channel file dentry */
53 struct kref kref; /* channel buffer refcount */
54 //ust// struct list_head pages; /* list of buffer pages */
55 void *buf_data; //ust//
56 size_t buf_size;
57 //ust// struct buf_page *wpage; /* current write page (cache) */
58 //ust// struct buf_page *hpage[2]; /* current subbuf header page (cache) */
59 //ust// struct buf_page *rpage; /* current subbuf read page (cache) */
60 //ust// unsigned int page_count; /* number of current buffer pages */
61 unsigned int finalized; /* buffer has been finalized */
62 //ust// unsigned int cpu; /* this buf's cpu */
63 int shmid; /* the shmid of the buffer data pages */
64 } ____cacheline_aligned;
65
66 /*
67 * Relay channel data structure
68 */
69 struct rchan {
70 u32 version; /* the version of this struct */
71 size_t subbuf_size; /* sub-buffer size */
72 size_t n_subbufs; /* number of sub-buffers per buffer */
73 size_t alloc_size; /* total buffer size allocated */
74 struct rchan_callbacks *cb; /* client callbacks */
75 struct kref kref; /* channel refcount */
76 void *private_data; /* for user-defined data */
77 //ust// struct rchan_buf *buf[NR_CPUS]; /* per-cpu channel buffers */
78 struct rchan_buf *buf;
79 struct list_head list; /* for channel list */
80 struct dentry *parent; /* parent dentry passed to open */
81 int subbuf_size_order; /* order of sub-buffer size */
82 //ust// char base_filename[NAME_MAX]; /* saved base filename */
83 };
84
85 /*
86 * Relay channel client callbacks
87 */
88 struct rchan_callbacks {
89 /*
90 * subbuf_start - called on buffer-switch to a new sub-buffer
91 * @buf: the channel buffer containing the new sub-buffer
92 * @subbuf: the start of the new sub-buffer
93 * @prev_subbuf: the start of the previous sub-buffer
94 * @prev_padding: unused space at the end of previous sub-buffer
95 *
96 * The client should return 1 to continue logging, 0 to stop
97 * logging.
98 *
99 * NOTE: subbuf_start will also be invoked when the buffer is
100 * created, so that the first sub-buffer can be initialized
101 * if necessary. In this case, prev_subbuf will be NULL.
102 *
103 * NOTE: the client can reserve bytes at the beginning of the new
104 * sub-buffer by calling subbuf_start_reserve() in this callback.
105 */
106 int (*subbuf_start) (struct rchan_buf *buf,
107 void *subbuf,
108 void *prev_subbuf,
109 size_t prev_padding);
110
111 /*
112 * create_buf_file - create file to represent a relay channel buffer
113 * @filename: the name of the file to create
114 * @parent: the parent of the file to create
115 * @mode: the mode of the file to create
116 * @buf: the channel buffer
117 *
118 * Called during relay_open(), once for each per-cpu buffer,
119 * to allow the client to create a file to be used to
120 * represent the corresponding channel buffer. If the file is
121 * created outside of relay, the parent must also exist in
122 * that filesystem.
123 *
124 * The callback should return the dentry of the file created
125 * to represent the relay buffer.
126 *
127 * Setting the is_global outparam to a non-zero value will
128 * cause relay_open() to create a single global buffer rather
129 * than the default set of per-cpu buffers.
130 *
131 * See Documentation/filesystems/relayfs.txt for more info.
132 */
133 struct dentry *(*create_buf_file)(const char *filename,
134 struct dentry *parent,
135 int mode,
136 struct rchan_buf *buf);
137
138 /*
139 * remove_buf_file - remove file representing a relay channel buffer
140 * @dentry: the dentry of the file to remove
141 *
142 * Called during relay_close(), once for each per-cpu buffer,
143 * to allow the client to remove a file used to represent a
144 * channel buffer.
145 *
146 * The callback should return 0 if successful, negative if not.
147 */
148 //ust// int (*remove_buf_file)(struct rchan_buf *buf);
149 };
150
151 extern struct buf_page *ltt_relay_find_prev_page(struct rchan_buf *buf,
152 struct buf_page *page, size_t offset, ssize_t diff_offset);
153
154 extern struct buf_page *ltt_relay_find_next_page(struct rchan_buf *buf,
155 struct buf_page *page, size_t offset, ssize_t diff_offset);
156
157 extern void _ltt_relay_write(struct rchan_buf *buf, size_t offset,
158 const void *src, size_t len, ssize_t cpy);
159
160 extern int ltt_relay_read(struct rchan_buf *buf, size_t offset,
161 void *dest, size_t len);
162
163 extern struct buf_page *ltt_relay_read_get_page(struct rchan_buf *buf,
164 size_t offset);
165
166 /*
167 * Return the address where a given offset is located.
168 * Should be used to get the current subbuffer header pointer. Given we know
169 * it's never on a page boundary, it's safe to write directly to this address,
170 * as long as the write is never bigger than a page size.
171 */
172 extern void *ltt_relay_offset_address(struct rchan_buf *buf,
173 size_t offset);
174
175 /*
176 * Find the page containing "offset". Cache it if it is after the currently
177 * cached page.
178 */
179 static inline struct buf_page *ltt_relay_cache_page(struct rchan_buf *buf,
180 struct buf_page **page_cache,
181 struct buf_page *page, size_t offset)
182 {
183 ssize_t diff_offset;
184 ssize_t half_buf_size = buf->chan->alloc_size >> 1;
185
186 /*
187 * Make sure this is the page we want to write into. The current
188 * page is changed concurrently by other writers. [wrh]page are
189 * used as a cache remembering the last page written
190 * to/read/looked up for header address. No synchronization;
191 * could have to find the previous page is a nested write
192 * occured. Finding the right page is done by comparing the
193 * dest_offset with the buf_page offsets.
194 * When at the exact opposite of the buffer, bias towards forward search
195 * because it will be cached.
196 */
197
198 diff_offset = (ssize_t)offset - (ssize_t)page->offset;
199 if (diff_offset <= -(ssize_t)half_buf_size)
200 diff_offset += buf->chan->alloc_size;
201 else if (diff_offset > half_buf_size)
202 diff_offset -= buf->chan->alloc_size;
203
204 if (unlikely(diff_offset >= (ssize_t)PAGE_SIZE)) {
205 page = ltt_relay_find_next_page(buf, page, offset, diff_offset);
206 *page_cache = page;
207 } else if (unlikely(diff_offset < 0)) {
208 page = ltt_relay_find_prev_page(buf, page, offset, diff_offset);
209 }
210 return page;
211 }
212
213 //ust// #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
214 static inline void ltt_relay_do_copy(void *dest, const void *src, size_t len)
215 {
216 switch (len) {
217 case 0: break;
218 case 1: *(u8 *)dest = *(const u8 *)src;
219 break;
220 case 2: *(u16 *)dest = *(const u16 *)src;
221 break;
222 case 4: *(u32 *)dest = *(const u32 *)src;
223 break;
224 //ust// #if (BITS_PER_LONG == 64)
225 case 8: *(u64 *)dest = *(const u64 *)src;
226 break;
227 //ust// #endif
228 default:
229 memcpy(dest, src, len);
230 }
231 }
232 //ust// #else
233 //ust// /*
234 //ust// * Returns whether the dest and src addresses are aligned on
235 //ust// * min(sizeof(void *), len). Call this with statically known len for efficiency.
236 //ust// */
237 //ust// static inline int addr_aligned(const void *dest, const void *src, size_t len)
238 //ust// {
239 //ust// if (ltt_align((size_t)dest, len))
240 //ust// return 0;
241 //ust// if (ltt_align((size_t)src, len))
242 //ust// return 0;
243 //ust// return 1;
244 //ust// }
245 //ust//
246 //ust// static inline void ltt_relay_do_copy(void *dest, const void *src, size_t len)
247 //ust// {
248 //ust// switch (len) {
249 //ust// case 0: break;
250 //ust// case 1: *(u8 *)dest = *(const u8 *)src;
251 //ust// break;
252 //ust// case 2: if (unlikely(!addr_aligned(dest, src, 2)))
253 //ust// goto memcpy_fallback;
254 //ust// *(u16 *)dest = *(const u16 *)src;
255 //ust// break;
256 //ust// case 4: if (unlikely(!addr_aligned(dest, src, 4)))
257 //ust// goto memcpy_fallback;
258 //ust// *(u32 *)dest = *(const u32 *)src;
259 //ust// break;
260 //ust// #if (BITS_PER_LONG == 64)
261 //ust// case 8: if (unlikely(!addr_aligned(dest, src, 8)))
262 //ust// goto memcpy_fallback;
263 //ust// *(u64 *)dest = *(const u64 *)src;
264 //ust// break;
265 //ust// #endif
266 //ust// default:
267 //ust// goto memcpy_fallback;
268 //ust// }
269 //ust// return;
270 //ust// memcpy_fallback:
271 //ust// memcpy(dest, src, len);
272 //ust// }
273 //ust// #endif
274
275 static inline int ltt_relay_write(struct rchan_buf *buf, size_t offset,
276 const void *src, size_t len)
277 {
278 //ust// struct buf_page *page;
279 //ust// ssize_t pagecpy;
280 //ust//
281 //ust// offset &= buf->chan->alloc_size - 1;
282 //ust// page = buf->wpage;
283 //ust//
284 //ust// page = ltt_relay_cache_page(buf, &buf->wpage, page, offset);
285 //ust// pagecpy = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
286 //ust// ltt_relay_do_copy(page_address(page->page)
287 //ust// + (offset & ~PAGE_MASK), src, pagecpy);
288 //ust//
289 //ust// if (unlikely(len != pagecpy))
290 //ust// _ltt_relay_write(buf, offset, src, len, page, pagecpy);
291 //ust// return len;
292
293
294 size_t cpy;
295 cpy = min_t(size_t, len, buf->buf_size - offset);
296 ltt_relay_do_copy(buf->buf_data + offset, src, cpy);
297
298 if (unlikely(len != cpy))
299 _ltt_relay_write(buf, offset, src, len, cpy);
300 return len;
301 }
302
303 /*
304 * CONFIG_LTT_RELAY kernel API, ltt/ltt-relay-alloc.c
305 */
306
307 struct rchan *ltt_relay_open(const char *base_filename,
308 struct dentry *parent,
309 size_t subbuf_size,
310 size_t n_subbufs,
311 void *private_data);
312 extern void ltt_relay_close(struct rchan *chan);
313
314 /*
315 * exported ltt_relay file operations, ltt/ltt-relay-alloc.c
316 */
317 extern const struct file_operations ltt_relay_file_operations;
318
319
320 /* LTTng lockless logging buffer info */
321 struct ltt_channel_buf_struct {
322 /* First 32 bytes cache-hot cacheline */
323 local_t offset; /* Current offset in the buffer */
324 local_t *commit_count; /* Commit count per sub-buffer */
325 atomic_long_t consumed; /*
326 * Current offset in the buffer
327 * standard atomic access (shared)
328 */
329 unsigned long last_tsc; /*
330 * Last timestamp written in the buffer.
331 */
332 /* End of first 32 bytes cacheline */
333 //ust// #ifdef CONFIG_LTT_VMCORE
334 //ust// local_t *commit_seq; /* Consecutive commits */
335 //ust// #endif
336 atomic_long_t active_readers; /*
337 * Active readers count
338 * standard atomic access (shared)
339 */
340 local_t events_lost;
341 local_t corrupted_subbuffers;
342 //ust// spinlock_t full_lock; /*
343 //ust// * buffer full condition spinlock, only
344 //ust// * for userspace tracing blocking mode
345 //ust// * synchronization with reader.
346 //ust// */
347 //ust// wait_queue_head_t write_wait; /*
348 //ust// * Wait queue for blocking user space
349 //ust// * writers
350 //ust// */
351 //ust// atomic_t wakeup_readers; /* Boolean : wakeup readers waiting ? */
352 /* one byte is written to this pipe when data is available, in order
353 to wake the consumer */
354 /* portability: Single byte writes must be as quick as possible. The kernel-side
355 buffer must be large enough so the writer doesn't block. From the pipe(7)
356 man page: Since linux 2.6.11, the pipe capacity is 65536 bytes. */
357 int data_ready_fd_write;
358 /* the reading end of the pipe */
359 int data_ready_fd_read;
360
361 /* commit count per subbuffer; must be at end of struct */
362 local_t commit_seq[0] ____cacheline_aligned;
363 } ____cacheline_aligned;
364
365 int ltt_do_get_subbuf(struct rchan_buf *buf, struct ltt_channel_buf_struct *ltt_buf, long *pconsumed_old);
366
367 int ltt_do_put_subbuf(struct rchan_buf *buf, struct ltt_channel_buf_struct *ltt_buf, u32 uconsumed_old);
368
369 #endif /* _LINUX_LTT_RELAY_H */
This page took 0.066927 seconds and 3 git commands to generate.