libtracefs: Add reading from per_cpu trace_pipe_raw helper functions
v3 cover libtracefs: Add reading from per_cpu trace_pipe_raw helper functions
From: "Steven Rostedt (Google)" <[email protected]>
Add functions that allow applications to read the trace_pipe_raw files:
tracefs_cpu_alloc_fd()
tracefs_cpu_free_fd()
tracefs_cpu_open()
tracefs_cpu_close()
tracefs_cpu_read_size()
tracefs_cpu_read()
tracefs_cpu_buffered_read()
tracefs_cpu_write()
tracefs_cpu_stop()
tracefs_cpu_flush()
tracefs_cpu_flush_write()
tracefs_cpu_pipe()
Changes since v2: https://lore.kernel.org/all/[email protected]/
- Renamed tracefs_cpu_create_fd() to tracefs_cpu_alloc_fd()
- Added tracefs_cpu_free_fd()
Steven Rostedt (Google) (8):
libtracefs: Add reading of per cpu files
libtracefs: Add tracefs_cpu_alloc_fd() and tracefs_cpu_free_fd()
libtracefs: Add tracefs_cpu_pipe()
libtracefs utest: Make helper functions for affinity
libtracefs: Add unit tests for tracefs_cpu functions
libtracefs: Move tracefs_cpu_open/close into its own man page
libtracefs: Add man pages for tracefs_cpu_alloc_fd() and
tracefs_cpu_free_fd()
libtracefs: Add man pages for tracefs_cpu_pipe()
Documentation/libtracefs-cpu-open.txt | 100 +++++ Documentation/libtracefs-cpu.txt | 240 +++++++++++ Documentation/libtracefs.txt | 15 + include/tracefs.h | 16 + samples/Makefile | 1 + scripts/utils.mk | 2 +- src/Makefile | 1 + src/tracefs-record.c | 598 ++++++++++++++++++++++++++ utest/tracefs-utest.c | 372 +++++++++++++++- 9 files changed, 1330 insertions(+), 15 deletions(-) create mode 100644 Documentation/libtracefs-cpu-open.txt create mode 100644 Documentation/libtracefs-cpu.txt create mode 100644 src/tracefs-record.c
Signature · 1 line
2.35.1
PATCH v3 1/8 libtracefs: Add reading of per cpu files
From: "Steven Rostedt (Google)" <[email protected]>
Add the APIs:
tracefs_cpu_open()
tracefs_cpu_close()
tracefs_cpu_read_size()
tracefs_cpu_read()
tracefs_cpu_buffered_read()
tracefs_cpu_write()
tracefs_cpu_stop()
tracefs_cpu_flush()
tracefs_cpu_flush_write()
That will attach to a trace_pipe_raw file for a given instance and allow opening, reading and writing to a file from it.
--- Documentation/libtracefs-cpu.txt | 239 +++++++++++++++ Documentation/libtracefs.txt | 12 + include/tracefs.h | 14 + samples/Makefile | 1 + scripts/utils.mk | 2 +- src/Makefile | 1 + src/tracefs-record.c | 505 +++++++++++++++++++++++++++++++ 7 files changed, 773 insertions(+), 1 deletion(-) create mode 100644 Documentation/libtracefs-cpu.txt create mode 100644 src/tracefs-record.c
Documentation/libtracefs-cpu.txt new file +239 -0
@@ -0,0 +1,239 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_cpu_open, tracefs_cpu_close, tracefs_cpu_read_size, tracefs_cpu_read, +tracefs_cpu_buffered_read, tracefs_cpu_write, tracefs_cpu_stop, tracefs_cpu_flush, +tracefs_cpu_flush_write - Reading trace_pipe_raw data + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +struct tracefs_cpu pass:[*]*tracefs_cpu_open*(struct tracefs_instance pass:[*]_instance_, + int _cpu_, bool _nonblock_); +void *tracefs_cpu_close*(struct tracefs_cpu pass:[*]_tcpu_); +int *tracefs_cpu_read_size*(struct tracefs_cpu pass:[*]_tcpu_); +int *tracefs_cpu_read*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_, bool _nonblock_); +int *tracefs_cpu_buffered_read*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_, bool _nonblock_); +int *tracefs_cpu_write*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_, bool _nonblock_); +int *tracefs_cpu_stop*(struct tracefs_cpu pass:[*]_tcpu_); +int *tracefs_cpu_flush*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_); +int *tracefs_cpu_flush_write*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_); +-- + +DESCRIPTION +----------- +This set of APIs can be used to read the raw data from the trace_pipe_raw +files in the tracefs file system. + +The *tracefs_cpu_open()* creates a descriptor that can read the tracefs +trace_pipe_raw file for a given _cpu_ in a given _instance_. If _instance_ is +NULL than the toplevel trace_pipe_raw file is used. + +The *tracefs_cpu_close()* closes all the file descriptors associated to the trace_pipe_raw +opened by *tracefs_cpu_open()*. + +The *tracefs_cpu_read_size()* returns the subbuffer size of the trace_pipe_raw. This +returns the minimum size of the buffer that is passed to the below functions. + +The *tracefs_cpu_read()* reads the trace_pipe_raw files associated to _tcpu_ into _buffer_. +_buffer_ must be at least the size of the sub buffer of the ring buffer, +which is returned by *tracefs_cpu_read_size()*. 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. + +The *tracefs_cpu_buffered_read()* is basically the same as *tracefs_cpu_read()* +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()* +should be done to read the rest of the file at the end of the trace. + +The *tracefs_cpu_write()* will pipe the data from the trace_pipe_raw +file associated with _tcpu_ into the _wfd_ file descriptor. If _nonblock_ is set, +then it will not block on if there's nothing to write. Note, it will only write +sub buffer size data to _wfd_. Calls to tracefs_cpu_flush_write() are needed to +write out the rest. + +The *tracefs_cpu_stop()* will attempt to unblock a task blocked on _tcpu_ reading it. +On older kernels, it may not do anything for the pipe reads, as older kernels do not +wake up tasks waiting on the ring buffer. Returns 0 if it definitely woke up any possible +waiters, but returns 1 if it is not sure it worked and waiters may need to have a signal +sent to them. + +The *tracefs_cpu_flush()* 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. +by *tracefs_cpu_read_size()*. 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. + +The *tracefs_cpu_flush_write()* same as *trace_cpu_flush()* except it takes a file +descriptor _wfd_ to flush the data into. + +RETURN VALUE +------------ +The *tracefs_cpu_open()* returns a struct tracefs_cpu descriptor that can be +used by the other functions or NULL on error. + +The *tracefs_cpu_read_size()* returns the minimum size of the buffers to be +used with *tracefs_cpu_read()*, *tracefs_cpu_buffered_read()* and *tracefs_cpu_flush()*. +Returns negative on error. + +The *tracefs_cpu_read()* returns the number of bytes read, or negative on error. + +The *tracefs_cpu_buffered_read()* returns the number of bytes read or negative on error. + +The *tracefs_cpu_write()* returns the number of bytes written to the file +or negative on error. + +The *tracefs_cpu_stop()* returns zero if any waiters were guaranteed to be +woken up from waiting on input, or returns one if this is an older kernel +that does not supply that guarantee, and a signal may need to be sent to +any waiters. Returns negative on error. + +The *tracefs_cpu_flush()* returns the number of bytes read or negative on error. + +The *tracefs_cpu_flush_write()* returns the number of bytes written to the +file or negative on error. + +EXAMPLE +------- +[source,c] +-- +#define _LARGEFILE64_SOURCE +#include <stdlib.h> +#include <ctype.h> +#include <pthread.h> +#include <unistd.h> +#include <tracefs.h> + +struct thread_data { + struct tracefs_cpu *tcpu; + int done; + int fd; +}; + +static void *thread_run(void *arg) +{ + struct thread_data *data = arg; + struct tracefs_cpu *tcpu = data->tcpu; + int fd = data->fd; + int ret; + + while (!data->done) { + ret = tracefs_cpu_write(tcpu, fd, false); + printf("wrote %d\n", ret); + } + return NULL; +} + +int main (int argc, char **argv) +{ + struct tracefs_instance *instance; + struct thread_data data; + pthread_t thread; + char *file; + int secs = 10; + int cpu; + int ret; + + if (argc < 3 || !isdigit(argv[1][0])) { + printf("usage: %s cpu file_destination [sleep secs]\n\n", argv[0]); + exit(-1); + } + + cpu = atoi(argv[1]); + file = argv[2]; + + if (argc > 3) + secs = atoi(argv[3]); + + instance = tracefs_instance_create("cpu_write"); + if (!instance) { + perror("create instance"); + exit(-1); + } + + memset(&data, 0, sizeof(data)); + + data.tcpu = tracefs_cpu_open(instance, cpu, 0); + if (!data.tcpu) { + perror("Open instance"); + exit(-1); + } + + data.fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, 0644); + if (data.fd < 0) { + perror(file); + exit(-1); + } + + pthread_create(&thread, NULL, thread_run, &data); + + sleep(secs); + + data.done = 1; + printf("stopping\n"); + ret = tracefs_cpu_stop(data.tcpu); + + printf("joining %d\n", ret); + pthread_join(thread, NULL); + + tracefs_trace_off(instance); + do { + ret = tracefs_cpu_flush_write(data.tcpu, data.fd); + printf("flushed %d\n", ret); + } while (ret > 0); + tracefs_trace_on(instance); + + tracefs_cpu_close(data.tcpu); + close(data.fd); + + return 0; +} +-- +FILES +----- +[verse] +-- +*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 +-------- +*libtracefs*(3), +*libtraceevent*(3), +*trace-cmd*(1) + +AUTHOR +------ +[verse] +-- +*Steven Rostedt* <[email protected]> +-- +REPORTING BUGS +-------------- +Report bugs to <[email protected]> + +LICENSE +------- +libtracefs is Free Software licensed under the GNU LGPL 2.1 + +RESOURCES +--------- +https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ + +COPYING +------- +Copyright \(C) 2022 Google, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Documentation/libtracefs.txt +12 -0
@@ -267,6 +267,18 @@ Histograms: int *tracefs_hist_continue*(struct tracefs_instance pass:[*]_instance_, struct tracefs_hist pass:[*]_hist_); int *tracefs_hist_reset*(struct tracefs_instance pass:[*]_instance_, struct tracefs_hist pass:[*]_hist_); +Recording of trace_pipe_raw files: + struct tracefs_cpu pass:[*]*tracefs_cpu_open*(struct tracefs_instance pass:[*]_instance_, + int _cpu_, bool _nonblock_); + void *tracefs_cpu_close*(struct tracefs_cpu pass:[*]_tcpu_); + int *tracefs_cpu_read_size*(struct tracefs_cpu pass:[*]_tcpu_); + int *tracefs_cpu_read*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_, bool _nonblock_); + int *tracefs_cpu_buffered_read*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_, bool _nonblock_); + int *tracefs_cpu_write*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_, bool _nonblock_); + int *tracefs_cpu_stop*(struct tracefs_cpu pass:[*]_tcpu_); + int *tracefs_cpu_flush*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_); + int *tracefs_cpu_flush_write*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_); + -- DESCRIPTION
include/tracefs.h +14 -0
@@ -595,4 +595,18 @@ struct tracefs_synth *tracefs_sql(struct tep_handle *tep, const char *name,struct tep_event *tracefs_synth_get_event (struct tep_handle *tep,struct tracefs_synth *synth); +struct tracefs_cpu ; + +struct tracefs_cpu *tracefs_cpu_open (struct tracefs_instance *instance, +int cpu,bool nonblock); +void tracefs_cpu_close (struct tracefs_cpu *tcpu); +int tracefs_cpu_read_size (struct tracefs_cpu *tcpu); +int tracefs_cpu_read (struct tracefs_cpu *tcpu,void *buffer,bool nonblock); +int tracefs_cpu_buffered_read (struct tracefs_cpu *tcpu,void *buffer,bool nonblock); +int tracefs_cpu_write (struct tracefs_cpu *tcpu,int wfd,bool nonblock); +int tracefs_cpu_stop (struct tracefs_cpu *tcpu); +int tracefs_cpu_flush (struct tracefs_cpu *tcpu,void *buffer); +int tracefs_cpu_flush_write (struct tracefs_cpu *tcpu,int wfd); + +#endif /* _TRACE_FS_H */
samples/Makefile +1 -0
@@ -21,6 +21,7 @@ EXAMPLES += hist-cont EXAMPLES += tracer EXAMPLES += stream EXAMPLES += instances-affinity +EXAMPLES += cpu TARGETS := TARGETS += sqlhist
scripts/utils.mk +1 -1
@@ -101,7 +101,7 @@ extract_example = \ do_sample_build = \ $(Q)($(print_sample_build) \ - $(CC) -o $1 $2 $(CFLAGS) $(LIBTRACEFS_STATIC) $(LIBTRACEEVENT_LIBS)) + $(CC) -o $1 $2 $(CFLAGS) $(LIBTRACEFS_STATIC) $(LIBTRACEEVENT_LIBS) -lpthread) do_sample_obj = \ $(Q)($(print_sample_obj) \
src/Makefile +1 -0
@@ -14,6 +14,7 @@ OBJS += tracefs-filter.o OBJS += tracefs-dynevents.o OBJS += tracefs-eprobes.o OBJS += tracefs-uprobes.o +OBJS += tracefs-record.o # Order matters for the the three below OBJS += sqlhist-lex.o
src/tracefs-record.c new file +505 -0
505 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v3 2/8 libtracefs: Add tracefs_cpu_alloc_fd() and tracefs_cpu_free_fd()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add tracefs_cpu_alloc_fd() to attach a tracefs_cpu descriptor to an already opened file descriptor, and tracefs_cpu_free_fd() to clean up the descriptor that tracefs_cpu_alloc_fd() created.
--- include/tracefs.h | 2 + src/tracefs-record.c | 118 +++++++++++++++++++++++++++++-------------- 2 files changed, 83 insertions(+), 37 deletions(-)
include/tracefs.h +2 -0
@@ -597,9 +597,11 @@ tracefs_synth_get_event(struct tep_handle *tep, struct tracefs_synth *synth);struct tracefs_cpu ; +struct tracefs_cpu *tracefs_cpu_alloc_fd (int fd,int subbuf_size,bool nonblock);struct tracefs_cpu *tracefs_cpu_open (struct tracefs_instance *instance,int cpu,bool nonblock);void tracefs_cpu_close (struct tracefs_cpu *tcpu); +void tracefs_cpu_free_fd (struct tracefs_cpu *tcpu);int tracefs_cpu_read_size (struct tracefs_cpu *tcpu);int tracefs_cpu_read (struct tracefs_cpu *tcpu,void *buffer,bool nonblock);int tracefs_cpu_buffered_read (struct tracefs_cpu *tcpu,void *buffer,bool nonblock);
src/tracefs-record.c +81 -37
174 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v3 3/8 libtracefs: Add tracefs_cpu_pipe()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add the interface to call splice directly from the tracefs_cpu descriptor. The requirement is that either the passed in file descriptor is a pipe, or that the tcpu was created with tracefs_cpu_create_fd() and the fd used there was a pipe.
--- include/tracefs.h | 2 +- src/tracefs-record.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-)
include/tracefs.h +1 -1
@@ -609,6 +609,6 @@ int tracefs_cpu_write(struct tracefs_cpu *tcpu, int wfd, bool nonblock);int tracefs_cpu_stop (struct tracefs_cpu *tcpu);int tracefs_cpu_flush (struct tracefs_cpu *tcpu,void *buffer);int tracefs_cpu_flush_write (struct tracefs_cpu *tcpu,int wfd); - +int tracefs_cpu_pipe (struct tracefs_cpu *tcpu,int wfd,bool nonblock);#endif /* _TRACE_FS_H */
src/tracefs-record.c +30 -0
@@ -547,3 +547,33 @@ int tracefs_cpu_write(struct tracefs_cpu *tcpu, int wfd, bool nonblock)return tot_write; } + +/** +* tracefs_cpu_pipe - Write the raw trace file into a pipe descriptor +* @tcpu: The descriptor representing the raw trace file +* @wfd: The write file descriptor to write the data to (must be a pipe) +* @nonblock: Hint to not block on the read if there's no data. +* +* This will splice directly the file descriptor of the trace_pipe_raw +* file to the given @wfd, which must be a pipe. This can also be used +* if @tcpu was created with tracefs_cpu_create_fd() where the passed +* in @fd there was a pipe, then @wfd does not need to be a pipe. +* +* Returns the number of bytes read or negative on error. +*/ +int tracefs_cpu_pipe (struct tracefs_cpu *tcpu,int wfd,bool nonblock) +{ +int mode =SPLICE_F_MOVE ; +int ret; + + ret =wait_on_input (tcpu, nonblock); +if (ret <=0 ) +return ret; + +if (nonblock || tcpu->flags &TC_NONBLOCK ) + mode |=SPLICE_F_NONBLOCK ; + + ret =splice (tcpu->fd,NULL , wfd,NULL , + tcpu->pipe_size, mode); +return ret; +}
Signature · 1 line
2.35.1
PATCH v3 4/8 libtracefs utest: Make helper functions for affinity
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add helper functions for setting and restoring affinity so that other tests do not need to reimplement them.
--- utest/tracefs-utest.c | 49 ++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 14 deletions(-)
utest/tracefs-utest.c +35 -14
@@ -87,20 +87,45 @@ static int test_callback(struct tep_event *event, struct tep_record *record,return 0 ; } +static cpu_set_t *cpuset_save; +static cpu_set_t *cpuset; +static int cpu_size; + +static void save_affinity (void ) +{ +int cpus; + + cpus =sysconf (_SC_NPROCESSORS_CONF); + cpuset_save =CPU_ALLOC (cpus); + cpuset =CPU_ALLOC (cpus); +CU_TEST (cpuset_save !=NULL && cpuset !=NULL ); +CU_TEST (sched_getaffinity (0 , cpu_size, cpuset_save) ==0 ); +} + +static void reset_affinity (void ) +{ +sched_setaffinity (0 , cpu_size, cpuset_save); +CPU_FREE (cpuset_save); +CPU_FREE (cpuset); +} + +static void set_affinity (int cpu) +{ +CPU_ZERO_S (cpu_size, cpuset); +CPU_SET_S (cpu, cpu_size, cpuset); +CU_TEST (sched_setaffinity (0 , cpu_size, cpuset) ==0 ); +sched_yield ();/* Force schedule */ +} +static void test_iter_write (struct tracefs_instance *instance) { -int cpus =sysconf (_SC_NPROCESSORS_CONF); -cpu_set_t *cpuset, *cpusave; -int cpu_size;char *path;int i, fd; +int cpus;int ret; - cpuset =CPU_ALLOC (cpus); - cpusave =CPU_ALLOC (cpus); - cpu_size =CPU_ALLOC_SIZE (cpus); -CPU_ZERO_S (cpu_size, cpuset); -sched_getaffinity (0 , cpu_size, cpusave); + cpus =sysconf (_SC_NPROCESSORS_CONF); +save_affinity (); path =tracefs_instance_get_file (instance,"trace_marker" );CU_TEST (path !=NULL );
@@ -114,17 +139,13 @@ static void test_iter_write(struct tracefs_instance *instance)if (!test_array[i].value) test_array[i].value++;CU_TEST (test_array[i].cpu < cpus); -CPU_ZERO_S (cpu_size, cpuset); -CPU_SET (test_array[i].cpu, cpuset); -sched_setaffinity (0 , cpu_size, cpuset); +set_affinity (test_array[i].cpu); ret =write (fd, test_array + i,sizeof (struct test_sample ));CU_TEST (ret ==sizeof (struct test_sample )); } -sched_setaffinity (0 , cpu_size, cpusave); +reset_affinity ();close (fd); -CPU_FREE (cpuset); -CPU_FREE (cpusave); }
Signature · 1 line
2.35.1
PATCH v3 5/8 libtracefs: Add unit tests for tracefs_cpu functions
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add unit tests to test the following functions:
tracefs_cpu_open() tracefs_cpu_close() tracefs_cpu_read_size(); tracefs_cpu_read() tracefs_cpu_write() tracefs_cpu_flush_write() tracefs_cpu_stop()
--- src/tracefs-record.c | 57 +++++--- utest/tracefs-utest.c | 323 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 361 insertions(+), 19 deletions(-)
src/tracefs-record.c +38 -19
@@ -19,8 +19,9 @@#include "tracefs-local.h" enum { -TC_STOP =1 <<0 ,/* Stop reading */ -TC_NONBLOCK =1 <<1 ,/* read is non blocking */ +TC_STOP =1 <<0 ,/* Stop reading */ +TC_PERM_NONBLOCK =1 <<1 ,/* read is always non blocking */ +TC_NONBLOCK =1 <<2 ,/* read is non blocking */ };struct tracefs_cpu {
@@ -59,7 +60,7 @@ tracefs_cpu_alloc_fd(int fd, int subbuf_size, bool nonblock)if (nonblock) { mode |=O_NONBLOCK ; - tcpu->flags |=TC_NONBLOCK ; + tcpu->flags |=TC_NONBLOCK |TC_PERM_NONBLOCK ; } tcpu->splice_pipe[0 ] =-1 ;
@@ -69,7 +70,7 @@ tracefs_cpu_alloc_fd(int fd, int subbuf_size, bool nonblock) tcpu->subbuf_size = subbuf_size; -if (tcpu->flags &TC_NONBLOCK ) { +if (tcpu->flags &TC_PERM_NONBLOCK ) { tcpu->ctrl_pipe[0 ] =-1 ; tcpu->ctrl_pipe[1 ] =-1 ; }else {
@@ -210,11 +211,27 @@ static void set_nonblock(struct tracefs_cpu *tcpu) {long flags; +if (tcpu->flags &TC_NONBLOCK ) +return ; + flags =fcntl (tcpu->fd,F_GETFL );fcntl (tcpu->fd,F_SETFL , flags |O_NONBLOCK ); tcpu->flags |=TC_NONBLOCK ; } +static void unset_nonblock (struct tracefs_cpu *tcpu) +{ +long flags; + +if (!(tcpu->flags &TC_NONBLOCK )) +return ; + + flags =fcntl (tcpu->fd,F_GETFL ); + flags &= ~O_NONBLOCK ; +fcntl (tcpu->fd,F_SETFL , flags); + tcpu->flags &= ~TC_NONBLOCK ; +} +/* * If set to blocking mode, block until the watermark has been * reached, or the control has said to stop. If the contol is
@@ -222,24 +239,24 @@ static void set_nonblock(struct tracefs_cpu *tcpu)*/ static int wait_on_input (struct tracefs_cpu *tcpu,bool nonblock) { -struct timeval tv, *ptv =NULL ;fd_set rfds;int ret; -if (tcpu->flags &TC_NONBLOCK ) +if (tcpu->flags &TC_PERM_NONBLOCK )return 1 ;if (nonblock) { - tv.tv_sec =0 ; - tv.tv_usec =0 ; - ptv = &tv; +set_nonblock (tcpu); +return 1 ; + }else { +unset_nonblock (tcpu); }FD_ZERO (&rfds);FD_SET (tcpu->fd, &rfds);FD_SET (tcpu->ctrl_pipe[0 ], &rfds); - ret =select (tcpu->nfds, &rfds,NULL ,NULL , ptv); + ret =select (tcpu->nfds, &rfds,NULL ,NULL ,NULL );/* Let the application decide what to do with signals and such */ if (ret <0 )
@@ -251,6 +268,8 @@ static int wait_on_input(struct tracefs_cpu *tcpu, bool nonblock)/* Make nonblock as it is now stopped */ set_nonblock (tcpu); +/* Permanently set unblock */ + tcpu->flags |=TC_PERM_NONBLOCK ; }return FD_ISSET (tcpu->fd, &rfds);
@@ -275,8 +294,6 @@ static int wait_on_input(struct tracefs_cpu *tcpu, bool nonblock)*/ int tracefs_cpu_read (struct tracefs_cpu *tcpu,void *buffer,bool nonblock) { -bool orig_nonblock = nonblock; -long flags =0 ;int ret;/*
@@ -290,8 +307,9 @@ int tracefs_cpu_read(struct tracefs_cpu *tcpu, void *buffer, bool nonblock) ret =read (tcpu->fd, buffer, tcpu->subbuf_size); -if (nonblock != orig_nonblock && !(tcpu->flags &TC_NONBLOCK )) -fcntl (tcpu->fd,F_SETFL , flags); +/* It's OK if there's no data to read */ +if (ret <0 && errno ==EAGAIN ) + ret =0 ;return ret; }
@@ -360,7 +378,7 @@ int tracefs_cpu_buffered_read(struct tracefs_cpu *tcpu, void *buffer, bool nonblif (ret <=0 )return ret; -if (nonblock || tcpu->flags &TC_NONBLOCK ) +if (tcpu->flags &TC_NONBLOCK ) mode |=SPLICE_F_NONBLOCK ; ret =init_splice (tcpu);
@@ -414,6 +432,8 @@ int tracefs_cpu_stop(struct tracefs_cpu *tcpu)else ret =0 ; +set_nonblock (tcpu); +return ret; }
@@ -436,8 +456,7 @@ int tracefs_cpu_flush(struct tracefs_cpu *tcpu, void *buffer)int ret;/* Make sure that reading is now non blocking */ -if (!(tcpu->flags &TC_NONBLOCK )) -set_nonblock (tcpu); +set_nonblock (tcpu);if (tcpu->buffered <0 ) tcpu->buffered =0 ;
@@ -505,7 +524,7 @@ int tracefs_cpu_write(struct tracefs_cpu *tcpu, int wfd, bool nonblock)if (ret <=0 )return ret; -if (nonblock || tcpu->flags &TC_NONBLOCK ) +if (tcpu->flags &TC_NONBLOCK ) mode |=SPLICE_F_NONBLOCK ; ret =init_splice (tcpu);
@@ -570,7 +589,7 @@ int tracefs_cpu_pipe(struct tracefs_cpu *tcpu, int wfd, bool nonblock)if (ret <=0 )return ret; -if (nonblock || tcpu->flags &TC_NONBLOCK ) +if (tcpu->flags &TC_NONBLOCK ) mode |=SPLICE_F_NONBLOCK ; ret = splice(tcpu->fd, NULL, wfd, NULL,
utest/tracefs-utest.c +323 -0
351 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v3 6/8 libtracefs: Move tracefs_cpu_open/close into its own man page
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Man pages only allow at most 9 functions to be described in a single man page. The libtracefs-cpu man page is already at that limit. In order to add more tracefs_cpu_* functions, this needs to be split up.
Move the tracefs_cpu_open() and tracefs_cpu_close() functions out into its own man page, to allow more to be added to both.
--- Documentation/libtracefs-cpu-open.txt | 78 +++++++++++++++++++++++++++ Documentation/libtracefs-cpu.txt | 17 ++---- 2 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 Documentation/libtracefs-cpu-open.txt
Documentation/libtracefs-cpu-open.txt new file +78 -0
@@ -0,0 +1,78 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_cpu_open, tracefs_cpu_close - Opening trace_pipe_raw data for reading + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +struct tracefs_cpu pass:[*]*tracefs_cpu_open*(struct tracefs_instance pass:[*]_instance_, + int _cpu_, bool _nonblock_); +void *tracefs_cpu_close*(struct tracefs_cpu pass:[*]_tcpu_); +-- + +DESCRIPTION +----------- +This set of APIs can be used to open the raw data from the trace_pipe_raw +files in the tracefs file system in oder to read them with the *tracefs_cpu_read*(3) +functions. + +The *tracefs_cpu_open()* creates a descriptor that can read the tracefs +trace_pipe_raw file for a given _cpu_ in a given _instance_. If _instance_ is +NULL than the toplevel trace_pipe_raw file is used. + +The *tracefs_cpu_close()* closes all the file descriptors associated to the trace_pipe_raw +opened by *tracefs_cpu_open()*. + +RETURN VALUE +------------ +The *tracefs_cpu_open()* returns a struct tracefs_cpu descriptor that can be +used by the other functions or NULL on error. + +EXAMPLE +------- +See *tracefs_cpu_read*(3) for an example. + +FILES +----- +[verse] +-- +*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 +-------- +*libtracefs*(3), +*libtraceevent*(3), +*trace-cmd*(1) + +AUTHOR +------ +[verse] +-- +*Steven Rostedt* <[email protected]> +-- +REPORTING BUGS +-------------- +Report bugs to <[email protected]> + +LICENSE +------- +libtracefs is Free Software licensed under the GNU LGPL 2.1 + +RESOURCES +--------- +https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ + +COPYING +------- +Copyright \(C) 2022 Google, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Documentation/libtracefs-cpu.txt +4 -13
@@ -3,9 +3,8 @@ libtracefs(3) NAME ---- -tracefs_cpu_open, tracefs_cpu_close, tracefs_cpu_read_size, tracefs_cpu_read, -tracefs_cpu_buffered_read, tracefs_cpu_write, tracefs_cpu_stop, tracefs_cpu_flush, -tracefs_cpu_flush_write - Reading trace_pipe_raw data +tracefs_cpu_read_size, tracefs_cpu_read, tracefs_cpu_buffered_read, tracefs_cpu_write, +tracefs_cpu_stop, tracefs_cpu_flush, tracefs_cpu_flush_write - Reading trace_pipe_raw data SYNOPSIS --------
@@ -13,9 +12,6 @@ SYNOPSIS -- *#include <tracefs.h>* -struct tracefs_cpu pass:[*]*tracefs_cpu_open*(struct tracefs_instance pass:[*]_instance_, - int _cpu_, bool _nonblock_); -void *tracefs_cpu_close*(struct tracefs_cpu pass:[*]_tcpu_); int *tracefs_cpu_read_size*(struct tracefs_cpu pass:[*]_tcpu_); int *tracefs_cpu_read*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_, bool _nonblock_); int *tracefs_cpu_buffered_read*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_, bool _nonblock_);
@@ -30,13 +26,6 @@ DESCRIPTION This set of APIs can be used to read the raw data from the trace_pipe_raw files in the tracefs file system. -The *tracefs_cpu_open()* creates a descriptor that can read the tracefs -trace_pipe_raw file for a given _cpu_ in a given _instance_. If _instance_ is -NULL than the toplevel trace_pipe_raw file is used. - -The *tracefs_cpu_close()* closes all the file descriptors associated to the trace_pipe_raw -opened by *tracefs_cpu_open()*. - The *tracefs_cpu_read_size()* returns the subbuffer size of the trace_pipe_raw. This returns the minimum size of the buffer that is passed to the below functions.
@@ -211,6 +200,8 @@ FILES SEE ALSO -------- +*tracefs_cpu_open*(3) +*tracefs_cpu_close*(3) *libtracefs*(3), *libtraceevent*(3), *trace-cmd*(1)
Signature · 1 line
2.35.1
PATCH v3 7/8 libtracefs: Add man pages for tracefs_cpu_alloc_fd() and tracefs_cpu_free_fd()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Update the man pages to describe the tracefs_cpu_alloc_fd() and tracefs_cpu_free_fd().
--- Documentation/libtracefs-cpu-open.txt | 24 +++++++++++++++++++++++- Documentation/libtracefs.txt | 2 ++ 2 files changed, 25 insertions(+), 1 deletion(-)
Documentation/libtracefs-cpu-open.txt +23 -1
@@ -3,7 +3,7 @@ libtracefs(3) NAME ---- -tracefs_cpu_open, tracefs_cpu_close - Opening trace_pipe_raw data for reading +tracefs_cpu_open, tracefs_cpu_close, tracefs_cpu_alloc_fd, tracefs_cpu_free_fd - Opening trace_pipe_raw data for reading SYNOPSIS --------
@@ -14,6 +14,9 @@ SYNOPSIS struct tracefs_cpu pass:[*]*tracefs_cpu_open*(struct tracefs_instance pass:[*]_instance_, int _cpu_, bool _nonblock_); void *tracefs_cpu_close*(struct tracefs_cpu pass:[*]_tcpu_); + +struct tracefs_cpu pass:[*]*tracefs_cpu_alloc_fd*(int _fd_, int _subbuf_size_, bool _nonblock_); +void *tracefs_cpu_free_fd*(struct tracefs_cpu pass:[*]_tcpu_); -- DESCRIPTION
@@ -29,11 +32,30 @@ NULL than the toplevel trace_pipe_raw file is used. The *tracefs_cpu_close()* closes all the file descriptors associated to the trace_pipe_raw opened by *tracefs_cpu_open()*. +The *tracefs_cpu_alloc_fd()* will create a tracefs_cpu descriptor from an existing +file descriptor _fd_. This is useful to use when connecting to a socket or pipe where +the other end is feeding raw tracing data in the same format as the trace_pipe_raw +file would (like in guest to host tracing). The caller is responsible for determining +the _subbuf_size_ that will be used to break up the sub-buffers being read by the +file descriptor. The _nonblock_ is treated the same as the same parameter in +*tracefs_cpu_open()*. + +The *tracefs_cpu_free_fd()* is used to free the descriptor returned by *tracefs_cpu_alloc_fd()*. +It does all the clean up that *tracefs_cpu_close()* performs, and that could also be +used to free up the descriptor created by *tracefs_cpu_alloc_fd()* but will also close +the file descriptor passed in. Note that *tracefs_cpu_free_fd()* should not be used +on the descriptor returned by *tracefs_cpu_open()* as it will not close the file descriptor +created by it. + RETURN VALUE ------------ The *tracefs_cpu_open()* returns a struct tracefs_cpu descriptor that can be used by the other functions or NULL on error. +The *tracefs_cpu_alloc_fd()* returns a struct tracefs_cpu descriptor that can +be used by the *tracefs_cpu_read*(3) related functions, where the descriptor +will be reading the passed in _fd_ file descriptor. + EXAMPLE ------- See *tracefs_cpu_read*(3) for an example.
Documentation/libtracefs.txt +2 -0
@@ -270,7 +270,9 @@ Histograms: Recording of trace_pipe_raw files: struct tracefs_cpu pass:[*]*tracefs_cpu_open*(struct tracefs_instance pass:[*]_instance_, int _cpu_, bool _nonblock_); + struct tracefs_cpu pass:[*]*tracefs_cpu_alloc_fd*(int _fd_, int _subbuf_size_, bool _nonblock_); void *tracefs_cpu_close*(struct tracefs_cpu pass:[*]_tcpu_); + void *tracefs_cpu_free_fd*(struct tracefs_cpu pass:[*]_tcpu_); int *tracefs_cpu_read_size*(struct tracefs_cpu pass:[*]_tcpu_); int *tracefs_cpu_read*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_, bool _nonblock_); int *tracefs_cpu_buffered_read*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_, bool _nonblock_);
Signature · 1 line
2.35.1
PATCH v3 8/8 libtracefs: Add man pages for tracefs_cpu_pipe()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Update the man pages to describe the tracefs_cpu_pipe() function.
--- Documentation/libtracefs-cpu.txt | 12 +++++++++++- Documentation/libtracefs.txt | 1 + 2 files changed, 12 insertions(+), 1 deletion(-)
Documentation/libtracefs-cpu.txt +11 -1
@@ -4,7 +4,8 @@ libtracefs(3) NAME ---- tracefs_cpu_read_size, tracefs_cpu_read, tracefs_cpu_buffered_read, tracefs_cpu_write, -tracefs_cpu_stop, tracefs_cpu_flush, tracefs_cpu_flush_write - Reading trace_pipe_raw data +tracefs_cpu_stop, tracefs_cpu_flush, tracefs_cpu_flush_write, tracefs_cpu_pipe +- Reading trace_pipe_raw data SYNOPSIS --------
@@ -19,6 +20,7 @@ int *tracefs_cpu_write*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_, bool _nonb int *tracefs_cpu_stop*(struct tracefs_cpu pass:[*]_tcpu_); int *tracefs_cpu_flush*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_); int *tracefs_cpu_flush_write*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_); +int *tracefs_cpu_pipe*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_, bool _nonblock_); -- DESCRIPTION
@@ -66,6 +68,14 @@ trace_pipe_raw into non-blocking mode. The *tracefs_cpu_flush_write()* same as *trace_cpu_flush()* except it takes a file descriptor _wfd_ to flush the data into. +The *tracefs_cpu_pipe()* is similar to *tracefs_cpu_write()* but the _wfd_ file descriptor +must be a pipe. This call is an optimization of *tracefs_cpu_write()* that uses two calls +to *splice*(2) in order to connect the trace_pipe_raw file descriptor with the write file +descriptor. *splice*(2) requires that one of the passed in file descriptors is a pipe. +If the application wants to pass the data to an existing pipe, there's no reason for +there to be two *splice*(2) system calls and *tracefs_cpu_pipe()* can simply use a single +call to _wfd_. + RETURN VALUE ------------ The *tracefs_cpu_open()* returns a struct tracefs_cpu descriptor that can be
Documentation/libtracefs.txt +1 -0
@@ -280,6 +280,7 @@ Recording of trace_pipe_raw files: int *tracefs_cpu_stop*(struct tracefs_cpu pass:[*]_tcpu_); int *tracefs_cpu_flush*(struct tracefs_cpu pass:[*]_tcpu_, void pass:[*]_buffer_); int *tracefs_cpu_flush_write*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_); + int *tracefs_cpu_pipe*(struct tracefs_cpu pass:[*]_tcpu_, int _wfd_, bool _nonblock_); --
Signature · 1 line
2.35.1