xref: /OK3568_Linux_fs/kernel/tools/perf/bench/futex-lock-pi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2015 Davidlohr Bueso.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun /* For the CLR_() macros */
7*4882a593Smuzhiyun #include <string.h>
8*4882a593Smuzhiyun #include <pthread.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <signal.h>
11*4882a593Smuzhiyun #include "../util/stat.h"
12*4882a593Smuzhiyun #include <subcmd/parse-options.h>
13*4882a593Smuzhiyun #include <linux/compiler.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/zalloc.h>
16*4882a593Smuzhiyun #include <errno.h>
17*4882a593Smuzhiyun #include <internal/cpumap.h>
18*4882a593Smuzhiyun #include <perf/cpumap.h>
19*4882a593Smuzhiyun #include "bench.h"
20*4882a593Smuzhiyun #include "futex.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <err.h>
23*4882a593Smuzhiyun #include <stdlib.h>
24*4882a593Smuzhiyun #include <sys/time.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun struct worker {
27*4882a593Smuzhiyun 	int tid;
28*4882a593Smuzhiyun 	u_int32_t *futex;
29*4882a593Smuzhiyun 	pthread_t thread;
30*4882a593Smuzhiyun 	unsigned long ops;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static u_int32_t global_futex = 0;
34*4882a593Smuzhiyun static struct worker *worker;
35*4882a593Smuzhiyun static unsigned int nsecs = 10;
36*4882a593Smuzhiyun static bool silent = false, multi = false;
37*4882a593Smuzhiyun static bool done = false, fshared = false;
38*4882a593Smuzhiyun static unsigned int nthreads = 0;
39*4882a593Smuzhiyun static int futex_flag = 0;
40*4882a593Smuzhiyun static pthread_mutex_t thread_lock;
41*4882a593Smuzhiyun static unsigned int threads_starting;
42*4882a593Smuzhiyun static struct stats throughput_stats;
43*4882a593Smuzhiyun static pthread_cond_t thread_parent, thread_worker;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun static const struct option options[] = {
46*4882a593Smuzhiyun 	OPT_UINTEGER('t', "threads",  &nthreads, "Specify amount of threads"),
47*4882a593Smuzhiyun 	OPT_UINTEGER('r', "runtime", &nsecs,     "Specify runtime (in seconds)"),
48*4882a593Smuzhiyun 	OPT_BOOLEAN( 'M', "multi",   &multi,     "Use multiple futexes"),
49*4882a593Smuzhiyun 	OPT_BOOLEAN( 's', "silent",  &silent,    "Silent mode: do not display data/details"),
50*4882a593Smuzhiyun 	OPT_BOOLEAN( 'S', "shared",  &fshared,   "Use shared futexes instead of private ones"),
51*4882a593Smuzhiyun 	OPT_END()
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun static const char * const bench_futex_lock_pi_usage[] = {
55*4882a593Smuzhiyun 	"perf bench futex lock-pi <options>",
56*4882a593Smuzhiyun 	NULL
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
print_summary(void)59*4882a593Smuzhiyun static void print_summary(void)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	unsigned long avg = avg_stats(&throughput_stats);
62*4882a593Smuzhiyun 	double stddev = stddev_stats(&throughput_stats);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	printf("%sAveraged %ld operations/sec (+- %.2f%%), total secs = %d\n",
65*4882a593Smuzhiyun 	       !silent ? "\n" : "", avg, rel_stddev_stats(stddev, avg),
66*4882a593Smuzhiyun 	       (int)bench__runtime.tv_sec);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
toggle_done(int sig __maybe_unused,siginfo_t * info __maybe_unused,void * uc __maybe_unused)69*4882a593Smuzhiyun static void toggle_done(int sig __maybe_unused,
70*4882a593Smuzhiyun 			siginfo_t *info __maybe_unused,
71*4882a593Smuzhiyun 			void *uc __maybe_unused)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun 	/* inform all threads that we're done for the day */
74*4882a593Smuzhiyun 	done = true;
75*4882a593Smuzhiyun 	gettimeofday(&bench__end, NULL);
76*4882a593Smuzhiyun 	timersub(&bench__end, &bench__start, &bench__runtime);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
workerfn(void * arg)79*4882a593Smuzhiyun static void *workerfn(void *arg)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	struct worker *w = (struct worker *) arg;
82*4882a593Smuzhiyun 	unsigned long ops = w->ops;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	pthread_mutex_lock(&thread_lock);
85*4882a593Smuzhiyun 	threads_starting--;
86*4882a593Smuzhiyun 	if (!threads_starting)
87*4882a593Smuzhiyun 		pthread_cond_signal(&thread_parent);
88*4882a593Smuzhiyun 	pthread_cond_wait(&thread_worker, &thread_lock);
89*4882a593Smuzhiyun 	pthread_mutex_unlock(&thread_lock);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	do {
92*4882a593Smuzhiyun 		int ret;
93*4882a593Smuzhiyun 	again:
94*4882a593Smuzhiyun 		ret = futex_lock_pi(w->futex, NULL, futex_flag);
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 		if (ret) { /* handle lock acquisition */
97*4882a593Smuzhiyun 			if (!silent)
98*4882a593Smuzhiyun 				warn("thread %d: Could not lock pi-lock for %p (%d)",
99*4882a593Smuzhiyun 				     w->tid, w->futex, ret);
100*4882a593Smuzhiyun 			if (done)
101*4882a593Smuzhiyun 				break;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 			goto again;
104*4882a593Smuzhiyun 		}
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 		usleep(1);
107*4882a593Smuzhiyun 		ret = futex_unlock_pi(w->futex, futex_flag);
108*4882a593Smuzhiyun 		if (ret && !silent)
109*4882a593Smuzhiyun 			warn("thread %d: Could not unlock pi-lock for %p (%d)",
110*4882a593Smuzhiyun 			     w->tid, w->futex, ret);
111*4882a593Smuzhiyun 		ops++; /* account for thread's share of work */
112*4882a593Smuzhiyun 	}  while (!done);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	w->ops = ops;
115*4882a593Smuzhiyun 	return NULL;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
create_threads(struct worker * w,pthread_attr_t thread_attr,struct perf_cpu_map * cpu)118*4882a593Smuzhiyun static void create_threads(struct worker *w, pthread_attr_t thread_attr,
119*4882a593Smuzhiyun 			   struct perf_cpu_map *cpu)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	cpu_set_t cpuset;
122*4882a593Smuzhiyun 	unsigned int i;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	threads_starting = nthreads;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
127*4882a593Smuzhiyun 		worker[i].tid = i;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 		if (multi) {
130*4882a593Smuzhiyun 			worker[i].futex = calloc(1, sizeof(u_int32_t));
131*4882a593Smuzhiyun 			if (!worker[i].futex)
132*4882a593Smuzhiyun 				err(EXIT_FAILURE, "calloc");
133*4882a593Smuzhiyun 		} else
134*4882a593Smuzhiyun 			worker[i].futex = &global_futex;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 		CPU_ZERO(&cpuset);
137*4882a593Smuzhiyun 		CPU_SET(cpu->map[i % cpu->nr], &cpuset);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 		if (pthread_attr_setaffinity_np(&thread_attr, sizeof(cpu_set_t), &cpuset))
140*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_attr_setaffinity_np");
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 		if (pthread_create(&w[i].thread, &thread_attr, workerfn, &worker[i]))
143*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_create");
144*4882a593Smuzhiyun 	}
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
bench_futex_lock_pi(int argc,const char ** argv)147*4882a593Smuzhiyun int bench_futex_lock_pi(int argc, const char **argv)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	int ret = 0;
150*4882a593Smuzhiyun 	unsigned int i;
151*4882a593Smuzhiyun 	struct sigaction act;
152*4882a593Smuzhiyun 	pthread_attr_t thread_attr;
153*4882a593Smuzhiyun 	struct perf_cpu_map *cpu;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	argc = parse_options(argc, argv, options, bench_futex_lock_pi_usage, 0);
156*4882a593Smuzhiyun 	if (argc)
157*4882a593Smuzhiyun 		goto err;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	cpu = perf_cpu_map__new(NULL);
160*4882a593Smuzhiyun 	if (!cpu)
161*4882a593Smuzhiyun 		err(EXIT_FAILURE, "calloc");
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	memset(&act, 0, sizeof(act));
164*4882a593Smuzhiyun 	sigfillset(&act.sa_mask);
165*4882a593Smuzhiyun 	act.sa_sigaction = toggle_done;
166*4882a593Smuzhiyun 	sigaction(SIGINT, &act, NULL);
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	if (!nthreads)
169*4882a593Smuzhiyun 		nthreads = cpu->nr;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	worker = calloc(nthreads, sizeof(*worker));
172*4882a593Smuzhiyun 	if (!worker)
173*4882a593Smuzhiyun 		err(EXIT_FAILURE, "calloc");
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	if (!fshared)
176*4882a593Smuzhiyun 		futex_flag = FUTEX_PRIVATE_FLAG;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	printf("Run summary [PID %d]: %d threads doing pi lock/unlock pairing for %d secs.\n\n",
179*4882a593Smuzhiyun 	       getpid(), nthreads, nsecs);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	init_stats(&throughput_stats);
182*4882a593Smuzhiyun 	pthread_mutex_init(&thread_lock, NULL);
183*4882a593Smuzhiyun 	pthread_cond_init(&thread_parent, NULL);
184*4882a593Smuzhiyun 	pthread_cond_init(&thread_worker, NULL);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	threads_starting = nthreads;
187*4882a593Smuzhiyun 	pthread_attr_init(&thread_attr);
188*4882a593Smuzhiyun 	gettimeofday(&bench__start, NULL);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	create_threads(worker, thread_attr, cpu);
191*4882a593Smuzhiyun 	pthread_attr_destroy(&thread_attr);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	pthread_mutex_lock(&thread_lock);
194*4882a593Smuzhiyun 	while (threads_starting)
195*4882a593Smuzhiyun 		pthread_cond_wait(&thread_parent, &thread_lock);
196*4882a593Smuzhiyun 	pthread_cond_broadcast(&thread_worker);
197*4882a593Smuzhiyun 	pthread_mutex_unlock(&thread_lock);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	sleep(nsecs);
200*4882a593Smuzhiyun 	toggle_done(0, NULL, NULL);
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
203*4882a593Smuzhiyun 		ret = pthread_join(worker[i].thread, NULL);
204*4882a593Smuzhiyun 		if (ret)
205*4882a593Smuzhiyun 			err(EXIT_FAILURE, "pthread_join");
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	/* cleanup & report results */
209*4882a593Smuzhiyun 	pthread_cond_destroy(&thread_parent);
210*4882a593Smuzhiyun 	pthread_cond_destroy(&thread_worker);
211*4882a593Smuzhiyun 	pthread_mutex_destroy(&thread_lock);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	for (i = 0; i < nthreads; i++) {
214*4882a593Smuzhiyun 		unsigned long t = bench__runtime.tv_sec > 0 ?
215*4882a593Smuzhiyun 			worker[i].ops / bench__runtime.tv_sec : 0;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 		update_stats(&throughput_stats, t);
218*4882a593Smuzhiyun 		if (!silent)
219*4882a593Smuzhiyun 			printf("[thread %3d] futex: %p [ %ld ops/sec ]\n",
220*4882a593Smuzhiyun 			       worker[i].tid, worker[i].futex, t);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 		if (multi)
223*4882a593Smuzhiyun 			zfree(&worker[i].futex);
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	print_summary();
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	free(worker);
229*4882a593Smuzhiyun 	perf_cpu_map__put(cpu);
230*4882a593Smuzhiyun 	return ret;
231*4882a593Smuzhiyun err:
232*4882a593Smuzhiyun 	usage_with_options(bench_futex_lock_pi_usage, options);
233*4882a593Smuzhiyun 	exit(EXIT_FAILURE);
234*4882a593Smuzhiyun }
235