linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] perf: Add more syscalls to benchmark
@ 2022-10-06  7:42 Tiezhu Yang
  2022-10-06  7:42 ` [PATCH v3 1/3] perf bench syscall: Introduce bench_syscall_common() Tiezhu Yang
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Tiezhu Yang @ 2022-10-06  7:42 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim
  Cc: linux-perf-users, linux-kernel

v3: Add __NR_ in tools/arch/x86/include/asm/unistd_*.h
    to fix build error about __NR_ can't be found on x86_64
    reported by kernel test robot, sorry for that.

Tiezhu Yang (3):
  perf bench syscall: Introduce bench_syscall_common()
  perf bench syscall: Add close syscall benchmark
  perf bench syscall: Add execve syscall benchmark

 tools/arch/x86/include/uapi/asm/unistd_32.h |  9 ++++
 tools/arch/x86/include/uapi/asm/unistd_64.h |  9 ++++
 tools/perf/bench/bench.h                    |  2 +
 tools/perf/bench/syscall.c                  | 76 +++++++++++++++++++++++++++--
 tools/perf/builtin-bench.c                  |  2 +
 5 files changed, 94 insertions(+), 4 deletions(-)

-- 
2.1.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 1/3] perf bench syscall: Introduce bench_syscall_common()
  2022-10-06  7:42 [PATCH v3 0/3] perf: Add more syscalls to benchmark Tiezhu Yang
@ 2022-10-06  7:42 ` Tiezhu Yang
  2022-10-06  7:42 ` [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark Tiezhu Yang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2022-10-06  7:42 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim
  Cc: linux-perf-users, linux-kernel

In the current code, there is only a basic syscall benchmark via getppid,
this is not enough. Introduce bench_syscall_common() so that we can add
more syscalls to benchmark. This is preparation for later patch.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/arch/x86/include/uapi/asm/unistd_32.h |  3 +++
 tools/arch/x86/include/uapi/asm/unistd_64.h |  3 +++
 tools/perf/bench/syscall.c                  | 29 +++++++++++++++++++++++++----
 3 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/tools/arch/x86/include/uapi/asm/unistd_32.h b/tools/arch/x86/include/uapi/asm/unistd_32.h
index 60a89db..4a480a0 100644
--- a/tools/arch/x86/include/uapi/asm/unistd_32.h
+++ b/tools/arch/x86/include/uapi/asm/unistd_32.h
@@ -5,6 +5,9 @@
 #ifndef __NR_futex
 # define __NR_futex 240
 #endif
+#ifndef __NR_getppid
+# define __NR_getppid 64
+#endif
 #ifndef __NR_gettid
 # define __NR_gettid 224
 #endif
diff --git a/tools/arch/x86/include/uapi/asm/unistd_64.h b/tools/arch/x86/include/uapi/asm/unistd_64.h
index cb52a3a..860257f 100644
--- a/tools/arch/x86/include/uapi/asm/unistd_64.h
+++ b/tools/arch/x86/include/uapi/asm/unistd_64.h
@@ -5,6 +5,9 @@
 #ifndef __NR_futex
 # define __NR_futex 202
 #endif
+#ifndef __NR_getppid
+# define __NR_getppid 110
+#endif
 #ifndef __NR_gettid
 # define __NR_gettid 186
 #endif
diff --git a/tools/perf/bench/syscall.c b/tools/perf/bench/syscall.c
index 9b75101..746fd71 100644
--- a/tools/perf/bench/syscall.c
+++ b/tools/perf/bench/syscall.c
@@ -30,25 +30,41 @@ static const char * const bench_syscall_usage[] = {
 	NULL
 };
 
-int bench_syscall_basic(int argc, const char **argv)
+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;
 
 	argc = parse_options(argc, argv, options, bench_syscall_usage, 0);
 
 	gettimeofday(&start, NULL);
 
-	for (i = 0; i < loops; i++)
-		getppid();
+	for (i = 0; i < loops; i++) {
+		switch (syscall) {
+		case __NR_getppid:
+			getppid();
+			break;
+		default:
+			break;
+		}
+	}
 
 	gettimeofday(&stop, NULL);
 	timersub(&stop, &start, &diff);
 
+	switch (syscall) {
+	case __NR_getppid:
+		name = "getppid()";
+		break;
+	default:
+		break;
+	}
+
 	switch (bench_format) {
 	case BENCH_FORMAT_DEFAULT:
-		printf("# Executed %'d getppid() calls\n", loops);
+		printf("# Executed %'d %s calls\n", loops, name);
 
 		result_usec = diff.tv_sec * 1000000;
 		result_usec += diff.tv_usec;
@@ -79,3 +95,8 @@ int bench_syscall_basic(int argc, const char **argv)
 
 	return 0;
 }
+
+int bench_syscall_basic(int argc, const char **argv)
+{
+	return bench_syscall_common(argc, argv, __NR_getppid);
+}
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark
  2022-10-06  7:42 [PATCH v3 0/3] perf: Add more syscalls to benchmark Tiezhu Yang
  2022-10-06  7:42 ` [PATCH v3 1/3] perf bench syscall: Introduce bench_syscall_common() Tiezhu Yang
@ 2022-10-06  7:42 ` Tiezhu Yang
  2022-10-25 15:37   ` Ian Rogers
  2022-10-06  7:42 ` [PATCH v3 3/3] perf bench syscall: Add execve " Tiezhu Yang
  2022-10-25  7:03 ` [PATCH v3 0/3] perf: Add more syscalls to benchmark Tiezhu Yang
  3 siblings, 1 reply; 7+ messages in thread
From: Tiezhu Yang @ 2022-10-06  7:42 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim
  Cc: linux-perf-users, linux-kernel

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 <yangtiezhu@loongson.cn>
---
 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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 3/3] perf bench syscall: Add execve syscall benchmark
  2022-10-06  7:42 [PATCH v3 0/3] perf: Add more syscalls to benchmark Tiezhu Yang
  2022-10-06  7:42 ` [PATCH v3 1/3] perf bench syscall: Introduce bench_syscall_common() Tiezhu Yang
  2022-10-06  7:42 ` [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark Tiezhu Yang
@ 2022-10-06  7:42 ` Tiezhu Yang
  2022-10-25  7:03 ` [PATCH v3 0/3] perf: Add more syscalls to benchmark Tiezhu Yang
  3 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2022-10-06  7:42 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim
  Cc: linux-perf-users, linux-kernel

This commit adds the execve 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
          execve: Benchmark for execve(2) calls
             all: Run all syscall benchmarks

  [loongson@linux perf]$ ./perf bench syscall execve
  # Running 'syscall/execve' benchmark:
  # Executed 10000 execve() calls
       Total time: 4.827 [sec]

       482.761800 usecs/op
             2071 ops/sec
  [loongson@linux perf]$ ./perf bench syscall all
  # Running syscall/basic benchmark...
  # Executed 10000000 getppid() calls
       Total time: 1.956 [sec]

         0.195692 usecs/op
          5110065 ops/sec

  # Running syscall/close benchmark...
  # Executed 10000000 close() calls
       Total time: 6.306 [sec]

         0.630692 usecs/op
          1585558 ops/sec

  # Running syscall/execve benchmark...
  # Executed 10000 execve() calls
       Total time: 4.842 [sec]

       484.299500 usecs/op
             2064 ops/sec

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 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                  | 36 +++++++++++++++++++++++++++++
 tools/perf/builtin-bench.c                  |  1 +
 5 files changed, 44 insertions(+)

diff --git a/tools/arch/x86/include/uapi/asm/unistd_32.h b/tools/arch/x86/include/uapi/asm/unistd_32.h
index 2f24b0eb..0c65a2c 100644
--- a/tools/arch/x86/include/uapi/asm/unistd_32.h
+++ b/tools/arch/x86/include/uapi/asm/unistd_32.h
@@ -5,6 +5,9 @@
 #ifndef __NR_close
 # define __NR_close 6
 #endif
+#ifndef __NR_execve
+# define __NR_execve 11
+#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 8eb32b2..7427910 100644
--- a/tools/arch/x86/include/uapi/asm/unistd_64.h
+++ b/tools/arch/x86/include/uapi/asm/unistd_64.h
@@ -5,6 +5,9 @@
 #ifndef __NR_close
 # define __NR_close 3
 #endif
+#ifndef __NR_execve
+# define __NR_execve 59
+#endif
 #ifndef __NR_futex
 # define __NR_futex 202
 #endif
diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h
index 916cd47..6ca3327 100644
--- a/tools/perf/bench/bench.h
+++ b/tools/perf/bench/bench.h
@@ -35,6 +35,7 @@ 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_syscall_execve(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 058394b..c8f8bee 100644
--- a/tools/perf/bench/syscall.c
+++ b/tools/perf/bench/syscall.c
@@ -14,6 +14,7 @@
 #include <sys/time.h>
 #include <sys/syscall.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 #include <stdlib.h>
 
@@ -30,6 +31,27 @@ static const char * const bench_syscall_usage[] = {
 	NULL
 };
 
+static void test_execve(void)
+{
+	const char *pathname = "/bin/true";
+	char *const argv[] = { (char *)pathname, NULL };
+	pid_t pid = fork();
+
+	if (pid < 0) {
+		fprintf(stderr, "fork failed\n");
+		exit(1);
+	} else if (pid == 0) {
+		execve(pathname, argv, NULL);
+		fprintf(stderr, "execve /bin/true failed\n");
+		exit(1);
+	} else {
+		if (waitpid(pid, NULL, 0) < 0) {
+			fprintf(stderr, "waitpid failed\n");
+			exit(1);
+		}
+	}
+}
+
 static int bench_syscall_common(int argc, const char **argv, int syscall)
 {
 	struct timeval start, stop, diff;
@@ -49,6 +71,12 @@ static int bench_syscall_common(int argc, const char **argv, int syscall)
 		case __NR_close:
 			close(dup(0));
 			break;
+		case __NR_execve:
+			test_execve();
+			/* Only loop 10000 times to save time */
+			if (i == 10000)
+				loops = 10000;
+			break;
 		default:
 			break;
 		}
@@ -64,6 +92,9 @@ static int bench_syscall_common(int argc, const char **argv, int syscall)
 	case __NR_close:
 		name = "close()";
 		break;
+	case __NR_execve:
+		name = "execve()";
+		break;
 	default:
 		break;
 	}
@@ -111,3 +142,8 @@ int bench_syscall_close(int argc, const char **argv)
 {
 	return bench_syscall_common(argc, argv, __NR_close);
 }
+
+int bench_syscall_execve(int argc, const char **argv)
+{
+	return bench_syscall_common(argc, argv, __NR_execve);
+}
diff --git a/tools/perf/builtin-bench.c b/tools/perf/builtin-bench.c
index b63c711..6f9ff75 100644
--- a/tools/perf/builtin-bench.c
+++ b/tools/perf/builtin-bench.c
@@ -53,6 +53,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	},
+	{ "execve",	"Benchmark for execve(2) calls",		bench_syscall_execve	},
 	{ "all",	"Run all syscall benchmarks",			NULL			},
 	{ NULL,		NULL,						NULL			},
 };
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] perf: Add more syscalls to benchmark
  2022-10-06  7:42 [PATCH v3 0/3] perf: Add more syscalls to benchmark Tiezhu Yang
                   ` (2 preceding siblings ...)
  2022-10-06  7:42 ` [PATCH v3 3/3] perf bench syscall: Add execve " Tiezhu Yang
@ 2022-10-25  7:03 ` Tiezhu Yang
  3 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2022-10-25  7:03 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim
  Cc: linux-perf-users, linux-kernel



On 10/06/2022 03:42 PM, Tiezhu Yang wrote:
> v3: Add __NR_ in tools/arch/x86/include/asm/unistd_*.h
>     to fix build error about __NR_ can't be found on x86_64
>     reported by kernel test robot, sorry for that.
>
> Tiezhu Yang (3):
>   perf bench syscall: Introduce bench_syscall_common()
>   perf bench syscall: Add close syscall benchmark
>   perf bench syscall: Add execve syscall benchmark
>
>  tools/arch/x86/include/uapi/asm/unistd_32.h |  9 ++++
>  tools/arch/x86/include/uapi/asm/unistd_64.h |  9 ++++
>  tools/perf/bench/bench.h                    |  2 +
>  tools/perf/bench/syscall.c                  | 76 +++++++++++++++++++++++++++--
>  tools/perf/builtin-bench.c                  |  2 +
>  5 files changed, 94 insertions(+), 4 deletions(-)
>

Hi Arnaldo,

Any comments? Are you OK with this series?

Thanks,
Tiezhu


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark
  2022-10-06  7:42 ` [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark Tiezhu Yang
@ 2022-10-25 15:37   ` Ian Rogers
  2022-10-26  4:37     ` Tiezhu Yang
  0 siblings, 1 reply; 7+ messages in thread
From: Ian Rogers @ 2022-10-25 15:37 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	linux-perf-users, linux-kernel

On Thu, Oct 6, 2022 at 12:42 AM Tiezhu Yang <yangtiezhu@loongson.cn> 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 <yangtiezhu@loongson.cn>
> ---
>  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
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark
  2022-10-25 15:37   ` Ian Rogers
@ 2022-10-26  4:37     ` Tiezhu Yang
  0 siblings, 0 replies; 7+ messages in thread
From: Tiezhu Yang @ 2022-10-26  4:37 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	linux-perf-users, linux-kernel



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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-10-26  4:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-06  7:42 [PATCH v3 0/3] perf: Add more syscalls to benchmark Tiezhu Yang
2022-10-06  7:42 ` [PATCH v3 1/3] perf bench syscall: Introduce bench_syscall_common() Tiezhu Yang
2022-10-06  7:42 ` [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark Tiezhu Yang
2022-10-25 15:37   ` Ian Rogers
2022-10-26  4:37     ` Tiezhu Yang
2022-10-06  7:42 ` [PATCH v3 3/3] perf bench syscall: Add execve " Tiezhu Yang
2022-10-25  7:03 ` [PATCH v3 0/3] perf: Add more syscalls to benchmark Tiezhu Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).