xref: /dragonfly/test/sysperf/mutex4.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1 /*
2  * mutex4.c
3  *
4  * $DragonFly: src/test/sysperf/mutex4.c,v 1.1 2008/05/09 15:49:42 dillon Exp $
5  */
6 
7 #include "blib.h"
8 
9 #include <sys/types.h>
10 #include <machine/atomic.h>
11 #include <machine/cpufunc.h>
12 
13 struct globaldata {
14           int gd_cpumask;
15           int gd_spinlocks;
16 };
17 
18 int *mtx;
19 struct globaldata gd;
20 
21 typedef struct globaldata *globaldata_t;
22 
23 void
rd_lock_contested(void)24 rd_lock_contested(void)
25 {
26 }
27 
28 static __inline void
rd_lock(void)29 rd_lock(void)
30 {
31           atomic_set_int(mtx, 1);
32           *mtx = 2;
33           *mtx = 3;
34 }
35 
36 static __inline void
rd_unlock(void)37 rd_unlock(void)
38 {
39 }
40 
41 int
main(int ac,char ** av)42 main(int ac, char **av)
43 {
44     long long count = 0;
45     long long max;
46     int j;
47     int *counter;
48     pid_t pid;
49 
50     printf("Test simple locked bus cycle mutex latency\n");
51     printf("auto-forks two processes for the test with shared memory\n");
52     printf("This test is only useful on a SMP box\n");
53 
54     start_timing();
55     mtx = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
56     counter = mtx + 64;
57     gd.gd_cpumask = 0x00000001;
58     gd.gd_spinlocks = 0;
59     while (stop_timing(0, NULL) == 0) {
60           for (j = 0; j < 100; ++j) {
61               rd_lock();
62               rd_unlock();
63           }
64           count += 100;
65     }
66     max = count;
67     *mtx = 0;
68 
69     start_timing();
70     for (count = 0; count < max; count += 100) {
71           for (j = 0; j < 100; ++j) {
72               rd_lock();
73               rd_unlock();    /* release */
74           }
75     }
76     stop_timing(count, "complex_mtx(uncontested/1cpu)");
77 
78     if ((pid = fork()) == 0) {
79           for (;;) {
80               for (j = 0; j < 100; ++j) {
81                     rd_lock();
82                     rd_unlock();        /* release */
83                     ++counter[128];
84               }
85           }
86     } else {
87           gd.gd_cpumask = 0x00000002;
88           gd.gd_spinlocks = 0;
89           start_timing();
90           for (count = 0; count < max; count += 100) {
91               for (j = 0; j < 100; ++j) {
92                     rd_lock();
93                     rd_unlock();        /* release */
94                     ++counter[64];
95               }
96           }
97           stop_timing(count, "complex_mtx");
98           printf("proc1=%d proc2=%d\n", counter[64], counter[128]);
99           kill(pid, 9);
100     }
101     return(0);
102 }
103 
104