linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tiezhu Yang <yangtiezhu@loongson.cn>
To: Ian Rogers <irogers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/3] perf bench syscall: Add close syscall benchmark
Date: Wed, 26 Oct 2022 12:37:42 +0800	[thread overview]
Message-ID: <6a2d2031-0773-e1af-027f-8939ce596d52@loongson.cn> (raw)
In-Reply-To: <CAP-5=fX8_Q3EujmfBXF_66y=n+VUyeBZtzjZU9WGj4QBN2TZ5w@mail.gmail.com>



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


  reply	other threads:[~2022-10-26  4:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a2d2031-0773-e1af-027f-8939ce596d52@loongson.cn \
    --to=yangtiezhu@loongson.cn \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).