7 #include "snapshot-interface.h"
9 #include "clockvector.h"
10 #include "cyclegraph.h"
12 #define INITIAL_THREAD_ID 0
16 /** @brief Constructor */
17 ModelChecker::ModelChecker()
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),
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()),
36 cyclegraph(new CycleGraph())
40 /** @brief Destructor */
41 ModelChecker::~ModelChecker()
43 std::map<int, Thread *>::iterator it;
44 for (it = thread_map->begin(); it != thread_map->end(); it++)
51 delete thrd_last_action;
58 * Restores user program to initial state and resets all model-checker data
61 void ModelChecker::reset_to_initial_state()
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;
69 next_backtrack = NULL;
70 snapshotObject->backTrackBeforeStep(0);
73 /** @returns a thread ID for a new Thread */
74 thread_id_t ModelChecker::get_next_id()
76 return next_thread_id++;
79 /** @returns the number of user threads created during this execution */
80 int ModelChecker::get_num_threads()
82 return next_thread_id;
85 /** @returns a sequence number for a new ModelAction */
86 modelclock_t ModelChecker::get_next_seq_num()
88 return ++used_sequence_numbers;
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.
97 * @return The next thread chosen by the model-checker. If the model-checker
98 * makes no selection, retuns NULL.
100 Thread * ModelChecker::schedule_next_thread()
103 if (nextThread == THREAD_ID_T_NONE)
105 t = (*thread_map)[id_to_int(nextThread)];
113 * Choose the next thread in the replay sequence.
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.
119 thread_id_t ModelChecker::get_next_replay_thread()
124 /* Have we completed exploring the preselected path? */
126 return THREAD_ID_T_NONE;
128 /* Else, we are trying to replay an execution */
129 next = node_stack->get_next()->get_action();
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);
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);
144 DEBUG("*** Divergence point ***\n");
147 tid = next->get_tid();
149 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
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.
157 * @return If there are more executions to explore, return true. Otherwise,
160 bool ModelChecker::next_execution()
166 if ((diverge = model->get_next_backtrack()) == NULL)
170 printf("Next execution will diverge at:\n");
174 model->reset_to_initial_state();
178 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
180 action_type type = act->get_type();
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))
201 void ModelChecker::set_backtracking(ModelAction *act)
205 Thread *t = get_thread(act->get_tid());
207 prev = get_last_conflict(act);
211 node = prev->get_node()->get_parent();
213 while (!node->is_enabled(t))
216 /* Check if this has been explored already */
217 if (node->has_been_explored(t->get_id()))
220 /* Cache the latest backtracking point */
221 if (!next_backtrack || *prev > *next_backtrack)
222 next_backtrack = prev;
224 /* If this is a new backtracking point, mark the tree */
225 if (!node->set_backtrack(t->get_id()))
227 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
228 prev->get_tid(), t->get_id());
235 ModelAction * ModelChecker::get_next_backtrack()
237 ModelAction *next = next_backtrack;
238 next_backtrack = NULL;
242 void ModelChecker::check_current_action(void)
244 ModelAction *curr = this->current_action;
246 current_action = NULL;
248 DEBUG("trying to push NULL action...\n");
252 tmp = node_stack->explore_action(curr);
254 /* Discard duplicate ModelAction; use action from NodeStack */
259 * Perform one-time actions when pushing new ModelAction onto
262 curr->create_cv(get_parent_action(curr->get_tid()));
263 /* Build may_read_from set */
265 build_reads_from_past(curr);
268 /* Assign 'creation' parent */
269 if (curr->get_type() == THREAD_CREATE) {
270 Thread *th = (Thread *)curr->get_location();
271 th->set_creation(curr);
274 nextThread = get_next_replay_thread();
276 Node *currnode = curr->get_node();
277 Node *parnode = currnode->get_parent();
279 if (!parnode->backtrack_empty()||!currnode->readsfrom_empty())
280 if (!next_backtrack || *curr > *next_backtrack)
281 next_backtrack = curr;
283 set_backtracking(curr);
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);
300 th->set_return_value(value);
302 /* Add action to list last. */
303 add_action_to_lists(curr);
306 bool ModelChecker::isfeasible() {
307 return !cyclegraph->checkForCycles();
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()];
313 ASSERT(curr->is_read());
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;
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);
338 void ModelChecker::w_modification_order(ModelAction * curr) {
339 std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
341 ASSERT(curr->is_write());
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);
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;
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());
364 cyclegraph->addEdge(curr, act);
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.
376 * @param act is the ModelAction to add.
378 void ModelChecker::add_action_to_lists(ModelAction *act)
380 int tid = id_to_int(act->get_tid());
381 action_trace->push_back(act);
383 (*obj_map)[act->get_location()].push_back(act);
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);
390 if ((int)thrd_last_action->size() <= tid)
391 thrd_last_action->resize(get_num_threads());
392 (*thrd_last_action)[tid] = act;
395 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
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)];
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
409 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
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())
420 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
422 ModelAction *parent = get_last_action(tid);
424 parent = get_thread(tid)->get_creation();
428 ClockVector * ModelChecker::get_cv(thread_id_t tid) {
429 return get_parent_action(tid)->get_cv();
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"
436 * @param curr is the current ModelAction that we are exploring; it must be a
439 void ModelChecker::build_reads_from_past(ModelAction *curr)
441 std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
443 ASSERT(curr->is_read());
445 ModelAction *last_seq_cst = NULL;
447 /* Track whether this object has been initialized */
448 bool initialized = false;
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)
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;
466 /* Only consider 'write' actions */
467 if (!act->is_write())
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");
477 curr->get_node()->add_read_from(act);
480 /* Include at most one act per-thread that "happens before" curr */
481 if (act->happens_before(curr)) {
489 /* TODO: need a more informative way of reporting errors */
490 printf("ERROR: may read from uninitialized atomic\n");
493 if (DBG_ENABLED() || !initialized) {
494 printf("Reached read action:\n");
496 printf("Printing may_read_from\n");
497 curr->get_node()->print_may_read_from();
498 printf("End printing may_read_from\n");
504 static void print_list(action_list_t *list)
506 action_list_t::iterator it;
508 printf("---------------------------------------------------------------------\n");
511 for (it = list->begin(); it != list->end(); it++) {
514 printf("---------------------------------------------------------------------\n");
517 void ModelChecker::print_summary(void)
521 printf("INFEASIBLE EXECUTION!\n");
527 printf("Number of executions: %d\n", num_executions);
528 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
533 print_list(action_trace);
537 int ModelChecker::add_thread(Thread *t)
539 (*thread_map)[id_to_int(t->get_id())] = t;
540 scheduler->add_thread(t);
544 void ModelChecker::remove_thread(Thread *t)
546 scheduler->remove_thread(t);
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)
560 int ModelChecker::switch_to_master(ModelAction *act)
565 old = thread_current();
566 set_current_action(act);
567 old->set_state(THREAD_READY);
568 return Thread::swap(old, get_system_context());