xref: /dragonfly/test/sysperf/mutex2.c (revision 37de577a)
1 /*
2  * mutex2.c
3  *
4  * $DragonFly: src/test/sysperf/mutex2.c,v 1.2 2006/04/22 22:32:52 dillon Exp $
5  */
6 
7 #include "blib.h"
8 
9 int *mtx;
10 
11 int
12 main(int ac, char **av)
13 {
14     long long count = 0;
15     long long max;
16     int j;
17     int *counter;
18     pid_t pid;
19 
20     printf("Test simple locked bus cycle mutex latency\n");
21     printf("auto-forks two processes for the test with shared memory\n");
22     printf("This test is only useful on a SMP box\n");
23 
24     start_timing();
25     mtx = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
26     counter = mtx + 64;
27     while (stop_timing(0, NULL) == 0) {
28 	for (j = 0; j < 100; ++j) {
29 	    get_mtx(1);
30 	    rel_mtx();
31 	}
32 	count += 100;
33     }
34     max = count;
35     *mtx = 0;
36 
37     start_timing();
38     for (count = 0; count < max; count += 100) {
39 	for (j = 0; j < 100; ++j) {
40 	    get_mtx(1);
41 	    rel_mtx();	/* release */
42 	    ++counter[64];
43 	}
44     }
45     stop_timing(count, "complex_mtx(uncontested/1cpu)");
46 
47     if ((pid = fork()) == 0) {
48 	for (;;) {
49 	    for (j = 0; j < 100; ++j) {
50 		get_mtx(2);
51 		rel_mtx();	/* release */
52 		++counter[128];
53 	    }
54 	}
55     } else {
56 	start_timing();
57 	for (count = 0; count < max; count += 100) {
58 	    for (j = 0; j < 100; ++j) {
59 		get_mtx(1);
60 		rel_mtx();	/* release */
61 		++counter[64];
62 	    }
63 	}
64 	stop_timing(count, "complex_mtx");
65 	printf("proc1=%d proc2=%d\n", counter[64], counter[128]);
66 	kill(pid, 9);
67     }
68     return(0);
69 }
70 
71