2023-06-02 15:18:27 -03:00
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/*
* uprobe . c
*
* uprobe benchmarks
*
* Copyright ( C ) 2023 , Red Hat Inc , Arnaldo Carvalho de Melo < acme @ redhat . com >
*/
# include "../perf.h"
# include "../util/util.h"
# include <subcmd/parse-options.h>
# include "../builtin.h"
# include "bench.h"
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
# include <linux/compiler.h>
2023-06-02 15:18:27 -03:00
# include <linux/time64.h>
# include <inttypes.h>
# include <stdio.h>
# include <sys/time.h>
# include <sys/types.h>
# include <time.h>
# include <unistd.h>
# include <stdlib.h>
# define LOOPS_DEFAULT 1000
static int loops = LOOPS_DEFAULT ;
2023-06-02 17:42:47 -03:00
enum bench_uprobe {
2024-04-05 21:09:11 -07:00
BENCH_UPROBE__BASELINE ,
BENCH_UPROBE__EMPTY ,
BENCH_UPROBE__TRACE_PRINTK ,
BENCH_UPROBE__EMPTY_RET ,
BENCH_UPROBE__TRACE_PRINTK_RET ,
2023-06-02 17:42:47 -03:00
} ;
2023-06-02 15:18:27 -03:00
static const struct option options [ ] = {
OPT_INTEGER ( ' l ' , " loop " , & loops , " Specify number of loops " ) ,
OPT_END ( )
} ;
static const char * const bench_uprobe_usage [ ] = {
" perf bench uprobe <options> " ,
NULL
} ;
2023-06-02 17:42:47 -03:00
# ifdef HAVE_BPF_SKEL
# include "bpf_skel/bench_uprobe.skel.h"
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
# define bench_uprobe__attach_uprobe(prog) \
skel - > links . prog = bpf_program__attach_uprobe_opts ( /*prog=*/ skel - > progs . prog , \
/*pid=*/ - 1 , \
2024-04-05 21:09:10 -07:00
/*binary_path=*/ " libc.so.6 " , \
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
/*func_offset=*/ 0 , \
/*opts=*/ & uprobe_opts ) ; \
if ( ! skel - > links . prog ) { \
err = - errno ; \
fprintf ( stderr , " Failed to attach bench uprobe \" %s \" : %s \n " , # prog , strerror ( errno ) ) ; \
goto cleanup ; \
}
2023-06-02 17:42:47 -03:00
struct bench_uprobe_bpf * skel ;
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
static int bench_uprobe__setup_bpf_skel ( enum bench_uprobe bench )
2023-06-02 17:42:47 -03:00
{
DECLARE_LIBBPF_OPTS ( bpf_uprobe_opts , uprobe_opts ) ;
int err ;
/* Load and verify BPF application */
skel = bench_uprobe_bpf__open ( ) ;
if ( ! skel ) {
fprintf ( stderr , " Failed to open and load uprobes bench BPF skeleton \n " ) ;
return - 1 ;
}
err = bench_uprobe_bpf__load ( skel ) ;
if ( err ) {
fprintf ( stderr , " Failed to load and verify BPF skeleton \n " ) ;
goto cleanup ;
}
uprobe_opts . func_name = " usleep " ;
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
switch ( bench ) {
case BENCH_UPROBE__BASELINE : break ;
case BENCH_UPROBE__EMPTY : bench_uprobe__attach_uprobe ( empty ) ; break ;
case BENCH_UPROBE__TRACE_PRINTK : bench_uprobe__attach_uprobe ( trace_printk ) ; break ;
2024-04-05 21:09:11 -07:00
case BENCH_UPROBE__EMPTY_RET : bench_uprobe__attach_uprobe ( empty_ret ) ; break ;
case BENCH_UPROBE__TRACE_PRINTK_RET : bench_uprobe__attach_uprobe ( trace_printk_ret ) ; break ;
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
default :
fprintf ( stderr , " Invalid bench: %d \n " , bench ) ;
2023-06-02 17:42:47 -03:00
goto cleanup ;
}
return err ;
cleanup :
bench_uprobe_bpf__destroy ( skel ) ;
2023-10-09 11:39:06 -07:00
skel = NULL ;
2023-06-02 17:42:47 -03:00
return err ;
}
static void bench_uprobe__teardown_bpf_skel ( void )
{
if ( skel ) {
bench_uprobe_bpf__destroy ( skel ) ;
skel = NULL ;
}
}
# else
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
static int bench_uprobe__setup_bpf_skel ( enum bench_uprobe bench __maybe_unused ) { return 0 ; }
2023-06-02 17:42:47 -03:00
static void bench_uprobe__teardown_bpf_skel ( void ) { } ;
# endif
2023-07-05 08:45:12 -03:00
static int bench_uprobe_format__default_fprintf ( const char * name , const char * unit , u64 diff , FILE * fp )
{
2023-07-05 08:49:40 -03:00
static u64 baseline , previous ;
s64 diff_to_baseline = diff - baseline ,
diff_to_previous = diff - previous ;
2023-07-05 08:45:12 -03:00
int printed = fprintf ( fp , " # Executed %'d %s calls \n " , loops , name ) ;
printed + = fprintf ( fp , " %14s: %' " PRIu64 " %ss " , " Total time " , diff , unit ) ;
2023-07-05 08:49:40 -03:00
if ( baseline ) {
2023-07-05 08:45:12 -03:00
printed + = fprintf ( fp , " %s%' " PRId64 " to baseline " , diff_to_baseline > 0 ? " + " : " " , diff_to_baseline ) ;
2023-07-05 08:49:40 -03:00
if ( previous ! = baseline )
fprintf ( stdout , " %s%' " PRId64 " to previous " , diff_to_previous > 0 ? " + " : " " , diff_to_previous ) ;
}
2023-07-05 08:45:12 -03:00
printed + = fprintf ( fp , " \n \n %'.3f %ss/op " , ( double ) diff / ( double ) loops , unit ) ;
2023-07-05 08:49:40 -03:00
if ( baseline ) {
2023-07-05 08:45:12 -03:00
printed + = fprintf ( fp , " %'.3f %ss/op to baseline " , ( double ) diff_to_baseline / ( double ) loops , unit ) ;
2023-07-05 08:49:40 -03:00
if ( previous ! = baseline )
printed + = fprintf ( fp , " %'.3f %ss/op to previous " , ( double ) diff_to_previous / ( double ) loops , unit ) ;
} else {
2023-07-05 08:45:12 -03:00
baseline = diff ;
2023-07-05 08:49:40 -03:00
}
2023-07-05 08:45:12 -03:00
fputc ( ' \n ' , fp ) ;
2023-07-05 08:49:40 -03:00
previous = diff ;
2023-07-05 08:45:12 -03:00
return printed + 1 ;
}
2023-06-02 17:42:47 -03:00
static int bench_uprobe ( int argc , const char * * argv , enum bench_uprobe bench )
2023-06-02 15:18:27 -03:00
{
const char * name = " usleep(1000) " , * unit = " usec " ;
struct timespec start , end ;
u64 diff ;
int i ;
argc = parse_options ( argc , argv , options , bench_uprobe_usage , 0 ) ;
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
if ( bench ! = BENCH_UPROBE__BASELINE & & bench_uprobe__setup_bpf_skel ( bench ) < 0 )
2023-06-02 17:42:47 -03:00
return 0 ;
clock_gettime ( CLOCK_REALTIME , & start ) ;
2023-06-02 15:18:27 -03:00
for ( i = 0 ; i < loops ; i + + ) {
usleep ( USEC_PER_MSEC ) ;
}
clock_gettime ( CLOCK_REALTIME , & end ) ;
diff = end . tv_sec * NSEC_PER_SEC + end . tv_nsec - ( start . tv_sec * NSEC_PER_SEC + start . tv_nsec ) ;
diff / = NSEC_PER_USEC ;
switch ( bench_format ) {
case BENCH_FORMAT_DEFAULT :
2023-07-05 08:45:12 -03:00
bench_uprobe_format__default_fprintf ( name , unit , diff , stdout ) ;
2023-06-02 15:18:27 -03:00
break ;
case BENCH_FORMAT_SIMPLE :
printf ( " % " PRIu64 " \n " , diff ) ;
break ;
default :
/* reaching here is something of a disaster */
fprintf ( stderr , " Unknown format:%d \n " , bench_format ) ;
exit ( 1 ) ;
}
2023-06-02 17:42:47 -03:00
if ( bench ! = BENCH_UPROBE__BASELINE )
bench_uprobe__teardown_bpf_skel ( ) ;
2023-06-02 15:18:27 -03:00
return 0 ;
}
int bench_uprobe_baseline ( int argc , const char * * argv )
{
2023-06-02 17:42:47 -03:00
return bench_uprobe ( argc , argv , BENCH_UPROBE__BASELINE ) ;
}
int bench_uprobe_empty ( int argc , const char * * argv )
{
return bench_uprobe ( argc , argv , BENCH_UPROBE__EMPTY ) ;
2023-06-02 15:18:27 -03:00
}
perf bench uprobe trace_printk: Add entry attaching an BPF program that does a trace_printk
[root@five ~]# perf bench uprobe all
# Running uprobe/baseline benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,053,963 usecs
1,053.963 usecs/op
# Running uprobe/empty benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,293 usecs +2,330 to baseline
1,056.293 usecs/op 2.330 usecs/op to baseline
# Running uprobe/trace_printk benchmark...
# Executed 1,000 usleep(1000) calls
Total time: 1,056,977 usecs +3,014 to baseline +684 to previous
1,056.977 usecs/op 3.014 usecs/op to baseline 0.684 usecs/op to previous
[root@five ~]#
Acked-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andre Fredette <anfredet@redhat.com>
Cc: Clark Williams <williams@redhat.com>
Cc: Dave Tucker <datucker@redhat.com>
Cc: Derek Barbosa <debarbos@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/20230719204910.539044-6-acme@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2023-06-02 17:42:47 -03:00
int bench_uprobe_trace_printk ( int argc , const char * * argv )
{
return bench_uprobe ( argc , argv , BENCH_UPROBE__TRACE_PRINTK ) ;
}
2024-04-05 21:09:11 -07:00
int bench_uprobe_empty_ret ( int argc , const char * * argv )
{
return bench_uprobe ( argc , argv , BENCH_UPROBE__EMPTY_RET ) ;
}
int bench_uprobe_trace_printk_ret ( int argc , const char * * argv )
{
return bench_uprobe ( argc , argv , BENCH_UPROBE__TRACE_PRINTK_RET ) ;
}