LIBTRACEFS(3)
LIBTRACEFS(3) π¦β¨
okay bestie, welcome to the libtracefs Manual β the one and only. This page is serving trace_pipe_raw reading realness and returning a kbuffer, so let's get into it. π
β¨ NAME β¨
tracefs_cpu_read_buf, tracefs_cpu_buffered_read_buf, tracefs_cpu_flush_buf β reading trace_pipe_raw data and returning a kbuffer. π
πΈ SYNOPSIS πΈ
#include <tracefs.h>
struct kbuffer *tracefs_cpu_read_buf(struct tracefs_cpu *tcpu, bool nonblock);
struct kbuffer *tracefs_cpu_buffered_read_buf(struct tracefs_cpu *tcpu, bool nonblock);
struct kbuffer *tracefs_cpu_flush_buf(struct tracefs_cpu *tcpu);
π DESCRIPTION π
Okay so this squad of APIs is your go-to for reading the raw data from the trace_pipe_raw files in the tracefs file system and returning a kbuffer structure to read it with. Literally the setup for event iteration. π«Ά
tracefs_cpu_read_buf() reads the trace_pipe_raw files associated to tcpu and returns a kbuffer structure that can be used to iterate the events. If nonblock is set, and there's no data available, it will return immediately. Otherwise, depending on how tcpu was opened, it will block. If tcpu was opened with nonblock set, then this nonblock will make no difference. no cap.
tracefs_cpu_buffered_read_buf() is basically the same as tracefs_cpu_read_buf() except that it uses a pipe through splice to buffer reads. This will batch reads, keeping the reading from the ring buffer less intrusive to the system, as just reading all the time can cause quite a disturbance. Note: one difference between this and tracefs_cpu_read() is that it will read only in sub buffer pages. If the ring buffer has not filled a page, then it will not return anything, even with nonblock set. Calls to tracefs_cpu_flush_buf() or tracefs_cpu_flush() should be done to read the rest of the file at the end of the trace. π (not a drill, flush at the end.)
tracefs_cpu_flush_buf() reads the trace_pipe_raw file associated by the tcpu and puts it into buffer, which must be the size of the sub buffer which is retrieved. This should be called at the end of tracing to get the rest of the data. This call will convert the file descriptor of trace_pipe_raw into non-blocking mode. periodt.
π₯ RETURN VALUE π₯
The functions tracefs_cpu_read_buf(), tracefs_cpu_buffered_read_buf()* (yes, that * is literally on the page β iykyk) and tracefs_cpu_flush() return a kbuffer descriptor that can be iterated over to find the events. Note: this descriptor is part of the tracefs_cpu structure and should not be freed. It will be freed. It returns NULL on error or if nonblock is set and there are no events available. In the case of no events, errno will be set with EAGAIN. π
π§ EXAMPLE π§
#include <stdlib.h>
#include <ctype.h>
#include <tracefs.h>
static void read_page(struct tep_handle *tep, struct kbuffer *kbuf)
{
static struct trace_seq seq;
struct tep_record record;
if (seq.buffer)
trace_seq_reset(&seq);
else
trace_seq_init(&seq);
while ((record.data = kbuffer_read_event(kbuf, &record.ts))) {
record.size = kbuffer_event_size(kbuf);
kbuffer_next_event(kbuf, NULL);
tep_print_event(tep, &seq, &record,
"%s-%d %9d\t%s: %s\n",
TEP_PRINT_COMM,
TEP_PRINT_PID,
TEP_PRINT_TIME,
TEP_PRINT_NAME,
TEP_PRINT_INFO);
trace_seq_do_printf(&seq);
trace_seq_reset(&seq);
}
}
int main (int argc, char **argv)
{
struct tracefs_cpu *tcpu;
struct tep_handle *tep;
struct kbuffer *kbuf;
int cpu;
if (argc < 2 || !isdigit(argv[1][0])) {
printf("usage: %s cpu\n\n", argv[0]);
exit(-1);
}
cpu = atoi(argv[1]);
tep = tracefs_local_events(NULL);
if (!tep) {
perror("Reading trace event formats");
exit(-1);
}
tcpu = tracefs_cpu_open(NULL, cpu, 0);
if (!tcpu) {
perror("Open CPU 0 file");
exit(-1);
}
while ((kbuf = tracefs_cpu_buffered_read_buf(tcpu, true))) {
read_page(tep, kbuf);
}
kbuf = tracefs_cpu_flush_buf(tcpu);
if (kbuf)
read_page(tep, kbuf);
tracefs_cpu_close(tcpu);
tep_free(tep);
return 0;
}
π FILES π
tracefs.hβ Header file to include in order to have access to the library APIs. π-ltracefsβ Linker switch to add when building a program that uses the library. π
π SEE ALSO π
tracefs_cpu_open(3) tracefs_cpu_close(3) tracefs_cpu_read(3) tracefs_cpu_buffered_read(3) tracefs_cpu_flush(3) libtracefs(3), libtraceevent(3), trace-cmd(1) β go read these too, bestie. π¦
π AUTHOR π
Steven Rostedt <rostedt@goodmis.org[1]> β iconic. π
π REPORTING BUGS π
Report bugs to <linux-trace-devel@vger.kernel.org[2]>. Please do, they want to know. π
πΈ LICENSE πΈ
libtracefs is Free Software licensed under the GNU LGPL 2.1. π
π¦ RESOURCES π¦
https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ β the source, the lore. β¨
π COPYING π
Copyright (C) 2022 Google, Inc. Free use of this software is granted under the terms of the GNU Public License (GPL). π«Ά
β NOTES β
rostedt@goodmis.orgmailto:rostedt@goodmis.orglinux-trace-devel@vger.kernel.orgmailto:linux-trace-devel@vger.kernel.org
and that's the whole vibe. libtracefs 1.8.4, dated 10/21/2026 β LIBTRACEFS(3) sign-off, mwah πβ¨
π¦ where this page comes from
- arch rolling: libtracefs 1.8.4-1 Β· GPL-2.0-only AND LGPL-2.1-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms