xref: /dflybsd-src/test/sysperf/syscall2.c (revision adc1b605934bacf302ccccade779ae0d96f51f2a)
16b055cd4SMatthew Dillon /*
26b055cd4SMatthew Dillon  * syscall2.c
36b055cd4SMatthew Dillon  *
4*adc1b605SMatthew Dillon  * All-threads getuid timing test.
56b055cd4SMatthew Dillon  */
66b055cd4SMatthew Dillon 
7*adc1b605SMatthew Dillon #include <sys/types.h>
8*adc1b605SMatthew Dillon #include <sys/errno.h>
9*adc1b605SMatthew Dillon #include <sys/mman.h>
109ee18353SEirik Nygaard #include <time.h>
11*adc1b605SMatthew Dillon #include <stdio.h>
12*adc1b605SMatthew Dillon 
13*adc1b605SMatthew Dillon #include <machine/atomic.h>
149ee18353SEirik Nygaard 
156b055cd4SMatthew Dillon #include "blib.h"
166b055cd4SMatthew Dillon 
179ee18353SEirik Nygaard extern int getuid_test(void);
186b055cd4SMatthew Dillon 
196b055cd4SMatthew Dillon int
main(void)209ee18353SEirik Nygaard main(void)
216b055cd4SMatthew Dillon {
229ee18353SEirik Nygaard 	struct timespec ts, ts2;
239ee18353SEirik Nygaard 	int error;
246b055cd4SMatthew Dillon 	long long count = 0;
256b055cd4SMatthew Dillon 	long long max;
266b055cd4SMatthew Dillon 	int j;
27*adc1b605SMatthew Dillon 	int cpuno;
28*adc1b605SMatthew Dillon 	int ncpu;
29*adc1b605SMatthew Dillon 	int *done;
30*adc1b605SMatthew Dillon 	size_t ncpu_size;
316b055cd4SMatthew Dillon 
32*adc1b605SMatthew Dillon 	done = mmap(NULL, 4096, PROT_READ|PROT_WRITE,
33*adc1b605SMatthew Dillon 		     MAP_SHARED|MAP_ANON, -1, 0);
34*adc1b605SMatthew Dillon 
35*adc1b605SMatthew Dillon 	/*
36*adc1b605SMatthew Dillon 	 * How many cpu threads are there?
37*adc1b605SMatthew Dillon 	 */
38*adc1b605SMatthew Dillon 	ncpu = 0;
39*adc1b605SMatthew Dillon 	ncpu_size = sizeof(ncpu);
40*adc1b605SMatthew Dillon 	if (sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0) < 0) {
41*adc1b605SMatthew Dillon 		perror("sysctl hw.ncpu");
42*adc1b605SMatthew Dillon 		exit(1);
43*adc1b605SMatthew Dillon 	}
44*adc1b605SMatthew Dillon 	printf("timing standard getuid() syscall, %d threads\n", ncpu);
45*adc1b605SMatthew Dillon 	printf("if using powerd, run several times\n");
46*adc1b605SMatthew Dillon 	*done = 0;
47*adc1b605SMatthew Dillon 
48*adc1b605SMatthew Dillon 	/*
49*adc1b605SMatthew Dillon 	 * Approximate timing run length
50*adc1b605SMatthew Dillon 	 */
516b055cd4SMatthew Dillon 	start_timing();
526b055cd4SMatthew Dillon 	while (stop_timing(0, NULL) == 0) {
536b055cd4SMatthew Dillon 		for (j = 0; j < 100; ++j)
54*adc1b605SMatthew Dillon 			getuid();
556b055cd4SMatthew Dillon 		count += 100;
566b055cd4SMatthew Dillon 	}
576b055cd4SMatthew Dillon 	max = count;
58*adc1b605SMatthew Dillon 
59*adc1b605SMatthew Dillon 	/*
60*adc1b605SMatthew Dillon 	 * Run same length on all threads.
61*adc1b605SMatthew Dillon 	 */
62*adc1b605SMatthew Dillon 	for (cpuno = 0; cpuno < ncpu; ++cpuno) {
63*adc1b605SMatthew Dillon 		if (fork() == 0) {
64*adc1b605SMatthew Dillon 			/*
65*adc1b605SMatthew Dillon 			 * Give scheduler time to move threads around
66*adc1b605SMatthew Dillon 			 */
67*adc1b605SMatthew Dillon 			start_timing();
68*adc1b605SMatthew Dillon 			while (stop_timing(0, NULL) == 0) {
69*adc1b605SMatthew Dillon 				for (j = 0; j < 100; ++j)
70*adc1b605SMatthew Dillon 					getuid();
71*adc1b605SMatthew Dillon 			}
72*adc1b605SMatthew Dillon 
73*adc1b605SMatthew Dillon 			/*
74*adc1b605SMatthew Dillon 			 * Actual timing test is here.
75*adc1b605SMatthew Dillon 			 */
766b055cd4SMatthew Dillon 			start_timing();
776b055cd4SMatthew Dillon 			for (count = 0; count < max; count += 100) {
786b055cd4SMatthew Dillon 				for (j = 0; j < 100; ++j)
79*adc1b605SMatthew Dillon 					getuid();
806b055cd4SMatthew Dillon 			}
819ee18353SEirik Nygaard 			stop_timing(count, "getuid() sysmsg");
82*adc1b605SMatthew Dillon 
83*adc1b605SMatthew Dillon 			/*
84*adc1b605SMatthew Dillon 			 * Don't unbusy the cpu until the other threads are
85*adc1b605SMatthew Dillon 			 * done.
86*adc1b605SMatthew Dillon 			 */
87*adc1b605SMatthew Dillon 			atomic_add_int(done, 1);
88*adc1b605SMatthew Dillon 			while (*done < ncpu)	/* wait for other threads */
89*adc1b605SMatthew Dillon 				getuid();
90*adc1b605SMatthew Dillon 			exit(0);
91*adc1b605SMatthew Dillon 		}
92*adc1b605SMatthew Dillon 	}
93*adc1b605SMatthew Dillon 	while (wait3(NULL, 0, NULL) > 0 || errno == EINTR)
94*adc1b605SMatthew Dillon 		;
95*adc1b605SMatthew Dillon 	return 0;
966b055cd4SMatthew Dillon }
97