1 /* 2 * mutex1.c 3 * 4 * $DragonFly: src/test/sysperf/mutex1.c,v 1.2 2006/04/22 22:34:06 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 try_spin_mtx(); 30 rel_spin_mtx(); 31 } 32 count += 100; 33 } 34 max = count; 35 *mtx = 0; 36 37 /* 38 * Single cpu case 39 */ 40 start_timing(); 41 for (count = 0; count < max; count += 100) { 42 for (j = 0; j < 100; ++j) { 43 while (try_spin_mtx() != 0) 44 ; 45 rel_spin_mtx(); /* release */ 46 ++counter[64]; 47 } 48 } 49 stop_timing(count, "simple_mtx(uncontested/1cpu)"); 50 51 if ((pid = fork()) == 0) { 52 for (;;) { 53 for (j = 0; j < 100; ++j) { 54 while (try_spin_mtx() != 0) 55 ; 56 rel_spin_mtx(); /* release */ 57 ++counter[128]; 58 } 59 } 60 } else { 61 start_timing(); 62 for (count = 0; count < max; count += 100) { 63 for (j = 0; j < 100; ++j) { 64 while (try_spin_mtx() != 0) 65 ; 66 rel_spin_mtx(); /* release */ 67 ++counter[64]; 68 } 69 } 70 stop_timing(count, "simple_mtx"); 71 printf("proc1=%d proc2=%d\n", counter[64], counter[128]); 72 kill(pid, 9); 73 } 74 return(0); 75 } 76 77