This commit adds a simple close syscall benchmark, more syscall
benchmarks can be added in the future.
Here are the test results:
[loongson@linux perf]$ ./perf bench syscall
# List of available benchmarks for collection 'syscall':
basic: Benchmark for basic getppid(2) calls
close: Benchmark for close(2) calls
all: Run all syscall benchmarks
[loongson@linux perf]$ ./perf bench syscall basic
# Running 'syscall/basic' benchmark:
# Executed 10000000 getppid() calls
Total time: 1.956 [sec]
0.195687 usecs/op
5110201 ops/sec
[loongson@linux perf]$ ./perf bench syscall close
# Running 'syscall/close' benchmark:
# Executed 10000000 close() calls
Total time: 6.302 [sec]
0.630297 usecs/op
1586553 ops/sec
[loongson@linux perf]$ ./perf bench syscall all
# Running syscall/basic benchmark...
# Executed 10000000 getppid() calls
Total time: 1.956 [sec]
0.195686 usecs/op
5110232 ops/sec
# Running syscall/close benchmark...
# Executed 10000000 close() calls
Total time: 6.302 [sec]
0.630271 usecs/op
1586619 ops/sec
Signed-off-by: Tiezhu Yang <[email protected]>
---
tools/arch/x86/include/uapi/asm/unistd_32.h | 3 +++
tools/arch/x86/include/uapi/asm/unistd_64.h | 3 +++
tools/perf/bench/bench.h | 1 +
tools/perf/bench/syscall.c | 11 +++++++++++
tools/perf/builtin-bench.c | 1 +
5 files changed, 19 insertions(+)
diff --git a/tools/arch/x86/include/uapi/asm/unistd_32.h b/tools/arch/x86/include/uapi/asm/unistd_32.h
index 4a480a0..2f24b0eb 100644
--- a/tools/arch/x86/include/uapi/asm/unistd_32.h
+++ b/tools/arch/x86/include/uapi/asm/unistd_32.h
@@ -2,6 +2,9 @@
#ifndef __NR_perf_event_open
# define __NR_perf_event_open 336
#endif
+#ifndef __NR_close
+# define __NR_close 6
+#endif
#ifndef __NR_futex
# define __NR_futex 240
#endif
diff --git a/tools/arch/x86/include/uapi/asm/unistd_64.h b/tools/arch/x86/include/uapi/asm/unistd_64.h
index 860257f..8eb32b2 100644
--- a/tools/arch/x86/include/uapi/asm/unistd_64.h
+++ b/tools/arch/x86/include/uapi/asm/unistd_64.h
@@ -2,6 +2,9 @@
#ifndef __NR_perf_event_open
# define __NR_perf_event_open 298
#endif
+#ifndef __NR_close
+# define __NR_close 3
+#endif
#ifndef __NR_futex
# define __NR_futex 202
#endif
diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
index 6cefb43..916cd47 100644
--- a/tools/perf/bench/bench.h
+++ b/tools/perf/bench/bench.h
@@ -34,6 +34,7 @@ int bench_numa(int argc, const char **argv);
int bench_sched_messaging(int argc, const char **argv);
int bench_sched_pipe(int argc, const char **argv);
int bench_syscall_basic(int argc, const char **argv);
+int bench_syscall_close(int argc, const char **argv);
int bench_mem_memcpy(int argc, const char **argv);
int bench_mem_memset(int argc, const char **argv);
int bench_mem_find_bit(int argc, const char **argv);
diff --git a/tools/perf/bench/syscall.c b/tools/perf/bench/syscall.c
index 746fd71..058394b 100644
--- a/tools/perf/bench/syscall.c
+++ b/tools/perf/bench/syscall.c
@@ -46,6 +46,9 @@ static int bench_syscall_common(int argc, const char **argv, int syscall)
case __NR_getppid:
getppid();
break;
+ case __NR_close:
+ close(dup(0));
+ break;
default:
break;
}
@@ -58,6 +61,9 @@ static int bench_syscall_common(int argc, const char **argv, int syscall)
case __NR_getppid:
name = "getppid()";
break;
+ case __NR_close:
+ name = "close()";
+ break;
default:
break;
}
@@ -100,3 +106,8 @@ int bench_syscall_basic(int argc, const char **argv)
{
return bench_syscall_common(argc, argv, __NR_getppid);
}
+
+int bench_syscall_close(int argc, const char **argv)
+{
+ return bench_syscall_common(argc, argv, __NR_close);
+}
diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
index 334ab89..b63c711 100644
--- a/tools/perf/builtin-bench.c
+++ b/tools/perf/builtin-bench.c
@@ -52,6 +52,7 @@ static struct bench sched_benchmarks[] = {
static struct bench syscall_benchmarks[] = {
{ "basic", "Benchmark for basic getppid(2) calls", bench_syscall_basic },
+ { "close", "Benchmark for close(2) calls", bench_syscall_close },
{ "all", "Run all syscall benchmarks", NULL },
{ NULL, NULL, NULL },
};
--
2.1.0
On Thu, Oct 6, 2022 at 12:42 AM Tiezhu Yang <[email protected]> wrote:
>
> This commit adds a simple close syscall benchmark, more syscall
> benchmarks can be added in the future.
>
> Here are the test results:
>
> [loongson@linux perf]$ ./perf bench syscall
>
> # List of available benchmarks for collection 'syscall':
>
> basic: Benchmark for basic getppid(2) calls
> close: Benchmark for close(2) calls
> all: Run all syscall benchmarks
>
> [loongson@linux perf]$ ./perf bench syscall basic
> # Running 'syscall/basic' benchmark:
> # Executed 10000000 getppid() calls
> Total time: 1.956 [sec]
>
> 0.195687 usecs/op
> 5110201 ops/sec
> [loongson@linux perf]$ ./perf bench syscall close
> # Running 'syscall/close' benchmark:
> # Executed 10000000 close() calls
> Total time: 6.302 [sec]
>
> 0.630297 usecs/op
> 1586553 ops/sec
> [loongson@linux perf]$ ./perf bench syscall all
> # Running syscall/basic benchmark...
> # Executed 10000000 getppid() calls
> Total time: 1.956 [sec]
>
> 0.195686 usecs/op
> 5110232 ops/sec
>
> # Running syscall/close benchmark...
> # Executed 10000000 close() calls
> Total time: 6.302 [sec]
>
> 0.630271 usecs/op
> 1586619 ops/sec
>
> Signed-off-by: Tiezhu Yang <[email protected]>
> ---
> tools/arch/x86/include/uapi/asm/unistd_32.h | 3 +++
> tools/arch/x86/include/uapi/asm/unistd_64.h | 3 +++
> tools/perf/bench/bench.h | 1 +
> tools/perf/bench/syscall.c | 11 +++++++++++
> tools/perf/builtin-bench.c | 1 +
> 5 files changed, 19 insertions(+)
>
> diff --git a/tools/arch/x86/include/uapi/asm/unistd_32.h b/tools/arch/x86/include/uapi/asm/unistd_32.h
> index 4a480a0..2f24b0eb 100644
> --- a/tools/arch/x86/include/uapi/asm/unistd_32.h
> +++ b/tools/arch/x86/include/uapi/asm/unistd_32.h
> @@ -2,6 +2,9 @@
> #ifndef __NR_perf_event_open
> # define __NR_perf_event_open 336
> #endif
> +#ifndef __NR_close
> +# define __NR_close 6
> +#endif
> #ifndef __NR_futex
> # define __NR_futex 240
> #endif
> diff --git a/tools/arch/x86/include/uapi/asm/unistd_64.h b/tools/arch/x86/include/uapi/asm/unistd_64.h
> index 860257f..8eb32b2 100644
> --- a/tools/arch/x86/include/uapi/asm/unistd_64.h
> +++ b/tools/arch/x86/include/uapi/asm/unistd_64.h
> @@ -2,6 +2,9 @@
> #ifndef __NR_perf_event_open
> # define __NR_perf_event_open 298
> #endif
> +#ifndef __NR_close
> +# define __NR_close 3
> +#endif
> #ifndef __NR_futex
> # define __NR_futex 202
> #endif
> diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
> index 6cefb43..916cd47 100644
> --- a/tools/perf/bench/bench.h
> +++ b/tools/perf/bench/bench.h
> @@ -34,6 +34,7 @@ int bench_numa(int argc, const char **argv);
> int bench_sched_messaging(int argc, const char **argv);
> int bench_sched_pipe(int argc, const char **argv);
> int bench_syscall_basic(int argc, const char **argv);
> +int bench_syscall_close(int argc, const char **argv);
> int bench_mem_memcpy(int argc, const char **argv);
> int bench_mem_memset(int argc, const char **argv);
> int bench_mem_find_bit(int argc, const char **argv);
> diff --git a/tools/perf/bench/syscall.c b/tools/perf/bench/syscall.c
> index 746fd71..058394b 100644
> --- a/tools/perf/bench/syscall.c
> +++ b/tools/perf/bench/syscall.c
> @@ -46,6 +46,9 @@ static int bench_syscall_common(int argc, const char **argv, int syscall)
> case __NR_getppid:
> getppid();
> break;
> + case __NR_close:
> + close(dup(0));
Thanks for contributing! This benchmark will compute the cost of close
and dup, naively dup could perform memory allocation and be slow.
Perhaps a number of file descriptors could be made outside of the
timed region?
Thanks,
Ian
> + break;
> default:
> break;
> }
> @@ -58,6 +61,9 @@ static int bench_syscall_common(int argc, const char **argv, int syscall)
> case __NR_getppid:
> name = "getppid()";
> break;
> + case __NR_close:
> + name = "close()";
> + break;
> default:
> break;
> }
> @@ -100,3 +106,8 @@ int bench_syscall_basic(int argc, const char **argv)
> {
> return bench_syscall_common(argc, argv, __NR_getppid);
> }
> +
> +int bench_syscall_close(int argc, const char **argv)
> +{
> + return bench_syscall_common(argc, argv, __NR_close);
> +}
> diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
> index 334ab89..b63c711 100644
> --- a/tools/perf/builtin-bench.c
> +++ b/tools/perf/builtin-bench.c
> @@ -52,6 +52,7 @@ static struct bench sched_benchmarks[] = {
>
> static struct bench syscall_benchmarks[] = {
> { "basic", "Benchmark for basic getppid(2) calls", bench_syscall_basic },
> + { "close", "Benchmark for close(2) calls", bench_syscall_close },
> { "all", "Run all syscall benchmarks", NULL },
> { NULL, NULL, NULL },
> };
> --
> 2.1.0
>
On 10/25/2022 11:37 PM, Ian Rogers wrote:
> On Thu, Oct 6, 2022 at 12:42 AM Tiezhu Yang <[email protected]> wrote:
>>
>> This commit adds a simple close syscall benchmark, more syscall
>> benchmarks can be added in the future.
>>
...
>> diff --git a/tools/perf/bench/syscall.c b/tools/perf/bench/syscall.c
>> index 746fd71..058394b 100644
>> --- a/tools/perf/bench/syscall.c
>> +++ b/tools/perf/bench/syscall.c
>> @@ -46,6 +46,9 @@ static int bench_syscall_common(int argc, const char **argv, int syscall)
>> case __NR_getppid:
>> getppid();
>> break;
>> + case __NR_close:
>> + close(dup(0));
>
> Thanks for contributing! This benchmark will compute the cost of close
> and dup, naively dup could perform memory allocation and be slow.
> Perhaps a number of file descriptors could be made outside of the
> timed region?
>
Hi Ian,
Thanks for your review and suggestion.
I tried the following changes based on this patchset, it shows
"dup failed" due to the default number of "max open files"
(ulimit -n) is 1024 but the loops is 10000000, if reduce the
loops to 1000, the test time is too short and seems meaningless.
diff --git a/tools/perf/bench/syscall.c b/tools/perf/bench/syscall.c
index c8f8bee..76571a4 100644
--- a/tools/perf/bench/syscall.c
+++ b/tools/perf/bench/syscall.c
@@ -57,10 +57,26 @@ static int bench_syscall_common(int argc, const char
**argv, int syscall)
struct timeval start, stop, diff;
unsigned long long result_usec = 0;
const char *name = NULL;
- int i;
+ int i, *fd = NULL;
argc = parse_options(argc, argv, options, bench_syscall_usage, 0);
+ if (syscall == __NR_close) {
+ fd = (int *) malloc(sizeof(int) * loops);
+ if (fd == NULL) {
+ fprintf(stderr, "malloc failed\n");
+ exit(1);
+ }
+
+ for (i = 0; i < loops; i++) {
+ fd[i] = dup(i);
+ if (fd[i] < 0) {
+ fprintf(stderr, "dup failed\n");
+ exit(1);
+ }
+ }
+ }
+
gettimeofday(&start, NULL);
for (i = 0; i < loops; i++) {
@@ -69,7 +85,7 @@ static int bench_syscall_common(int argc, const char
**argv, int syscall)
getppid();
break;
case __NR_close:
- close(dup(0));
+ close(fd[i]);
break;
case __NR_execve:
test_execve();
@@ -85,6 +101,9 @@ static int bench_syscall_common(int argc, const char
**argv, int syscall)
gettimeofday(&stop, NULL);
timersub(&stop, &start, &diff);
+ if (syscall == __NR_close)
+ free(fd);
+
switch (syscall) {
case __NR_getppid:
name = "getppid()";
What about the following changes? Use "open" instead of "dup" to
generate fd (it nees more test time), or just leave the code as
it is?
diff --git a/tools/perf/bench/syscall.c b/tools/perf/bench/syscall.c
index c8f8bee..3aab5fd 100644
--- a/tools/perf/bench/syscall.c
+++ b/tools/perf/bench/syscall.c
@@ -57,7 +57,7 @@ static int bench_syscall_common(int argc, const char
**argv, int syscall)
struct timeval start, stop, diff;
unsigned long long result_usec = 0;
const char *name = NULL;
- int i;
+ int i, fd;
argc = parse_options(argc, argv, options, bench_syscall_usage, 0);
@@ -69,7 +69,12 @@ static int bench_syscall_common(int argc, const char
**argv, int syscall)
getppid();
break;
case __NR_close:
- close(dup(0));
+ fd = open("/etc/passwd", O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "open failed\n");
+ exit(1);
+ }
+ close(fd);
break;
case __NR_execve:
test_execve();
Thanks,
Tiezhu