Commit 5d205045 authored by Yang Jihong's avatar Yang Jihong Committed by Namhyung Kim

perf bench messaging: Factor out create_worker()

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: default avatarYang Jihong <yangjihong1@huawei.com>
Reviewed-by: default avatarIan Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20230923093037.961232-3-yangjihong1@huawei.comSigned-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
parent 8870261a
...@@ -139,30 +139,12 @@ static void *receiver(struct receiver_context* ctx) ...@@ -139,30 +139,12 @@ static void *receiver(struct receiver_context* ctx)
return NULL; 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_attr_t attr;
pthread_t childid;
int ret; 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) if (pthread_attr_init(&attr) != 0)
err(EXIT_FAILURE, "pthread_attr_init:"); err(EXIT_FAILURE, "pthread_attr_init:");
...@@ -171,12 +153,32 @@ static pthread_t create_worker(void *ctx, void *(*func)(void *)) ...@@ -171,12 +153,32 @@ static pthread_t create_worker(void *ctx, void *(*func)(void *))
err(EXIT_FAILURE, "pthread_attr_setstacksize"); err(EXIT_FAILURE, "pthread_attr_setstacksize");
#endif #endif
ret = pthread_create(&childid, &attr, func, ctx); ret = pthread_create(thread, &attr, func, ctx);
if (ret != 0) if (ret != 0)
err(EXIT_FAILURE, "pthread_create failed"); err(EXIT_FAILURE, "pthread_create failed");
pthread_attr_destroy(&attr); 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) static void reap_worker(pthread_t id)
...@@ -226,7 +228,7 @@ static unsigned int group(pthread_t *pth, ...@@ -226,7 +228,7 @@ static unsigned int group(pthread_t *pth,
ctx->ready_out = ready_out; ctx->ready_out = ready_out;
ctx->wakefd = wakefd; ctx->wakefd = wakefd;
pth[i] = create_worker(ctx, (void *)receiver); create_worker(pth + i, ctx, (void *)receiver);
snd_ctx->out_fds[i] = fds[1]; snd_ctx->out_fds[i] = fds[1];
if (!thread_mode) if (!thread_mode)
...@@ -239,7 +241,7 @@ static unsigned int group(pthread_t *pth, ...@@ -239,7 +241,7 @@ static unsigned int group(pthread_t *pth,
snd_ctx->wakefd = wakefd; snd_ctx->wakefd = wakefd;
snd_ctx->num_fds = num_fds; 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 */ /* Close the fds we have left */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment