selftests/bpf: Add read_trace_pipe_iter function
Subsystems (3)
Jiri Olsa
reviewerPATCH v2 1/1 selftests/bpf: Add read_trace_pipe_iter function
We have two printk tests reading trace_pipe in non blocking way, with the very same code. Moving that in new read_trace_pipe_iter function.
Current read_trace_pipe is used from sampless/bpf and needs to do blocking read and printf of the trace_pipe data, using new read_trace_pipe_iter to implement that.
Both printk tests do early checks for the number of found messages and can bail earlier, but I did not find any speed difference w/o that condition, so I did not complicate the change more for that.
Some of the samples/bpf programs use read_trace_pipe function, so I kept that interface untouched. I did not see any issues with affected samples/bpf programs other than there's slight change in read_trace_pipe output. The current code uses puts that adds new line after the printed string, so we would occasionally see extra new line. With this patch we read output per lines, so there's no need to use puts and we can use just printf instead without extra new line.
---
v2 changes:
- call read_trace_pipe_iter callback only in case there's new data read from getline
.../selftests/bpf/prog_tests/trace_printk.c | 36 +++-------- .../selftests/bpf/prog_tests/trace_vprintk.c | 36 +++-------- tools/testing/selftests/bpf/trace_helpers.c | 63 ++++++++++++------- tools/testing/selftests/bpf/trace_helpers.h | 2 + 4 files changed, 60 insertions(+), 77 deletions(-)
tools/testing/selftests/bpf/prog_tests/trace_printk.c +9 -27
@@ -5,18 +5,19 @@#include "trace_printk.lskel.h" -#define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" -#define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe"#define SEARCHMSG "testing,testing" +static void trace_pipe_cb (const char *str,void *data) +{ +if (strstr (str,SEARCHMSG ) !=NULL ) + (*(int *)data)++; +} +void serial_test_trace_printk (void ) {struct trace_printk_lskel__bss *bss; -int err =0 , iter =0 , found =0 ;struct trace_printk_lskel *skel; -char *buf =NULL ; -FILE *fp =NULL ; -size_t buflen; +int err =0 , found =0 ; skel =trace_printk_lskel__open ();if (!ASSERT_OK_PTR (skel,"trace_printk__open" ))
@@ -35,16 +36,6 @@ void serial_test_trace_printk(void)if (!ASSERT_OK (err,"trace_printk__attach" )) goto cleanup; -if (access (TRACEFS_PIPE ,F_OK ) ==0 ) - fp =fopen (TRACEFS_PIPE ,"r" ); -else - fp =fopen (DEBUGFS_PIPE ,"r" ); -if (!ASSERT_OK_PTR (fp,"fopen(TRACE_PIPE)" )) - goto cleanup; - -/* We do not want to wait forever if this test fails... */ -fcntl (fileno (fp),F_SETFL ,O_NONBLOCK ); -/* wait for tracepoint to trigger */ usleep (1 );trace_printk_lskel__detach (skel);
@@ -56,21 +47,12 @@ void serial_test_trace_printk(void) goto cleanup;/* verify our search string is in the trace buffer */ -while (getline (&buf, &buflen, fp) >=0 || errno ==EAGAIN ) { -if (strstr (buf,SEARCHMSG ) !=NULL ) - found++; -if (found == bss->trace_printk_ran) -break ; -if (++iter >1000 ) -break ; - } +ASSERT_OK (read_trace_pipe_iter (trace_pipe_cb, &found,1000 ), +"read_trace_pipe_iter" );if (!ASSERT_EQ (found, bss->trace_printk_ran,"found" )) goto cleanup; cleanup:trace_printk_lskel__destroy (skel); -free (buf); -if (fp) -fclose (fp); }
tools/testing/selftests/bpf/prog_tests/trace_vprintk.c +9 -27
@@ -5,18 +5,19 @@#include "trace_vprintk.lskel.h" -#define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" -#define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe"#define SEARCHMSG "1,2,3,4,5,6,7,8,9,10" +static void trace_pipe_cb (const char *str,void *data) +{ +if (strstr (str,SEARCHMSG ) !=NULL ) + (*(int *)data)++; +} +void serial_test_trace_vprintk (void ) {struct trace_vprintk_lskel__bss *bss; -int err =0 , iter =0 , found =0 ;struct trace_vprintk_lskel *skel; -char *buf =NULL ; -FILE *fp =NULL ; -size_t buflen; +int err =0 , found =0 ; skel =trace_vprintk_lskel__open_and_load ();if (!ASSERT_OK_PTR (skel,"trace_vprintk__open_and_load" ))
@@ -28,16 +29,6 @@ void serial_test_trace_vprintk(void)if (!ASSERT_OK (err,"trace_vprintk__attach" )) goto cleanup; -if (access (TRACEFS_PIPE ,F_OK ) ==0 ) - fp =fopen (TRACEFS_PIPE ,"r" ); -else - fp =fopen (DEBUGFS_PIPE ,"r" ); -if (!ASSERT_OK_PTR (fp,"fopen(TRACE_PIPE)" )) - goto cleanup; - -/* We do not want to wait forever if this test fails... */ -fcntl (fileno (fp),F_SETFL ,O_NONBLOCK ); -/* wait for tracepoint to trigger */ usleep (1 );trace_vprintk_lskel__detach (skel);
@@ -49,14 +40,8 @@ void serial_test_trace_vprintk(void) goto cleanup;/* verify our search string is in the trace buffer */ -while (getline (&buf, &buflen, fp) >=0 || errno ==EAGAIN ) { -if (strstr (buf,SEARCHMSG ) !=NULL ) - found++; -if (found == bss->trace_vprintk_ran) -break ; -if (++iter >1000 ) -break ; - } +ASSERT_OK (read_trace_pipe_iter (trace_pipe_cb, &found,1000 ), +"read_trace_pipe_iter" );if (!ASSERT_EQ (found, bss->trace_vprintk_ran,"found" )) goto cleanup;
@@ -66,7 +51,4 @@ void serial_test_trace_vprintk(void) cleanup:trace_vprintk_lskel__destroy (skel); -free (buf); -if (fp) -fclose (fp); }
tools/testing/selftests/bpf/trace_helpers.c +40 -23
72 lines not on this page · raw message
tools/testing/selftests/bpf/trace_helpers.h +2 -0
@@ -33,6 +33,8 @@ struct ksym *search_kallsyms_custom_local(struct ksyms *ksyms, const void *p1,int kallsyms_find (const char *sym,unsigned long long *addr);void read_trace_pipe (void ); +int read_trace_pipe_iter (void (*cb)(const char *str,void *data), +void *data,int iter);ssize_t get_uprobe_offset (const void *addr);ssize_t get_rel_offset (uintptr_t addr);
Signature · 1 line
2.44.0
Quoted 21 lines · Jiri Olsa
We have two printk tests reading
trace_pipein non blocking way, with the very same code. Moving that in newread_trace_pipe_iterfunction.Current
read_trace_pipeis used from sampless/bpf and needs to do blocking read and printf of thetrace_pipedata, using newread_trace_pipe_iterto implement that.Both printk tests do early checks for the number of found messages and can bail earlier, but I did not find any speed difference w/o that condition, so I did not complicate the change more for that.
Some of the
samples/bpfprograms useread_trace_pipefunction, so I kept that interface untouched. I did not see any issues with affectedsamples/bpfprograms other than there's slight change inread_trace_pipeoutput. The current code uses puts that adds new line after the printed string, so we would occasionally see extra new line. With this patch we read output per lines, so there's no need to use puts and we can use just printf instead without extra new line.
Quoted 1 line
Signed-off-by Jiri Olsa <[email protected]>
Ack with a nit below.
Quoted 59 lines
---
v2 changes:
- callread_trace_pipe_itercallback only in case there's new data read from getline.../selftests/bpf/prog_tests/trace_printk.c | 36 +++-------- .../selftests/bpf/prog_tests/trace_vprintk.c | 36 +++-------- tools/testing/selftests/bpf/trace_helpers.c | 63 ++++++++++++------- tools/testing/selftests/bpf/trace_helpers.h | 2 + 4 files changed, 60 insertions(+), 77 deletions(-)tools/testing/selftests/bpf/prog_tests/trace_printk.c +7
-16@@ -5,18 +5,19 @@#include "trace_printk.lskel.h" -#define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" -#define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe"#define SEARCHMSG "testing,testing" +static void trace_pipe_cb (const char *str,void *data) +{ +if (strstr (str,SEARCHMSG ) !=NULL ) + (*(int *)data)++; +} +void serial_test_trace_printk (void ) {struct trace_printk_lskel__bss *bss; -int err =0 , iter =0 , found =0 ;struct trace_printk_lskel *skel; -char *buf =NULL ; -FILE *fp =NULL ; -size_t buflen; +int err =0 , found =0 ; skel =trace_printk_lskel__open ();if (!ASSERT_OK_PTR (skel,"trace_printk__open" ))@@ -35,16 +36,6 @@ void serial_test_trace_printk(void)if (!ASSERT_OK (err,"trace_printk__attach" )) goto cleanup; -if (access (TRACEFS_PIPE ,F_OK ) ==0 ) - fp =fopen (TRACEFS_PIPE ,"r" ); -else - fp =fopen (DEBUGFS_PIPE ,"r" ); -if (!ASSERT_OK_PTR (fp,"fopen(TRACE_PIPE)" )) - goto cleanup; - -/* We do not want to wait forever if this test fails... */ -fcntl (fileno (fp),F_SETFL ,O_NONBLOCK ); -/* wait for tracepoint to trigger */ usleep (1 );trace_printk_lskel__detach (skel);
tools/testing/selftests/bpf/prog_tests/trace_printk.c void serial_test_trace_printk(void) · line 63
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/trace_printk.c +0
-5@@ -56,21 +47,12 @@ void serial_test_trace_printk(void) goto cleanup;/* verify our search string is in the trace buffer */ -while (getline (&buf, &buflen, fp) >=0 || errno ==EAGAIN ) { -if (strstr (buf,SEARCHMSG ) !=NULL ) - found++; -if (found == bss->trace_printk_ran) -break ;
The above condition is not covered, but I think it is okay since the test will run in serial mode.
Quoted 87 lines
tools/testing/selftests/bpf/prog_tests/trace_printk.c +2
-6-if (++iter >1000 ) -break ; - } +ASSERT_OK (read_trace_pipe_iter (trace_pipe_cb, &found,1000 ), +"read_trace_pipe_iter" );if (!ASSERT_EQ (found, bss->trace_printk_ran,"found" )) goto cleanup; cleanup:trace_printk_lskel__destroy (skel); -free (buf); -if (fp) -fclose (fp); }tools/testing/selftests/bpf/prog_tests/trace_vprintk.c +9
-27@@ -5,18 +5,19 @@#include "trace_vprintk.lskel.h" -#define TRACEFS_PIPE "/sys/kernel/tracing/trace_pipe" -#define DEBUGFS_PIPE "/sys/kernel/debug/tracing/trace_pipe"#define SEARCHMSG "1,2,3,4,5,6,7,8,9,10" +static void trace_pipe_cb (const char *str,void *data) +{ +if (strstr (str,SEARCHMSG ) !=NULL ) + (*(int *)data)++; +} +void serial_test_trace_vprintk (void ) {struct trace_vprintk_lskel__bss *bss; -int err =0 , iter =0 , found =0 ;struct trace_vprintk_lskel *skel; -char *buf =NULL ; -FILE *fp =NULL ; -size_t buflen; +int err =0 , found =0 ; skel =trace_vprintk_lskel__open_and_load ();if (!ASSERT_OK_PTR (skel,"trace_vprintk__open_and_load" ))@@ -28,16 +29,6 @@ void serial_test_trace_vprintk(void)if (!ASSERT_OK (err,"trace_vprintk__attach" )) goto cleanup; -if (access (TRACEFS_PIPE ,F_OK ) ==0 ) - fp =fopen (TRACEFS_PIPE ,"r" ); -else - fp =fopen (DEBUGFS_PIPE ,"r" ); -if (!ASSERT_OK_PTR (fp,"fopen(TRACE_PIPE)" )) - goto cleanup; - -/* We do not want to wait forever if this test fails... */ -fcntl (fileno (fp),F_SETFL ,O_NONBLOCK ); -/* wait for tracepoint to trigger */ usleep (1 );trace_vprintk_lskel__detach (skel);@@ -49,14 +40,8 @@ void serial_test_trace_vprintk(void) goto cleanup;/* verify our search string is in the trace buffer */ -while (getline (&buf, &buflen, fp) >=0 || errno ==EAGAIN ) { -if (strstr (buf,SEARCHMSG ) !=NULL ) - found++; -if (found == bss->trace_vprintk_ran) -break ; -if (++iter >1000 ) -break ; - } +ASSERT_OK (read_trace_pipe_iter (trace_pipe_cb, &found,1000 ), +"read_trace_pipe_iter" );if (!ASSERT_EQ (found, bss->trace_vprintk_ran,"found" )) goto cleanup;@@ -66,7 +51,4 @@ void serial_test_trace_vprintk(void) cleanup:trace_vprintk_lskel__destroy (skel); -free (buf); -if (fp) -fclose (fp); }
[...]
Quoted 19 lines
ssize_t get_uprobe_offset(const void *addr) { size_t start, end, base;tools/testing/selftests/bpf/prog_tests/trace_vprintk.c +12
-0@@ -413,3 +390,43 @@ int read_build_id(const char *path, char *build_id, size_t size)close (fd);return err; } + +int read_trace_pipe_iter (void (*cb)(const char *str,void *data),void *data,int iter) +{ +size_t buflen, n; +char *buf =NULL ; +FILE *fp =NULL ; + +if (access (TRACEFS_PIPE ,F_OK ) ==0 ) + fp =fopen (TRACEFS_PIPE ,"r" ); +else + fp =fopen (DEBUGFS_PIPE ,"r" ); +if (!fp)
tools/testing/selftests/bpf/prog_tests/trace_vprintk.c int read_build_id(const char *path, char *build_id, size_t size) · line 414
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/trace_vprintk.c +10
-0+return -1 ; + +/* We do not want to wait forever when iter is specified. */ +if (iter) +fcntl (fileno (fp),F_SETFL ,O_NONBLOCK ); + +while ((n =getline (&buf, &buflen, fp) >=0 ) || errno ==EAGAIN ) { +if (n >0 ) +cb (buf, data); +if (iter && !(--iter))
"if (iter-- == 1)" should also work. But your code works too.
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/trace_vprintk.c +9
-0+break ; + } + +free (buf); +if (fp) +fclose (fp); +return 0 ; +} +
[...]
Quoted 28 lines · Yonghong Song
Quoted 21 lines · Jiri Olsa
We have two printk tests reading
trace_pipein non blocking way, with the very same code. Moving that in newread_trace_pipe_iterfunction.Current
read_trace_pipeis used from sampless/bpf and needs to do blocking read and printf of thetrace_pipedata, using newread_trace_pipe_iterto implement that.Both printk tests do early checks for the number of found messages and can bail earlier, but I did not find any speed difference w/o that condition, so I did not complicate the change more for that.
Some of the
samples/bpfprograms useread_trace_pipefunction, so I kept that interface untouched. I did not see any issues with affectedsamples/bpfprograms other than there's slight change inread_trace_pipeoutput. The current code uses puts that adds new line after the printed string, so we would occasionally see extra new line. With this patch we read output per lines, so there's no need to use puts and we can use just printf instead without extra new line.Quoted 1 line
Signed-off-by Jiri Olsa <[email protected]>Ack with a nit below.
Acked-by Yonghong Song <[email protected]>
SNIP
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/trace_printk.c void serial_test_trace_printk(void) · line 62
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/trace_printk.c +0
-5@@ -56,21 +47,12 @@ void serial_test_trace_printk(void) goto cleanup;/* verify our search string is in the trace buffer */ -while (getline (&buf, &buflen, fp) >=0 || errno ==EAGAIN ) { -if (strstr (buf,SEARCHMSG ) !=NULL ) - found++; -if (found == bss->trace_printk_ran) -break ;The above condition is not covered, but I think it is okay since the test will run in serial mode.
right, I made a note about that in the changelog:
Quoted 3 lines
Quoted 3 lines
Both printk tests do early checks for the number of found messages and can bail earlier, but I did not find any speed difference w/o that condition, so I did not complicate the change more for that.
I think it's fine
SNIP
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/bpf/trace_helpers.c +15
-0+int read_trace_pipe_iter (void (*cb)(const char *str,void *data),void *data,int iter) +{ +size_t buflen, n; +char *buf =NULL ; +FILE *fp =NULL ; + +if (access (TRACEFS_PIPE ,F_OK ) ==0 ) + fp =fopen (TRACEFS_PIPE ,"r" ); +else + fp =fopen (DEBUGFS_PIPE ,"r" ); +if (!fp) +return -1 ; + +/* We do not want to wait forever when iter is specified. */ +if (iter)
Quoted 8 lines
tools/testing/selftests/bpf/trace_helpers.c int read_build_id(const char *path, char *build_id, size_t size) · line 414
Quoted 6 lines
tools/testing/selftests/bpf/trace_helpers.c +6
-0+fcntl (fileno (fp),F_SETFL ,O_NONBLOCK ); + +while ((n =getline (&buf, &buflen, fp) >=0 ) || errno ==EAGAIN ) { +if (n >0 ) +cb (buf, data); +if (iter && !(--iter))"if (iter-- == 1)" should also work. But your code works too.
ok, keeping the current one ;-)
thanks,
jirka
Quoted 11 lines
Quoted 10 lines
tools/testing/selftests/bpf/trace_helpers.c +9
-0+break ; + } + +free (buf); +if (fp) +fclose (fp); +return 0 ; +} +[...]
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 9 lines · On Wed, 10 Apr 2024 16:09:52 +0200 you wrote:
We have two printk tests reading
trace_pipein non blocking way, with the very same code. Moving that in newread_trace_pipe_iterfunction.Current
read_trace_pipeis used from sampless/bpf and needs to do blocking read and printf of thetrace_pipedata, using newread_trace_pipe_iterto implement that.[...]
Here is the summary with links:
- [PATCHv2,bpf-next] selftests/bpf: Add read_trace_pipe_iter function
https://git.kernel.org/bpf/bpf-next/c/4d4992ff5876
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html