1 /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
3 * ring_buffer_iterator.c
5 * Ring buffer and channel iterators. Get each event of a channel in order. Uses
6 * a prio heap for per-cpu buffers, giving a O(log(NR_CPUS)) algorithmic
7 * complexity for the "get next event" operation.
9 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #include <wrapper/ringbuffer/iterator.h>
13 #include <wrapper/cpu.h>
14 #include <wrapper/file.h>
15 #include <wrapper/uaccess.h>
16 #include <linux/jiffies.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
21 * Safety factor taking into account internal kernel interrupt latency.
22 * Assuming 250ms worse-case latency.
24 #define MAX_SYSTEM_LATENCY 250
27 * Maximum delta expected between trace clocks. At most 1 jiffy delta.
29 #define MAX_CLOCK_DELTA (jiffies_to_usecs(1) * 1000)
32 * lib_ring_buffer_get_next_record - Get the next record in a buffer.
36 * Returns the size of the event read, -EAGAIN if buffer is empty, -ENODATA if
37 * buffer is empty and finalized. The buffer must already be opened for reading.
39 ssize_t
lib_ring_buffer_get_next_record(struct channel
*chan
,
40 struct lib_ring_buffer
*buf
)
42 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
43 struct lib_ring_buffer_iter
*iter
= &buf
->iter
;
47 switch (iter
->state
) {
49 ret
= lib_ring_buffer_get_next_subbuf(buf
);
50 if (ret
&& !LTTNG_READ_ONCE(buf
->finalized
)
51 && config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
53 * Use "pull" scheme for global buffers. The reader
54 * itself flushes the buffer to "pull" data not visible
55 * to readers yet. Flush current subbuffer and re-try.
57 * Per-CPU buffers rather use a "push" scheme because
58 * the IPI needed to flush all CPU's buffers is too
59 * costly. In the "push" scheme, the reader waits for
60 * the writer periodic timer to flush the
61 * buffers (keeping track of a quiescent state
62 * timestamp). Therefore, the writer "pushes" data out
63 * of the buffers rather than letting the reader "pull"
64 * data from the buffer.
66 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
);
67 ret
= lib_ring_buffer_get_next_subbuf(buf
);
71 iter
->consumed
= buf
->cons_snapshot
;
72 iter
->data_size
= lib_ring_buffer_get_read_data_size(config
, buf
);
73 iter
->read_offset
= iter
->consumed
;
75 iter
->read_offset
+= config
->cb
.subbuffer_header_size();
76 iter
->state
= ITER_TEST_RECORD
;
78 case ITER_TEST_RECORD
:
79 if (iter
->read_offset
- iter
->consumed
>= iter
->data_size
) {
80 iter
->state
= ITER_PUT_SUBBUF
;
82 CHAN_WARN_ON(chan
, !config
->cb
.record_get
);
83 config
->cb
.record_get(config
, chan
, buf
,
88 iter
->read_offset
+= iter
->header_len
;
89 subbuffer_consume_record(config
, &buf
->backend
);
90 iter
->state
= ITER_NEXT_RECORD
;
91 return iter
->payload_len
;
94 case ITER_NEXT_RECORD
:
95 iter
->read_offset
+= iter
->payload_len
;
96 iter
->state
= ITER_TEST_RECORD
;
99 lib_ring_buffer_put_next_subbuf(buf
);
100 iter
->state
= ITER_GET_SUBBUF
;
103 CHAN_WARN_ON(chan
, 1); /* Should not happen */
107 EXPORT_SYMBOL_GPL(lib_ring_buffer_get_next_record
);
109 static int buf_is_higher(void *a
, void *b
)
111 struct lib_ring_buffer
*bufa
= a
;
112 struct lib_ring_buffer
*bufb
= b
;
114 /* Consider lowest timestamps to be at the top of the heap */
115 return (bufa
->iter
.timestamp
< bufb
->iter
.timestamp
);
119 void lib_ring_buffer_get_empty_buf_records(const struct lib_ring_buffer_config
*config
,
120 struct channel
*chan
)
122 struct lttng_ptr_heap
*heap
= &chan
->iter
.heap
;
123 struct lib_ring_buffer
*buf
, *tmp
;
126 list_for_each_entry_safe(buf
, tmp
, &chan
->iter
.empty_head
,
128 len
= lib_ring_buffer_get_next_record(chan
, buf
);
131 * Deal with -EAGAIN and -ENODATA.
132 * len >= 0 means record contains data.
133 * -EBUSY should never happen, because we support only one
138 /* Keep node in empty list */
142 * Buffer is finalized. Don't add to list of empty
143 * buffer, because it has no more data to provide, ever.
145 list_del(&buf
->iter
.empty_node
);
148 CHAN_WARN_ON(chan
, 1);
152 * Insert buffer into the heap, remove from empty buffer
155 CHAN_WARN_ON(chan
, len
< 0);
156 list_del(&buf
->iter
.empty_node
);
157 CHAN_WARN_ON(chan
, lttng_heap_insert(heap
, buf
));
163 void lib_ring_buffer_wait_for_qs(const struct lib_ring_buffer_config
*config
,
164 struct channel
*chan
)
167 unsigned long wait_msecs
;
170 * No need to wait if no empty buffers are present.
172 if (list_empty(&chan
->iter
.empty_head
))
175 timestamp_qs
= config
->cb
.ring_buffer_clock_read(chan
);
177 * We need to consider previously empty buffers.
178 * Do a get next buf record on each of them. Add them to
179 * the heap if they have data. If at least one of them
180 * don't have data, we need to wait for
181 * switch_timer_interval + MAX_SYSTEM_LATENCY (so we are sure the
182 * buffers have been switched either by the timer or idle entry) and
183 * check them again, adding them if they have data.
185 lib_ring_buffer_get_empty_buf_records(config
, chan
);
188 * No need to wait if no empty buffers are present.
190 if (list_empty(&chan
->iter
.empty_head
))
194 * We need to wait for the buffer switch timer to run. If the
195 * CPU is idle, idle entry performed the switch.
196 * TODO: we could optimize further by skipping the sleep if all
197 * empty buffers belong to idle or offline cpus.
199 wait_msecs
= jiffies_to_msecs(chan
->switch_timer_interval
);
200 wait_msecs
+= MAX_SYSTEM_LATENCY
;
202 lib_ring_buffer_get_empty_buf_records(config
, chan
);
204 * Any buffer still in the empty list here cannot possibly
205 * contain an event with a timestamp prior to "timestamp_qs".
206 * The new quiescent state timestamp is the one we grabbed
207 * before waiting for buffer data. It is therefore safe to
208 * ignore empty buffers up to last_qs timestamp for fusion
211 chan
->iter
.last_qs
= timestamp_qs
;
215 * channel_get_next_record - Get the next record in a channel.
217 * @ret_buf: the buffer in which the event is located (output)
219 * Returns the size of new current event, -EAGAIN if all buffers are empty,
220 * -ENODATA if all buffers are empty and finalized. The channel must already be
221 * opened for reading.
224 ssize_t
channel_get_next_record(struct channel
*chan
,
225 struct lib_ring_buffer
**ret_buf
)
227 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
228 struct lib_ring_buffer
*buf
;
229 struct lttng_ptr_heap
*heap
;
232 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
233 *ret_buf
= channel_get_ring_buffer(config
, chan
, 0);
234 return lib_ring_buffer_get_next_record(chan
, *ret_buf
);
237 heap
= &chan
->iter
.heap
;
240 * get next record for topmost buffer.
242 buf
= lttng_heap_maximum(heap
);
244 len
= lib_ring_buffer_get_next_record(chan
, buf
);
246 * Deal with -EAGAIN and -ENODATA.
247 * len >= 0 means record contains data.
251 buf
->iter
.timestamp
= 0;
252 list_add(&buf
->iter
.empty_node
, &chan
->iter
.empty_head
);
253 /* Remove topmost buffer from the heap */
254 CHAN_WARN_ON(chan
, lttng_heap_remove(heap
) != buf
);
258 * Buffer is finalized. Remove buffer from heap and
259 * don't add to list of empty buffer, because it has no
260 * more data to provide, ever.
262 CHAN_WARN_ON(chan
, lttng_heap_remove(heap
) != buf
);
265 CHAN_WARN_ON(chan
, 1);
269 * Reinsert buffer into the heap. Note that heap can be
270 * partially empty, so we need to use
271 * lttng_heap_replace_max().
273 CHAN_WARN_ON(chan
, len
< 0);
274 CHAN_WARN_ON(chan
, lttng_heap_replace_max(heap
, buf
) != buf
);
279 buf
= lttng_heap_maximum(heap
);
280 if (!buf
|| buf
->iter
.timestamp
> chan
->iter
.last_qs
) {
282 * Deal with buffers previously showing no data.
283 * Add buffers containing data to the heap, update
286 lib_ring_buffer_wait_for_qs(config
, chan
);
289 *ret_buf
= buf
= lttng_heap_maximum(heap
);
292 * If this warning triggers, you probably need to check your
293 * system interrupt latency. Typical causes: too many printk()
294 * output going to a serial console with interrupts off.
295 * Allow for MAX_CLOCK_DELTA ns timestamp delta going backward.
296 * Observed on SMP KVM setups with trace_clock().
298 if (chan
->iter
.last_timestamp
299 > (buf
->iter
.timestamp
+ MAX_CLOCK_DELTA
)) {
300 printk(KERN_WARNING
"ring_buffer: timestamps going "
301 "backward. Last time %llu ns, cpu %d, "
302 "current time %llu ns, cpu %d, "
304 chan
->iter
.last_timestamp
, chan
->iter
.last_cpu
,
305 buf
->iter
.timestamp
, buf
->backend
.cpu
,
306 chan
->iter
.last_timestamp
- buf
->iter
.timestamp
);
307 CHAN_WARN_ON(chan
, 1);
309 chan
->iter
.last_timestamp
= buf
->iter
.timestamp
;
310 chan
->iter
.last_cpu
= buf
->backend
.cpu
;
311 return buf
->iter
.payload_len
;
314 if (list_empty(&chan
->iter
.empty_head
))
315 return -ENODATA
; /* All buffers finalized */
317 return -EAGAIN
; /* Temporarily empty */
320 EXPORT_SYMBOL_GPL(channel_get_next_record
);
323 void lib_ring_buffer_iterator_init(struct channel
*chan
, struct lib_ring_buffer
*buf
)
325 if (buf
->iter
.allocated
)
328 buf
->iter
.allocated
= 1;
329 if (chan
->iter
.read_open
&& !buf
->iter
.read_open
) {
330 CHAN_WARN_ON(chan
, lib_ring_buffer_open_read(buf
) != 0);
331 buf
->iter
.read_open
= 1;
334 /* Add to list of buffers without any current record */
335 if (chan
->backend
.config
.alloc
== RING_BUFFER_ALLOC_PER_CPU
)
336 list_add(&buf
->iter
.empty_node
, &chan
->iter
.empty_head
);
339 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
341 int lttng_cpuhp_rb_iter_online(unsigned int cpu
,
342 struct lttng_cpuhp_node
*node
)
344 struct channel
*chan
= container_of(node
, struct channel
,
346 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
347 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
349 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
351 lib_ring_buffer_iterator_init(chan
, buf
);
354 EXPORT_SYMBOL_GPL(lttng_cpuhp_rb_iter_online
);
356 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
358 #ifdef CONFIG_HOTPLUG_CPU
360 int channel_iterator_cpu_hotplug(struct notifier_block
*nb
,
361 unsigned long action
,
364 unsigned int cpu
= (unsigned long)hcpu
;
365 struct channel
*chan
= container_of(nb
, struct channel
,
367 struct lib_ring_buffer
*buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
368 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
370 if (!chan
->hp_iter_enable
)
373 CHAN_WARN_ON(chan
, config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
);
376 case CPU_DOWN_FAILED
:
377 case CPU_DOWN_FAILED_FROZEN
:
379 case CPU_ONLINE_FROZEN
:
380 lib_ring_buffer_iterator_init(chan
, buf
);
388 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
390 int channel_iterator_init(struct channel
*chan
)
392 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
393 struct lib_ring_buffer
*buf
;
395 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
398 INIT_LIST_HEAD(&chan
->iter
.empty_head
);
399 ret
= lttng_heap_init(&chan
->iter
.heap
,
401 GFP_KERNEL
, buf_is_higher
);
405 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
406 chan
->cpuhp_iter_online
.component
= LTTNG_RING_BUFFER_ITER
;
407 ret
= cpuhp_state_add_instance(lttng_rb_hp_online
,
408 &chan
->cpuhp_iter_online
.node
);
411 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
416 * In case of non-hotplug cpu, if the ring-buffer is allocated
417 * in early initcall, it will not be notified of secondary cpus.
418 * In that off case, we need to allocate for all possible cpus.
420 #ifdef CONFIG_HOTPLUG_CPU
421 chan
->hp_iter_notifier
.notifier_call
=
422 channel_iterator_cpu_hotplug
;
423 chan
->hp_iter_notifier
.priority
= 10;
424 register_cpu_notifier(&chan
->hp_iter_notifier
);
426 lttng_cpus_read_lock();
427 for_each_online_cpu(cpu
) {
428 buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
429 lib_ring_buffer_iterator_init(chan
, buf
);
431 chan
->hp_iter_enable
= 1;
432 lttng_cpus_read_unlock();
434 for_each_possible_cpu(cpu
) {
435 buf
= per_cpu_ptr(chan
->backend
.buf
, cpu
);
436 lib_ring_buffer_iterator_init(chan
, buf
);
440 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
442 buf
= channel_get_ring_buffer(config
, chan
, 0);
443 lib_ring_buffer_iterator_init(chan
, buf
);
448 void channel_iterator_unregister_notifiers(struct channel
*chan
)
450 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
452 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
453 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0))
457 ret
= cpuhp_state_remove_instance(lttng_rb_hp_online
,
458 &chan
->cpuhp_iter_online
.node
);
461 #else /* #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
462 chan
->hp_iter_enable
= 0;
463 unregister_cpu_notifier(&chan
->hp_iter_notifier
);
464 #endif /* #else #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(4,10,0)) */
468 void channel_iterator_free(struct channel
*chan
)
470 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
472 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
473 lttng_heap_free(&chan
->iter
.heap
);
476 int lib_ring_buffer_iterator_open(struct lib_ring_buffer
*buf
)
478 struct channel
*chan
= buf
->backend
.chan
;
479 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
480 CHAN_WARN_ON(chan
, config
->output
!= RING_BUFFER_ITERATOR
);
481 return lib_ring_buffer_open_read(buf
);
483 EXPORT_SYMBOL_GPL(lib_ring_buffer_iterator_open
);
486 * Note: Iterators must not be mixed with other types of outputs, because an
487 * iterator can leave the buffer in "GET" state, which is not consistent with
488 * other types of output (mmap, splice, raw data read).
490 void lib_ring_buffer_iterator_release(struct lib_ring_buffer
*buf
)
492 lib_ring_buffer_release_read(buf
);
494 EXPORT_SYMBOL_GPL(lib_ring_buffer_iterator_release
);
496 int channel_iterator_open(struct channel
*chan
)
498 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
499 struct lib_ring_buffer
*buf
;
502 CHAN_WARN_ON(chan
, config
->output
!= RING_BUFFER_ITERATOR
);
504 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
505 lttng_cpus_read_lock();
506 /* Allow CPU hotplug to keep track of opened reader */
507 chan
->iter
.read_open
= 1;
508 for_each_channel_cpu(cpu
, chan
) {
509 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
510 ret
= lib_ring_buffer_iterator_open(buf
);
513 buf
->iter
.read_open
= 1;
515 lttng_cpus_read_unlock();
517 buf
= channel_get_ring_buffer(config
, chan
, 0);
518 ret
= lib_ring_buffer_iterator_open(buf
);
522 /* Error should always happen on CPU 0, hence no close is required. */
523 CHAN_WARN_ON(chan
, cpu
!= 0);
524 lttng_cpus_read_unlock();
527 EXPORT_SYMBOL_GPL(channel_iterator_open
);
529 void channel_iterator_release(struct channel
*chan
)
531 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
532 struct lib_ring_buffer
*buf
;
535 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
536 lttng_cpus_read_lock();
537 for_each_channel_cpu(cpu
, chan
) {
538 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
539 if (buf
->iter
.read_open
) {
540 lib_ring_buffer_iterator_release(buf
);
541 buf
->iter
.read_open
= 0;
544 chan
->iter
.read_open
= 0;
545 lttng_cpus_read_unlock();
547 buf
= channel_get_ring_buffer(config
, chan
, 0);
548 lib_ring_buffer_iterator_release(buf
);
551 EXPORT_SYMBOL_GPL(channel_iterator_release
);
553 void lib_ring_buffer_iterator_reset(struct lib_ring_buffer
*buf
)
555 struct channel
*chan
= buf
->backend
.chan
;
557 if (buf
->iter
.state
!= ITER_GET_SUBBUF
)
558 lib_ring_buffer_put_next_subbuf(buf
);
559 buf
->iter
.state
= ITER_GET_SUBBUF
;
560 /* Remove from heap (if present). */
561 if (lttng_heap_cherrypick(&chan
->iter
.heap
, buf
))
562 list_add(&buf
->iter
.empty_node
, &chan
->iter
.empty_head
);
563 buf
->iter
.timestamp
= 0;
564 buf
->iter
.header_len
= 0;
565 buf
->iter
.payload_len
= 0;
566 buf
->iter
.consumed
= 0;
567 buf
->iter
.read_offset
= 0;
568 buf
->iter
.data_size
= 0;
569 /* Don't reset allocated and read_open */
572 void channel_iterator_reset(struct channel
*chan
)
574 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
575 struct lib_ring_buffer
*buf
;
578 /* Empty heap, put into empty_head */
579 while ((buf
= lttng_heap_remove(&chan
->iter
.heap
)) != NULL
)
580 list_add(&buf
->iter
.empty_node
, &chan
->iter
.empty_head
);
582 for_each_channel_cpu(cpu
, chan
) {
583 buf
= channel_get_ring_buffer(config
, chan
, cpu
);
584 lib_ring_buffer_iterator_reset(buf
);
586 /* Don't reset read_open */
587 chan
->iter
.last_qs
= 0;
588 chan
->iter
.last_timestamp
= 0;
589 chan
->iter
.last_cpu
= 0;
590 chan
->iter
.len_left
= 0;
594 * Ring buffer payload extraction read() implementation.
597 ssize_t
channel_ring_buffer_file_read(struct file
*filp
,
598 char __user
*user_buf
,
601 struct channel
*chan
,
602 struct lib_ring_buffer
*buf
,
605 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
606 size_t read_count
= 0, read_offset
;
610 if (!lttng_access_ok(VERIFY_WRITE
, user_buf
, count
))
613 /* Finish copy of previous record */
615 if (read_count
< count
) {
616 len
= chan
->iter
.len_left
;
618 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
620 buf
= lttng_heap_maximum(&chan
->iter
.heap
);
621 CHAN_WARN_ON(chan
, !buf
);
626 while (read_count
< count
) {
627 size_t copy_len
, space_left
;
630 len
= channel_get_next_record(chan
, &buf
);
632 len
= lib_ring_buffer_get_next_record(chan
, buf
);
636 * Check if buffer is finalized (end of file).
638 if (len
== -ENODATA
) {
639 /* A 0 read_count will tell about end of file */
642 if (filp
->f_flags
& O_NONBLOCK
) {
644 read_count
= -EAGAIN
;
650 * No data available at the moment, return what
657 * Wait for returned len to be >= 0 or -ENODATA.
660 error
= wait_event_interruptible(
662 ((len
= channel_get_next_record(chan
,
663 &buf
)), len
!= -EAGAIN
));
665 error
= wait_event_interruptible(
667 ((len
= lib_ring_buffer_get_next_record(
668 chan
, buf
)), len
!= -EAGAIN
));
669 CHAN_WARN_ON(chan
, len
== -EBUSY
);
674 CHAN_WARN_ON(chan
, len
< 0 && len
!= -ENODATA
);
678 read_offset
= buf
->iter
.read_offset
;
680 space_left
= count
- read_count
;
681 if (len
<= space_left
) {
683 chan
->iter
.len_left
= 0;
686 copy_len
= space_left
;
687 chan
->iter
.len_left
= len
- copy_len
;
688 *ppos
= read_offset
+ copy_len
;
690 if (__lib_ring_buffer_copy_to_user(&buf
->backend
, read_offset
,
691 &user_buf
[read_count
],
694 * Leave the len_left and ppos values at their current
695 * state, as we currently have a valid event to read.
699 read_count
+= copy_len
;
705 chan
->iter
.len_left
= 0;
710 * lib_ring_buffer_file_read - Read buffer record payload.
711 * @filp: file structure pointer.
712 * @buffer: user buffer to read data into.
713 * @count: number of bytes to read.
714 * @ppos: file read position.
716 * Returns a negative value on error, or the number of bytes read on success.
717 * ppos is used to save the position _within the current record_ between calls
721 ssize_t
lib_ring_buffer_file_read(struct file
*filp
,
722 char __user
*user_buf
,
726 struct inode
*inode
= filp
->lttng_f_dentry
->d_inode
;
727 struct lib_ring_buffer
*buf
= inode
->i_private
;
728 struct channel
*chan
= buf
->backend
.chan
;
730 return channel_ring_buffer_file_read(filp
, user_buf
, count
, ppos
,
735 * channel_file_read - Read channel record payload.
736 * @filp: file structure pointer.
737 * @buffer: user buffer to read data into.
738 * @count: number of bytes to read.
739 * @ppos: file read position.
741 * Returns a negative value on error, or the number of bytes read on success.
742 * ppos is used to save the position _within the current record_ between calls
746 ssize_t
channel_file_read(struct file
*filp
,
747 char __user
*user_buf
,
751 struct inode
*inode
= filp
->lttng_f_dentry
->d_inode
;
752 struct channel
*chan
= inode
->i_private
;
753 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
755 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
756 return channel_ring_buffer_file_read(filp
, user_buf
, count
,
757 ppos
, chan
, NULL
, 1);
759 struct lib_ring_buffer
*buf
=
760 channel_get_ring_buffer(config
, chan
, 0);
761 return channel_ring_buffer_file_read(filp
, user_buf
, count
,
767 int lib_ring_buffer_file_open(struct inode
*inode
, struct file
*file
)
769 struct lib_ring_buffer
*buf
= inode
->i_private
;
772 ret
= lib_ring_buffer_iterator_open(buf
);
776 file
->private_data
= buf
;
777 ret
= nonseekable_open(inode
, file
);
783 lib_ring_buffer_iterator_release(buf
);
788 int lib_ring_buffer_file_release(struct inode
*inode
, struct file
*file
)
790 struct lib_ring_buffer
*buf
= inode
->i_private
;
792 lib_ring_buffer_iterator_release(buf
);
797 int channel_file_open(struct inode
*inode
, struct file
*file
)
799 struct channel
*chan
= inode
->i_private
;
802 ret
= channel_iterator_open(chan
);
806 file
->private_data
= chan
;
807 ret
= nonseekable_open(inode
, file
);
813 channel_iterator_release(chan
);
818 int channel_file_release(struct inode
*inode
, struct file
*file
)
820 struct channel
*chan
= inode
->i_private
;
822 channel_iterator_release(chan
);
826 const struct file_operations channel_payload_file_operations
= {
827 .owner
= THIS_MODULE
,
828 .open
= channel_file_open
,
829 .release
= channel_file_release
,
830 .read
= channel_file_read
,
831 .llseek
= vfs_lib_ring_buffer_no_llseek
,
833 EXPORT_SYMBOL_GPL(channel_payload_file_operations
);
835 const struct file_operations lib_ring_buffer_payload_file_operations
= {
836 .owner
= THIS_MODULE
,
837 .open
= lib_ring_buffer_file_open
,
838 .release
= lib_ring_buffer_file_release
,
839 .read
= lib_ring_buffer_file_read
,
840 .llseek
= vfs_lib_ring_buffer_no_llseek
,
842 EXPORT_SYMBOL_GPL(lib_ring_buffer_payload_file_operations
);