Skip to content

Commit 3528647

Browse files
captain5050namhyung
authored andcommitted
perf test workload: Add thread count argument to thloop
Allow the number of threads for the thloop workload to be increased beyond the normal 2. Add error checking to the parsed time and thread count values. Signed-off-by: Ian Rogers <irogers@google.com> Acked-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 2fee899 commit 3528647

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

tools/perf/tests/workloads/thloop.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,52 @@ static void *thfunc(void *arg)
3131

3232
static int thloop(int argc, const char **argv)
3333
{
34-
int sec = 1;
35-
pthread_t th;
34+
int nt = 2, sec = 1, err = 1;
35+
pthread_t *thread_list = NULL;
3636

3737
if (argc > 0)
3838
sec = atoi(argv[0]);
3939

40+
if (sec <= 0) {
41+
fprintf(stderr, "Error: seconds (%d) must be >= 1\n", sec);
42+
return 1;
43+
}
44+
45+
if (argc > 1)
46+
nt = atoi(argv[1]);
47+
48+
if (nt <= 0) {
49+
fprintf(stderr, "Error: thread count (%d) must be >= 1\n", nt);
50+
return 1;
51+
}
52+
4053
signal(SIGINT, sighandler);
4154
signal(SIGALRM, sighandler);
42-
alarm(sec);
4355

44-
pthread_create(&th, NULL, thfunc, test_loop);
45-
test_loop();
46-
pthread_join(th, NULL);
56+
thread_list = calloc(nt, sizeof(pthread_t));
57+
if (thread_list == NULL) {
58+
fprintf(stderr, "Error: malloc failed for %d threads\n", nt);
59+
goto out;
60+
}
61+
for (int i = 1; i < nt; i++) {
62+
int ret = pthread_create(&thread_list[i], NULL, thfunc, test_loop);
4763

48-
return 0;
64+
if (ret) {
65+
fprintf(stderr, "Error: failed to create thread %d\n", i);
66+
done = 1; // Ensure started threads terminate.
67+
goto out;
68+
}
69+
}
70+
alarm(sec);
71+
test_loop();
72+
err = 0;
73+
out:
74+
for (int i = 1; i < nt; i++) {
75+
if (thread_list && thread_list[i])
76+
pthread_join(thread_list[i], /*retval=*/NULL);
77+
}
78+
free(thread_list);
79+
return err;
4980
}
5081

5182
DEFINE_WORKLOAD(thloop);

0 commit comments

Comments
 (0)