print less stuff
[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         print_summary();
166         if ((diverge = model->get_next_backtrack()) == NULL)
167                 return false;
168
169         if (DBG_ENABLED()) {
170                 printf("Next execution will diverge at:\n");
171                 diverge->print();
172         }
173
174         model->reset_to_initial_state();
175         return true;
176 }
177
178 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
179 {
180         action_type type = act->get_type();
181
182         switch (type) {
183                 case ATOMIC_READ:
184                 case ATOMIC_WRITE:
185                 case ATOMIC_RMW:
186                         break;
187                 default:
188                         return NULL;
189         }
190         /* linear search: from most recent to oldest */
191         action_list_t *list = &(*obj_map)[act->get_location()];
192         action_list_t::reverse_iterator rit;
193         for (rit = list->rbegin(); rit != list->rend(); rit++) {
194                 ModelAction *prev = *rit;
195                 if (act->is_synchronizing(prev))
196                         return prev;
197         }
198         return NULL;
199 }
200
201 void ModelChecker::set_backtracking(ModelAction *act)
202 {
203         ModelAction *prev;
204         Node *node;
205         Thread *t = get_thread(act->get_tid());
206
207         prev = get_last_conflict(act);
208         if (prev == NULL)
209                 return;
210
211         node = prev->get_node()->get_parent();
212
213         while (!node->is_enabled(t))
214                 t = t->get_parent();
215
216         /* Check if this has been explored already */
217         if (node->has_been_explored(t->get_id()))
218                 return;
219
220         /* Cache the latest backtracking point */
221         if (!next_backtrack || *prev > *next_backtrack)
222                 next_backtrack = prev;
223
224         /* If this is a new backtracking point, mark the tree */
225         if (!node->set_backtrack(t->get_id()))
226                 return;
227         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
228                         prev->get_tid(), t->get_id());
229         if (DBG_ENABLED()) {
230                 prev->print();
231                 act->print();
232         }
233 }
234
235 ModelAction * ModelChecker::get_next_backtrack()
236 {
237         ModelAction *next = next_backtrack;
238         next_backtrack = NULL;
239         return next;
240 }
241
242 void ModelChecker::check_current_action(void)
243 {
244         ModelAction *curr = this->current_action;
245         ModelAction *tmp;
246         current_action = NULL;
247         if (!curr) {
248                 DEBUG("trying to push NULL action...\n");
249                 return;
250         }
251
252         tmp = node_stack->explore_action(curr);
253         if (tmp) {
254                 /* Discard duplicate ModelAction; use action from NodeStack */
255                 delete curr;
256                 curr = tmp;
257         } else {
258                 /*
259                  * Perform one-time actions when pushing new ModelAction onto
260                  * NodeStack
261                  */
262                 curr->create_cv(get_parent_action(curr->get_tid()));
263                 /* Build may_read_from set */
264                 if (curr->is_read())
265                         build_reads_from_past(curr);
266         }
267
268         /* Assign 'creation' parent */
269         if (curr->get_type() == THREAD_CREATE) {
270                 Thread *th = (Thread *)curr->get_location();
271                 th->set_creation(curr);
272         }
273
274         nextThread = get_next_replay_thread();
275
276         Node *currnode = curr->get_node();
277         Node *parnode = currnode->get_parent();
278
279         if (!parnode->backtrack_empty()||!currnode->readsfrom_empty())
280                 if (!next_backtrack || *curr > *next_backtrack)
281                         next_backtrack = curr;
282
283         set_backtracking(curr);
284
285         /* Assign reads_from values */
286         /* TODO: perform release/acquire synchronization here; include
287          * reads_from as ModelAction member? */
288         Thread *th = get_thread(curr->get_tid());
289         uint64_t value = VALUE_NONE;
290         if (curr->is_read()) {
291                 const ModelAction *reads_from = curr->get_node()->get_read_from();
292                 value = reads_from->get_value();
293                 /* Assign reads_from, perform release/acquire synchronization */
294                 curr->read_from(reads_from);
295                 r_modification_order(curr,reads_from);
296         } else if (curr->is_write()) {
297                 w_modification_order(curr);
298         }
299
300         th->set_return_value(value);
301
302         /* Add action to list last.  */
303         add_action_to_lists(curr);
304 }
305
306 bool ModelChecker::isfeasible() {
307         return !cyclegraph->checkForCycles();
308 }
309
310 void ModelChecker::r_modification_order(ModelAction * curr, const ModelAction *rf) {
311         std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
312         unsigned int i;
313         ASSERT(curr->is_read());
314
315         /* Iterate over all threads */
316         for (i = 0; i < thrd_lists->size(); i++) {
317                 /* Iterate over actions in thread, starting from most recent */
318                 action_list_t *list = &(*thrd_lists)[i];
319                 action_list_t::reverse_iterator rit;
320                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
321                         ModelAction *act = *rit;
322
323                         /* Include at most one act per-thread that "happens before" curr */
324                         if (act->happens_before(curr)) {
325                                 if (act->is_read()) {
326                                         const ModelAction * prevreadfrom=act->get_reads_from();
327                                         if (rf!=prevreadfrom)
328                                                 cyclegraph->addEdge(rf, prevreadfrom);
329                                 } else if (rf!=act) {
330                                         cyclegraph->addEdge(rf, act);
331                                 }
332                                 break;
333                         }
334                 }
335         }
336 }
337
338 void ModelChecker::w_modification_order(ModelAction * curr) {
339         std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
340         unsigned int i;
341         ASSERT(curr->is_write());
342
343         if (curr->is_seqcst()) {
344                 /* We have to at least see the last sequentially consistent write,
345                          so we are initialized. */
346                 ModelAction * last_seq_cst=get_last_seq_cst(curr->get_location());
347                 if (last_seq_cst!=NULL)
348                         cyclegraph->addEdge(curr, last_seq_cst);
349         }
350
351         /* Iterate over all threads */
352         for (i = 0; i < thrd_lists->size(); i++) {
353                 /* Iterate over actions in thread, starting from most recent */
354                 action_list_t *list = &(*thrd_lists)[i];
355                 action_list_t::reverse_iterator rit;
356                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
357                         ModelAction *act = *rit;
358
359                         /* Include at most one act per-thread that "happens before" curr */
360                         if (act->happens_before(curr)) {
361                                 if (act->is_read()) {
362                                         cyclegraph->addEdge(curr, act->get_reads_from());
363                                 } else
364                                         cyclegraph->addEdge(curr, act);
365                                 break;
366                         }
367                 }
368         }
369 }
370
371 /**
372  * Performs various bookkeeping operations for the current ModelAction. For
373  * instance, adds action to the per-object, per-thread action vector and to the
374  * action trace list of all thread actions.
375  *
376  * @param act is the ModelAction to add.
377  */
378 void ModelChecker::add_action_to_lists(ModelAction *act)
379 {
380         int tid = id_to_int(act->get_tid());
381         action_trace->push_back(act);
382
383         (*obj_map)[act->get_location()].push_back(act);
384
385         std::vector<action_list_t> *vec = &(*obj_thrd_map)[act->get_location()];
386         if (tid >= (int)vec->size())
387                 vec->resize(next_thread_id);
388         (*vec)[tid].push_back(act);
389
390         if ((int)thrd_last_action->size() <= tid)
391                 thrd_last_action->resize(get_num_threads());
392         (*thrd_last_action)[tid] = act;
393 }
394
395 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
396 {
397         int nthreads = get_num_threads();
398         if ((int)thrd_last_action->size() < nthreads)
399                 thrd_last_action->resize(nthreads);
400         return (*thrd_last_action)[id_to_int(tid)];
401 }
402
403 /**
404  * Gets the last memory_order_seq_cst action (in the total global sequence)
405  * performed on a particular object (i.e., memory location).
406  * @param location The object location to check
407  * @return The last seq_cst action performed
408  */
409 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
410 {
411         action_list_t *list = &(*obj_map)[location];
412         /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
413         action_list_t::reverse_iterator rit;
414         for (rit = list->rbegin(); rit != list->rend(); rit++)
415                 if ((*rit)->is_write() && (*rit)->is_seqcst())
416                         return *rit;
417         return NULL;
418 }
419
420 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
421 {
422         ModelAction *parent = get_last_action(tid);
423         if (!parent)
424                 parent = get_thread(tid)->get_creation();
425         return parent;
426 }
427
428 ClockVector * ModelChecker::get_cv(thread_id_t tid) {
429         return get_parent_action(tid)->get_cv();
430 }
431
432 /**
433  * Build up an initial set of all past writes that this 'read' action may read
434  * from. This set is determined by the clock vector's "happens before"
435  * relationship.
436  * @param curr is the current ModelAction that we are exploring; it must be a
437  * 'read' operation.
438  */
439 void ModelChecker::build_reads_from_past(ModelAction *curr)
440 {
441         std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
442         unsigned int i;
443         ASSERT(curr->is_read());
444
445         ModelAction *last_seq_cst = NULL;
446
447         /* Track whether this object has been initialized */
448         bool initialized = false;
449
450         if (curr->is_seqcst()) {
451                 last_seq_cst = get_last_seq_cst(curr->get_location());
452                 /* We have to at least see the last sequentially consistent write,
453                          so we are initialized. */
454                 if (last_seq_cst != NULL)
455                         initialized = true;
456         }
457
458         /* Iterate over all threads */
459         for (i = 0; i < thrd_lists->size(); i++) {
460                 /* Iterate over actions in thread, starting from most recent */
461                 action_list_t *list = &(*thrd_lists)[i];
462                 action_list_t::reverse_iterator rit;
463                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
464                         ModelAction *act = *rit;
465
466                         /* Only consider 'write' actions */
467                         if (!act->is_write())
468                                 continue;
469
470                         /* Don't consider more than one seq_cst write if we are a seq_cst read. */
471                         if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
472                                 DEBUG("Adding action to may_read_from:\n");
473                                 if (DBG_ENABLED()) {
474                                         act->print();
475                                         curr->print();
476                                 }
477                                 curr->get_node()->add_read_from(act);
478                         }
479
480                         /* Include at most one act per-thread that "happens before" curr */
481                         if (act->happens_before(curr)) {
482                                 initialized = true;
483                                 break;
484                         }
485                 }
486         }
487
488         if (!initialized) {
489                 /* TODO: need a more informative way of reporting errors */
490                 printf("ERROR: may read from uninitialized atomic\n");
491         }
492         
493         if (DBG_ENABLED() || !initialized) {
494                 printf("Reached read action:\n");
495                 curr->print();
496                 printf("Printing may_read_from\n");
497                 curr->get_node()->print_may_read_from();
498                 printf("End printing may_read_from\n");
499         }
500         
501         ASSERT(initialized);
502 }
503
504 static void print_list(action_list_t *list)
505 {
506         action_list_t::iterator it;
507
508         printf("---------------------------------------------------------------------\n");
509         printf("Trace:\n");
510
511         for (it = list->begin(); it != list->end(); it++) {
512                 (*it)->print();
513         }
514         printf("---------------------------------------------------------------------\n");
515 }
516
517 void ModelChecker::print_summary(void)
518 {
519         if (!isfeasible()) {
520                 if (DBG_ENABLED())
521                         printf("INFEASIBLE EXECUTION!\n");
522                 else
523                         return;
524         }
525
526         printf("\n");
527         printf("Number of executions: %d\n", num_executions);
528         printf("Total nodes created: %d\n", node_stack->get_total_nodes());
529
530
531         scheduler->print();
532
533         print_list(action_trace);
534         printf("\n");
535 }
536
537 int ModelChecker::add_thread(Thread *t)
538 {
539         (*thread_map)[id_to_int(t->get_id())] = t;
540         scheduler->add_thread(t);
541         return 0;
542 }
543
544 void ModelChecker::remove_thread(Thread *t)
545 {
546         scheduler->remove_thread(t);
547 }
548
549 /**
550  * Switch from a user-context to the "master thread" context (a.k.a. system
551  * context). This switch is made with the intention of exploring a particular
552  * model-checking action (described by a ModelAction object). Must be called
553  * from a user-thread context.
554  * @param act The current action that will be explored. May be NULL, although
555  * there is little reason to switch to the model-checker without an action to
556  * explore (note: act == NULL is sometimes used as a hack to allow a thread to
557  * yield control without performing any progress; see thrd_join()).
558  * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
559  */
560 int ModelChecker::switch_to_master(ModelAction *act)
561 {
562         Thread *old;
563
564         DBG();
565         old = thread_current();
566         set_current_action(act);
567         old->set_state(THREAD_READY);
568         return Thread::swap(old, get_system_context());
569 }