Re: [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark

From: Tiezhu Yang
Date: Wed Oct 26 2022 - 00:37:51 EST




On 10/25/2022 11:37 PM, Ian Rogers wrote:
On Thu, Oct 6, 2022 at 12:42 AM Tiezhu Yang <yangtiezhu@xxxxxxxxxxx> 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