typos
[model-checker.git] / scanalysis.cc
1 #include "scanalysis.h"
2 #include "action.h"
3 #include "threads-model.h"
4 #include "clockvector.h"
5 #include "execution.h"
6 #include <sys/time.h>
7
8
9 SCAnalysis::SCAnalysis() :
10         cvmap(),
11         cyclic(false),
12         badrfset(),
13         lastwrmap(),
14         threadlists(1),
15         execution(NULL),
16         print_always(false),
17         print_buggy(true),
18         print_nonsc(false),
19         time(false),
20         stats((struct sc_statistics *)model_calloc(1, sizeof(struct sc_statistics)))
21 {
22 }
23
24 SCAnalysis::~SCAnalysis() {
25         delete(stats);
26 }
27
28 void SCAnalysis::setExecution(ModelExecution * execution) {
29         this->execution=execution;
30 }
31
32 const char * SCAnalysis::name() {
33         const char * name = "SC";
34         return name;
35 }
36
37 void SCAnalysis::finish() {
38         if (time)
39                 model_print("Elapsed time in usec %llu\n", stats->elapsedtime);
40         model_print("SC count: %u\n", stats->sccount);
41         model_print("Non-SC count: %u\n", stats->nonsccount);
42 }
43
44 bool SCAnalysis::option(char * opt) {
45         if (strcmp(opt, "verbose")==0) {
46                 print_always=true;
47                 return false;
48         } else if (strcmp(opt, "buggy")==0) {
49                 return false;
50         } else if (strcmp(opt, "quiet")==0) {
51                 print_buggy=false;
52                 return false;
53         } else if (strcmp(opt, "nonsc")==0) {
54                 print_nonsc=true;
55                 return false;
56         } else if (strcmp(opt, "time")==0) {
57                 time=true;
58                 return false;
59         } else if (strcmp(opt, "help") != 0) {
60                 model_print("Unrecognized option: %s\n", opt);
61         }
62
63         model_print("SC Analysis options\n");
64         model_print("verbose -- print all feasible executions\n");
65         model_print("buggy -- print only buggy executions (default)\n");
66         model_print("nonsc -- print non-sc execution\n");
67         model_print("quiet -- print nothing\n");
68         model_print("time -- time execution of scanalysis\n");
69         model_print("\n");
70         
71         return true;
72 }
73
74 void SCAnalysis::print_list(action_list_t *list) {
75         model_print("---------------------------------------------------------------------\n");
76         if (cyclic)
77                 model_print("Not SC\n");
78         unsigned int hash = 0;
79
80         for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
81                 const ModelAction *act = *it;
82                 if (act->get_seq_number() > 0) {
83                         if (badrfset.contains(act))
84                                 model_print("BRF ");
85                         act->print();
86                         if (badrfset.contains(act)) {
87                                 model_print("Desired Rf: %u \n", badrfset.get(act)->get_seq_number());
88                         }
89                 }
90                 hash = hash ^ (hash << 3) ^ ((*it)->hash());
91         }
92         model_print("HASH %u\n", hash);
93         model_print("---------------------------------------------------------------------\n");
94 }
95
96 void SCAnalysis::analyze(action_list_t *actions) {
97
98         struct timeval start;
99         struct timeval finish;
100         if (time)
101                 gettimeofday(&start, NULL);
102         action_list_t *list = generateSC(actions);
103         check_rf(list);
104         if (print_always || (print_buggy && execution->have_bug_reports())|| (print_nonsc && cyclic))
105                 print_list(list);
106         if (time) {
107                 gettimeofday(&finish, NULL);
108                 stats->elapsedtime+=((finish.tv_sec*1000000+finish.tv_usec)-(start.tv_sec*1000000+start.tv_usec));
109         }
110         update_stats();
111 }
112
113 void SCAnalysis::update_stats() {
114         if (cyclic) {
115                 stats->nonsccount++;
116         } else {
117                 stats->sccount++;
118         }
119 }
120
121 void SCAnalysis::check_rf(action_list_t *list) {
122         for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
123                 const ModelAction *act = *it;
124                 if (act->is_read()) {
125                         if (act->get_reads_from() != lastwrmap.get(act->get_location()))
126                                 badrfset.put(act, lastwrmap.get(act->get_location()));
127                 }
128                 if (act->is_write())
129                         lastwrmap.put(act->get_location(), act);
130         }
131 }
132
133 bool SCAnalysis::merge(ClockVector *cv, const ModelAction *act, const ModelAction *act2) {
134         ClockVector *cv2 = cvmap.get(act2);
135         if (cv2 == NULL)
136                 return true;
137         if (cv2->getClock(act->get_tid()) >= act->get_seq_number() && act->get_seq_number() != 0) {
138                 cyclic = true;
139                 //refuse to introduce cycles into clock vectors
140                 return false;
141         }
142
143         return cv->merge(cv2);
144 }
145
146 int SCAnalysis::getNextActions(ModelAction ** array) {
147         int count=0;
148
149         for (int t = 0; t <= maxthreads; t++) {
150                 action_list_t *tlt = &threadlists[t];
151                 if (tlt->empty())
152                         continue;
153                 ModelAction *act = tlt->front();
154                 ClockVector *cv = cvmap.get(act);
155                 
156                 /* Find the earliest in SC ordering */
157                 for (int i = 0; i <= maxthreads; i++) {
158                         if ( i == t )
159                                 continue;
160                         action_list_t *threadlist = &threadlists[i];
161                         if (threadlist->empty())
162                                 continue;
163                         ModelAction *first = threadlist->front();
164                         if (cv->synchronized_since(first)) {
165                                 act = NULL;
166                                 break;
167                         }
168                 }
169                 if (act != NULL) {
170                         array[count++]=act;
171                 }
172         }
173         if (count != 0)
174                 return count;
175         for (int t = 0; t <= maxthreads; t++) {
176                 action_list_t *tlt = &threadlists[t];
177                 if (tlt->empty())
178                         continue;
179                 ModelAction *act = tlt->front();
180                 ClockVector *cv = act->get_cv();
181                 
182                 /* Find the earliest in SC ordering */
183                 for (int i = 0; i <= maxthreads; i++) {
184                         if ( i == t )
185                                 continue;
186                         action_list_t *threadlist = &threadlists[i];
187                         if (threadlist->empty())
188                                 continue;
189                         ModelAction *first = threadlist->front();
190                         if (cv->synchronized_since(first)) {
191                                 act = NULL;
192                                 break;
193                         }
194                 }
195                 if (act != NULL) {
196                         array[count++]=act;
197                 }
198         }
199
200         ASSERT(count==0 || cyclic);
201
202         return count;
203 }
204
205 ModelAction * SCAnalysis::pruneArray(ModelAction **array,int count) {
206         /* No choice */
207         if (count == 1)
208                 return array[0];
209
210         /* Choose first non-write action */
211         ModelAction *nonwrite=NULL;
212         for(int i=0;i<count;i++) {
213                 if (!array[i]->is_write())
214                         if (nonwrite==NULL || nonwrite->get_seq_number() > array[i]->get_seq_number())
215                                 nonwrite = array[i];
216         }
217         if (nonwrite != NULL)
218                 return nonwrite;
219         
220         /* Look for non-conflicting action */
221         ModelAction *nonconflict=NULL;
222         for(int a=0;a<count;a++) {
223                 ModelAction *act=array[a];
224                 for (int i = 0; i <= maxthreads && act != NULL; i++) {
225                         thread_id_t tid = int_to_id(i);
226                         if (tid == act->get_tid())
227                                 continue;
228                         
229                         action_list_t *list = &threadlists[id_to_int(tid)];
230                         for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
231                                 ModelAction *write = *rit;
232                                 if (!write->is_write())
233                                         continue;
234                                 ClockVector *writecv = cvmap.get(write);
235                                 if (writecv->synchronized_since(act))
236                                         break;
237                                 if (write->get_location() == act->get_location()) {
238                                         //write is sc after act
239                                         act = NULL;
240                                         break;
241                                 }
242                         }
243                 }
244                 if (act != NULL) {
245                         if (nonconflict == NULL || nonconflict->get_seq_number() > act->get_seq_number())
246                                 nonconflict=act;
247                 }
248         }
249         return nonconflict;
250 }
251
252 action_list_t * SCAnalysis::generateSC(action_list_t *list) {
253         int numactions=buildVectors(list);
254         computeCV(list);
255
256         action_list_t *sclist = new action_list_t();
257         ModelAction **array = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
258         int * choices = (int *) model_calloc(1, sizeof(int)*numactions);
259         int endchoice = 0;
260         int currchoice = 0;
261         int lastchoice = -1;
262         while (true) {
263                 int numActions = getNextActions(array);
264                 if (numActions == 0)
265                         break;
266                 ModelAction * act=pruneArray(array, numActions);
267                 if (act == NULL) {
268                         if (currchoice < endchoice) {
269                                 act = array[choices[currchoice]];
270                                 //check whether there is still another option
271                                 if ((choices[currchoice]+1)<numActions)
272                                         lastchoice=currchoice;
273                                 currchoice++;
274                         } else {
275                                 act = array[0];
276                                 choices[currchoice]=0;
277                                 if (numActions>1)
278                                         lastchoice=currchoice;
279                                 currchoice++;
280                         }
281                 }
282                 thread_id_t tid = act->get_tid();
283                 //remove action
284                 threadlists[id_to_int(tid)].pop_front();
285                 //add ordering constraints from this choice
286                 if (updateConstraints(act)) {
287                         //propagate changes if we have them
288                         bool prevc=cyclic;
289                         computeCV(list);
290                         if (!prevc && cyclic) {
291                                 model_print("ROLLBACK in SC\n");
292                                 //check whether we have another choice
293                                 if (lastchoice != -1) {
294                                         //have to reset everything
295                                         choices[lastchoice]++;
296                                         endchoice=lastchoice+1;
297                                         currchoice=0;
298                                         lastchoice=-1;
299                                         reset(list);
300                                         buildVectors(list);
301                                         computeCV(list);
302                                         sclist->clear();
303                                         continue;
304                                 }
305                         }
306                 }
307                 //add action to end
308                 sclist->push_back(act);
309         }
310         model_free(array);
311         return sclist;
312 }
313
314 int SCAnalysis::buildVectors(action_list_t *list) {
315         maxthreads = 0;
316         int numactions = 0;
317         for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
318                 ModelAction *act = *it;
319                 numactions++;
320                 int threadid = id_to_int(act->get_tid());
321                 if (threadid > maxthreads) {
322                         threadlists.resize(threadid + 1);
323                         maxthreads = threadid;
324                 }
325                 threadlists[threadid].push_back(act);
326         }
327         return numactions;
328 }
329
330 void SCAnalysis::reset(action_list_t *list) {
331         for (int t = 0; t <= maxthreads; t++) {
332                 action_list_t *tlt = &threadlists[t];
333                 tlt->clear();
334         }
335         for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
336                 ModelAction *act = *it;
337                 delete cvmap.get(act);
338                 cvmap.put(act, NULL);
339         }
340
341         cyclic=false;   
342 }
343
344 bool SCAnalysis::updateConstraints(ModelAction *act) {
345         bool changed = false;
346         for (int i = 0; i <= maxthreads; i++) {
347                 thread_id_t tid = int_to_id(i);
348                 if (tid == act->get_tid())
349                         continue;
350
351                 action_list_t *list = &threadlists[id_to_int(tid)];
352                 for (action_list_t::iterator rit = list->begin(); rit != list->end(); rit++) {
353                         ModelAction *write = *rit;
354                         if (!write->is_write())
355                                 continue;
356                         ClockVector *writecv = cvmap.get(write);
357                         if (writecv->synchronized_since(act))
358                                 break;
359                         if (write->get_location() == act->get_location()) {
360                                 //write is sc after act
361                                 merge(writecv, write, act);
362                                 changed = true;
363                                 break;
364                         }
365                 }
366         }
367         return changed;
368 }
369
370 bool SCAnalysis::processRead(ModelAction *read, ClockVector *cv) {
371         bool changed = false;
372
373         /* Merge in the clock vector from the write */
374         const ModelAction *write = read->get_reads_from();
375         ClockVector *writecv = cvmap.get(write);
376         changed |= merge(cv, read, write) && (*read < *write);
377
378         for (int i = 0; i <= maxthreads; i++) {
379                 thread_id_t tid = int_to_id(i);
380                 if (tid == read->get_tid())
381                         continue;
382                 if (tid == write->get_tid())
383                         continue;
384                 action_list_t *list = execution->get_actions_on_obj(read->get_location(), tid);
385                 if (list == NULL)
386                         continue;
387                 for (action_list_t::reverse_iterator rit = list->rbegin(); rit != list->rend(); rit++) {
388                         ModelAction *write2 = *rit;
389                         if (!write2->is_write())
390                                 continue;
391
392                         ClockVector *write2cv = cvmap.get(write2);
393                         if (write2cv == NULL)
394                                 continue;
395
396                         /* write -sc-> write2 &&
397                                  write -rf-> R =>
398                                  R -sc-> write2 */
399                         if (write2cv->synchronized_since(write)) {
400                                 changed |= merge(write2cv, write2, read);
401                         }
402
403                         //looking for earliest write2 in iteration to satisfy this
404                         /* write2 -sc-> R &&
405                                  write -rf-> R =>
406                                  write2 -sc-> write */
407                         if (cv->synchronized_since(write2)) {
408                                 changed |= writecv == NULL || merge(writecv, write, write2);
409                                 break;
410                         }
411                 }
412         }
413         return changed;
414 }
415
416 void SCAnalysis::computeCV(action_list_t *list) {
417         bool changed = true;
418         bool firsttime = true;
419         ModelAction **last_act = (ModelAction **)model_calloc(1, (maxthreads + 1) * sizeof(ModelAction *));
420         while (changed) {
421                 changed = changed&firsttime;
422                 firsttime = false;
423
424                 for (action_list_t::iterator it = list->begin(); it != list->end(); it++) {
425                         ModelAction *act = *it;
426                         ModelAction *lastact = last_act[id_to_int(act->get_tid())];
427                         if (act->is_thread_start())
428                                 lastact = execution->get_thread(act)->get_creation();
429                         last_act[id_to_int(act->get_tid())] = act;
430                         ClockVector *cv = cvmap.get(act);
431                         if (cv == NULL) {
432                                 cv = new ClockVector(NULL, act);
433                                 cvmap.put(act, cv);
434                         }
435                         if (lastact != NULL) {
436                                 merge(cv, act, lastact);
437                         }
438                         if (act->is_thread_join()) {
439                                 Thread *joinedthr = act->get_thread_operand();
440                                 ModelAction *finish = execution->get_last_action(joinedthr->get_id());
441                                 changed |= merge(cv, act, finish);
442                         }
443                         if (act->is_read()) {
444                                 changed |= processRead(act, cv);
445                         }
446                 }
447                 /* Reset the last action array */
448                 if (changed) {
449                         bzero(last_act, (maxthreads + 1) * sizeof(ModelAction *));
450                 }
451         }
452         model_free(last_act);
453 }