38279403b7ead5b47bf00af40be44789f8d2411b
[model-checker-benchmarks.git] / mcs-queue / main.c
1 #include "main.h"
2
3 #define NUM_PROCESSORS                  12
4
5 struct tms tim;
6 struct tms tim1;
7
8 usptr_t *Handle;
9 barrier_t *Barrier;
10 usptr_t *lock_handle;
11 ulock_t native_lock;
12
13 int shmid;
14
15 unsigned pid;
16 unsigned backoff;
17 unsigned backoff_base;
18 unsigned backoff_cap;
19 unsigned backoff_addend;
20 char* name = "";
21 unsigned backoff_base_bits = 0;
22 unsigned backoff_cap_bits = 0;
23 unsigned procs = 1;
24 unsigned multi = 1;
25 unsigned iterations = 1;
26 unsigned initial_nodes = 0;
27 unsigned repetitions = 1;
28 unsigned backoff_shift_bits = 0;
29 unsigned work = 0;
30 private_t private;
31 shared_mem_t *smp;
32
33 void native_acquire()
34 {
35         ussetlock(native_lock);
36
37
38 void native_release()
39 {
40         usunsetlock(native_lock);
41 }
42
43 void tts_acq(unsigned* plock)
44 {
45         do {
46                 if (*plock == 1) {
47                         backoff = backoff_base;
48                         do {
49                                 backoff_delay();
50                         } while(*plock == 1);
51                 }
52         } while (tas(plock) == 1);
53 }
54
55 void time_test()
56 {
57         unsigned i,j;
58         struct tms time_val;
59         clock_t t1, t2;
60         unsigned val;
61
62         if(pid==0) {
63                 init_queue();
64         }
65         init_memory();
66         init_private();
67         init_backoff();
68         barrier(Barrier, procs*multi);
69         if(pid==0)
70         {
71                 t1=times(&time_val);
72         }
73         for(i=0;i<iterations;i++) {
74                 val = private.value;
75                 enqueue(val);
76                 for(j=0; j<work;) j++;
77                 val = dequeue();
78                 for(j=0; j<work;) j++;
79                 private.value++;
80         }
81         barrier(Barrier, procs*multi);
82         if(pid==0)
83         {
84                 t2=times(&time_val);
85                 printf("p%d  m%d  i%d  b%d c%d s%d  w%d time %.0f ms.\n",
86                                 procs, multi, iterations*procs*multi,
87                                 backoff_base_bits, backoff_cap_bits,
88                                 backoff_shift_bits, work, ((t2-t1)*1000)/(double)HZ);
89                 fflush(stdout);
90         }
91 }
92
93 void main_task()
94 {
95         unsigned processor;
96         unsigned i;
97
98         processor = (pid/multi)+1;
99         processor %= NUM_PROCESSORS;
100         if(sysmp(MP_MUSTRUN, processor) == -1) { perror("Could not MUSTRUN"); }
101         if (pid==0) {
102                 printf("--- %s\n", name);
103                 fflush(stdout);
104         }
105         for (i=0; i<repetitions; i++) {
106                 time_test();
107         }
108 }
109
110 void setup_shmem()
111 {
112         shmid = shmget(IPC_PRIVATE, sizeof(shared_mem_t), 511);
113         assert(shmid != -1);
114         smp = (shared_mem_t *)shmat(shmid, 0, 0);
115         assert((int)smp != -1);
116 }
117
118 void my_m_fork(void (*func)(), int num_procs)
119 {
120         for (pid=1;pid<num_procs;pid++) {
121                 if(fork()==0) /* Child */ {
122                         (*func)(); /* Call the program */
123                         return;
124                 }
125         }
126         pid=0;
127         (*func)(); /* Call the program */
128 }
129
130 main(int argc,char **argv)
131 {
132         parse_args(argc, argv);
133         name = argv[0];
134         iterations = (iterations + ((procs*multi)>>1))/(procs*multi);
135         setup_shmem();
136         Handle = usinit("/tmp/foo_barrier");
137         Barrier = new_barrier(Handle);
138         init_barrier(Barrier);
139         lock_handle = usinit("/tmp/foo_lock");
140         native_lock = usnewlock(lock_handle);
141         my_m_fork(main_task, procs*multi); 
142         shmctl(shmid, IPC_RMID, smp);
143         exit(0);
144 }