1b379000d75f4832795190acc8e583f933ee7675
[c11tester.git] / model.cc
1 #include <stdio.h>
2
3 #include "model.h"
4 #include "action.h"
5 #include "nodestack.h"
6 #include "schedule.h"
7 #include "snapshot-interface.h"
8 #include "common.h"
9 #include "clockvector.h"
10 #include "cyclegraph.h"
11
12 #define INITIAL_THREAD_ID       0
13
14 ModelChecker *model;
15
16 /** @brief Constructor */
17 ModelChecker::ModelChecker()
18         :
19         /* Initialize default scheduler */
20         scheduler(new Scheduler()),
21         /* First thread created will have id INITIAL_THREAD_ID */
22         next_thread_id(INITIAL_THREAD_ID),
23         used_sequence_numbers(0),
24
25         num_executions(0),
26         current_action(NULL),
27         diverge(NULL),
28         nextThread(THREAD_ID_T_NONE),
29         action_trace(new action_list_t()),
30         thread_map(new std::map<int, Thread *>),
31         obj_map(new std::map<const void *, action_list_t>()),
32         obj_thrd_map(new std::map<void *, std::vector<action_list_t> >()),
33         thrd_last_action(new std::vector<ModelAction *>(1)),
34         node_stack(new NodeStack()),
35         next_backtrack(NULL),
36         cyclegraph(new CycleGraph())
37 {
38 }
39
40 /** @brief Destructor */
41 ModelChecker::~ModelChecker()
42 {
43         std::map<int, Thread *>::iterator it;
44         for (it = thread_map->begin(); it != thread_map->end(); it++)
45                 delete (*it).second;
46         delete thread_map;
47
48         delete obj_thrd_map;
49         delete obj_map;
50         delete action_trace;
51         delete thrd_last_action;
52         delete node_stack;
53         delete scheduler;
54         delete cyclegraph;
55 }
56
57 /**
58  * Restores user program to initial state and resets all model-checker data
59  * structures.
60  */
61 void ModelChecker::reset_to_initial_state()
62 {
63         DEBUG("+++ Resetting to initial state +++\n");
64         node_stack->reset_execution();
65         current_action = NULL;
66         next_thread_id = INITIAL_THREAD_ID;
67         used_sequence_numbers = 0;
68         nextThread = 0;
69         next_backtrack = NULL;
70         snapshotObject->backTrackBeforeStep(0);
71 }
72
73 /** @returns a thread ID for a new Thread */
74 thread_id_t ModelChecker::get_next_id()
75 {
76         return next_thread_id++;
77 }
78
79 /** @returns the number of user threads created during this execution */
80 int ModelChecker::get_num_threads()
81 {
82         return next_thread_id;
83 }
84
85 /** @returns a sequence number for a new ModelAction */
86 modelclock_t ModelChecker::get_next_seq_num()
87 {
88         return ++used_sequence_numbers;
89 }
90
91 /**
92  * Performs the "scheduling" for the model-checker. That is, it checks if the
93  * model-checker has selected a "next thread to run" and returns it, if
94  * available. This function should be called from the Scheduler routine, where
95  * the Scheduler falls back to a default scheduling routine if needed.
96  *
97  * @return The next thread chosen by the model-checker. If the model-checker
98  * makes no selection, retuns NULL.
99  */
100 Thread * ModelChecker::schedule_next_thread()
101 {
102         Thread *t;
103         if (nextThread == THREAD_ID_T_NONE)
104                 return NULL;
105         t = (*thread_map)[id_to_int(nextThread)];
106
107         ASSERT(t != NULL);
108
109         return t;
110 }
111
112 /**
113  * Choose the next thread in the replay sequence.
114  *
115  * If the replay sequence has reached the 'diverge' point, returns a thread
116  * from the backtracking set. Otherwise, simply returns the next thread in the
117  * sequence that is being replayed.
118  */
119 thread_id_t ModelChecker::get_next_replay_thread()
120 {
121         ModelAction *next;
122         thread_id_t tid;
123
124         /* Have we completed exploring the preselected path? */
125         if (diverge == NULL)
126                 return THREAD_ID_T_NONE;
127
128         /* Else, we are trying to replay an execution */
129         next = node_stack->get_next()->get_action();
130
131         if (next == diverge) {
132                 Node *nextnode = next->get_node();
133                 /* Reached divergence point */
134                 if (nextnode->increment_read_from()) {
135                         /* The next node will read from a different value */
136                         tid = next->get_tid();
137                         node_stack->pop_restofstack(2);
138                 } else {
139                         /* Make a different thread execute for next step */
140                         Node *node = nextnode->get_parent();
141                         tid = node->get_next_backtrack();
142                         node_stack->pop_restofstack(1);
143                 }
144                 DEBUG("*** Divergence point ***\n");
145                 diverge = NULL;
146         } else {
147                 tid = next->get_tid();
148         }
149         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
150         return tid;
151 }
152
153 /**
154  * Queries the model-checker for more executions to explore and, if one
155  * exists, resets the model-checker state to execute a new execution.
156  *
157  * @return If there are more executions to explore, return true. Otherwise,
158  * return false.
159  */
160 bool ModelChecker::next_execution()
161 {
162         DBG();
163
164         num_executions++;
165
166         if (isfeasible() || DBG_ENABLED())
167                 print_summary();
168
169         if ((diverge = model->get_next_backtrack()) == NULL)
170                 return false;
171
172         if (DBG_ENABLED()) {
173                 printf("Next execution will diverge at:\n");
174                 diverge->print();
175         }
176
177         model->reset_to_initial_state();
178         return true;
179 }
180
181 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
182 {
183         action_type type = act->get_type();
184
185         switch (type) {
186                 case ATOMIC_READ:
187                 case ATOMIC_WRITE:
188                 case ATOMIC_RMW:
189                         break;
190                 default:
191                         return NULL;
192         }
193         /* linear search: from most recent to oldest */
194         action_list_t *list = &(*obj_map)[act->get_location()];
195         action_list_t::reverse_iterator rit;
196         for (rit = list->rbegin(); rit != list->rend(); rit++) {
197                 ModelAction *prev = *rit;
198                 if (act->is_synchronizing(prev))
199                         return prev;
200         }
201         return NULL;
202 }
203
204 void ModelChecker::set_backtracking(ModelAction *act)
205 {
206         ModelAction *prev;
207         Node *node;
208         Thread *t = get_thread(act->get_tid());
209
210         prev = get_last_conflict(act);
211         if (prev == NULL)
212                 return;
213
214         node = prev->get_node()->get_parent();
215
216         while (!node->is_enabled(t))
217                 t = t->get_parent();
218
219         /* Check if this has been explored already */
220         if (node->has_been_explored(t->get_id()))
221                 return;
222
223         /* Cache the latest backtracking point */
224         if (!next_backtrack || *prev > *next_backtrack)
225                 next_backtrack = prev;
226
227         /* If this is a new backtracking point, mark the tree */
228         if (!node->set_backtrack(t->get_id()))
229                 return;
230         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
231                         prev->get_tid(), t->get_id());
232         if (DBG_ENABLED()) {
233                 prev->print();
234                 act->print();
235         }
236 }
237
238 ModelAction * ModelChecker::get_next_backtrack()
239 {
240         ModelAction *next = next_backtrack;
241         next_backtrack = NULL;
242         return next;
243 }
244
245 void ModelChecker::check_current_action(void)
246 {
247         ModelAction *curr = this->current_action;
248         ModelAction *tmp;
249         current_action = NULL;
250         if (!curr) {
251                 DEBUG("trying to push NULL action...\n");
252                 return;
253         }
254
255         if (curr->is_rmw()) {
256                 //We have a RMW action
257                 process_rmw(curr);
258                 //Force the current thread to continue since the RMW should be atomic
259                 nextThread = thread_current()->get_id();
260                 delete curr;
261                 return;
262         }
263
264         tmp = node_stack->explore_action(curr);
265         if (tmp) {
266                 /* Discard duplicate ModelAction; use action from NodeStack */
267                 delete curr;
268                 curr = tmp;
269         } else {
270                 /*
271                  * Perform one-time actions when pushing new ModelAction onto
272                  * NodeStack
273                  */
274                 curr->create_cv(get_parent_action(curr->get_tid()));
275                 /* Build may_read_from set */
276                 if (curr->is_read())
277                         build_reads_from_past(curr);
278         }
279
280         /* Assign 'creation' parent */
281         if (curr->get_type() == THREAD_CREATE) {
282                 Thread *th = (Thread *)curr->get_location();
283                 th->set_creation(curr);
284         }
285
286         /* Is there a better interface for setting the next thread rather
287                  than this field/convoluted approach?  Perhaps like just returning
288                  it or something? */
289
290         nextThread = get_next_replay_thread();
291
292         Node *currnode = curr->get_node();
293         Node *parnode = currnode->get_parent();
294
295         if (!parnode->backtrack_empty()||!currnode->readsfrom_empty())
296                 if (!next_backtrack || *curr > *next_backtrack)
297                         next_backtrack = curr;
298
299         set_backtracking(curr);
300
301         /* Assign reads_from values */
302         /* TODO: perform release/acquire synchronization here; include
303          * reads_from as ModelAction member? */
304         Thread *th = get_thread(curr->get_tid());
305         uint64_t value = VALUE_NONE;
306         if (curr->is_read()) {
307                 const ModelAction *reads_from = curr->get_node()->get_read_from();
308                 value = reads_from->get_value();
309                 /* Assign reads_from, perform release/acquire synchronization */
310                 curr->read_from(reads_from);
311                 r_modification_order(curr,reads_from);
312         } else if (curr->is_write()) {
313                 w_modification_order(curr);
314         }
315
316         th->set_return_value(value);
317
318         /* Add action to list last.  */
319         add_action_to_lists(curr);
320 }
321
322 bool ModelChecker::isfeasible() {
323         return !cyclegraph->checkForCycles();
324 }
325
326 /** Process a RMW by converting previous read into a RMW. */
327 void ModelChecker::process_rmw(ModelAction * act) {
328         int tid = id_to_int(act->get_tid());
329         std::vector<action_list_t> *vec = &(*obj_thrd_map)[act->get_location()];
330         ASSERT(tid < (int) vec->size());
331         ModelAction *lastread=(*vec)[tid].back();
332         lastread->upgrade_rmw(act);
333 }
334
335 void ModelChecker::r_modification_order(ModelAction * curr, const ModelAction *rf) {
336         std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
337         unsigned int i;
338         ASSERT(curr->is_read());
339
340         /* Iterate over all threads */
341         for (i = 0; i < thrd_lists->size(); i++) {
342                 /* Iterate over actions in thread, starting from most recent */
343                 action_list_t *list = &(*thrd_lists)[i];
344                 action_list_t::reverse_iterator rit;
345                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
346                         ModelAction *act = *rit;
347
348                         /* Include at most one act per-thread that "happens before" curr */
349                         if (act->happens_before(curr)) {
350                                 if (act->is_read()) {
351                                         const ModelAction * prevreadfrom=act->get_reads_from();
352                                         if (rf!=prevreadfrom)
353                                                 cyclegraph->addEdge(rf, prevreadfrom);
354                                 } else if (rf!=act) {
355                                         cyclegraph->addEdge(rf, act);
356                                 }
357                                 break;
358                         }
359                 }
360         }
361 }
362
363 void ModelChecker::w_modification_order(ModelAction * curr) {
364         std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
365         unsigned int i;
366         ASSERT(curr->is_write());
367
368         if (curr->is_seqcst()) {
369                 /* We have to at least see the last sequentially consistent write,
370                          so we are initialized. */
371                 ModelAction * last_seq_cst=get_last_seq_cst(curr->get_location());
372                 if (last_seq_cst!=NULL)
373                         cyclegraph->addEdge(curr, last_seq_cst);
374         }
375
376         /* Iterate over all threads */
377         for (i = 0; i < thrd_lists->size(); i++) {
378                 /* Iterate over actions in thread, starting from most recent */
379                 action_list_t *list = &(*thrd_lists)[i];
380                 action_list_t::reverse_iterator rit;
381                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
382                         ModelAction *act = *rit;
383
384                         /* Include at most one act per-thread that "happens before" curr */
385                         if (act->happens_before(curr)) {
386                                 if (act->is_read()) {
387                                         cyclegraph->addEdge(curr, act->get_reads_from());
388                                 } else
389                                         cyclegraph->addEdge(curr, act);
390                                 break;
391                         }
392                 }
393         }
394 }
395
396 /**
397  * Performs various bookkeeping operations for the current ModelAction. For
398  * instance, adds action to the per-object, per-thread action vector and to the
399  * action trace list of all thread actions.
400  *
401  * @param act is the ModelAction to add.
402  */
403 void ModelChecker::add_action_to_lists(ModelAction *act)
404 {
405         int tid = id_to_int(act->get_tid());
406         action_trace->push_back(act);
407
408         (*obj_map)[act->get_location()].push_back(act);
409
410         std::vector<action_list_t> *vec = &(*obj_thrd_map)[act->get_location()];
411         if (tid >= (int)vec->size())
412                 vec->resize(next_thread_id);
413         (*vec)[tid].push_back(act);
414
415         if ((int)thrd_last_action->size() <= tid)
416                 thrd_last_action->resize(get_num_threads());
417         (*thrd_last_action)[tid] = act;
418 }
419
420 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
421 {
422         int nthreads = get_num_threads();
423         if ((int)thrd_last_action->size() < nthreads)
424                 thrd_last_action->resize(nthreads);
425         return (*thrd_last_action)[id_to_int(tid)];
426 }
427
428 /**
429  * Gets the last memory_order_seq_cst action (in the total global sequence)
430  * performed on a particular object (i.e., memory location).
431  * @param location The object location to check
432  * @return The last seq_cst action performed
433  */
434 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
435 {
436         action_list_t *list = &(*obj_map)[location];
437         /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
438         action_list_t::reverse_iterator rit;
439         for (rit = list->rbegin(); rit != list->rend(); rit++)
440                 if ((*rit)->is_write() && (*rit)->is_seqcst())
441                         return *rit;
442         return NULL;
443 }
444
445 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
446 {
447         ModelAction *parent = get_last_action(tid);
448         if (!parent)
449                 parent = get_thread(tid)->get_creation();
450         return parent;
451 }
452
453 ClockVector * ModelChecker::get_cv(thread_id_t tid) {
454         return get_parent_action(tid)->get_cv();
455 }
456
457 /**
458  * Build up an initial set of all past writes that this 'read' action may read
459  * from. This set is determined by the clock vector's "happens before"
460  * relationship.
461  * @param curr is the current ModelAction that we are exploring; it must be a
462  * 'read' operation.
463  */
464 void ModelChecker::build_reads_from_past(ModelAction *curr)
465 {
466         std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
467         unsigned int i;
468         ASSERT(curr->is_read());
469
470         ModelAction *last_seq_cst = NULL;
471
472         /* Track whether this object has been initialized */
473         bool initialized = false;
474
475         if (curr->is_seqcst()) {
476                 last_seq_cst = get_last_seq_cst(curr->get_location());
477                 /* We have to at least see the last sequentially consistent write,
478                          so we are initialized. */
479                 if (last_seq_cst != NULL)
480                         initialized = true;
481         }
482
483         /* Iterate over all threads */
484         for (i = 0; i < thrd_lists->size(); i++) {
485                 /* Iterate over actions in thread, starting from most recent */
486                 action_list_t *list = &(*thrd_lists)[i];
487                 action_list_t::reverse_iterator rit;
488                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
489                         ModelAction *act = *rit;
490
491                         /* Only consider 'write' actions */
492                         if (!act->is_write())
493                                 continue;
494
495                         /* Don't consider more than one seq_cst write if we are a seq_cst read. */
496                         if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
497                                 DEBUG("Adding action to may_read_from:\n");
498                                 if (DBG_ENABLED()) {
499                                         act->print();
500                                         curr->print();
501                                 }
502                                 curr->get_node()->add_read_from(act);
503                         }
504
505                         /* Include at most one act per-thread that "happens before" curr */
506                         if (act->happens_before(curr)) {
507                                 initialized = true;
508                                 break;
509                         }
510                 }
511         }
512
513         if (!initialized) {
514                 /* TODO: need a more informative way of reporting errors */
515                 printf("ERROR: may read from uninitialized atomic\n");
516         }
517         
518         if (DBG_ENABLED() || !initialized) {
519                 printf("Reached read action:\n");
520                 curr->print();
521                 printf("Printing may_read_from\n");
522                 curr->get_node()->print_may_read_from();
523                 printf("End printing may_read_from\n");
524         }
525         
526         ASSERT(initialized);
527 }
528
529 static void print_list(action_list_t *list)
530 {
531         action_list_t::iterator it;
532
533         printf("---------------------------------------------------------------------\n");
534         printf("Trace:\n");
535
536         for (it = list->begin(); it != list->end(); it++) {
537                 (*it)->print();
538         }
539         printf("---------------------------------------------------------------------\n");
540 }
541
542 void ModelChecker::print_summary(void)
543 {
544         printf("\n");
545         printf("Number of executions: %d\n", num_executions);
546         printf("Total nodes created: %d\n", node_stack->get_total_nodes());
547
548         scheduler->print();
549
550         if (!isfeasible())
551                 printf("INFEASIBLE EXECUTION!\n");
552         print_list(action_trace);
553         printf("\n");
554 }
555
556 int ModelChecker::add_thread(Thread *t)
557 {
558         (*thread_map)[id_to_int(t->get_id())] = t;
559         scheduler->add_thread(t);
560         return 0;
561 }
562
563 void ModelChecker::remove_thread(Thread *t)
564 {
565         scheduler->remove_thread(t);
566 }
567
568 /**
569  * Switch from a user-context to the "master thread" context (a.k.a. system
570  * context). This switch is made with the intention of exploring a particular
571  * model-checking action (described by a ModelAction object). Must be called
572  * from a user-thread context.
573  * @param act The current action that will be explored. May be NULL, although
574  * there is little reason to switch to the model-checker without an action to
575  * explore (note: act == NULL is sometimes used as a hack to allow a thread to
576  * yield control without performing any progress; see thrd_join()).
577  * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
578  */
579 int ModelChecker::switch_to_master(ModelAction *act)
580 {
581         Thread *old;
582
583         DBG();
584         old = thread_current();
585         set_current_action(act);
586         old->set_state(THREAD_READY);
587         return Thread::swap(old, get_system_context());
588 }