perf bench messaging: Factor out create_worker()
authorYang Jihong <yangjihong1@huawei.com>
Sat, 23 Sep 2023 09:30:35 +0000 (09:30 +0000)
committerNamhyung Kim <namhyung@kernel.org>
Wed, 27 Sep 2023 04:47:12 +0000 (21:47 -0700)
Refactor the create_worker() helper:
1. Modify the return value and use pthread pointer as a parameter to
   facilitate value assignment in create_worker().
2. The thread worker creation and process worker creation are abstracted
   into independent helpers.

No functional change.

Test result:

  # perf bench sched messaging
  # Running 'sched/messaging' benchmark:
  # 20 sender and receiver processes per group
  # 10 groups == 400 processes run

       Total time: 6.332 [sec]
  # perf bench sched messaging -t
  # Running 'sched/messaging' benchmark:
  # 20 sender and receiver threads per group
  # 10 groups == 400 threads run

       Total time: 5.545 [sec]

Signed-off-by: Yang Jihong <yangjihong1@huawei.com>
Reviewed-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20230923093037.961232-3-yangjihong1@huawei.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
tools/perf/bench/sched-messaging.c

index 6a33118c8f9bf1dc24ef51fe62145c87ba268f54..ad8596bed77a279b5f9b7282dacde4a60083229c 100644 (file)
@@ -139,30 +139,12 @@ again:
        return NULL;
 }
 
-static pthread_t create_worker(void *ctx, void *(*func)(void *))
+static void create_thread_worker(pthread_t *thread,
+                                void *ctx, void *(*func)(void *))
 {
        pthread_attr_t attr;
-       pthread_t childid;
        int ret;
 
-       if (!thread_mode) {
-               /* process mode */
-               /* Fork the receiver. */
-               switch (fork()) {
-               case -1:
-                       err(EXIT_FAILURE, "fork()");
-                       break;
-               case 0:
-                       (*func) (ctx);
-                       exit(0);
-                       break;
-               default:
-                       break;
-               }
-
-               return (pthread_t)0;
-       }
-
        if (pthread_attr_init(&attr) != 0)
                err(EXIT_FAILURE, "pthread_attr_init:");
 
@@ -171,12 +153,32 @@ static pthread_t create_worker(void *ctx, void *(*func)(void *))
                err(EXIT_FAILURE, "pthread_attr_setstacksize");
 #endif
 
-       ret = pthread_create(&childid, &attr, func, ctx);
+       ret = pthread_create(thread, &attr, func, ctx);
        if (ret != 0)
                err(EXIT_FAILURE, "pthread_create failed");
 
        pthread_attr_destroy(&attr);
-       return childid;
+}
+
+static void create_process_worker(void *ctx, void *(*func)(void *))
+{
+       /* Fork the receiver. */
+       pid_t pid = fork();
+
+       if (pid == -1) {
+               err(EXIT_FAILURE, "fork()");
+       } else if (pid == 0) {
+               (*func) (ctx);
+               exit(0);
+       }
+}
+
+static void create_worker(pthread_t *thread, void *ctx, void *(*func)(void *))
+{
+       if (!thread_mode)
+               return create_process_worker(ctx, func);
+       else
+               return create_thread_worker(thread, ctx, func);
 }
 
 static void reap_worker(pthread_t id)
@@ -226,7 +228,7 @@ static unsigned int group(pthread_t *pth,
                ctx->ready_out = ready_out;
                ctx->wakefd = wakefd;
 
-               pth[i] = create_worker(ctx, (void *)receiver);
+               create_worker(pth + i, ctx, (void *)receiver);
 
                snd_ctx->out_fds[i] = fds[1];
                if (!thread_mode)
@@ -239,7 +241,7 @@ static unsigned int group(pthread_t *pth,
                snd_ctx->wakefd = wakefd;
                snd_ctx->num_fds = num_fds;
 
-               pth[num_fds + i] = create_worker(snd_ctx, (void *)sender);
+               create_worker(pth + num_fds + i, snd_ctx, (void *)sender);
        }
 
        /* Close the fds we have left */