Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Ian Rogers and committed by
Namhyung Kim
35286478 2fee899c

+38 -7
+38 -7
tools/perf/tests/workloads/thloop.c
··· 31 31 32 32 static int thloop(int argc, const char **argv) 33 33 { 34 - int sec = 1; 35 - pthread_t th; 34 + int nt = 2, sec = 1, err = 1; 35 + pthread_t *thread_list = NULL; 36 36 37 37 if (argc > 0) 38 38 sec = atoi(argv[0]); 39 39 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 + 40 53 signal(SIGINT, sighandler); 41 54 signal(SIGALRM, sighandler); 55 + 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); 63 + 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 + } 42 70 alarm(sec); 43 - 44 - pthread_create(&th, NULL, thfunc, test_loop); 45 71 test_loop(); 46 - pthread_join(th, NULL); 47 - 48 - return 0; 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; 49 80 } 50 81 51 82 DEFINE_WORKLOAD(thloop);