libtracefs: Several updates
cover libtracefs: Several updates
From: "Steven Rostedt (Google)" <[email protected]>
Spent the days between Christmas and New Years doing some updates to the libtracefs library!
Steven Rostedt (Google) (23):
libtracefs Documentation: Fix tracefs_event_file_exists() issues libtracefs testing: Use one tep handle for most tests
libtracefs: Free "missed_followers" of instance
libtracefs: Free buf in clear_func_filter()
libtracefs: Free tracing_dir in case of remount
libtracefs: Free dynamic event list in utest
libtracefs: Reset tracing before and after unit tests
libtracefs: Add API to remove followers from an instance or toplevel
libtracefs: Increase splice to use pipe max size
libtracefs: Add tracefs_instance_file_write_number()
libtracefs: Add API to read tracefs_cpu and return a kbuffer
libtracefs: Add tracefs_instance_get/set_buffer_percent()
libtracefs: Add tracefs_instance_clear() API
libtracefs utest: Add test to test
tracefs_instance_set/get_buffer_percent()
libtracefs: Add kerneldoc comments to
tracefs_instance_set_buffer_size()
libtracefs: Add tracefs_load_headers() API
libtracefs: Add API to extract ring buffer statistics
libtracefs: Add tracefs_instance_set/get_subbuf_size()
libtracefs: Add ring buffer memory mapping APIs
libtracefs: Add TIMESTAMP_USECS_DELTA to simplify SQL timestamp
compares
libtracefs: Also clear max_graph_depth on reset
libtracefs: Add PID filtering API
libtracefs: Add updating and reading snapshot buffers
Documentation/libtracefs-cpu-buf.txt | 171 ++++ Documentation/libtracefs-cpu-map.txt | 194 +++++ Documentation/libtracefs-cpu-open.txt | 16 +- Documentation/libtracefs-cpu.txt | 3 + Documentation/libtracefs-events-file.txt | 3 +- Documentation/libtracefs-events-tep.txt | 7 +- Documentation/libtracefs-filter-pid.txt | 181 +++++ .../libtracefs-instances-file-manip.txt | 9 +- Documentation/libtracefs-instances-manage.txt | 9 +- Documentation/libtracefs-instances-stat.txt | 183 +++++ Documentation/libtracefs-instances-subbuf.txt | 152 ++++ Documentation/libtracefs-instances-utils.txt | 28 +- Documentation/libtracefs-iterator.txt | 38 +- Documentation/libtracefs-sql.txt | 16 + Documentation/libtracefs.txt | 46 ++ include/tracefs-local.h | 17 + include/tracefs.h | 58 ++ samples/Makefile | 4 + src/Makefile | 2 + src/tracefs-events.c | 218 ++++- src/tracefs-filter.c | 132 +++ src/tracefs-instance.c | 84 ++ src/tracefs-mmap.c | 190 +++++ src/tracefs-record.c | 295 ++++++- src/tracefs-sqlhist.c | 64 +- src/tracefs-stats.c | 162 ++++ src/tracefs-utils.c | 30 +- utest/tracefs-utest.c | 755 +++++++++++++++++- 28 files changed, 2972 insertions(+), 95 deletions(-) create mode 100644 Documentation/libtracefs-cpu-buf.txt create mode 100644 Documentation/libtracefs-cpu-map.txt create mode 100644 Documentation/libtracefs-filter-pid.txt create mode 100644 Documentation/libtracefs-instances-stat.txt create mode 100644 Documentation/libtracefs-instances-subbuf.txt create mode 100644 src/tracefs-mmap.c create mode 100644 src/tracefs-stats.c
Signature · 1 line
2.42.0
PATCH 1/23 libtracefs Documentation: Fix tracefs_event_file_exists() issues
From: "Steven Rostedt (Google)" <[email protected]>
In the man pages, tracefs_event_file_exists() is missing a semicolon and in the main man page, it's missing it's last parameter too.
--- Documentation/libtracefs-events-file.txt | 3 +-- Documentation/libtracefs.txt | 1 + 2 files changed, 2 insertions(+), 2 deletions(-)
Documentation/libtracefs-events-file.txt +1 -2
@@ -23,8 +23,7 @@ int *tracefs_event_file_append*(struct tracefs_instance pass:[*]_instance_, cons int *tracefs_event_file_clear*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, const char pass:[*]_file_); bool *tracefs_event_file_exists*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, - const char pass:[*]_file_) - + const char pass:[*]_file_); -- DESCRIPTION
Documentation/libtracefs.txt +1 -0
@@ -87,6 +87,7 @@ Trace events: int *tracefs_event_file_clear*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, const char pass:[*]_file_); bool *tracefs_event_file_exists*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, + const char pass:[*]_file_); Event filters: int *tracefs_filter_string_append*(struct tep_event pass:[*]_event_, char pass:[**]_filter_,
Signature · 1 line
2.42.0
PATCH 2/23 libtracefs testing: Use one tep handle for most tests
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Creating a tep handle with all events takes a bit of time. Instead of recreating one for every test, have the main test_tep handle load all events and use that for all tests that do not need to add special events (like the dynamic event tests).
--- utest/tracefs-utest.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-)
utest/tracefs-utest.c +4 -15
@@ -366,8 +366,7 @@ static void test_instance_trace_sql(struct tracefs_instance *instance)struct tep_event *event;int ret; - tep =tracefs_local_events (NULL ); -CU_TEST (tep !=NULL ); + tep = test_tep;trace_seq_init (&seq);
@@ -409,7 +408,6 @@ static void test_instance_trace_sql(struct tracefs_instance *instance)trace_seq_reset (&seq); } -tep_free (tep);trace_seq_destroy (&seq); }
@@ -436,7 +434,6 @@ struct test_cpu_data {static void cleanup_trace_cpu (struct test_cpu_data *data) {close (data->fd); -tep_free (data->tep);tracefs_cpu_close (data->tcpu);free (data->buf);kbuffer_free (data->kbuf);
@@ -467,10 +464,7 @@ static int setup_trace_cpu(struct tracefs_instance *instance, struct test_cpu_daif (data->fd <0 )return -1 ; - data->tep =tracefs_local_events (NULL ); -CU_TEST (data->tep !=NULL ); -if (!data->tep) - goto fail; + data->tep = test_tep; data->tcpu =tracefs_cpu_open (instance,0 , true);CU_TEST (data->tcpu !=NULL );
@@ -685,10 +679,7 @@ static void test_instance_follow_events(struct tracefs_instance *instance)memset (&fdata,0 ,sizeof (fdata)); - tep =tracefs_local_events (NULL ); -CU_TEST (tep !=NULL ); -if (!tep) -return ; + tep = test_tep; fdata.sched_switch =tep_find_event_by_name (tep,"sched" ,"sched_switch" );CU_TEST (fdata.sched_switch !=NULL );
@@ -2529,9 +2520,7 @@ static int test_suite_destroy(void)static int test_suite_init (void ) { -const char *systems[] = {"ftrace" ,NULL }; - - test_tep =tracefs_local_events_system (NULL , systems); + test_tep =tracefs_local_events (NULL );if (test_tep ==NULL )return 1 ; test_instance =tracefs_instance_create (TEST_INSTANCE_NAME );
Signature · 1 line
2.42.0
PATCH 3/23 libtracefs: Free "missed_followers" of instance
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
The missed_followers array was allocated but not freed in the clean up of the instance.
--- src/tracefs-instance.c | 1 + 1 file changed, 1 insertion(+)
src/tracefs-instance.c +1 -0
@@ -124,6 +124,7 @@ __hidden void trace_put_instance(struct tracefs_instance *instance)free (instance->trace_dir);free (instance->followers); +free (instance->missed_followers);free (instance->name);pthread_mutex_destroy (&instance->lock);free (instance);
Signature · 1 line
2.42.0
PATCH 4/23 libtracefs: Free buf in clear_func_filter()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
The buf variable was allocated via tracefs_instance_file_read() but must be freed with free().
--- src/tracefs-instance.c | 1 + 1 file changed, 1 insertion(+)
src/tracefs-instance.c +1 -0
@@ -1371,6 +1371,7 @@ static void clear_func_filter(struct tracefs_instance *instance, const char *fil filter[len+1 ] ='\0' ;tracefs_instance_file_append (instance, file, filter); } +free (buf); }static void clear_func_filters (struct tracefs_instance *instance)
Signature · 1 line
2.42.0
PATCH 5/23 libtracefs: Free tracing_dir in case of remount
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
On remount, the tracing directory may move, and the tracing_dir needs to be updated. If tracing_dir is the same it is simply returned, but if it is not, it has to be calculated again. This was fixed but if it was changed, the old tracing_dir was never freed.
Always free the tracing_dir before calculating a new one. This also requires changing the type by removing the "const" attribute.
--- src/tracefs-utils.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
src/tracefs-utils.c +2 -1
@@ -248,7 +248,7 @@ static int test_dir(const char *dir, const char *file)*/ const char *tracefs_tracing_dir (void ) { -static const char *tracing_dir; +static char *tracing_dir;/* Do not check custom_tracing_dir */ if (custom_tracing_dir)
@@ -257,6 +257,7 @@ const char *tracefs_tracing_dir(void)if (tracing_dir &&test_dir (tracing_dir,"trace" ))return tracing_dir; +free (tracing_dir); tracing_dir =find_tracing_dir (false, true);return tracing_dir; }
Signature · 1 line
2.42.0
PATCH 6/23 libtracefs: Free dynamic event list in utest
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
When adding the tests for the tracefs_kprobe_destroy() API, the call to get_dynevents_check() did not save the results and free it. This check can only be ignored if the expected result is zero (passed in as the second parameter), as in that case, the return would be NULL.
--- utest/tracefs-utest.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
utest/tracefs-utest.c +6 -2
@@ -1364,7 +1364,9 @@ static void test_kprobes_instance(struct tracefs_instance *instance) ret =tracefs_kprobe_destroy (ktests[i].system, ktests[i].event, ktests[i].address, ktests[i].format, true);CU_TEST (ret ==0 ); -get_dynevents_check (TRACEFS_DYNEVENT_KPROBE , kprobe_count - (i +1 )); + devents =get_dynevents_check (TRACEFS_DYNEVENT_KPROBE , + kprobe_count - (i +1 )); +tracefs_dynevent_list_free (devents); }get_dynevents_check (TRACEFS_DYNEVENT_KPROBE ,0 );
@@ -1372,7 +1374,9 @@ static void test_kprobes_instance(struct tracefs_instance *instance) ret =tracefs_kprobe_destroy (kretests[i].system, kretests[i].event, kretests[i].address, kretests[i].format, true);CU_TEST (ret ==0 ); -get_dynevents_check (TRACEFS_DYNEVENT_KRETPROBE , kretprobe_count - (i +1 )); + devents =get_dynevents_check (TRACEFS_DYNEVENT_KRETPROBE , + kretprobe_count - (i +1 )); +tracefs_dynevent_list_free (devents); }get_dynevents_check (TRACEFS_DYNEVENT_KRETPROBE ,0 );
Signature · 1 line
2.42.0
PATCH 7/23 libtracefs: Reset tracing before and after unit tests
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
After having tests constantly fail because of some left over tracing that was still enabled before the test started, call tracefs_instance_reset() before and after the unit tests to make sure it starts with a clean slate and does not fail due to residual tracing that was from an external event.
--- utest/tracefs-utest.c | 4 ++++ 1 file changed, 4 insertions(+)
utest/tracefs-utest.c +4 -0
@@ -2516,6 +2516,7 @@ static void test_custom_trace_dir(void)static int test_suite_destroy (void ) { +tracefs_instance_reset (NULL );tracefs_instance_destroy (test_instance);tracefs_instance_free (test_instance);tep_free (test_tep);
@@ -2531,6 +2532,9 @@ static int test_suite_init(void)if (!test_instance)return 1 ; +/* Start with a new slate */ +tracefs_instance_reset (NULL ); +return 0 ; }
Signature · 1 line
2.42.0
PATCH 8/23 libtracefs: Add API to remove followers from an instance or toplevel
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add the APIs:
tracefs_follow_event_clear()
tracefs_follow_missed_events_clear()
When adding some more tracefs unit tests, the follower callbacks were being called unexpectedly and causing failures and crashes, as the data used by the callbacks was already freed by the time it hit the other test.
Add a way to remove the followers and do so in the unit tests.
--- Documentation/libtracefs-iterator.txt | 30 +++- include/tracefs.h | 4 + src/tracefs-events.c | 103 ++++++++++++ utest/tracefs-utest.c | 230 ++++++++++++++++++++++++++ 4 files changed, 366 insertions(+), 1 deletion(-)
Documentation/libtracefs-iterator.txt +29 -1
@@ -3,7 +3,8 @@ libtracefs(3) NAME ---- -tracefs_iterate_raw_events, tracefs_iterate_stop, tracefs_follow_event, tracefs_follow_missed_events - Iterate over events in the ring buffer +tracefs_iterate_raw_events, tracefs_iterate_stop, tracefs_follow_event, tracefs_follow_missed_events, +tracefs_follow_event_clear, tracefs_follow_missed_events_clear - Iterate over events in the ring buffer SYNOPSIS --------
@@ -28,6 +29,10 @@ int *tracefs_follow_missed_events*(struct tracefs_instance pass:[*]_instance_, struct tep_record pass:[*], int, void pass:[*]), void pass:[*]_callback_data_); + +int *tracefs_follow_event_clear*(struct tracefs_instance pass:[*]_instance_, + const char pass:[*]_system_, const char pass:[*]_event_name_); +int *tracefs_follow_missed_events_clear*(struct tracefs_instance pass:[*]_instance_); -- DESCRIPTION
@@ -68,11 +73,24 @@ record that came after the missed events and _event_ will be of the type of event _record_ is. _cpu_ will be set to the CPU that missed the events, and _callback_data_ will be the content that was passed in to the function. +The *tracefs_follow_event_clear()* will remove followers from _instance_ that +match _system_ and _event_name_. If _system_ and _event_name_ are both NULL, +then it will remove all event followers associated to _instance_. If just _system_ +is NULL, then it will remove all followers that follow events that match _event_name_. If just _event_name_ +is NULL, then it will remove all followers that are attached to events that are +apart of a system that matches _system_. + +The *tracefs_follow_missed_events_clear()* will remove all followers for missed +events. + RETURN VALUE ------------ The *tracefs_iterate_raw_events()* function returns -1 in case of an error or 0 otherwise. +Both *tracefs_follow_event_clear()* and *tracefs_follow_missed_events_clear()* return +0 on success and -1 on error, or if it found no followers that match and should be removed. + EXAMPLE ------- [source,c]
@@ -176,7 +194,17 @@ int main (int argc, char **argv, char **env) tracefs_follow_missed_events(instance, missed_callback, NULL); tracefs_follow_event(tep, instance, "sched", "sched_switch", sched_callback, &this_pid); tracefs_iterate_raw_events(tep, instance, NULL, 0, callback, &my_data); + + /* Note, the clear here is to show how to clear all followers + * in case tracefs_iterate_raw_events() is called again, but + * does not want to include the followers. It's not needed + * here because tracefs_instance_free() will clean them up. + */ + tracefs_follow_event_clear(instance, NULL, NULL); + tracefs_follow_missed_events_clear(instance); + tracefs_instance_destroy(instance); + tracefs_instance_free(instance); if (my_data.stopped) { if (counter > MAX_COUNT)
include/tracefs.h +4 -0
@@ -138,6 +138,10 @@ int tracefs_follow_missed_events(struct tracefs_instance *instance, struct tep_record *, int, void *), void *callback_data); +int tracefs_follow_event_clear (struct tracefs_instance *instance, +const char *system,const char *event_name); +int tracefs_follow_missed_events_clear (struct tracefs_instance *instance); + char *tracefs_event_get_file(struct tracefs_instance *instance, const char *system, const char *event,
src/tracefs-events.c +103 -0
109 lines not on this page · raw message
utest/tracefs-utest.c +230 -0
282 lines not on this page · raw message
Signature · 1 line
2.42.0
PATCH 9/23 libtracefs: Increase splice to use pipe max size
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
When a pipe is created for splicing to read the tracing ring buffer, instead of using the default size, which is usually around 65K, read /proc/sys/fs/pipe_max_size and try to set the pipe to use that size instead.
--- src/tracefs-record.c | 7 +++++++ 1 file changed, 7 insertions(+)
src/tracefs-record.c +7 -0
@@ -319,6 +319,7 @@ int tracefs_cpu_read(struct tracefs_cpu *tcpu, void *buffer, bool nonblock)static int init_splice (struct tracefs_cpu *tcpu) { +char *buf;int ret;if (tcpu->splice_pipe[0 ] >=0 )
@@ -328,6 +329,12 @@ static int init_splice(struct tracefs_cpu *tcpu)if (ret <0 )return ret; +if (str_read_file ("/proc/sys/fs/pipe-max-size" , &buf, false)) { +int size =atoi (buf); +fcntl (tcpu->splice_pipe[0 ],F_SETPIPE_SZ , &size); +free (buf); + } + ret =fcntl (tcpu->splice_pipe[0 ],F_GETPIPE_SZ , &tcpu->pipe_size);/* * F_GETPIPE_SZ was introduced in 2.6.35, ftrace was introduced
Signature · 1 line
2.42.0
PATCH 10/23 libtracefs: Add tracefs_instance_file_write_number()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add the helper function to write a number into a tracefs file. As tracefs files require writing strings and not binary numbers, this is a helper function that does the conversion to string followed by a tracefs_instance_file_write() on that string.
--- .../libtracefs-instances-file-manip.txt | 9 +++++- Documentation/libtracefs.txt | 1 + include/tracefs.h | 2 ++ src/tracefs-instance.c | 21 +++++++++++++ utest/tracefs-utest.c | 30 ++++++++++++++++++- 5 files changed, 61 insertions(+), 2 deletions(-)
Documentation/libtracefs-instances-file-manip.txt +8 -1
@@ -5,7 +5,7 @@ NAME ---- tracefs_instance_file_open, -tracefs_instance_file_write, tracefs_instance_file_append, tracefs_instance_file_clear, +tracefs_instance_file_write, tracefs_instance_file_write_number, tracefs_instance_file_append, tracefs_instance_file_clear, tracefs_instance_file_read, tracefs_instance_file_read_number - Work with files in tracing instances. SYNOPSIS
@@ -16,6 +16,7 @@ SYNOPSIS int *tracefs_instance_file_open*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int _mode_); int *tracefs_instance_file_write*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_); +int *tracefs_instance_file_write_number*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, size_t _val_); int *tracefs_instance_file_append*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_); int *tracefs_instance_file_clear*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_); char pass:[*]*tracefs_instance_file_read*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int pass:[*]_psize_);
@@ -38,6 +39,10 @@ The *tracefs_instance_file_write()* function writes a string _str_ in a _file_ f the given _instance_, without the terminating NULL character. When opening the file, this function tries to truncates the size of the file to zero, which clears all previously existing settings. +The *tracefs_instance_file_write_number()* function converts _val_ into a string +and then writes it to the given file. This is a helper function that does the number +conversion to string and then calls *tracefs_instance_file_write()*. + The *tracefs_instance_file_append()* function writes a string _str_ in a _file_ from the given _instance_, without the terminating NULL character. This function is similar to *tracefs_instance_file_write()*, but the existing content of the is not cleared. Thus the
@@ -61,6 +66,8 @@ closed with *close*(3). In case of an error, -1 is returned. The *tracefs_instance_file_write()* function returns the number of written bytes, or -1 in case of an error. +The *tracefs_instance_file_write_number()* function returns 0 on success and -1 on error. + The *tracefs_instance_file_append()* function returns the number of written bytes, or -1 in case of an error.
Documentation/libtracefs.txt +1 -0
@@ -33,6 +33,7 @@ Trace instances: char pass:[*]*tracefs_instance_get_dir*(struct tracefs_instance pass:[*]_instance_); int *tracefs_instance_file_open*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int _mode_); int *tracefs_instance_file_write*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_); + int *tracefs_instance_file_write_number*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, size_t _val_); int *tracefs_instance_file_append*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, const char pass:[*]_str_); int *tracefs_instance_file_clear*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_); char pass:[*]*tracefs_instance_file_read*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_file_, int pass:[*]_psize_);
include/tracefs.h +2 -0
@@ -36,6 +36,8 @@ tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);char *tracefs_instance_get_dir (struct tracefs_instance *instance);int tracefs_instance_file_write (struct tracefs_instance *instance,const char *file,const char *str); +int tracefs_instance_file_write_number (struct tracefs_instance *instance, +const char *file,size_t val);int tracefs_instance_file_append (struct tracefs_instance *instance,const char *file,const char *str); int tracefs_instance_file_clear(struct tracefs_instance *instance,
src/tracefs-instance.c +21 -0
@@ -493,6 +493,27 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,return instance_file_write (instance, file, str,O_WRONLY |O_TRUNC ); } +/** +* tracefs_instance_file_write_number - Write integer from a trace file. +* @instance: ftrace instance, can be NULL for the top instance +* @file: name of the file +* @res: The integer to write to @file +* +* Returns 0 if the write succeeds, -1 on error. +*/ +int tracefs_instance_file_write_number (struct tracefs_instance *instance, +const char *file,size_t val) +{ +char buf[64 ]; +int ret; + +snprintf (buf,64 ,"%zd\n" , val); + + ret =tracefs_instance_file_write (instance, file, buf); + +return ret >1 ?0 :-1 ; +} +/** * tracefs_instance_file_append - Append to a trace file of specific instance. * @instance: ftrace instance, can be NULL for the top instance.
utest/tracefs-utest.c +29 -1
@@ -488,7 +488,7 @@ static int setup_trace_cpu(struct tracefs_instance *instance, struct test_cpu_daif (!data->buf) goto fail; - data->kbuf =kbuffer_alloc (sizeof (long ) ==8 , !tep_is_bigendian ()); + data->kbuf =tep_kbuffer (data->tep);CU_TEST (data->kbuf !=NULL );if (!data->kbuf) goto fail;
@@ -2127,6 +2127,7 @@ static void test_instance_file_fd(struct tracefs_instance *instance)const char *name =get_rand_str ();const char *tdir =tracefs_instance_get_trace_dir (instance);long long res =-1 ; +long long res2;char rd[2 ];int fd;
@@ -2146,7 +2147,34 @@ static void test_instance_file_fd(struct tracefs_instance *instance)CU_TEST (read (fd, &rd,1 ) ==1 ); rd[1 ] =0 ;CU_TEST (res ==atoi (rd)); +close (fd); + +/* Inverse tracing_on and test changing it with write_number */ + res ^=1 ; +CU_TEST (tracefs_instance_file_write_number (instance,TRACE_ON , (size_t )res) ==0 ); + +CU_TEST (tracefs_instance_file_read_number (instance,TRACE_ON , &res2) ==0 ); +CU_TEST (res2 == res); + fd =tracefs_instance_file_open (instance,TRACE_ON ,O_RDONLY ); +CU_TEST (fd >=0 ); +CU_TEST (read (fd, &rd,1 ) ==1 ); + rd[1 ] =0 ; +CU_TEST (res2 ==atoi (rd)); +close (fd); + +/* Put back the result of tracing_on */ + res ^=1 ; + +CU_TEST (tracefs_instance_file_write_number (instance,TRACE_ON , (size_t )res) ==0 ); + +CU_TEST (tracefs_instance_file_read_number (instance,TRACE_ON , &res2) ==0 ); +CU_TEST (res2 == res); + fd =tracefs_instance_file_open (instance,TRACE_ON ,O_RDONLY ); +CU_TEST (fd >=0 ); +CU_TEST (read (fd, &rd,1 ) ==1 ); + rd[1 ] =0 ; +CU_TEST (res2 ==atoi (rd));close (fd); }
Signature · 1 line
2.42.0
PATCH 11/23 libtracefs: Add API to read tracefs_cpu and return a kbuffer
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Instead of having the caller have to allocate a buffer and a kbuffer to read the content of a tracefs_cpu, add an interface to allow the tracefs_cpu to handle this for the application.
The following three functions are added to the API:
tracefs_cpu_read_buf()
tracefs_cpu_buffered_read_buf()
tracefs_cpu_flush_buf()
That act the same as the corresponding functions with the same name minus the ending "_buf", but instead of requiring to send a buffer in and then add it to a kbuffer element to read, just return a kbuffer structure that can be used to read.
--- Documentation/libtracefs-cpu-buf.txt | 171 +++++++++++++++++++++++++++ Documentation/libtracefs-cpu.txt | 3 + Documentation/libtracefs.txt | 3 + include/tracefs.h | 4 + samples/Makefile | 1 + src/tracefs-record.c | 116 ++++++++++++++++++ 6 files changed, 298 insertions(+) create mode 100644 Documentation/libtracefs-cpu-buf.txt
Documentation/libtracefs-cpu-buf.txt new file +171 -0
@@ -0,0 +1,171 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_cpu_read_buf, tracefs_cpu_buffered_read_buf, tracefs_cpu_flush_buf +- Reading trace_pipe_raw data returning a kbuffer + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +struct kbuffer pass:[*]*tracefs_cpu_read_buf*(struct tracefs_cpu pass:[*]_tcpu_, bool _nonblock_); +struct kbuffer pass:[*]*tracefs_cpu_buffered_read_buf*(struct tracefs_cpu pass:[*]_tcpu_, bool _nonblock_); +struct kbuffer pass:[*]*tracefs_cpu_flush_buf*(struct tracefs_cpu pass:[*]_tcpu_); +-- + +DESCRIPTION +----------- +This set of APIs can be used to read the raw data from the trace_pipe_raw +files in the tracefs file system and return a kbuffer structure to read it with. + +The *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. + +The *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. + +The *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. + +RETURN VALUE +------------ +The functions *tracefs_cpu_read_buf()*, tracefs_cpu_buffered_read_buf()* and +*tracefs_cpu_flush()* returns 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 +------- +[source,c] +-- +#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 +----- +[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 +-------- +*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) + +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 +3 -0
@@ -212,6 +212,9 @@ SEE ALSO -------- *tracefs_cpu_open*(3) *tracefs_cpu_close*(3) +*tracefs_cpu_read_buf*(3) +*tracefs_cpu_buffered_read_buf*(3) +*tracefs_cpu_flush_buf*(3) *libtracefs*(3), *libtraceevent*(3), *trace-cmd*(1)
Documentation/libtracefs.txt +3 -0
@@ -304,6 +304,9 @@ Recording of trace_pipe_raw files: 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_); + struct kbuffer pass:[*]*tracefs_cpu_read_buf*(struct tracefs_cpu pass:[*]_tcpu_, bool _nonblock_); + struct kbuffer pass:[*]*tracefs_cpu_buffered_read_buf*(struct tracefs_cpu pass:[*]_tcpu_, bool _nonblock_); + struct kbuffer pass:[*]*tracefs_cpu_flush_buf*(struct tracefs_cpu pass:[*]_tcpu_); Helper functions for guest tracing: char pass:[*]*tracefs_find_cid_pid*(int _cid_);
include/tracefs.h +4 -0
@@ -9,6 +9,7 @@#include <fcntl.h> #include <sched.h> #include <event-parse.h> +#include <kbuffer.h> char *tracefs_get_tracing_file (const char *name);void tracefs_put_tracing_file (char *name);
@@ -641,10 +642,13 @@ 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); +struct kbuffer *tracefs_cpu_read_buf (struct tracefs_cpu *tcpu,bool nonblock);int tracefs_cpu_buffered_read (struct tracefs_cpu *tcpu,void *buffer,bool nonblock); +struct kbuffer *tracefs_cpu_buffered_read_buf (struct tracefs_cpu *tcpu,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); +struct kbuffer *tracefs_cpu_flush_buf (struct tracefs_cpu *tcpu);int tracefs_cpu_flush_write (struct tracefs_cpu *tcpu,int wfd);int tracefs_cpu_pipe (struct tracefs_cpu *tcpu,int wfd,bool nonblock);
samples/Makefile +1 -0
@@ -23,6 +23,7 @@ EXAMPLES += stream EXAMPLES += instances-affinity EXAMPLES += cpu EXAMPLES += guest +EXAMPLES += cpu-buf TARGETS := TARGETS += sqlhist
src/tracefs-record.c +116 -0
164 lines not on this page · raw message
Signature · 1 line
2.42.0
PATCH 12/23 libtracefs: Add tracefs_instance_get/set_buffer_percent()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add an API that allows modifying the buffer_percent of the tracing ring buffer. That is the amount that is written into the ring buffer before the trace is woken up.
--- Documentation/libtracefs-instances-utils.txt | 28 +++++++++++++++++++- Documentation/libtracefs.txt | 2 ++ include/tracefs.h | 3 +++ src/tracefs-utils.c | 27 +++++++++++++++++++ 4 files changed, 59 insertions(+), 1 deletion(-)
Documentation/libtracefs-instances-utils.txt +27 -1
@@ -4,7 +4,8 @@ libtracefs(3) NAME ---- tracefs_instance_get_name, tracefs_instance_get_trace_dir, tracefs_instances_walk, tracefs_instance_exists, -tracefs_instance_get_buffer_size, tracefs_instance_set_buffer_size - Helper functions for working with tracing instances. +tracefs_instance_get_buffer_size, tracefs_instance_set_buffer_size, tracefs_instance_get_buffer_percent, +tracefs_instance_set_buffer_percent - Helper functions for working with tracing instances. SYNOPSIS --------
@@ -18,6 +19,8 @@ int *tracefs_instances_walk*(int (pass:[*]_callback_)(const char pass:[*], void bool *tracefs_instance_exists*(const char pass:[*]_name_); size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); int *tracefs_instance_set_buffer_size*(struct tracefs_instance pass:[*]_instance_, size_t _size_, int _cpu_); +int *tracefs_instance_get_buffer_percent*(struct tracefs_instance pass:[*]_instance_); +int *tracefs_instance_set_buffer_percent*(struct tracefs_instance pass:[*]_instance_, int _val_); -- DESCRIPTION
@@ -48,6 +51,29 @@ If _cpu_ is negative, then it sets all the per CPU ring buffers to _size_ (note the total size is the number of CPUs * _size_). If _cpu_ is specified, then it only sets the size of the per CPU ring buffer. +The *tracefs_instance_set_buffer_percent()* sets the buffer percent value of +the tracing ring buffer for _instance_ or the top level buffer if _instance_ is +NULL. The buffer percent decides when readers on *tracefs_cpu_read*(3), +*tracefs_cpu_buffered_read*(3), *tracefs_cpu_write*(3) and *tracefs_cpu_pipe*(3) +will block when O_NONBLOCK is not set. The value of _val_ must be between 0 and +100, where: + +[verse] +-- + 0 - block until there's any data in the ring buffer + 1 - block until 1% of the ring buffer sub-buffers are filled + 50 - block until 50% of the ring buffer sub-buffers are filled + 100 - block until the entire ring buffer is filled +-- + +Note, any number from 0 to 100 can be used where it is the percentage of the +ring buffer that must be filled before a blocked reader will be notified that +there's data to be retrieved. + +The *tracefs_instance_get_buffer_percent()* retrieves the current buffer percent +setting of the tracing ring buffer for _instance_ or the top level buffer +if _instance_ is NULL. + RETURN VALUE ------------ The *tracefs_instance_get_name()* returns a string or NULL in case of the top
Documentation/libtracefs.txt +2 -0
@@ -50,6 +50,8 @@ Trace instances: char pass:[*]*tracefs_instance_get_affinity_raw*(struct tracefs_instance pass:[*]_instance_); size_t *tracefs_instance_get_buffer_size*(struct tracefs_instance pass:[*]_instance_, int _cpu_); int *tracefs_instance_set_buffer_size*(struct tracefs_instance pass:[*]_instance_, size_t _size_, int _cpu_); + int *tracefs_instance_get_buffer_percent*(struct tracefs_instance pass:[*]_instance_); + int *tracefs_instance_set_buffer_percent*(struct tracefs_instance pass:[*]_instance_, int _val_); Trace events: char pass:[*]pass:[*]*tracefs_event_systems*(const char pass:[*]_tracing_dir_);
include/tracefs.h +3 -0
@@ -64,6 +64,9 @@ ssize_t tracefs_instance_get_buffer_size(struct tracefs_instance *instance, intint tracefs_instance_set_buffer_size (struct tracefs_instance *instance,size_t size,int cpu);char **tracefs_instances (const char *regex); +int tracefs_instance_get_buffer_percent (struct tracefs_instance *instance); +int tracefs_instance_set_buffer_percent (struct tracefs_instance *instance,int val); +bool tracefs_instance_exists (const char *name);bool tracefs_file_exists (struct tracefs_instance *instance,const char *name);bool tracefs_dir_exists (struct tracefs_instance *instance,const char *name);
src/tracefs-utils.c +27 -0
@@ -643,3 +643,30 @@ bool tracefs_tracer_available(const char *tracing_dir, const char *tracer)tracefs_list_free (tracers);return ret; } + +/** +* tracefs_instance_get_buffer_percent - get the instance buffer percent +* @instance: The instance to get from (NULL for toplevel) +* +* Returns the buffer percent setting of the given instance. +* (-1 if not found). +*/ +int tracefs_instance_get_buffer_percent (struct tracefs_instance *instance) +{ +long long val; +int ret; + + ret =tracefs_instance_file_read_number (instance,"buffer_percent" , &val); +return !ret ? (int )val : ret; +} + +/** +* tracefs_instance_set_buffer_percent - set the instance buffer percent +* @instance: The instance to set (NULL for toplevel) +* +* Returns zero on success or -1 on error +*/ +int tracefs_instance_set_buffer_percent (struct tracefs_instance *instance,int val) +{ +return tracefs_instance_file_write_number (instance,"buffer_percent" , val); +}
Signature · 1 line
2.42.0
PATCH 13/23 libtracefs: Add tracefs_instance_clear() API
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add tracefs_instance_clear() that will clear the ring buffer of a given instance or the top level ring buffer if NULL is passed to it.
--- Documentation/libtracefs-instances-manage.txt | 9 ++++++++- Documentation/libtracefs.txt | 1 + include/tracefs.h | 1 + src/tracefs-instance.c | 11 +++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-)
Documentation/libtracefs-instances-manage.txt +8 -1
@@ -4,7 +4,7 @@ libtracefs(3) NAME ---- tracefs_instance_create, tracefs_instance_destroy, tracefs_instance_alloc, tracefs_instance_free, -tracefs_instance_is_new, tracefs_instances, tracefs_instance_reset - Manage trace instances. +tracefs_instance_is_new, tracefs_instances, tracefs_instance_clear, tracefs_instance_reset - Manage trace instances. SYNOPSIS --------
@@ -18,6 +18,7 @@ struct tracefs_instance pass:[*]*tracefs_instance_alloc*(const char pass:[*]_tra void *tracefs_instance_free*(struct tracefs_instance pass:[*]_instance_); bool *tracefs_instance_is_new*(struct tracefs_instance pass:[*]_instance_); char pass:[**]*tracefs_instances*(const char pass:[*]_regex_); +void *tracefs_instance_clear*(struct tracefs_instance pass:[*]_instance_); void *tracefs_instance_reset*(struct tracefs_instance pass:[*]_instance_); --
@@ -61,6 +62,9 @@ it will match all instances that exist. The returned list must be freed with *tracefs_list_free*(3). Note, if no instances are found an empty list is returned and that too needs to be free with *tracefs_list_free*(3). +The *tracefs_instance_clear()* function clears the ring buffer of the given _instance_ +or the top level ring buffer if _instance_ is NULL. + The *tracefs_instance_reset*() function resets the given _instance_ to its default state. RETURN VALUE
@@ -83,6 +87,9 @@ The list must be freed with *tracefs_list_free*(3). An empty list is returned if no instance exists that matches _regex_, and this needs to be freed with *tracefs_list_free*(3) as well. NULL is returned on error. +The *tracefs_instance_clear()* returns 0 if it successfully cleared the ring buffer, +or -1 on error. + EXAMPLE ------- [source,c]
Documentation/libtracefs.txt +1 -0
@@ -25,6 +25,7 @@ Trace instances: struct tracefs_instance pass:[*]*tracefs_instance_alloc*(const char pass:[*]_tracing_dir_, const char pass:[*]_name_); void *tracefs_instance_free*(struct tracefs_instance pass:[*]_instance_); char pass:[**]*tracefs_instances*(const char pass:[*]_regex_); + void *tracefs_instance_clear*(struct tracefs_instance pass:[*]_instance_); void *tracefs_instance_reset*(struct tracefs_instance pass:[*]_instance_); bool *tracefs_instance_is_new*(struct tracefs_instance pass:[*]_instance_); bool *tracefs_file_exists*(struct tracefs_instance pass:[*]_instance_, char pass:[*]_name_);
include/tracefs.h +1 -0
@@ -25,6 +25,7 @@ struct tracefs_instance;void tracefs_instance_free (struct tracefs_instance *instance);void tracefs_instance_reset (struct tracefs_instance *instance); +int tracefs_instance_clear (struct tracefs_instance *instance);struct tracefs_instance *tracefs_instance_create (const char *name);struct tracefs_instance *tracefs_instance_alloc (const char *tracing_dir,const char *name);
src/tracefs-instance.c +11 -0
@@ -1409,6 +1409,17 @@ static void clear_func_filters(struct tracefs_instance *instance)clear_func_filter (instance, files[i]); } +/** +* tracefs_instance_clear - clear the trace buffer +* @instance: The instance to clear the trace for. +* +* Returns 0 on succes, -1 on error +*/ +int tracefs_instance_clear (struct tracefs_instance *instance) +{ +return tracefs_instance_file_clear (instance,"trace" ); +} +/** * tracefs_instance_reset - Reset a ftrace instance to its default state * @instance - a ftrace instance to be reseted
Signature · 1 line
2.42.0
PATCH 14/23 libtracefs utest: Add test to test tracefs_instance_set/get_buffer_percent()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add a utest that tests different "buffer_percent" values using the tracefs_instance_set_buffer_percent() API. It also will test the tracefs_instance_get_buffer_percent().
--- utest/tracefs-utest.c | 163 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 154 insertions(+), 9 deletions(-)
utest/tracefs-utest.c +154 -9
@@ -53,6 +53,8 @@#define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"#define TRACEFS_DEFAULT2_PATH "/sys/kernel/debug/tracing" +static pthread_barrier_t trace_barrier; +static struct tracefs_instance *test_instance;static struct tep_handle *test_tep;struct test_sample {
@@ -435,6 +437,7 @@ struct test_cpu_data {void *buf;int events_per_buf;int bufsize; +int nr_subbufs;int data_size;int this_pid;int fd;
@@ -452,11 +455,21 @@ static void cleanup_trace_cpu(struct test_cpu_data *data)#define EVENT_SYSTEM "syscalls"#define EVENT_NAME "sys_enter_getppid" -static int setup_trace_cpu (struct tracefs_instance *instance,struct test_cpu_data *data) +static int make_trace_temp_file (void ) +{ +char tmpfile[] ="/tmp/utest-libtracefsXXXXXX" ; +int fd; + + fd =mkstemp (tmpfile); +unlink (tmpfile); +return fd; +} + +static int setup_trace_cpu (struct tracefs_instance *instance,struct test_cpu_data *data,bool nonblock) {struct tep_format_field **fields;struct tep_event *event; -char tmpfile[] ="/tmp/utest-libtracefsXXXXXX" ; +ssize_t buffer_size;int max =0 ;int ret;int i;
@@ -468,20 +481,26 @@ static int setup_trace_cpu(struct tracefs_instance *instance, struct test_cpu_da data->instance = instance; - data->fd =mkstemp (tmpfile); + data->fd =make_trace_temp_file ();CU_TEST (data->fd >=0 ); -unlink (tmpfile);if (data->fd <0 )return -1 ; data->tep = test_tep; - data->tcpu =tracefs_cpu_open (instance,0 , true); + data->tcpu =tracefs_cpu_open (instance,0 , nonblock);CU_TEST (data->tcpu !=NULL );if (!data->tcpu) goto fail; data->bufsize =tracefs_cpu_read_size (data->tcpu); +CU_TEST (data->bufsize >0 ); + + data->data_size =tep_get_sub_buffer_data_size (data->tep); +CU_TEST (data->data_size >0 ); + + buffer_size =tracefs_instance_get_buffer_size (instance,0 ) *1024 ; + data->nr_subbufs = buffer_size/ data->data_size; data->buf =calloc (1 , data->bufsize);CU_TEST (data->buf !=NULL );
@@ -493,8 +512,6 @@ static int setup_trace_cpu(struct tracefs_instance *instance, struct test_cpu_daif (!data->kbuf) goto fail; - data->data_size = data->bufsize -kbuffer_start_of_data (data->kbuf); -tracefs_instance_file_clear (instance,"trace" ); event =tep_find_event_by_name (data->tep,EVENT_SYSTEM ,EVENT_NAME );
@@ -518,6 +535,12 @@ static int setup_trace_cpu(struct tracefs_instance *instance, struct test_cpu_daif (!max) goto fail; +/* round up to long size alignment */ + max = ((max +sizeof (long ) -1 )) & ~(sizeof (long ) -1 ); + +/* Add meta header */ + max +=4 ; + data->events_per_buf = data->data_size / max; data->this_pid =getpid ();
@@ -549,6 +572,17 @@ static void shutdown_trace_cpu(struct test_cpu_data *data)cleanup_trace_cpu (data); } +static void reset_trace_cpu (struct test_cpu_data *data,bool nonblock) +{ +close (data->fd); +tracefs_cpu_close (data->tcpu); + + data->fd =make_trace_temp_file (); +CU_TEST (data->fd >=0 ); + data->tcpu =tracefs_cpu_open (data->instance,0 , nonblock); +CU_TEST (data->tcpu !=NULL ); +} +static void call_getppid (int cnt) {int i;
@@ -597,7 +631,7 @@ static void test_instance_trace_cpu_read(struct tracefs_instance *instance) {struct test_cpu_data data; -if (setup_trace_cpu (instance, &data)) +if (setup_trace_cpu (instance, &data, true))return ;test_cpu_read (&data,1 );
@@ -615,6 +649,115 @@ static void test_trace_cpu_read(void)test_instance_trace_cpu_read (test_instance); } +static void *trace_cpu_read_thread (void *arg) +{ +struct test_cpu_data *data = arg; +struct tracefs_cpu *tcpu = data->tcpu; +struct kbuffer *kbuf; +long ret =0 ; + +pthread_barrier_wait (&trace_barrier); + + kbuf =tracefs_cpu_read_buf (tcpu, false); +CU_TEST (kbuf !=NULL ); + data->done = true; + +return (void *)ret; +} + +static void test_cpu_read_buf_percent (struct test_cpu_data *data,int percent) +{ +pthread_t thread; +int save_percent; +ssize_t expect; +int ret; + +tracefs_instance_clear (data->instance); + + save_percent =tracefs_instance_get_buffer_percent (data->instance); +CU_TEST (save_percent >=0 ); + + ret =tracefs_instance_set_buffer_percent (data->instance, percent); +CU_TEST (ret ==0 ); + + data->done = false; + +pthread_barrier_init (&trace_barrier,NULL ,2 ); + +pthread_create (&thread,NULL , trace_cpu_read_thread, data); + +pthread_barrier_wait (&trace_barrier); + +msleep (100 ); + +CU_TEST (data->done == false); + +/* For percent == 0, just test for any data */ +if (percent) { + expect = data->nr_subbufs * data->events_per_buf * percent /100 ; + +/* Add just under the percent */ + expect -= data->events_per_buf; +CU_TEST (expect >0 ); + +call_getppid (expect); + +msleep (100 ); + +CU_TEST (data->done == false); + +/* Add just over the percent */ + expect = data->events_per_buf *2 ; + }else { + expect = data->events_per_buf; + } + +call_getppid (expect); + +msleep (100 ); + +CU_TEST (data->done == true); + +while (tracefs_cpu_flush_buf (data->tcpu)) + ; + +tracefs_cpu_stop (data->tcpu); +pthread_join (thread,NULL ); + + ret =tracefs_instance_set_buffer_percent (data->instance, save_percent); +CU_TEST (ret ==0 ); +} + +static void test_instance_trace_cpu_read_buf_percent (struct tracefs_instance *instance) +{ +struct test_cpu_data data; + +if (setup_trace_cpu (instance, &data, false)) +return ; + +test_cpu_read_buf_percent (&data,0 ); + +reset_trace_cpu (&data, false); + +test_cpu_read_buf_percent (&data,1 ); + +reset_trace_cpu (&data, false); + +test_cpu_read_buf_percent (&data,50 ); + +reset_trace_cpu (&data, false); + +test_cpu_read_buf_percent (&data,100 ); + +shutdown_trace_cpu (&data); +} + +static void test_trace_cpu_read_buf_percent (void ) +{ +test_instance_trace_cpu_read_buf_percent (NULL ); +test_instance_trace_cpu_read_buf_percent (test_instance); +} +struct follow_data {struct tep_event *sched_switch;struct tep_event *sched_waking;
@@ -1152,7 +1295,7 @@ static void test_instance_trace_cpu_pipe(struct tracefs_instance *instance) {struct test_cpu_data data; -if (setup_trace_cpu (instance, &data)) +if (setup_trace_cpu (instance, &data, true))return ;test_cpu_pipe (&data,1 );
@@ -2808,6 +2951,8 @@ void test_tracefs_lib(void)CU_add_test (suite,"Test tracefs/debugfs mounting" , test_mounting);CU_add_test (suite,"trace cpu read" , test_trace_cpu_read); +CU_add_test (suite,"trace cpu read_buf_percent" , + test_trace_cpu_read_buf_percent);CU_add_test (suite,"trace cpu pipe" , test_trace_cpu_pipe); CU_add_test(suite, "trace sql",
Signature · 1 line
2.42.0
PATCH 15/23 libtracefs: Add kerneldoc comments to tracefs_instance_set_buffer_size()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
When the API tracefs_instance_set_buffer_size() was added, the kerneldoc associated to it was not.
--- src/tracefs-instance.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
src/tracefs-instance.c +12 -0
@@ -402,6 +402,18 @@ ssize_t tracefs_instance_get_buffer_size(struct tracefs_instance *instance, intreturn size; } +/** +* tracefs_instance_set_buffer_size - modify the ring buffer size +* @instance: The instance to modify (NULL for the top level) +* @size: The size in kilobytes to to set the size to +* @cpu: the CPU to set it to (-1 for all CPUs) +* +* Sets the size of the ring buffer per CPU buffers. If @cpu is negative, +* then it sets the ring buffer size for all the per CPU buffers, otherwise +* it only sets the per CPU buffer specified by @cpu. +* +* Returns 0 on success and -1 on error. +*/ int tracefs_instance_set_buffer_size (struct tracefs_instance *instance,size_t size,int cpu) {char *path;
Signature · 1 line
2.42.0
PATCH 16/23 libtracefs: Add tracefs_load_headers() API
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Sometimes the only thing that is needed from the tracefs directory is how to parse the sub-buffers. The tracefs_fill_local_events() has a lot of overhead as it reads pretty much everything. But if the only thing needed is the header file parsing, add this helper function to do it.
--- Documentation/libtracefs-events-tep.txt | 7 ++++++- Documentation/libtracefs.txt | 1 + include/tracefs.h | 2 ++ src/tracefs-events.c | 22 ++++++++++++++++++++++ src/tracefs-record.c | 9 +-------- 5 files changed, 32 insertions(+), 9 deletions(-)
Documentation/libtracefs-events-tep.txt +6 -1
@@ -4,7 +4,7 @@ libtracefs(3) NAME ---- tracefs_local_events, tracefs_local_events_system, tracefs_fill_local_events, -tracefs_load_cmdlines - +tracefs_load_cmdlines, tracefs_load_headers - Initialize a tep handler with trace events from the local system. SYNOPSIS
@@ -17,6 +17,7 @@ struct tep_handle pass:[*]*tracefs_local_events*(const char pass:[*]_tracing_dir struct tep_handle pass:[*]*tracefs_local_events_system*(const char pass:[*]_tracing_dir_, const char pass:[*] const pass:[*]_sys_names_); int *tracefs_fill_local_events*(const char pass:[*]_tracing_dir_, struct tep_handle pass:[*]_tep_, int pass:[*]_parsing_failures_); int *tracefs_load_cmdlines*(const char pass:[*]_tracing_dir_, struct tep_handle pass:[*]_tep_); +int *tracefs_load_headers*(const char pass:[*]_tracing_dir_, struct tep_handle pass:[*]_tep_); -- DESCRIPTION
@@ -55,6 +56,10 @@ The *tracefs_load_cmdlines()* does just that. The _tracing_dir_ is the directory of the mount point to load from, or NULL to use the mount point of the tracefs file system. +The *tracefs_load_headers()* will reade the "header_page" of the events +directory that will update the _tep_ handle with information on how to parse the +tracing ring buffer sub-buffer. + RETURN VALUE ------------ The *tracefs_local_events()* and *tracefs_local_events_system()* functions
Documentation/libtracefs.txt +1 -0
@@ -80,6 +80,7 @@ Trace events: struct tep_handle pass:[*]*tracefs_local_events_system*(const char pass:[*]_tracing_dir_, const char pass:[*] const pass:[*]_sys_names_); int *tracefs_fill_local_events*(const char pass:[*]_tracing_dir_, struct tep_handle pass:[*]_tep_, int pass:[*]_parsing_failures_); int *tracefs_load_cmdlines*(const char pass:[*]_tracing_dir_, struct tep_handle pass:[*]_tep_); + int *tracefs_load_headers*(const char pass:[*]_tracing_dir_, struct tep_handle pass:[*]_tep_); char pass:[*]*tracefs_event_get_file*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, const char pass:[*]_file_); char pass:[*]*tracefs_event_file_read*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_,
include/tracefs.h +2 -0
@@ -180,6 +180,8 @@ int tracefs_fill_local_events(const char *tracing_dir,int tracefs_load_cmdlines (const char *tracing_dir,struct tep_handle *tep); +int tracefs_load_headers (const char *tracing_dir,struct tep_handle *tep); +char *tracefs_get_clock (struct tracefs_instance *instance);enum tracefs_option_id {
src/tracefs-events.c +22 -0
@@ -1216,6 +1216,28 @@ int tracefs_load_cmdlines(const char *tracing_dir, struct tep_handle *tep)return load_saved_cmdlines (tracing_dir, tep, true); } +/** +* tracefs_load_headers - load just the headers into a tep handle +* @tracing_dir: The directory to load from (NULL to figure it out) +* @tep: The tep handle to load the headers into. +* +* Updates the @tep handle with the event and sub-buffer header +* information. +* +* Returns 0 on success and -1 on error. +*/ +int tracefs_load_headers (const char *tracing_dir,struct tep_handle *tep) +{ +int ret; + +if (!tracing_dir) + tracing_dir =tracefs_tracing_dir (); + + ret =read_header (tep, tracing_dir); + +return ret <0 ?-1 :0 ; +} + static int fill_local_events_system(const char *tracing_dir, struct tep_handle *tep, const char * const *sys_names,
src/tracefs-record.c +1 -8
@@ -110,10 +110,8 @@ tracefs_cpu_open(struct tracefs_instance *instance, int cpu, bool nonblock)struct tep_handle *tep;struct kbuffer *kbuf;char path[128 ]; -char *buf;int mode =O_RDONLY ;int subbuf_size; -int len;int ret;int fd;
@@ -131,12 +129,7 @@ tracefs_cpu_open(struct tracefs_instance *instance, int cpu, bool nonblock) goto fail;/* Get the size of the page */ - buf =tracefs_instance_file_read (NULL ,"events/header_page" , &len); -if (!buf) - goto fail; - - ret =tep_parse_header_page (tep, buf, len,sizeof (long )); -free (buf); + ret =tracefs_load_headers (NULL , tep);if (ret <0 ) goto fail;
Signature · 1 line
2.42.0
PATCH 17/23 libtracefs: Add API to extract ring buffer statistics
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add the API that reads the tracefs/per_cpu/cpu*/stats
tracefs_instance_get_stat()
tracefs_instance_put_stat()
tracefs_buffer_stat_entries()
tracefs_buffer_stat_overrun()
tracefs_buffer_stat_commit_overrun()
tracefs_buffer_stat_bytes()
tracefs_buffer_stat_event_timestamp()
tracefs_buffer_stat_timestamp()
tracefs_buffer_stat_dropped_events()
tracefs_buffer_stat_read_events()
--- Documentation/libtracefs-instances-stat.txt | 183 ++++++++++++++++++++ Documentation/libtracefs.txt | 12 ++ include/tracefs-local.h | 11 ++ include/tracefs.h | 13 ++ samples/Makefile | 1 + src/Makefile | 1 + src/tracefs-stats.c | 162 +++++++++++++++++ 7 files changed, 383 insertions(+) create mode 100644 Documentation/libtracefs-instances-stat.txt create mode 100644 src/tracefs-stats.c
Documentation/libtracefs-instances-stat.txt new file +183 -0
@@ -0,0 +1,183 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_instance_get_stat, tracefs_instance_put_stat, tracefs_buffer_stat_entries, tracefs_buffer_stat_overrun, +tracefs_buffer_stat_commit_overrun, tracefs_buffer_stat_bytes, tracefs_buffer_stat_event_timestamp, +tracefs_buffer_stat_timestamp, tracefs_buffer_stat_dropped_events, tracefs_buffer_stat_read_events +- Handling tracing buffer stats + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +struct tracefs_buffer_stat pass:[*]*tracefs_instance_get_stat*(struct tracefs_instance pass:[*]_instance_, int _cpu_); +void *tracefs_instance_put_stat*(struct tracefs_buffer_stat pass:[*]_tstat_); +ssize_t *tracefs_buffer_stat_entries*(struct tracefs_buffer_stat pass:[*]_tstat_); +ssize_t *tracefs_buffer_stat_overrun*(struct tracefs_buffer_stat pass:[*]_tstat_); +ssize_t *tracefs_buffer_stat_commit_overrun*(struct tracefs_buffer_stat pass:[*]_tstat_); +ssize_t *tracefs_buffer_stat_bytes*(struct tracefs_buffer_stat pass:[*]_tstat_); +long long *tracefs_buffer_stat_event_timestamp*(struct tracefs_buffer_stat pass:[*]_tstat_); +long long *tracefs_buffer_stat_timestamp*(struct tracefs_buffer_stat pass:[*]_tstat_); +ssize_t *tracefs_buffer_stat_dropped_events*(struct tracefs_buffer_stat pass:[*]_tstat_); +ssize_t *tracefs_buffer_stat_read_events*(struct tracefs_buffer_stat pass:[*]_tstat_); +-- + +DESCRIPTION +----------- +This set of functions read and parse the tracefs/per_cpu/cpuX/stats file. +These files hold the statistics of the per CPU ring buffer, such as how +many events are in the ring buffer, how many have been read and so on. + +The *tracefs_instance_get_stat()* function will read and parse a given statistics +file for a given _instance_ and _cpu_. As the ring buffer is split into per_cpu buffers, +the information is only associated to the given _cpu_. The returned tracefs_buffer_stat +pointer can be used with the other *tracefs_buffer_stat* functions and must be freed with +*tracefs_instance_put_stat()*. + +The *tracefs_instance_put_stat()* will free the resources allocated for the given _stat_ +that was created by *tracefs_instance_get_stat()*. + +The *tracefs_buffer_stat_entries()* returns the number of events that are currently +in the ring buffer associated with _tstat_. + +The *tracefs_buffer_stat_overrun()* returns the number of events that were lost by +the ring buffer writer overrunning the reader. + +The *tracefs_buffer_stat_commit_overrun()* returns the number of events that were +lost because the ring buffer was too small and an interrupt interrupted a lower +context event being recorded and it added more events than the ring buffer could +hold. Note this is not a common occurrence and when it happens it means that +something was not set up properly. + +The *tracefs_buffer_stat_bytes()* returns the number of bytes that the current events +take up. Note, it includes the meta data for the events, but does not include the +meta data for the sub-buffers. + +The *tracefs_buffer_stat_event_timestamp()* returns the timestamp of the last event in the +ring buffer. + +The *tracefs_buffer_stat_timestamp()* returns the current timestamp of the ring buffer. +Note, it is only read when *tracefs_instance_get_stat()* is called. It will have the +timestamp of the ring buffer when that function was called. + +The *tracefs_buffer_stat_dropped_events()* returns the number of events that were +dropped if overwrite mode is disabled. It will show the events that were lost because +the writer caught up to the reader and could not write any more events. + +The *tracefs_buffer_stat_read_events()* returns the number of events that were consumed +by a reader. + + +RETURN VALUE +------------ +The *tracefs_instance_get_stat()* returns a tracefs_buffer_stat structure that can +be used to retrieve the statistics via the other functions. It must be freed with +*tracefs_instance_put_stat()*. + +The other functions that return different values from the tracefs_buffer_stat structure +all return the value, or -1 if the value was not found. + + +EXAMPLE +------- +[source,c] +-- +#include <stdlib.h> +#include <unistd.h> +#include <tracefs.h> + +int main(int argc, char **argv) +{ + char *trace; + char buf[1000]; + int ret; + int i; + + for (i = 0; i < sizeof(buf) - 1; i++) { + buf[i] = '0' + i % 10; + } + buf[i] = '\0'; + + tracefs_instance_clear(NULL); + + for (i = 0; i < 4; i++) { + ret = tracefs_printf(NULL, "%s\n", buf); + if (ret < 0) + perror("write"); + } + + trace = tracefs_instance_file_read(NULL, "trace", NULL); + printf("%s\n", trace); + free(trace); + + for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++) { + struct tracefs_buffer_stat *tstat; + ssize_t entries, eread; + + tstat = tracefs_instance_get_stat(NULL, i); + if (!tstat) + continue; + + entries = tracefs_buffer_stat_entries(tstat); + eread = tracefs_buffer_stat_read_events(tstat); + if (!entries && !eread) { + tracefs_instance_put_stat(tstat); + continue; + } + + printf("CPU: %d\n", i);; + printf("\tentries: %zd\n", entries); + printf("\toverrun: %zd\n", tracefs_buffer_stat_overrun(tstat)); + printf("\tcommit_overrun: %zd\n", tracefs_buffer_stat_commit_overrun(tstat)); + printf("\tbytes: %zd\n", tracefs_buffer_stat_bytes(tstat)); + printf("\tevent_timestamp: %lld\n", tracefs_buffer_stat_event_timestamp(tstat)); + printf("\ttimestamp: %lld\n", tracefs_buffer_stat_timestamp(tstat)); + printf("\tdropped_events: %zd\n", tracefs_buffer_stat_dropped_events(tstat)); + printf("\tread_events: %zd\n", eread); + + tracefs_instance_put_stat(tstat); + } +} +-- +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) 2020 VMware, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Documentation/libtracefs.txt +12 -0
@@ -155,6 +155,18 @@ Writing data in the trace buffer: Control library logs: int *tracefs_set_loglevel*(enum tep_loglevel _level_); +Read the ring buffer statistics: + struct tracefs_buffer_stat pass:[*]*tracefs_instance_get_stat*(struct tracefs_instance pass:[*]_instance_, int _cpu_); + void *tracefs_instance_put_stat*(struct tracefs_buffer_stat pass:[*]_tstat_); + ssize_t *tracefs_buffer_stat_entries*(struct tracefs_buffer_stat pass:[*]_tstat_); + ssize_t *tracefs_buffer_stat_overrun*(struct tracefs_buffer_stat pass:[*]_tstat_); + ssize_t *tracefs_buffer_stat_commit_overrun*(struct tracefs_buffer_stat pass:[*]_tstat_); + ssize_t *tracefs_buffer_stat_bytes*(struct tracefs_buffer_stat pass:[*]_tstat_); + long long *tracefs_buffer_stat_event_timestamp*(struct tracefs_buffer_stat pass:[*]_tstat_); + long long *tracefs_buffer_stat_timestamp*(struct tracefs_buffer_stat pass:[*]_tstat_); + ssize_t *tracefs_buffer_stat_dropped_events*(struct tracefs_buffer_stat pass:[*]_tstat_); + ssize_t *tracefs_buffer_stat_read_events*(struct tracefs_buffer_stat pass:[*]_tstat_); + Dynamic event generic APIs: struct *tracefs_dynevent*; enum *tracefs_dynevent_type*;
include/tracefs-local.h +11 -0
@@ -51,6 +51,17 @@ struct tracefs_instance {bool iterate_keep_going; }; +struct tracefs_buffer_stat { +ssize_t entries; +ssize_t overrun; +ssize_t commit_overrun; +ssize_t bytes; +long long oldest_ts; +long long now_ts; +ssize_t dropped_events; +ssize_t read_events; +}; +extern const struct tep_format_field common_stacktrace;extern pthread_mutex_t toplevel_lock;
include/tracefs.h +13 -0
@@ -68,6 +68,19 @@ char **tracefs_instances(const char *regex);int tracefs_instance_get_buffer_percent (struct tracefs_instance *instance);int tracefs_instance_set_buffer_percent (struct tracefs_instance *instance,int val); +struct tracefs_buffer_stat ; + +struct tracefs_buffer_stat *tracefs_instance_get_stat (struct tracefs_instance *instance,int cpu); +void tracefs_instance_put_stat (struct tracefs_buffer_stat *tstat); +ssize_t tracefs_buffer_stat_entries (struct tracefs_buffer_stat *tstat); +ssize_t tracefs_buffer_stat_overrun (struct tracefs_buffer_stat *tstat); +ssize_t tracefs_buffer_stat_commit_overrun (struct tracefs_buffer_stat *tstat); +ssize_t tracefs_buffer_stat_bytes (struct tracefs_buffer_stat *tstat); +long long tracefs_buffer_stat_event_timestamp (struct tracefs_buffer_stat *tstat); +long long tracefs_buffer_stat_timestamp (struct tracefs_buffer_stat *tstat); +ssize_t tracefs_buffer_stat_dropped_events (struct tracefs_buffer_stat *tstat); +ssize_t tracefs_buffer_stat_read_events (struct tracefs_buffer_stat *tstat); +bool tracefs_instance_exists (const char *name);bool tracefs_file_exists (struct tracefs_instance *instance,const char *name);bool tracefs_dir_exists (struct tracefs_instance *instance,const char *name);
samples/Makefile +1 -0
@@ -24,6 +24,7 @@ EXAMPLES += instances-affinity EXAMPLES += cpu EXAMPLES += guest EXAMPLES += cpu-buf +EXAMPLES += instances-stat TARGETS := TARGETS += sqlhist
src/Makefile +1 -0
@@ -10,6 +10,7 @@ OBJS += tracefs-tools.o OBJS += tracefs-marker.o OBJS += tracefs-kprobes.o OBJS += tracefs-hist.o +OBJS += tracefs-stats.o OBJS += tracefs-filter.o OBJS += tracefs-dynevents.o OBJS += tracefs-eprobes.o
src/tracefs-stats.c new file +162 -0
162 lines not on this page · raw message
Signature · 1 line
2.42.0
PATCH 18/23 libtracefs: Add tracefs_instance_set/get_subbuf_size()
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add API to retrieve and modify the current sub-buffer size.
tracefs_instance_set_subbuf_size()
tracefs_instance_get_subbuf_size()
--- Documentation/libtracefs-instances-subbuf.txt | 152 ++++++++++++++++++ Documentation/libtracefs.txt | 4 + include/tracefs.h | 2 + samples/Makefile | 1 + src/tracefs-instance.c | 37 +++++ 5 files changed, 196 insertions(+) create mode 100644 Documentation/libtracefs-instances-subbuf.txt
Documentation/libtracefs-instances-subbuf.txt new file +152 -0
@@ -0,0 +1,152 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_instance_get_subbuf_size, tracefs_instance_set_subbuf_size - Helper functions for working with ring buffer sub buffers. + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +size_t *tracefs_instance_get_subbuf_size*(struct tracefs_instance pass:[*]_instance_); +int *tracefs_instance_set_subbuf_size*(struct tracefs_instance pass:[*]_instance_, size_t _size_); +-- + +DESCRIPTION +----------- +Helper functions for working with the sub-buffers of the tracing ring buffer. +The tracing ring buffer is broken up into *sub-buffers*. An event can not be +bigger than the data section of the sub-buffer (see *tep_get_sub_buffer_data_size*(3)). +By default, the ring buffer uses the architectures *page_size* as the default +size of the sub-buffer, but this can be limiting if there is a need for large +events, for example, the application wants to write large strings into +the trace_marker file. + +The *tracefs_instance_get_subbuf_size()* returns the current size in kilobytes +fo the ring buffer sub-buffers. + +The *tracefs_instance_set_subbuf_size()* will write the size in kilobytes of +what the new sub-buffer size should be. Note, that this is only a hint to what +the minimum sub-buffer size should be. It also does not take into account the +meta-data that is used by the sub-buffer, so the size written should be no less +than 16 bytes more than the maximum event size that will be used. The kernel +will likely make the sub-buffer size larger than specified, as it may need to +align the size for implementation purposes. + +RETURN VALUE +------------ +The *tracefs_instance_get_subbuf_size()* returns the size of the current +sub-buffer for the given _instance_ ring buffer or -1 on error. + +The *tracefs_instance_set_subbuf_size()* will return 0 if it successfully set +the _instance_ ring buffer sub-buffer size in kilobytes, or -1 on error. + +EXAMPLE +------- +[source,c] +-- +#include <stdlib.h> +#include <tracefs.h> +#include <errno.h> + +int main(int argc, char **argv) +{ + struct tep_handle *tep; + ssize_t save_subsize; + ssize_t subsize; + char *trace; + char buf[3000]; + int meta_size; + int ret; + int i; + + tep = tep_alloc(); + ret = tracefs_load_headers(NULL, tep); + tep_free(tep); + + if (ret < 0) { + perror("reading headers"); + exit(-1); + } + + meta_size = tep_get_sub_buffer_size(tep) - tep_get_sub_buffer_data_size(tep); + + save_subsize = tracefs_instance_get_subbuf_size(NULL); + if (save_subsize < 0) { + printf("Changing sub-buffer size not available\n"); + exit(-1); + } + + subsize = save_subsize * 1024; + + /* Have at least 4 writes fit on a sub-buffer */ + if (subsize - meta_size < sizeof(buf) *4 ) { + subsize = ((sizeof(buf) * 4 + meta_size) + 1023) / 1024; + tracefs_instance_set_subbuf_size(NULL, subsize); + } + + for (i = 0; i < sizeof(buf) - 1; i++) { + buf[i] = '0' + i % 10; + } + buf[i] = '\0'; + + tracefs_instance_clear(NULL); + + for (i = 0; i < 4; i++) { + ret = tracefs_printf(NULL, "%s\n", buf); + if (ret < 0) + perror("write"); + } + + trace = tracefs_instance_file_read(NULL, "trace", NULL); + printf("%s\n", trace); + free(trace); + + printf("Buffer size was: %zd * 1024\n", + tracefs_instance_get_subbuf_size(NULL)); + + tracefs_instance_set_subbuf_size(NULL, save_subsize); +} +-- +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]> +*Tzvetomir Stoyanov* <[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) 2020 VMware, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Documentation/libtracefs.txt +4 -0
@@ -324,6 +324,10 @@ Recording of trace_pipe_raw files: struct kbuffer pass:[*]*tracefs_cpu_buffered_read_buf*(struct tracefs_cpu pass:[*]_tcpu_, bool _nonblock_); struct kbuffer pass:[*]*tracefs_cpu_flush_buf*(struct tracefs_cpu pass:[*]_tcpu_); +Helper functions for modifying the ring buffer sub-buffers: + size_t *tracefs_instance_get_subbuf_size*(struct tracefs_instance pass:[*]_instance_); + int *tracefs_instance_set_subbuf_size*(struct tracefs_instance pass:[*]_instance_, size_t _size_); + Helper functions for guest tracing: char pass:[*]*tracefs_find_cid_pid*(int _cid_); char pass:[*]*tracefs_instance_find_cid_pid*(struct tracefs_instance pass:[*]_instance_, int _cid_);
include/tracefs.h +2 -0
@@ -63,6 +63,8 @@ int tracefs_instance_get_affinity_set(struct tracefs_instance *instance, cpu_set_t *set, size_t set_size);ssize_t tracefs_instance_get_buffer_size (struct tracefs_instance *instance,int cpu);int tracefs_instance_set_buffer_size (struct tracefs_instance *instance,size_t size,int cpu); +ssize_t tracefs_instance_get_subbuf_size (struct tracefs_instance *instance); +int tracefs_instance_set_subbuf_size (struct tracefs_instance *instance,size_t size);char **tracefs_instances (const char *regex);int tracefs_instance_get_buffer_percent (struct tracefs_instance *instance);
samples/Makefile +1 -0
@@ -25,6 +25,7 @@ EXAMPLES += cpu EXAMPLES += guest EXAMPLES += cpu-buf EXAMPLES += instances-stat +EXAMPLES += instances-subbuf TARGETS := TARGETS += sqlhist
src/tracefs-instance.c +37 -0
@@ -441,6 +441,43 @@ int tracefs_instance_set_buffer_size(struct tracefs_instance *instance, size_t sreturn ret <0 ?-1 :0 ; } +/** +* tracefs_instance_get_subbuf_size - return the sub-buffer size of the ring buffer +* @instance: The instance to get the buffer size from +* +* Returns the sub-buffer size in kilobytes. +* Returns -1 on error. +*/ +ssize_t tracefs_instance_get_subbuf_size (struct tracefs_instance *instance) +{ +long long size; +int ret; + + ret =tracefs_instance_file_read_number (instance,"buffer_subbuf_size_kb" , &size); +if (ret <0 ) +return ret; + +return size; +} + +/** +* tracefs_instance_set_buffer_size - modify the ring buffer sub-buffer size +* @instance: The instance to modify (NULL for the top level) +* @size: The size in kilobytes to to set the sub-buffer size to +* +* Sets the sub-buffer size in kilobytes for the given ring buffer. +* +* Returns 0 on success and -1 on error. +*/ +int tracefs_instance_set_subbuf_size (struct tracefs_instance *instance,size_t size) +{ +int ret; + + ret =tracefs_instance_file_write_number (instance,"buffer_subbuf_size_kb" , size); + +return ret <0 ?-1 :0 ; +} +/** * tracefs_instance_get_trace_dir - return the top trace directory, where the instance is confuigred * @instance: ftrace instance
Signature · 1 line
2.42.0
PATCH 19/23 libtracefs: Add ring buffer memory mapping APIs
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add the following APIs:
tracefs_cpu_open_mapped()
tracefs_cpu_is_mapped()
tracefs_cpu_map()
tracefs_cpu_unmap()
This will allow applications to choose to memory map the tracing ring buffer if it is supported. This will improve the performance of tracefs_cpu_read() and tracefs_cpu_read_buf(), but it is not done by default because it will also hurt the performance of tracefs_cpu_buffered_read() and tracefs_cpu_buffered_read_buf() as those use splicing, and with the ring buffer memory mapped, the splice has to do a copy instead of a copyless subbuffer move.
--- Documentation/libtracefs-cpu-map.txt | 194 +++++++++++++++++++++++++++ Documentation/libtracefs.txt | 7 + include/tracefs-local.h | 6 + include/tracefs.h | 7 + samples/Makefile | 1 + src/Makefile | 1 + src/tracefs-mmap.c | 190 ++++++++++++++++++++++++++ src/tracefs-record.c | 61 +++++++++ 8 files changed, 467 insertions(+) create mode 100644 Documentation/libtracefs-cpu-map.txt create mode 100644 src/tracefs-mmap.c
Documentation/libtracefs-cpu-map.txt new file +194 -0
@@ -0,0 +1,194 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_cpu_open_mapped, tracefs_cpu_is_mapped, tracefs_cpu_map, tracefs_cpu_unmap - Memory mapping of the ring buffer + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +bool *tracefs_cpu_is_mapped*(struct tracefs_cpu pass:[*]tcpu); +int *tracefs_cpu_map*(struct tracefs_cpu pass:[*]tcpu); +void *tracefs_cpu_unmap*(struct tracefs_cpu pass:[*]tcpu); +struct tracefs_cpu pass:[*]*tracefs_cpu_open_mapped*(struct tracefs_instance pass:[*]instance, + int cpu, bool nonblock); +-- + +DESCRIPTION +----------- +If the trace_pipe_raw supports memory mapping, this is usually a more efficient +method to stream data from the kernel ring buffer than by reading it, as it does +not require copying the memory that is being read. + +If memory mapping is supported by the kernel and the application asks to use the +memory mapping via either *tracefs_cpu_map()* or by *tracefs_cpu_open_mapped()* +then the functions *tracefs_cpu_read*(3) and *tracefs_cpu_read_buf*(3) will use +the mapping directly instead of calling the read system call. + +Note, mapping can also slow down *tracefs_cpu_buffered_read*(3) and +*tracefs_cpu_buffered_read_buf*(3), as those use splice piping and when the +kernel ring buffer is memory mapped, splice does a copy instead of using the +ring buffer directly. Thus care must be used when determining to map the +ring buffer or not, and why it does not get mapped by default. + +The *tracefs_cpu_is_mapped()* function will return true if _tcpu_ currently has +its ring buffer memory mapped and false otherwise. This does not return whether or +not that the kernel supports memory mapping, but that can usually be determined +by calling *tracefs_cpu_map()*. + +The *tracefs_cpu_map()* function will attempt to map the ring buffer associated +to _tcpu_ if it is not already mapped. + +The *tracefs_cpu_unmap()* function will unmap the ring buffer associated to +_tcpu_ if it is mapped. + +The *tracefs_cpu_open_mapped()* is equivalent to calling *tracefs_cpu_open*(3) followed +by *tracefs_cpu_map()* on the returned _tcpu_ of *tracefs_cpu_open*(3). Note, this +will still succeed if the mapping fails, in which case it acts the same as +*tracefs_cpu_open*(3). If knowing if the mapping succeed or not, *tracefs_cpu_is_mapped()* +should be called on the return _tcpu_. + +RETURN VALUE +------------ +*tracefs_cpu_is_mapped()* returns true if the given _tcpu_ has its ring buffer +memory mapped or false otherwise. + +*tracefs_cpu_map()* returns 0 on success and -1 on error in mapping. If 0 is +returned then *tracefs_cpu_is_mapped()* will return true afterward, or false +if the mapping failed. + +*tracefs_cpu_open_mapped()* returns an allocated tracefs_cpu on success of creation +regardless if it succeed in mapping the ring buffer or not. It returns NULL for +the same reasons *tracefs_cpu_open*(3) returns NULL. If success of mapping is +to be known, then calling *tracefs_cpu_is_mapped()* afterward is required. + +EXAMPLE +------- +[source,c] +-- +#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; + bool mapped; + 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_mapped(NULL, cpu, 0); + if (!tcpu) { + perror("Open CPU 0 file"); + exit(-1); + } + + /* + * If this kernel supports mapping, use normal read, + * otherwise use the piped buffer read. + */ + mapped = tracefs_cpu_is_mapped(tcpu); + if (!mapped) + printf("Was not able to map, falling back to buffered read\n"); + while ((kbuf = mapped ? tracefs_cpu_read_buf(tcpu, true) : + 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 +----- +[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 +-------- +*tracefs_cpu_open*(3), +*tracefs_cpu_read*(3), +*tracefs_cpu_read_buf*(3), +*tracefs_cpu_buffered_read*(3), +*tracefs_cpu_buffered_read_buf*(3), +*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 +7 -0
@@ -124,6 +124,13 @@ Trace stream: ssize_t *tracefs_trace_pipe_print*(struct tracefs_instance pass:[*]_instance_, int _flags_); void *tracefs_trace_pipe_stop*(struct tracefs_instance pass:[*]_instance_); +Memory mapping the ring buffer: + bool *tracefs_cpu_is_mapped*(struct tracefs_cpu pass:[*]tcpu); + int *tracefs_cpu_map*(struct tracefs_cpu pass:[*]tcpu); + void *tracefs_cpu_unmap*(struct tracefs_cpu pass:[*]tcpu); + struct tracefs_cpu pass:[*]*tracefs_cpu_open_mapped*(struct tracefs_instance pass:[*]instance, + int cpu, bool nonblock); + Trace options: const struct tracefs_options_mask pass:[*]*tracefs_options_get_supported*(struct tracefs_instance pass:[*]_instance_); bool *tracefs_option_is_supported*(struct tracefs_instance pass:[*]_instance_, enum tracefs_option_id _id_);
include/tracefs-local.h +6 -0
@@ -6,6 +6,7 @@#ifndef _TRACE_FS_LOCAL_H#define _TRACE_FS_LOCAL_H +#include <tracefs.h> #include <pthread.h> #define __hidden __attribute__((visibility ("hidden")))
@@ -116,6 +117,11 @@ int trace_append_filter(char **filter, unsigned int *state,enum tracefs_compare compare, const char *val); +void *trace_mmap (int fd,struct kbuffer *kbuf); +void trace_unmap (void *mapping); +int trace_mmap_load_subbuf (void *mapping,struct kbuffer *kbuf); +int trace_mmap_read (void *mapping,void *buffer); +struct tracefs_synth *synth_init_from (struct tep_handle *tep,const char *start_system,const char *start_event);
include/tracefs.h +7 -0
@@ -673,6 +673,13 @@ struct kbuffer *tracefs_cpu_flush_buf(struct tracefs_cpu *tcpu);int tracefs_cpu_flush_write (struct tracefs_cpu *tcpu,int wfd);int tracefs_cpu_pipe (struct tracefs_cpu *tcpu,int wfd,bool nonblock); +/* Memory mapping of ring buffer */ +bool tracefs_cpu_is_mapped (struct tracefs_cpu *tcpu); +int tracefs_cpu_map (struct tracefs_cpu *tcpu); +void tracefs_cpu_unmap (struct tracefs_cpu *tcpu); +struct tracefs_cpu *tracefs_cpu_open_mapped (struct tracefs_instance *instance, +int cpu,bool nonblock); +/* Mapping vsocket cids to pids using tracing */ int tracefs_instance_find_cid_pid (struct tracefs_instance *instance,int cid);int tracefs_find_cid_pid (int cid);
samples/Makefile +1 -0
@@ -26,6 +26,7 @@ EXAMPLES += guest EXAMPLES += cpu-buf EXAMPLES += instances-stat EXAMPLES += instances-subbuf +EXAMPLES += cpu-map TARGETS := TARGETS += sqlhist
src/Makefile +1 -0
@@ -16,6 +16,7 @@ OBJS += tracefs-dynevents.o OBJS += tracefs-eprobes.o OBJS += tracefs-uprobes.o OBJS += tracefs-record.o +OBJS += tracefs-mmap.o ifeq ($(VSOCK_DEFINED), 1) OBJS += tracefs-vsock.o endif
src/tracefs-mmap.c new file +190 -0
190 lines not on this page · raw message
src/tracefs-record.c +61 -0
91 lines not on this page · raw message
Signature · 1 line
2.42.0
PATCH 20/23 libtracefs: Add TIMESTAMP_USECS_DELTA to simplify SQL timestamp compares
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Since it is so common to have in the SQL synthetic event creation logic:
(end.TIMESTAMP_USECS - start.TIMESTAMP_USECS) as delta
for creating latency synthetic events, add a shortcut that does the above.
Now the user only needs to write:
TIMESTAMP_USECS_DELTA as delta
and it will produce the same compare.
Note, TIMESTAMP_DELTA is also equivalent to: (end.TIMESTAMP - start.TIMESTAMP)
--- Documentation/libtracefs-sql.txt | 16 ++++++++ src/tracefs-sqlhist.c | 64 +++++++++++++++++++++++++++++++- utest/tracefs-utest.c | 10 +++++ 3 files changed, 88 insertions(+), 2 deletions(-)
Documentation/libtracefs-sql.txt +16 -0
@@ -127,6 +127,22 @@ The *TIMESTAMP_USECS* will truncate the time down to microseconds as the timesta recorded in the tracing buffer has nanosecond resolution. If you do not want that truncation, use *TIMESTAMP* instead of *TIMESTAMP_USECS*. +Because it is so common to have: + +[source,c] +-- + (end.TIMESTAMP_USECS - start.TIMESTAMP_USECS) +-- + +The above can be represented with *TIMESTAMP_USECS_DELTA* or if nanoseconds are OK, you can +use *TIMESTAMP_DELTA*. That is, the previous select can also be represented by: + +[source,c] +-- +select start.pid, TIMESTAMP_USECS_DELTA as lat from sched_waking as start JOIN sched_switch as end ON start.pid = end.next_pid +-- + + Finally, the *WHERE* clause can be added, that will let you add filters on either or both events. [source,c]
src/tracefs-sqlhist.c +62 -2
97 lines not on this page · raw message
utest/tracefs-utest.c +10 -0
@@ -49,6 +49,9 @@#define SQL_5_SQL "select end.common_pid as pid, (end.common_timestamp.usecs - start.common_timestamp.usecs) as irq_lat from irq_disable as start join irq_enable as end on start.common_pid = end.common_pid, start.parent_offs == end.parent_offs where start.common_pid != 0"#define SQL_5_START "irq_disable" +#define SQL_6_EVENT "wakeup_lat_3" +#define SQL_6_SQL "select start.pid, end.next_prio as prio, (end.TIMESTAMP_USECS - start.TIMESTAMP_USECS) as lat from sched_waking as start join sched_switch as end on start.pid = end.next_pid where (start.prio >= 1 && start.prio < 100) || !(start.pid >= 0 && start.pid <= 1) && end.prev_pid != 0" +#define DEBUGFS_DEFAULT_PATH "/sys/kernel/debug"#define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"#define TRACEFS_DEFAULT2_PATH "/sys/kernel/debug/tracing"
@@ -420,6 +423,13 @@ static void test_instance_trace_sql(struct tracefs_instance *instance)trace_seq_reset (&seq); } + synth =tracefs_sql (tep,SQL_6_EVENT ,SQL_6_SQL ,NULL ); +CU_TEST (synth !=NULL ); + ret =tracefs_synth_echo_cmd (&seq, synth); +CU_TEST (ret ==0 ); +tracefs_synth_free (synth); +trace_seq_reset (&seq); +trace_seq_destroy (&seq); }
Signature · 1 line
2.42.0
PATCH 21/23 libtracefs: Also clear max_graph_depth on reset
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Clear the "max_graph_depth" file on tracefs_instance_reset()
--- src/tracefs-instance.c | 1 + 1 file changed, 1 insertion(+)
src/tracefs-instance.c +1 -0
@@ -1490,6 +1490,7 @@ void tracefs_instance_reset(struct tracefs_instance *instance)tracefs_tracer_clear (instance);tracefs_instance_file_write (instance,"events/enable" ,"0" );tracefs_instance_file_write (instance,"set_ftrace_pid" ,"" ); +tracefs_instance_file_write (instance,"max_graph_depth" ,"0" );tracefs_instance_file_clear (instance,"trace" ); systems =tracefs_event_systems (NULL );
Signature · 1 line
2.42.0
PATCH 22/23 libtracefs: Add PID filtering API
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add an API that sets and clears PID filtering for functions and events.
tracefs_filter_pid_function()
tracefs_filter_pid_events()
tracefs_filter_pid_function_clear()
tracefs_filter_pid_events_clear()
--- Documentation/libtracefs-filter-pid.txt | 181 +++++++++++++++++ Documentation/libtracefs.txt | 8 + include/tracefs.h | 7 + src/tracefs-filter.c | 132 ++++++++++++ utest/tracefs-utest.c | 257 +++++++++++++++++++++++- 5 files changed, 577 insertions(+), 8 deletions(-) create mode 100644 Documentation/libtracefs-filter-pid.txt
Documentation/libtracefs-filter-pid.txt new file +181 -0
@@ -0,0 +1,181 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_filter_pid_function, tracefs_filter_pid_events, tracefs_filter_pid_function_clear, tracefs_filter_pid_events_clear - +Add and remove PID filtering for functions and events + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +int *tracefs_filter_pid_function*(struct tracefs_instance pass:[*]_instance,_ int _pid_, + bool _reset_, bool _notrace_); +int *tracefs_filter_pid_function_clear*(struct tracefs_instance pass:[*]_instance_, bool _notrace_); +int *tracefs_filter_pid_events*(struct tracefs_instance pass:[*]_instance_, int _pid_, + bool _reset_, bool _notrace_); +int *tracefs_filter_pid_events_clear*(struct tracefs_instance pass:[*]_instance_, bool _notrace_); +-- + +DESCRIPTION +----------- +Both events and functions can be filtered by PID, but they are done separately. +PID filtering for functions affect the function and function_graph tracer, where +as PID filtering for events affect all events such as _sched_switch_ and _sched_waking_. +If the *TRACEFS_OPTION_FUNCTION_FORK* is enabled (see *tracefs_option_enable*(3)), +any PID that is set as part of the function PID filtering will automatically +have its children added when they are spawned, as well as the PID removed when +they exit. If the *TRACEFS_OPTION_EVENT_FORK* is set, the same is true for +event PID filtering. This also includes the _notrace_ option where the child +threads and processes of PIDs that are labled as notrace will also not be +traced. + +The *tracefs_filter_pid_function()* affects function PID filtering and *tracefs_filter_pid_events()* +affects the PID event filtering. For both functions, they add a _pid_ to be filtered in the given _instance_. +If _reset_ is true, then any PIDs already being filtered will be removed, otherwise +the _pid_ is simply added to the filtering. If _notrace_ is true, then the PID +is added to the list of PIDs that are not to be traced. Note, that _reset_ only affects +the list associated with _notrace_. That is, if both _reset_ and _notrace_ are true, +then it will not affect PIDs that are to be traced. Same is if _reset_ is true and _notrace_ +is false, it will not affect PIDs that are not to be traced. + +The *tracefs_filter_pid_function_clear()* affects function PID filtering and +*tracefs_filter_pid_events_clear()* affects the PID event filtering. For both +functions it will clear all the PIDs that are being filtered for the given +filter. If _notrace_ is true it clears all the PIDs that are not to be traced +otherwise if it is false, it clears all the PIDs that are to be traced. + +RETURN VALUE +------------ +All the functions return 0 on success and -1 on error. + +EXAMPLE +------- +[source,c] +-- +#include <stdlib.h> +#include <stdio.h> +#include <ctype.h> +#include <tracefs.h> + +static void usage(char **argv) +{ + fprintf(stderr, "usage: %s [-e|-f][-c|-n] pid [pid ...]\n", argv[0]); + fprintf(stderr, " -e enable event filter\n"); + fprintf(stderr, " -f enable function filter\n"); + fprintf(stderr, " (default is both, function and event)\n"); + fprintf(stderr, " -c clear the filter\n"); + fprintf(stderr, " -n notrace filter\n"); + exit(-1); +} + +int main (int argc, char **argv) +{ + bool events = false; + bool funcs = false; + bool neg = false; + bool clear = false; + bool reset = true; + int i; + + for (i = 1; i < argc && argv[i][0] == '-'; i++) { + char *arg = argv[i]; + int c; + for (c = 1; arg[c]; c++) { + switch (arg[c]) { + case 'e': events = true; break; + case 'f': funcs = true; break; + case 'n': neg = true; break; + case 'c': clear = true; break; + default: + usage(argv); + } + } + if (c == 1) + usage(argv); + } + + if (i == argc && !clear) + usage(argv); + + if (!events && !funcs) { + events = true; + funcs = true; + } + + if (clear) { + if (events) + tracefs_filter_pid_events_clear(NULL, neg); + if (funcs) + tracefs_filter_pid_function_clear(NULL, neg); + exit(0); + } + + for (; i < argc; i++) { + int pid = atoi(argv[i]); + + if (events) + tracefs_filter_pid_events(NULL, pid, reset, neg); + if (funcs) + tracefs_filter_pid_function(NULL, pid, reset, neg); + + reset = false; + } + + exit(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), +*tracefs_hist_alloc*(3), +*tracefs_hist_alloc_2d*(3), +*tracefs_hist_alloc_nd*(3), +*tracefs_hist_free*(3), +*tracefs_hist_add_key*(3), +*tracefs_hist_add_value*(3), +*tracefs_hist_add_name*(3), +*tracefs_hist_start*(3), +*tracefs_hist_destory*(3), +*tracefs_hist_add_sort_key*(3), +*tracefs_hist_sort_key_direction*(3) + +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) 2023 Google, LLC. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Documentation/libtracefs.txt +8 -0
@@ -107,6 +107,14 @@ Function filters: int *tracefs_function_notrace*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_filter_, const char pass:[*]_module_, int _flags_); int *tracefs_filter_functions*(const char pass:[*]_filter_, const char pass:[*]_module_, char pass:[*]pass:[*]pass:[*]_list_); +PID filters: + int *tracefs_filter_pid_function*(struct tracefs_instance pass:[*]_instance,_ int _pid_, + bool _reset_, bool _notrace_); + int *tracefs_filter_pid_function_clear*(struct tracefs_instance pass:[*]_instance_, bool _notrace_); + int *tracefs_filter_pid_events*(struct tracefs_instance pass:[*]_instance_, int _pid_, + bool _reset_, bool _notrace_); + int *tracefs_filter_pid_events_clear*(struct tracefs_instance pass:[*]_instance_, bool _notrace_); + Trace helper functions: void *tracefs_list_free*(char pass:[*]pass:[*]_list_); char pass:[**]*tracefs_list_add*(char **_list_, const char *_string_);
include/tracefs.h +7 -0
@@ -269,6 +269,13 @@ enum { TRACEFS_FL_FUTURE = (1 << 2), }; +int tracefs_filter_pid_function (struct tracefs_instance *instance,int pid, +bool reset,bool notrace); +int tracefs_filter_pid_function_clear (struct tracefs_instance *instance,bool notrace); +int tracefs_filter_pid_events (struct tracefs_instance *instance,int pid, +bool reset,bool notrace); +int tracefs_filter_pid_events_clear (struct tracefs_instance *instance,bool notrace); +int tracefs_function_filter (struct tracefs_instance *instance,const char *filter,const char *module,unsigned int flags); int tracefs_function_notrace(struct tracefs_instance *instance, const char *filter,
src/tracefs-filter.c +132 -0
138 lines not on this page · raw message
utest/tracefs-utest.c +249 -8
287 lines not on this page · raw message
Signature · 1 line
2.42.0
PATCH 23/23 libtracefs: Add updating and reading snapshot buffers
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
Add the API:
tracefs_cpu_snapshot_open()
That will read the snapshot_raw file just like tracefs_cpu_open() does to the trace_pipe_raw file, except the blocking will block only if empty and until another snapshot occurs.
Add blocking and see if a snapshot will unblock it!
--- Documentation/libtracefs-cpu-open.txt | 16 +++- Documentation/libtracefs-iterator.txt | 10 ++- Documentation/libtracefs.txt | 6 ++ include/tracefs.h | 13 ++++ src/tracefs-events.c | 97 +++++++++++++++++------- src/tracefs-record.c | 102 ++++++++++++++++++++++---- utest/tracefs-utest.c | 42 +++++++++-- 7 files changed, 237 insertions(+), 49 deletions(-)
Documentation/libtracefs-cpu-open.txt +13 -3
@@ -3,7 +3,7 @@ libtracefs(3) NAME ---- -tracefs_cpu_open, tracefs_cpu_close, tracefs_cpu_alloc_fd, tracefs_cpu_free_fd - Opening trace_pipe_raw data for reading +tracefs_cpu_open, tracefs_cpu_close, tracefs_cpu_alloc_fd, tracefs_cpu_free_fd, tracefs_cpu_snapshot_open - Opening trace_pipe_raw data for reading SYNOPSIS --------
@@ -17,6 +17,9 @@ 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_); + +struct tracefs_cpu pass:[*]*tracefs_cpu_snapshot-open*(struct tracefs_instance pass:[*]_instance_, + int _cpu_, bool _nonblock_); -- DESCRIPTION
@@ -47,10 +50,17 @@ the file descriptor passed in. Note that *tracefs_cpu_free_fd()* should not be u on the descriptor returned by *tracefs_cpu_open()* as it will not close the file descriptor created by it. +The *tracefs_cpu_snapshot_open()* is similar to *tracefs_cpu_open()* except that it +opens the snapshot buffer (see *tracefs_snapshot_snap*(3)). The snapshot buffer +does not have a writer to it, it is only created by a snapshot action that swaps +the current ring buffer with the snapshot buffer. The _nonblock_, when false, acts a little +differently here too. Reads are not affected by the "buffer_percent" file. If the +snapshot buffer is empty, it will block until a new snapshot happens. + 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_open()* and *tracefs_cpu_snapshot_open() both return 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
Documentation/libtracefs-iterator.txt +9 -1
@@ -4,7 +4,7 @@ libtracefs(3) NAME ---- tracefs_iterate_raw_events, tracefs_iterate_stop, tracefs_follow_event, tracefs_follow_missed_events, -tracefs_follow_event_clear, tracefs_follow_missed_events_clear - Iterate over events in the ring buffer +tracefs_follow_event_clear, tracefs_follow_missed_events_clear, tracefs_iterate_snapshot_events - Iterate over events in the ring buffer SYNOPSIS --------
@@ -33,6 +33,11 @@ int *tracefs_follow_missed_events*(struct tracefs_instance pass:[*]_instance_, int *tracefs_follow_event_clear*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_name_); int *tracefs_follow_missed_events_clear*(struct tracefs_instance pass:[*]_instance_); + +int *tracefs_iterate_snapshot_events*(struct tep_handle pass:[*]_tep_, struct tracefs_instance pass:[*]_instance_, + cpu_set_t pass:[*]_cpus_, int _cpu_size_, + int (pass:[*]_callback_)(struct tep_event pass:[*], struct tep_record pass:[*], int, void pass:[*]), + void pass:[*]_callback_context_); -- DESCRIPTION
@@ -54,6 +59,9 @@ record is; The record representing the event; The CPU that the event occurred on; and a pointer to user specified _callback_context_. If the _callback_ returns non-zero, the iteration stops. +The *tracefs_iterate_snapshot_events()* works the same as *tracefs_iterate_raw_events()* +except that it works on the snapshot buffer. + Use *tracefs_iterate_stop()* to force a executing *tracefs_iterate_raw_events()* to halt. This can be called from either a callback that is called by the iterator (even though a return of non-zero will stop it), or from another
Documentation/libtracefs.txt +6 -0
@@ -94,6 +94,12 @@ Trace events: bool *tracefs_event_file_exists*(struct tracefs_instance pass:[*]_instance_, const char pass:[*]_system_, const char pass:[*]_event_, const char pass:[*]_file_); +Snapshot buffer: + int *tracefs_iterate_snapshot_events*(struct tep_handle pass:[*]_tep_, struct tracefs_instance pass:[*]_instance_, + cpu_set_t pass:[*]_cpus_, int _cpu_size_, + int (pass:[*]_callback_)(struct tep_event pass:[*], struct tep_record pass:[*], int, void pass:[*]), + void pass:[*]_callback_context_); + Event filters: int *tracefs_filter_string_append*(struct tep_event pass:[*]_event_, char pass:[**]_filter_, struct tracefs_filter _type_, const char pass:[*]_field_,
include/tracefs.h +13 -0
@@ -680,6 +680,19 @@ struct kbuffer *tracefs_cpu_flush_buf(struct tracefs_cpu *tcpu);int tracefs_cpu_flush_write (struct tracefs_cpu *tcpu,int wfd);int tracefs_cpu_pipe (struct tracefs_cpu *tcpu,int wfd,bool nonblock); +struct tracefs_cpu * +tracefs_cpu_snapshot_open (struct tracefs_instance *instance,int cpu,bool nonblock); +int tracefs_iterate_snapshot_events (struct tep_handle *tep, +struct tracefs_instance *instance, +cpu_set_t *cpus,int cpu_size, +int (*callback)(struct tep_event *, +struct tep_record *, +int ,void *), +void *callback_context); +int tracefs_snapshot_snap (struct tracefs_instance *instance); +int tracefs_snapshot_clear (struct tracefs_instance *instance); +int tracefs_snapshot_free (struct tracefs_instance *instance); +/* Memory mapping of ring buffer */ bool tracefs_cpu_is_mapped (struct tracefs_cpu *tcpu);int tracefs_cpu_map (struct tracefs_cpu *tcpu);
src/tracefs-events.c +71 -26
128 lines not on this page · raw message
src/tracefs-record.c +88 -14
120 lines not on this page · raw message
utest/tracefs-utest.c +37 -5
80 lines not on this page · raw message
Signature · 1 line
2.42.0