7 #include "snapshot-interface.h"
9 #include "clockvector.h"
10 #include "cyclegraph.h"
13 #define INITIAL_THREAD_ID 0
17 /** @brief Constructor */
18 ModelChecker::ModelChecker()
20 /* Initialize default scheduler */
21 scheduler(new Scheduler()),
22 /* First thread created will have id INITIAL_THREAD_ID */
23 next_thread_id(INITIAL_THREAD_ID),
24 used_sequence_numbers(0),
28 nextThread(THREAD_ID_T_NONE),
29 action_trace(new action_list_t()),
30 thread_map(new HashTable<int, Thread *, int>()),
31 obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
32 obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
33 promises(new std::vector<Promise *>()),
34 thrd_last_action(new std::vector<ModelAction *>(1)),
35 node_stack(new NodeStack()),
37 cyclegraph(new CycleGraph()),
42 /** @brief Destructor */
43 ModelChecker::~ModelChecker()
45 /* std::map<int, Thread *>::iterator it;
46 for (it = thread_map->begin(); it != thread_map->end(); it++)
47 delete (*it).second;*/
53 delete thrd_last_action;
60 * Restores user program to initial state and resets all model-checker data
63 void ModelChecker::reset_to_initial_state()
65 DEBUG("+++ Resetting to initial state +++\n");
66 node_stack->reset_execution();
67 current_action = NULL;
68 next_thread_id = INITIAL_THREAD_ID;
69 used_sequence_numbers = 0;
71 next_backtrack = NULL;
72 failed_promise = false;
73 snapshotObject->backTrackBeforeStep(0);
76 /** @returns a thread ID for a new Thread */
77 thread_id_t ModelChecker::get_next_id()
79 return next_thread_id++;
82 /** @returns the number of user threads created during this execution */
83 int ModelChecker::get_num_threads()
85 return next_thread_id;
88 /** @returns a sequence number for a new ModelAction */
89 modelclock_t ModelChecker::get_next_seq_num()
91 return ++used_sequence_numbers;
95 * Performs the "scheduling" for the model-checker. That is, it checks if the
96 * model-checker has selected a "next thread to run" and returns it, if
97 * available. This function should be called from the Scheduler routine, where
98 * the Scheduler falls back to a default scheduling routine if needed.
100 * @return The next thread chosen by the model-checker. If the model-checker
101 * makes no selection, retuns NULL.
103 Thread * ModelChecker::schedule_next_thread()
106 if (nextThread == THREAD_ID_T_NONE)
108 t = thread_map->get(id_to_int(nextThread));
116 * Choose the next thread in the replay sequence.
118 * If the replay sequence has reached the 'diverge' point, returns a thread
119 * from the backtracking set. Otherwise, simply returns the next thread in the
120 * sequence that is being replayed.
122 thread_id_t ModelChecker::get_next_replay_thread()
126 /* Have we completed exploring the preselected path? */
128 return THREAD_ID_T_NONE;
130 /* Else, we are trying to replay an execution */
131 ModelAction * next = node_stack->get_next()->get_action();
133 if (next == diverge) {
134 Node *nextnode = next->get_node();
135 /* Reached divergence point */
136 if (nextnode->increment_read_from()) {
137 /* The next node will read from a different value. */
138 tid = next->get_tid();
139 node_stack->pop_restofstack(2);
140 } else if (nextnode->increment_future_values()) {
141 /* The next node will try to read from a different future value. */
142 tid = next->get_tid();
143 node_stack->pop_restofstack(2);
145 /* Make a different thread execute for next step */
146 Node *node = nextnode->get_parent();
147 tid = node->get_next_backtrack();
148 node_stack->pop_restofstack(1);
150 DEBUG("*** Divergence point ***\n");
153 tid = next->get_tid();
155 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
160 * Queries the model-checker for more executions to explore and, if one
161 * exists, resets the model-checker state to execute a new execution.
163 * @return If there are more executions to explore, return true. Otherwise,
166 bool ModelChecker::next_execution()
172 if (isfeasible() || DBG_ENABLED())
175 if ((diverge = model->get_next_backtrack()) == NULL)
179 printf("Next execution will diverge at:\n");
183 model->reset_to_initial_state();
187 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
189 action_type type = act->get_type();
199 /* linear search: from most recent to oldest */
200 action_list_t *list = obj_map->ensureptr(act->get_location());
201 action_list_t::reverse_iterator rit;
202 for (rit = list->rbegin(); rit != list->rend(); rit++) {
203 ModelAction *prev = *rit;
204 if (act->is_synchronizing(prev))
210 void ModelChecker::set_backtracking(ModelAction *act)
214 Thread *t = get_thread(act->get_tid());
216 prev = get_last_conflict(act);
220 node = prev->get_node()->get_parent();
222 while (!node->is_enabled(t))
225 /* Check if this has been explored already */
226 if (node->has_been_explored(t->get_id()))
229 /* Cache the latest backtracking point */
230 if (!next_backtrack || *prev > *next_backtrack)
231 next_backtrack = prev;
233 /* If this is a new backtracking point, mark the tree */
234 if (!node->set_backtrack(t->get_id()))
236 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
237 prev->get_tid(), t->get_id());
245 * Returns last backtracking point. The model checker will explore a different
246 * path for this point in the next execution.
247 * @return The ModelAction at which the next execution should diverge.
249 ModelAction * ModelChecker::get_next_backtrack()
251 ModelAction *next = next_backtrack;
252 next_backtrack = NULL;
256 void ModelChecker::check_current_action(void)
258 ModelAction *curr = this->current_action;
259 bool already_added = false;
260 this->current_action = NULL;
262 DEBUG("trying to push NULL action...\n");
266 if (curr->is_rmwc()||curr->is_rmw()) {
267 ModelAction *tmp=process_rmw(curr);
268 already_added = true;
272 ModelAction * tmp = node_stack->explore_action(curr);
274 /* Discard duplicate ModelAction; use action from NodeStack */
275 /* First restore type and order in case of RMW operation */
277 tmp->copy_typeandorder(curr);
282 * Perform one-time actions when pushing new ModelAction onto
285 curr->create_cv(get_parent_action(curr->get_tid()));
286 /* Build may_read_from set */
288 build_reads_from_past(curr);
292 /* Assign 'creation' parent */
293 if (curr->get_type() == THREAD_CREATE) {
294 Thread *th = (Thread *)curr->get_location();
295 th->set_creation(curr);
298 /* Deal with new thread */
299 if (curr->get_type() == THREAD_START) {
300 check_promises(NULL, curr->get_cv());
303 /* Assign reads_from values */
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 if (reads_from!=NULL) {
309 value = reads_from->get_value();
310 /* Assign reads_from, perform release/acquire synchronization */
311 curr->read_from(reads_from);
312 r_modification_order(curr,reads_from);
314 /* Read from future value */
315 value = curr->get_node()->get_future_value();
316 curr->read_from(NULL);
317 Promise * valuepromise=new Promise(curr, value);
318 promises->push_back(valuepromise);
320 } else if (curr->is_write()) {
321 w_modification_order(curr);
322 resolve_promises(curr);
325 th->set_return_value(value);
327 /* Add action to list. */
329 add_action_to_lists(curr);
331 /* Is there a better interface for setting the next thread rather
332 than this field/convoluted approach? Perhaps like just returning
335 /* Do not split atomic actions. */
336 if (curr->is_rmwr()) {
337 nextThread = thread_current()->get_id();
339 nextThread = get_next_replay_thread();
342 Node *currnode = curr->get_node();
343 Node *parnode = currnode->get_parent();
345 if (!parnode->backtrack_empty()||!currnode->readsfrom_empty()||!currnode->futurevalues_empty())
346 if (!next_backtrack || *curr > *next_backtrack)
347 next_backtrack = curr;
349 set_backtracking(curr);
352 /** @returns whether the current trace is feasible. */
353 bool ModelChecker::isfeasible() {
354 return !cyclegraph->checkForCycles() && !failed_promise;
357 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
358 ModelAction * ModelChecker::process_rmw(ModelAction * act) {
359 int tid = id_to_int(act->get_tid());
360 ModelAction *lastread=get_last_action(tid);
361 lastread->process_rmw(act);
363 cyclegraph->addRMWEdge(lastread, lastread->get_reads_from());
368 * Updates the cyclegraph with the constraints imposed from the current read.
369 * @param curr The current action. Must be a read.
370 * @param rf The action that curr reads from. Must be a write.
372 void ModelChecker::r_modification_order(ModelAction * curr, const ModelAction *rf) {
373 std::vector<action_list_t> *thrd_lists = obj_thrd_map->ensureptr(curr->get_location());
375 ASSERT(curr->is_read());
377 /* Iterate over all threads */
378 for (i = 0; i < thrd_lists->size(); i++) {
379 /* Iterate over actions in thread, starting from most recent */
380 action_list_t *list = &(*thrd_lists)[i];
381 action_list_t::reverse_iterator rit;
382 for (rit = list->rbegin(); rit != list->rend(); rit++) {
383 ModelAction *act = *rit;
385 /* Include at most one act per-thread that "happens before" curr */
386 if (act->happens_before(curr)) {
387 if (act->is_read()) {
388 const ModelAction * prevreadfrom=act->get_reads_from();
389 if (rf!=prevreadfrom)
390 cyclegraph->addEdge(rf, prevreadfrom);
391 } else if (rf!=act) {
392 cyclegraph->addEdge(rf, act);
401 * Updates the cyclegraph with the constraints imposed from the current write.
402 * @param curr The current action. Must be a write.
404 void ModelChecker::w_modification_order(ModelAction * curr) {
405 std::vector<action_list_t> *thrd_lists = obj_thrd_map->ensureptr(curr->get_location());
407 ASSERT(curr->is_write());
409 if (curr->is_seqcst()) {
410 /* We have to at least see the last sequentially consistent write,
411 so we are initialized. */
412 ModelAction * last_seq_cst=get_last_seq_cst(curr->get_location());
413 if (last_seq_cst!=NULL)
414 cyclegraph->addEdge(curr, last_seq_cst);
417 /* Iterate over all threads */
418 for (i = 0; i < thrd_lists->size(); i++) {
419 /* Iterate over actions in thread, starting from most recent */
420 action_list_t *list = &(*thrd_lists)[i];
421 action_list_t::reverse_iterator rit;
422 for (rit = list->rbegin(); rit != list->rend(); rit++) {
423 ModelAction *act = *rit;
425 /* Include at most one act per-thread that "happens before" curr */
426 if (act->happens_before(curr)) {
427 if (act->is_read()) {
428 cyclegraph->addEdge(curr, act->get_reads_from());
430 cyclegraph->addEdge(curr, act);
433 if (act->is_read()&&!act->is_synchronizing(curr)&&!act->same_thread(curr)) {
434 /* We have an action that:
435 (1) did not happen before us
436 (2) is a read and we are a write
437 (3) cannot synchronize with us
438 (4) is in a different thread
440 that read could potentially read from our write.
443 if (act->get_node()->add_future_value(curr->get_value())&&
444 (!next_backtrack || *act > * next_backtrack))
445 next_backtrack = act;
453 * Performs various bookkeeping operations for the current ModelAction. For
454 * instance, adds action to the per-object, per-thread action vector and to the
455 * action trace list of all thread actions.
457 * @param act is the ModelAction to add.
459 void ModelChecker::add_action_to_lists(ModelAction *act)
461 int tid = id_to_int(act->get_tid());
462 action_trace->push_back(act);
464 obj_map->ensureptr(act->get_location())->push_back(act);
466 std::vector<action_list_t> *vec = obj_thrd_map->ensureptr(act->get_location());
467 if (tid >= (int)vec->size())
468 vec->resize(next_thread_id);
469 (*vec)[tid].push_back(act);
471 if ((int)thrd_last_action->size() <= tid)
472 thrd_last_action->resize(get_num_threads());
473 (*thrd_last_action)[tid] = act;
476 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
478 int nthreads = get_num_threads();
479 if ((int)thrd_last_action->size() < nthreads)
480 thrd_last_action->resize(nthreads);
481 return (*thrd_last_action)[id_to_int(tid)];
485 * Gets the last memory_order_seq_cst action (in the total global sequence)
486 * performed on a particular object (i.e., memory location).
487 * @param location The object location to check
488 * @return The last seq_cst action performed
490 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
492 action_list_t *list = obj_map->ensureptr(location);
493 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
494 action_list_t::reverse_iterator rit;
495 for (rit = list->rbegin(); rit != list->rend(); rit++)
496 if ((*rit)->is_write() && (*rit)->is_seqcst())
501 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
503 ModelAction *parent = get_last_action(tid);
505 parent = get_thread(tid)->get_creation();
510 * Returns the clock vector for a given thread.
511 * @param tid The thread whose clock vector we want
512 * @return Desired clock vector
514 ClockVector * ModelChecker::get_cv(thread_id_t tid) {
515 return get_parent_action(tid)->get_cv();
519 /** Resolve promises. */
521 void ModelChecker::resolve_promises(ModelAction *curr) {
522 for(unsigned int i=0;i<promises->size();i++) {
523 Promise * promise=(*promises)[i];
524 const ModelAction * act=promise->get_action();
525 if (!act->happens_before(curr)&&
527 !act->is_synchronizing(curr)&&
528 !act->same_thread(curr)&&
529 promise->get_value()==curr->get_value()) {
536 /** Checks promises in response to change in ClockVector Threads. */
538 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector * merge_cv) {
539 for(unsigned int i=0;i<promises->size();i++) {
540 Promise * promise=(*promises)[i];
541 const ModelAction * act=promise->get_action();
542 if ((old_cv==NULL||!old_cv->synchronized_since(act))&&
543 merge_cv->synchronized_since(act)) {
544 //This thread is no longer able to send values back to satisfy the promise
545 int num_synchronized_threads=promise->increment_threads();
546 if (num_synchronized_threads==model->get_num_threads()) {
548 failed_promise = true;
556 * Build up an initial set of all past writes that this 'read' action may read
557 * from. This set is determined by the clock vector's "happens before"
559 * @param curr is the current ModelAction that we are exploring; it must be a
562 void ModelChecker::build_reads_from_past(ModelAction *curr)
564 std::vector<action_list_t> *thrd_lists = obj_thrd_map->ensureptr(curr->get_location());
566 ASSERT(curr->is_read());
568 ModelAction *last_seq_cst = NULL;
570 /* Track whether this object has been initialized */
571 bool initialized = false;
573 if (curr->is_seqcst()) {
574 last_seq_cst = get_last_seq_cst(curr->get_location());
575 /* We have to at least see the last sequentially consistent write,
576 so we are initialized. */
577 if (last_seq_cst != NULL)
581 /* Iterate over all threads */
582 for (i = 0; i < thrd_lists->size(); i++) {
583 /* Iterate over actions in thread, starting from most recent */
584 action_list_t *list = &(*thrd_lists)[i];
585 action_list_t::reverse_iterator rit;
586 for (rit = list->rbegin(); rit != list->rend(); rit++) {
587 ModelAction *act = *rit;
589 /* Only consider 'write' actions */
590 if (!act->is_write())
593 /* Don't consider more than one seq_cst write if we are a seq_cst read. */
594 if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
595 DEBUG("Adding action to may_read_from:\n");
600 curr->get_node()->add_read_from(act);
603 /* Include at most one act per-thread that "happens before" curr */
604 if (act->happens_before(curr)) {
612 /** @todo Need a more informative way of reporting errors. */
613 printf("ERROR: may read from uninitialized atomic\n");
616 if (DBG_ENABLED() || !initialized) {
617 printf("Reached read action:\n");
619 printf("Printing may_read_from\n");
620 curr->get_node()->print_may_read_from();
621 printf("End printing may_read_from\n");
627 static void print_list(action_list_t *list)
629 action_list_t::iterator it;
631 printf("---------------------------------------------------------------------\n");
634 for (it = list->begin(); it != list->end(); it++) {
637 printf("---------------------------------------------------------------------\n");
640 void ModelChecker::print_summary(void)
643 printf("Number of executions: %d\n", num_executions);
644 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
649 printf("INFEASIBLE EXECUTION!\n");
650 print_list(action_trace);
654 int ModelChecker::add_thread(Thread *t)
656 thread_map->put(id_to_int(t->get_id()), t);
657 scheduler->add_thread(t);
661 void ModelChecker::remove_thread(Thread *t)
663 scheduler->remove_thread(t);
667 * Switch from a user-context to the "master thread" context (a.k.a. system
668 * context). This switch is made with the intention of exploring a particular
669 * model-checking action (described by a ModelAction object). Must be called
670 * from a user-thread context.
671 * @param act The current action that will be explored. May be NULL, although
672 * there is little reason to switch to the model-checker without an action to
673 * explore (note: act == NULL is sometimes used as a hack to allow a thread to
674 * yield control without performing any progress; see thrd_join()).
675 * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
677 int ModelChecker::switch_to_master(ModelAction *act)
680 Thread * old = thread_current();
681 set_current_action(act);
682 old->set_state(THREAD_READY);
683 return Thread::swap(old, get_system_context());