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