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

perf bench: Update use of pthread mutex/cond

Switch to the use of mutex wrappers that provide better error checking.

Signed-off-by: Ian Rogers <irogers@google.com>
Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Truong <alexandre.truong@arm.com>
Cc: Alexey Bayduraev <alexey.v.bayduraev@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andres Freund <andres@anarazel.de>
Cc: Andrii Nakryiko <andrii@kernel.org>
Cc: André Almeida <andrealmeid@igalia.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Cc: Colin Ian King <colin.king@intel.com>
Cc: Dario Petrillo <dario.pk1@gmail.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Dave Marchevsky <davemarchevsky@fb.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Fangrui Song <maskray@google.com>
Cc: Hewenliang <hewenliang4@huawei.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jason Wang <wangborong@cdjrlc.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kim Phillips <kim.phillips@amd.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Martin Liška <mliska@suse.cz>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Pavithra Gurushankar <gpavithrasha@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Quentin Monnet <quentin@isovalent.com>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Remi Bernon <rbernon@codeweavers.com>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Song Liu <songliubraving@fb.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Tom Rix <trix@redhat.com>
Cc: Weiguo Li <liwg06@foxmail.com>
Cc: Wenyu Liu <liuwenyu7@huawei.com>
Cc: William Cohen <wcohen@redhat.com>
Cc: Zechuan Chen <chenzechuan1@huawei.com>
Cc: bpf@vger.kernel.org
Cc: llvm@lists.linux.dev
Cc: yaowenbin <yaowenbin1@huawei.com>
Link: https://lore.kernel.org/r/20220826164242.43412-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
a64d3af5 e57d8977

+153 -171
+17 -16
tools/perf/bench/epoll-ctl.c
··· 23 23 #include <sys/eventfd.h> 24 24 #include <perf/cpumap.h> 25 25 26 + #include "../util/mutex.h" 26 27 #include "../util/stat.h" 27 28 #include <subcmd/parse-options.h> 28 29 #include "bench.h" ··· 59 58 /* amount of fds to monitor, per thread */ 60 59 static unsigned int nfds = 64; 61 60 62 - static pthread_mutex_t thread_lock; 61 + static struct mutex thread_lock; 63 62 static unsigned int threads_starting; 64 63 static struct stats all_stats[EPOLL_NR_OPS]; 65 - static pthread_cond_t thread_parent, thread_worker; 64 + static struct cond thread_parent, thread_worker; 66 65 67 66 struct worker { 68 67 int tid; ··· 175 174 struct timespec ts = { .tv_sec = 0, 176 175 .tv_nsec = 250 }; 177 176 178 - pthread_mutex_lock(&thread_lock); 177 + mutex_lock(&thread_lock); 179 178 threads_starting--; 180 179 if (!threads_starting) 181 - pthread_cond_signal(&thread_parent); 182 - pthread_cond_wait(&thread_worker, &thread_lock); 183 - pthread_mutex_unlock(&thread_lock); 180 + cond_signal(&thread_parent); 181 + cond_wait(&thread_worker, &thread_lock); 182 + mutex_unlock(&thread_lock); 184 183 185 184 /* Let 'em loose */ 186 185 do { ··· 368 367 for (i = 0; i < EPOLL_NR_OPS; i++) 369 368 init_stats(&all_stats[i]); 370 369 371 - pthread_mutex_init(&thread_lock, NULL); 372 - pthread_cond_init(&thread_parent, NULL); 373 - pthread_cond_init(&thread_worker, NULL); 370 + mutex_init(&thread_lock); 371 + cond_init(&thread_parent); 372 + cond_init(&thread_worker); 374 373 375 374 threads_starting = nthreads; 376 375 ··· 378 377 379 378 do_threads(worker, cpu); 380 379 381 - pthread_mutex_lock(&thread_lock); 380 + mutex_lock(&thread_lock); 382 381 while (threads_starting) 383 - pthread_cond_wait(&thread_parent, &thread_lock); 384 - pthread_cond_broadcast(&thread_worker); 385 - pthread_mutex_unlock(&thread_lock); 382 + cond_wait(&thread_parent, &thread_lock); 383 + cond_broadcast(&thread_worker); 384 + mutex_unlock(&thread_lock); 386 385 387 386 sleep(nsecs); 388 387 toggle_done(0, NULL, NULL); ··· 395 394 } 396 395 397 396 /* cleanup & report results */ 398 - pthread_cond_destroy(&thread_parent); 399 - pthread_cond_destroy(&thread_worker); 400 - pthread_mutex_destroy(&thread_lock); 397 + cond_destroy(&thread_parent); 398 + cond_destroy(&thread_worker); 399 + mutex_destroy(&thread_lock); 401 400 402 401 for (i = 0; i < nthreads; i++) { 403 402 unsigned long t[EPOLL_NR_OPS];
+17 -16
tools/perf/bench/epoll-wait.c
··· 79 79 #include <perf/cpumap.h> 80 80 81 81 #include "../util/stat.h" 82 + #include "../util/mutex.h" 82 83 #include <subcmd/parse-options.h> 83 84 #include "bench.h" 84 85 ··· 110 109 /* amount of fds to monitor, per thread */ 111 110 static unsigned int nfds = 64; 112 111 113 - static pthread_mutex_t thread_lock; 112 + static struct mutex thread_lock; 114 113 static unsigned int threads_starting; 115 114 static struct stats throughput_stats; 116 - static pthread_cond_t thread_parent, thread_worker; 115 + static struct cond thread_parent, thread_worker; 117 116 118 117 struct worker { 119 118 int tid; ··· 190 189 int to = nonblocking? 0 : -1; 191 190 int efd = multiq ? w->epollfd : epollfd; 192 191 193 - pthread_mutex_lock(&thread_lock); 192 + mutex_lock(&thread_lock); 194 193 threads_starting--; 195 194 if (!threads_starting) 196 - pthread_cond_signal(&thread_parent); 197 - pthread_cond_wait(&thread_worker, &thread_lock); 198 - pthread_mutex_unlock(&thread_lock); 195 + cond_signal(&thread_parent); 196 + cond_wait(&thread_worker, &thread_lock); 197 + mutex_unlock(&thread_lock); 199 198 200 199 do { 201 200 /* ··· 486 485 getpid(), nthreads, oneshot ? " (EPOLLONESHOT semantics)": "", nfds, nsecs); 487 486 488 487 init_stats(&throughput_stats); 489 - pthread_mutex_init(&thread_lock, NULL); 490 - pthread_cond_init(&thread_parent, NULL); 491 - pthread_cond_init(&thread_worker, NULL); 488 + mutex_init(&thread_lock); 489 + cond_init(&thread_parent); 490 + cond_init(&thread_worker); 492 491 493 492 threads_starting = nthreads; 494 493 ··· 496 495 497 496 do_threads(worker, cpu); 498 497 499 - pthread_mutex_lock(&thread_lock); 498 + mutex_lock(&thread_lock); 500 499 while (threads_starting) 501 - pthread_cond_wait(&thread_parent, &thread_lock); 502 - pthread_cond_broadcast(&thread_worker); 503 - pthread_mutex_unlock(&thread_lock); 500 + cond_wait(&thread_parent, &thread_lock); 501 + cond_broadcast(&thread_worker); 502 + mutex_unlock(&thread_lock); 504 503 505 504 /* 506 505 * At this point the workers should be blocked waiting for read events ··· 523 522 err(EXIT_FAILURE, "pthread_join"); 524 523 525 524 /* cleanup & report results */ 526 - pthread_cond_destroy(&thread_parent); 527 - pthread_cond_destroy(&thread_worker); 528 - pthread_mutex_destroy(&thread_lock); 525 + cond_destroy(&thread_parent); 526 + cond_destroy(&thread_worker); 527 + mutex_destroy(&thread_lock); 529 528 530 529 /* sort the array back before reporting */ 531 530 if (randomize)
+17 -16
tools/perf/bench/futex-hash.c
··· 23 23 #include <sys/mman.h> 24 24 #include <perf/cpumap.h> 25 25 26 + #include "../util/mutex.h" 26 27 #include "../util/stat.h" 27 28 #include <subcmd/parse-options.h> 28 29 #include "bench.h" ··· 35 34 static int futex_flag = 0; 36 35 37 36 struct timeval bench__start, bench__end, bench__runtime; 38 - static pthread_mutex_t thread_lock; 37 + static struct mutex thread_lock; 39 38 static unsigned int threads_starting; 40 39 static struct stats throughput_stats; 41 - static pthread_cond_t thread_parent, thread_worker; 40 + static struct cond thread_parent, thread_worker; 42 41 43 42 struct worker { 44 43 int tid; ··· 74 73 unsigned int i; 75 74 unsigned long ops = w->ops; /* avoid cacheline bouncing */ 76 75 77 - pthread_mutex_lock(&thread_lock); 76 + mutex_lock(&thread_lock); 78 77 threads_starting--; 79 78 if (!threads_starting) 80 - pthread_cond_signal(&thread_parent); 81 - pthread_cond_wait(&thread_worker, &thread_lock); 82 - pthread_mutex_unlock(&thread_lock); 79 + cond_signal(&thread_parent); 80 + cond_wait(&thread_worker, &thread_lock); 81 + mutex_unlock(&thread_lock); 83 82 84 83 do { 85 84 for (i = 0; i < params.nfutexes; i++, ops++) { ··· 166 165 getpid(), params.nthreads, params.nfutexes, params.fshared ? "shared":"private", params.runtime); 167 166 168 167 init_stats(&throughput_stats); 169 - pthread_mutex_init(&thread_lock, NULL); 170 - pthread_cond_init(&thread_parent, NULL); 171 - pthread_cond_init(&thread_worker, NULL); 168 + mutex_init(&thread_lock); 169 + cond_init(&thread_parent); 170 + cond_init(&thread_worker); 172 171 173 172 threads_starting = params.nthreads; 174 173 pthread_attr_init(&thread_attr); ··· 204 203 CPU_FREE(cpuset); 205 204 pthread_attr_destroy(&thread_attr); 206 205 207 - pthread_mutex_lock(&thread_lock); 206 + mutex_lock(&thread_lock); 208 207 while (threads_starting) 209 - pthread_cond_wait(&thread_parent, &thread_lock); 210 - pthread_cond_broadcast(&thread_worker); 211 - pthread_mutex_unlock(&thread_lock); 208 + cond_wait(&thread_parent, &thread_lock); 209 + cond_broadcast(&thread_worker); 210 + mutex_unlock(&thread_lock); 212 211 213 212 sleep(params.runtime); 214 213 toggle_done(0, NULL, NULL); ··· 220 219 } 221 220 222 221 /* cleanup & report results */ 223 - pthread_cond_destroy(&thread_parent); 224 - pthread_cond_destroy(&thread_worker); 225 - pthread_mutex_destroy(&thread_lock); 222 + cond_destroy(&thread_parent); 223 + cond_destroy(&thread_worker); 224 + mutex_destroy(&thread_lock); 226 225 227 226 for (i = 0; i < params.nthreads; i++) { 228 227 unsigned long t = bench__runtime.tv_sec > 0 ?
+17 -16
tools/perf/bench/futex-lock-pi.c
··· 8 8 #include <pthread.h> 9 9 10 10 #include <signal.h> 11 + #include "../util/mutex.h" 11 12 #include "../util/stat.h" 12 13 #include <subcmd/parse-options.h> 13 14 #include <linux/compiler.h> ··· 35 34 static struct worker *worker; 36 35 static bool done = false; 37 36 static int futex_flag = 0; 38 - static pthread_mutex_t thread_lock; 37 + static struct mutex thread_lock; 39 38 static unsigned int threads_starting; 40 39 static struct stats throughput_stats; 41 - static pthread_cond_t thread_parent, thread_worker; 40 + static struct cond thread_parent, thread_worker; 42 41 43 42 static struct bench_futex_parameters params = { 44 43 .runtime = 10, ··· 84 83 struct worker *w = (struct worker *) arg; 85 84 unsigned long ops = w->ops; 86 85 87 - pthread_mutex_lock(&thread_lock); 86 + mutex_lock(&thread_lock); 88 87 threads_starting--; 89 88 if (!threads_starting) 90 - pthread_cond_signal(&thread_parent); 91 - pthread_cond_wait(&thread_worker, &thread_lock); 92 - pthread_mutex_unlock(&thread_lock); 89 + cond_signal(&thread_parent); 90 + cond_wait(&thread_worker, &thread_lock); 91 + mutex_unlock(&thread_lock); 93 92 94 93 do { 95 94 int ret; ··· 198 197 getpid(), params.nthreads, params.runtime); 199 198 200 199 init_stats(&throughput_stats); 201 - pthread_mutex_init(&thread_lock, NULL); 202 - pthread_cond_init(&thread_parent, NULL); 203 - pthread_cond_init(&thread_worker, NULL); 200 + mutex_init(&thread_lock); 201 + cond_init(&thread_parent); 202 + cond_init(&thread_worker); 204 203 205 204 threads_starting = params.nthreads; 206 205 pthread_attr_init(&thread_attr); ··· 209 208 create_threads(worker, thread_attr, cpu); 210 209 pthread_attr_destroy(&thread_attr); 211 210 212 - pthread_mutex_lock(&thread_lock); 211 + mutex_lock(&thread_lock); 213 212 while (threads_starting) 214 - pthread_cond_wait(&thread_parent, &thread_lock); 215 - pthread_cond_broadcast(&thread_worker); 216 - pthread_mutex_unlock(&thread_lock); 213 + cond_wait(&thread_parent, &thread_lock); 214 + cond_broadcast(&thread_worker); 215 + mutex_unlock(&thread_lock); 217 216 218 217 sleep(params.runtime); 219 218 toggle_done(0, NULL, NULL); ··· 225 224 } 226 225 227 226 /* cleanup & report results */ 228 - pthread_cond_destroy(&thread_parent); 229 - pthread_cond_destroy(&thread_worker); 230 - pthread_mutex_destroy(&thread_lock); 227 + cond_destroy(&thread_parent); 228 + cond_destroy(&thread_worker); 229 + mutex_destroy(&thread_lock); 231 230 232 231 for (i = 0; i < params.nthreads; i++) { 233 232 unsigned long t = bench__runtime.tv_sec > 0 ?
+17 -16
tools/perf/bench/futex-requeue.c
··· 15 15 #include <pthread.h> 16 16 17 17 #include <signal.h> 18 + #include "../util/mutex.h" 18 19 #include "../util/stat.h" 19 20 #include <subcmd/parse-options.h> 20 21 #include <linux/compiler.h> ··· 35 34 36 35 static pthread_t *worker; 37 36 static bool done = false; 38 - static pthread_mutex_t thread_lock; 39 - static pthread_cond_t thread_parent, thread_worker; 37 + static struct mutex thread_lock; 38 + static struct cond thread_parent, thread_worker; 40 39 static struct stats requeuetime_stats, requeued_stats; 41 40 static unsigned int threads_starting; 42 41 static int futex_flag = 0; ··· 83 82 { 84 83 int ret; 85 84 86 - pthread_mutex_lock(&thread_lock); 85 + mutex_lock(&thread_lock); 87 86 threads_starting--; 88 87 if (!threads_starting) 89 - pthread_cond_signal(&thread_parent); 90 - pthread_cond_wait(&thread_worker, &thread_lock); 91 - pthread_mutex_unlock(&thread_lock); 88 + cond_signal(&thread_parent); 89 + cond_wait(&thread_worker, &thread_lock); 90 + mutex_unlock(&thread_lock); 92 91 93 92 while (1) { 94 93 if (!params.pi) { ··· 210 209 init_stats(&requeued_stats); 211 210 init_stats(&requeuetime_stats); 212 211 pthread_attr_init(&thread_attr); 213 - pthread_mutex_init(&thread_lock, NULL); 214 - pthread_cond_init(&thread_parent, NULL); 215 - pthread_cond_init(&thread_worker, NULL); 212 + mutex_init(&thread_lock); 213 + cond_init(&thread_parent); 214 + cond_init(&thread_worker); 216 215 217 216 for (j = 0; j < bench_repeat && !done; j++) { 218 217 unsigned int nrequeued = 0, wakeups = 0; ··· 222 221 block_threads(worker, thread_attr, cpu); 223 222 224 223 /* make sure all threads are already blocked */ 225 - pthread_mutex_lock(&thread_lock); 224 + mutex_lock(&thread_lock); 226 225 while (threads_starting) 227 - pthread_cond_wait(&thread_parent, &thread_lock); 228 - pthread_cond_broadcast(&thread_worker); 229 - pthread_mutex_unlock(&thread_lock); 226 + cond_wait(&thread_parent, &thread_lock); 227 + cond_broadcast(&thread_worker); 228 + mutex_unlock(&thread_lock); 230 229 231 230 usleep(100000); 232 231 ··· 298 297 } 299 298 300 299 /* cleanup & report results */ 301 - pthread_cond_destroy(&thread_parent); 302 - pthread_cond_destroy(&thread_worker); 303 - pthread_mutex_destroy(&thread_lock); 300 + cond_destroy(&thread_parent); 301 + cond_destroy(&thread_worker); 302 + mutex_destroy(&thread_lock); 304 303 pthread_attr_destroy(&thread_attr); 305 304 306 305 print_summary();
+17 -16
tools/perf/bench/futex-wake-parallel.c
··· 10 10 #include "bench.h" 11 11 #include <linux/compiler.h> 12 12 #include "../util/debug.h" 13 + #include "../util/mutex.h" 13 14 14 15 #ifndef HAVE_PTHREAD_BARRIER 15 16 int bench_futex_wake_parallel(int argc __maybe_unused, const char **argv __maybe_unused) ··· 50 49 51 50 static pthread_t *blocked_worker; 52 51 static bool done = false; 53 - static pthread_mutex_t thread_lock; 54 - static pthread_cond_t thread_parent, thread_worker; 52 + static struct mutex thread_lock; 53 + static struct cond thread_parent, thread_worker; 55 54 static pthread_barrier_t barrier; 56 55 static struct stats waketime_stats, wakeup_stats; 57 56 static unsigned int threads_starting; ··· 126 125 127 126 static void *blocked_workerfn(void *arg __maybe_unused) 128 127 { 129 - pthread_mutex_lock(&thread_lock); 128 + mutex_lock(&thread_lock); 130 129 threads_starting--; 131 130 if (!threads_starting) 132 - pthread_cond_signal(&thread_parent); 133 - pthread_cond_wait(&thread_worker, &thread_lock); 134 - pthread_mutex_unlock(&thread_lock); 131 + cond_signal(&thread_parent); 132 + cond_wait(&thread_worker, &thread_lock); 133 + mutex_unlock(&thread_lock); 135 134 136 135 while (1) { /* handle spurious wakeups */ 137 136 if (futex_wait(&futex, 0, NULL, futex_flag) != EINTR) ··· 295 294 init_stats(&waketime_stats); 296 295 297 296 pthread_attr_init(&thread_attr); 298 - pthread_mutex_init(&thread_lock, NULL); 299 - pthread_cond_init(&thread_parent, NULL); 300 - pthread_cond_init(&thread_worker, NULL); 297 + mutex_init(&thread_lock); 298 + cond_init(&thread_parent); 299 + cond_init(&thread_worker); 301 300 302 301 for (j = 0; j < bench_repeat && !done; j++) { 303 302 waking_worker = calloc(params.nwakes, sizeof(*waking_worker)); ··· 308 307 block_threads(blocked_worker, thread_attr, cpu); 309 308 310 309 /* make sure all threads are already blocked */ 311 - pthread_mutex_lock(&thread_lock); 310 + mutex_lock(&thread_lock); 312 311 while (threads_starting) 313 - pthread_cond_wait(&thread_parent, &thread_lock); 314 - pthread_cond_broadcast(&thread_worker); 315 - pthread_mutex_unlock(&thread_lock); 312 + cond_wait(&thread_parent, &thread_lock); 313 + cond_broadcast(&thread_worker); 314 + mutex_unlock(&thread_lock); 316 315 317 316 usleep(100000); 318 317 ··· 333 332 } 334 333 335 334 /* cleanup & report results */ 336 - pthread_cond_destroy(&thread_parent); 337 - pthread_cond_destroy(&thread_worker); 338 - pthread_mutex_destroy(&thread_lock); 335 + cond_destroy(&thread_parent); 336 + cond_destroy(&thread_worker); 337 + mutex_destroy(&thread_lock); 339 338 pthread_attr_destroy(&thread_attr); 340 339 341 340 print_summary();
+17 -16
tools/perf/bench/futex-wake.c
··· 14 14 #include <pthread.h> 15 15 16 16 #include <signal.h> 17 + #include "../util/mutex.h" 17 18 #include "../util/stat.h" 18 19 #include <subcmd/parse-options.h> 19 20 #include <linux/compiler.h> ··· 35 34 36 35 static pthread_t *worker; 37 36 static bool done = false; 38 - static pthread_mutex_t thread_lock; 39 - static pthread_cond_t thread_parent, thread_worker; 37 + static struct mutex thread_lock; 38 + static struct cond thread_parent, thread_worker; 40 39 static struct stats waketime_stats, wakeup_stats; 41 40 static unsigned int threads_starting; 42 41 static int futex_flag = 0; ··· 66 65 67 66 static void *workerfn(void *arg __maybe_unused) 68 67 { 69 - pthread_mutex_lock(&thread_lock); 68 + mutex_lock(&thread_lock); 70 69 threads_starting--; 71 70 if (!threads_starting) 72 - pthread_cond_signal(&thread_parent); 73 - pthread_cond_wait(&thread_worker, &thread_lock); 74 - pthread_mutex_unlock(&thread_lock); 71 + cond_signal(&thread_parent); 72 + cond_wait(&thread_worker, &thread_lock); 73 + mutex_unlock(&thread_lock); 75 74 76 75 while (1) { 77 76 if (futex_wait(&futex1, 0, NULL, futex_flag) != EINTR) ··· 179 178 init_stats(&wakeup_stats); 180 179 init_stats(&waketime_stats); 181 180 pthread_attr_init(&thread_attr); 182 - pthread_mutex_init(&thread_lock, NULL); 183 - pthread_cond_init(&thread_parent, NULL); 184 - pthread_cond_init(&thread_worker, NULL); 181 + mutex_init(&thread_lock); 182 + cond_init(&thread_parent); 183 + cond_init(&thread_worker); 185 184 186 185 for (j = 0; j < bench_repeat && !done; j++) { 187 186 unsigned int nwoken = 0; ··· 191 190 block_threads(worker, thread_attr, cpu); 192 191 193 192 /* make sure all threads are already blocked */ 194 - pthread_mutex_lock(&thread_lock); 193 + mutex_lock(&thread_lock); 195 194 while (threads_starting) 196 - pthread_cond_wait(&thread_parent, &thread_lock); 197 - pthread_cond_broadcast(&thread_worker); 198 - pthread_mutex_unlock(&thread_lock); 195 + cond_wait(&thread_parent, &thread_lock); 196 + cond_broadcast(&thread_worker); 197 + mutex_unlock(&thread_lock); 199 198 200 199 usleep(100000); 201 200 ··· 225 224 } 226 225 227 226 /* cleanup & report results */ 228 - pthread_cond_destroy(&thread_parent); 229 - pthread_cond_destroy(&thread_worker); 230 - pthread_mutex_destroy(&thread_lock); 227 + cond_destroy(&thread_parent); 228 + cond_destroy(&thread_worker); 229 + mutex_destroy(&thread_lock); 231 230 pthread_attr_destroy(&thread_attr); 232 231 233 232 print_summary();
+34 -59
tools/perf/bench/numa.c
··· 6 6 */ 7 7 8 8 #include <inttypes.h> 9 - /* For the CLR_() macros */ 10 - #include <pthread.h> 11 9 12 10 #include <subcmd/parse-options.h> 13 11 #include "../util/cloexec.h" ··· 33 35 #include <linux/zalloc.h> 34 36 35 37 #include "../util/header.h" 38 + #include "../util/mutex.h" 36 39 #include <numa.h> 37 40 #include <numaif.h> 38 41 ··· 66 67 u64 system_time_ns; 67 68 u64 user_time_ns; 68 69 double speed_gbs; 69 - pthread_mutex_t *process_lock; 70 + struct mutex *process_lock; 70 71 }; 71 72 72 73 /* Parameters set by options: */ ··· 136 137 struct global_info { 137 138 u8 *data; 138 139 139 - pthread_mutex_t startup_mutex; 140 - pthread_cond_t startup_cond; 140 + struct mutex startup_mutex; 141 + struct cond startup_cond; 141 142 int nr_tasks_started; 142 143 143 - pthread_mutex_t start_work_mutex; 144 - pthread_cond_t start_work_cond; 144 + struct mutex start_work_mutex; 145 + struct cond start_work_cond; 145 146 int nr_tasks_working; 146 147 bool start_work; 147 148 148 - pthread_mutex_t stop_work_mutex; 149 + struct mutex stop_work_mutex; 149 150 u64 bytes_done; 150 151 151 152 struct thread_data *threads; ··· 521 522 static void * setup_private_data(ssize_t bytes) 522 523 { 523 524 return alloc_data(bytes, MAP_PRIVATE, 0, g->p.init_cpu0, g->p.thp, g->p.init_random); 524 - } 525 - 526 - /* 527 - * Return a process-shared (global) mutex: 528 - */ 529 - static void init_global_mutex(pthread_mutex_t *mutex) 530 - { 531 - pthread_mutexattr_t attr; 532 - 533 - pthread_mutexattr_init(&attr); 534 - pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); 535 - pthread_mutex_init(mutex, &attr); 536 - } 537 - 538 - /* 539 - * Return a process-shared (global) condition variable: 540 - */ 541 - static void init_global_cond(pthread_cond_t *cond) 542 - { 543 - pthread_condattr_t attr; 544 - 545 - pthread_condattr_init(&attr); 546 - pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); 547 - pthread_cond_init(cond, &attr); 548 525 } 549 526 550 527 static int parse_cpu_list(const char *arg) ··· 1195 1220 } 1196 1221 1197 1222 if (g->p.serialize_startup) { 1198 - pthread_mutex_lock(&g->startup_mutex); 1223 + mutex_lock(&g->startup_mutex); 1199 1224 g->nr_tasks_started++; 1200 1225 /* The last thread wakes the main process. */ 1201 1226 if (g->nr_tasks_started == g->p.nr_tasks) 1202 - pthread_cond_signal(&g->startup_cond); 1227 + cond_signal(&g->startup_cond); 1203 1228 1204 - pthread_mutex_unlock(&g->startup_mutex); 1229 + mutex_unlock(&g->startup_mutex); 1205 1230 1206 1231 /* Here we will wait for the main process to start us all at once: */ 1207 - pthread_mutex_lock(&g->start_work_mutex); 1232 + mutex_lock(&g->start_work_mutex); 1208 1233 g->start_work = false; 1209 1234 g->nr_tasks_working++; 1210 1235 while (!g->start_work) 1211 - pthread_cond_wait(&g->start_work_cond, &g->start_work_mutex); 1236 + cond_wait(&g->start_work_cond, &g->start_work_mutex); 1212 1237 1213 - pthread_mutex_unlock(&g->start_work_mutex); 1238 + mutex_unlock(&g->start_work_mutex); 1214 1239 } 1215 1240 1216 1241 gettimeofday(&start0, NULL); ··· 1229 1254 val += do_work(thread_data, g->p.bytes_thread, 0, 1, l, val); 1230 1255 1231 1256 if (g->p.sleep_usecs) { 1232 - pthread_mutex_lock(td->process_lock); 1257 + mutex_lock(td->process_lock); 1233 1258 usleep(g->p.sleep_usecs); 1234 - pthread_mutex_unlock(td->process_lock); 1259 + mutex_unlock(td->process_lock); 1235 1260 } 1236 1261 /* 1237 1262 * Amount of work to be done under a process-global lock: 1238 1263 */ 1239 1264 if (g->p.bytes_process_locked) { 1240 - pthread_mutex_lock(td->process_lock); 1265 + mutex_lock(td->process_lock); 1241 1266 val += do_work(process_data, g->p.bytes_process_locked, thread_nr, g->p.nr_threads, l, val); 1242 - pthread_mutex_unlock(td->process_lock); 1267 + mutex_unlock(td->process_lock); 1243 1268 } 1244 1269 1245 1270 work_done = g->p.bytes_global + g->p.bytes_process + ··· 1336 1361 1337 1362 free_data(thread_data, g->p.bytes_thread); 1338 1363 1339 - pthread_mutex_lock(&g->stop_work_mutex); 1364 + mutex_lock(&g->stop_work_mutex); 1340 1365 g->bytes_done += bytes_done; 1341 - pthread_mutex_unlock(&g->stop_work_mutex); 1366 + mutex_unlock(&g->stop_work_mutex); 1342 1367 1343 1368 return NULL; 1344 1369 } ··· 1348 1373 */ 1349 1374 static void worker_process(int process_nr) 1350 1375 { 1351 - pthread_mutex_t process_lock; 1376 + struct mutex process_lock; 1352 1377 struct thread_data *td; 1353 1378 pthread_t *pthreads; 1354 1379 u8 *process_data; ··· 1356 1381 int ret; 1357 1382 int t; 1358 1383 1359 - pthread_mutex_init(&process_lock, NULL); 1384 + mutex_init(&process_lock); 1360 1385 set_taskname("process %d", process_nr); 1361 1386 1362 1387 /* ··· 1515 1540 g->data = setup_shared_data(g->p.bytes_global); 1516 1541 1517 1542 /* Startup serialization: */ 1518 - init_global_mutex(&g->start_work_mutex); 1519 - init_global_cond(&g->start_work_cond); 1520 - init_global_mutex(&g->startup_mutex); 1521 - init_global_cond(&g->startup_cond); 1522 - init_global_mutex(&g->stop_work_mutex); 1543 + mutex_init_pshared(&g->start_work_mutex); 1544 + cond_init_pshared(&g->start_work_cond); 1545 + mutex_init_pshared(&g->startup_mutex); 1546 + cond_init_pshared(&g->startup_cond); 1547 + mutex_init_pshared(&g->stop_work_mutex); 1523 1548 1524 1549 init_thread_data(); 1525 1550 ··· 1608 1633 * Wait for all the threads to start up. The last thread will 1609 1634 * signal this process. 1610 1635 */ 1611 - pthread_mutex_lock(&g->startup_mutex); 1636 + mutex_lock(&g->startup_mutex); 1612 1637 while (g->nr_tasks_started != g->p.nr_tasks) 1613 - pthread_cond_wait(&g->startup_cond, &g->startup_mutex); 1638 + cond_wait(&g->startup_cond, &g->startup_mutex); 1614 1639 1615 - pthread_mutex_unlock(&g->startup_mutex); 1640 + mutex_unlock(&g->startup_mutex); 1616 1641 1617 1642 /* Wait for all threads to be at the start_work_cond. */ 1618 1643 while (!threads_ready) { 1619 - pthread_mutex_lock(&g->start_work_mutex); 1644 + mutex_lock(&g->start_work_mutex); 1620 1645 threads_ready = (g->nr_tasks_working == g->p.nr_tasks); 1621 - pthread_mutex_unlock(&g->start_work_mutex); 1646 + mutex_unlock(&g->start_work_mutex); 1622 1647 if (!threads_ready) 1623 1648 usleep(1); 1624 1649 } ··· 1636 1661 1637 1662 start = stop; 1638 1663 /* Start all threads running. */ 1639 - pthread_mutex_lock(&g->start_work_mutex); 1664 + mutex_lock(&g->start_work_mutex); 1640 1665 g->start_work = true; 1641 - pthread_mutex_unlock(&g->start_work_mutex); 1642 - pthread_cond_broadcast(&g->start_work_cond); 1666 + mutex_unlock(&g->start_work_mutex); 1667 + cond_broadcast(&g->start_work_cond); 1643 1668 } else { 1644 1669 gettimeofday(&start, NULL); 1645 1670 }