7 #include "snapshot-interface.h"
9 #include "clockvector.h"
11 #define INITIAL_THREAD_ID 0
15 /** @brief Constructor */
16 ModelChecker::ModelChecker()
18 /* Initialize default scheduler */
19 scheduler(new Scheduler()),
20 /* First thread created will have id INITIAL_THREAD_ID */
21 next_thread_id(INITIAL_THREAD_ID),
22 used_sequence_numbers(0),
27 nextThread(THREAD_ID_T_NONE),
28 action_trace(new action_list_t()),
29 thread_map(new std::map<int, Thread *>),
30 obj_map(new std::map<const void *, action_list_t>()),
31 obj_thrd_map(new std::map<void *, std::vector<action_list_t> >()),
32 thrd_last_action(new std::vector<ModelAction *>(1)),
33 node_stack(new NodeStack()),
38 /** @brief Destructor */
39 ModelChecker::~ModelChecker()
41 std::map<int, Thread *>::iterator it;
42 for (it = thread_map->begin(); it != thread_map->end(); it++)
49 delete thrd_last_action;
55 * Restores user program to initial state and resets all model-checker data
58 void ModelChecker::reset_to_initial_state()
60 DEBUG("+++ Resetting to initial state +++\n");
61 node_stack->reset_execution();
62 current_action = NULL;
63 next_thread_id = INITIAL_THREAD_ID;
64 used_sequence_numbers = 0;
66 next_backtrack = NULL;
67 snapshotObject->backTrackBeforeStep(0);
70 /** @returns a thread ID for a new Thread */
71 thread_id_t ModelChecker::get_next_id()
73 return next_thread_id++;
76 /** @returns the number of user threads created during this execution */
77 int ModelChecker::get_num_threads()
79 return next_thread_id;
82 /** @returns a sequence number for a new ModelAction */
83 modelclock_t ModelChecker::get_next_seq_num()
85 return ++used_sequence_numbers;
89 * Performs the "scheduling" for the model-checker. That is, it checks if the
90 * model-checker has selected a "next thread to run" and returns it, if
91 * available. This function should be called from the Scheduler routine, where
92 * the Scheduler falls back to a default scheduling routine if needed.
94 * @return The next thread chosen by the model-checker. If the model-checker
95 * makes no selection, retuns NULL.
97 Thread * ModelChecker::schedule_next_thread()
100 if (nextThread == THREAD_ID_T_NONE)
102 t = (*thread_map)[id_to_int(nextThread)];
110 * Choose the next thread in the replay sequence.
112 * If the replay sequence has reached the 'diverge' point, returns a thread
113 * from the backtracking set. Otherwise, simply returns the next thread in the
114 * sequence that is being replayed.
116 thread_id_t ModelChecker::get_next_replay_thread()
121 /* Have we completed exploring the preselected path? */
123 return THREAD_ID_T_NONE;
125 /* Else, we are trying to replay an execution */
126 next = node_stack->get_next()->get_action();
128 if (next == diverge) {
129 Node *node = next->get_node()->get_parent();
131 /* Reached divergence point */
132 DEBUG("*** Divergence point ***\n");
133 tid = node->get_next_backtrack();
136 tid = next->get_tid();
138 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
143 * Queries the model-checker for more executions to explore and, if one
144 * exists, resets the model-checker state to execute a new execution.
146 * @return If there are more executions to explore, return true. Otherwise,
149 bool ModelChecker::next_execution()
155 if ((diverge = model->get_next_backtrack()) == NULL)
159 printf("Next execution will diverge at:\n");
163 model->reset_to_initial_state();
167 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
169 action_type type = act->get_type();
179 /* linear search: from most recent to oldest */
180 action_list_t::reverse_iterator rit;
181 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
182 ModelAction *prev = *rit;
183 if (act->is_synchronizing(prev))
189 void ModelChecker::set_backtracking(ModelAction *act)
193 Thread *t = get_thread(act->get_tid());
195 prev = get_last_conflict(act);
199 node = prev->get_node()->get_parent();
201 while (!node->is_enabled(t))
204 /* Check if this has been explored already */
205 if (node->has_been_explored(t->get_id()))
208 /* Cache the latest backtracking point */
209 if (!next_backtrack || *prev > *next_backtrack)
210 next_backtrack = prev;
212 /* If this is a new backtracking point, mark the tree */
213 if (!node->set_backtrack(t->get_id()))
215 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
216 prev->get_tid(), t->get_id());
223 ModelAction * ModelChecker::get_next_backtrack()
225 ModelAction *next = next_backtrack;
226 next_backtrack = NULL;
230 void ModelChecker::check_current_action(void)
234 ModelAction *curr = this->current_action;
236 current_action = NULL;
238 DEBUG("trying to push NULL action...\n");
242 tmp = node_stack->explore_action(curr);
244 /* Discard duplicate ModelAction; use action from NodeStack */
249 * Perform one-time actions when pushing new ModelAction onto
252 curr->create_cv(get_parent_action(curr->get_tid()));
253 /* Build may_read_from set */
255 build_reads_from_past(curr);
258 /* Assign 'creation' parent */
259 if (curr->get_type() == THREAD_CREATE) {
260 Thread *th = (Thread *)curr->get_location();
261 th->set_creation(curr);
264 nextThread = get_next_replay_thread();
266 currnode = curr->get_node()->get_parent();
268 if (!currnode->backtrack_empty())
269 if (!next_backtrack || *curr > *next_backtrack)
270 next_backtrack = curr;
272 set_backtracking(curr);
274 add_action_to_lists(curr);
276 /* Assign reads_from values */
277 /* TODO: perform release/acquire synchronization here; include
278 * reads_from as ModelAction member? */
279 Thread *th = get_thread(curr->get_tid());
280 int value = VALUE_NONE;
281 if (curr->is_read()) {
282 const ModelAction *reads_from = curr->get_node()->get_next_read_from();
283 value = reads_from->get_value();
284 /* Assign reads_from, perform release/acquire synchronization */
285 curr->read_from(reads_from);
287 th->set_return_value(value);
291 * Performs various bookkeeping operations for the current ModelAction. For
292 * instance, adds action to the per-object, per-thread action vector and to the
293 * action trace list of all thread actions.
295 * @param act is the ModelAction to add.
297 void ModelChecker::add_action_to_lists(ModelAction *act)
299 int tid = id_to_int(act->get_tid());
300 action_trace->push_back(act);
302 (*obj_map)[act->get_location()].push_back(act);
304 std::vector<action_list_t> *vec = &(*obj_thrd_map)[act->get_location()];
305 if (tid >= (int)vec->size())
306 vec->resize(next_thread_id);
307 (*vec)[tid].push_back(act);
309 if ((int)thrd_last_action->size() <= tid)
310 thrd_last_action->resize(get_num_threads());
311 (*thrd_last_action)[tid] = act;
314 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
316 int nthreads = get_num_threads();
317 if ((int)thrd_last_action->size() < nthreads)
318 thrd_last_action->resize(nthreads);
319 return (*thrd_last_action)[id_to_int(tid)];
322 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
324 ModelAction *parent = get_last_action(tid);
326 parent = get_thread(tid)->get_creation();
330 ClockVector * ModelChecker::get_cv(thread_id_t tid) {
331 return get_parent_action(tid)->get_cv();
335 * Build up an initial set of all past writes that this 'read' action may read
336 * from. This set is determined by the clock vector's "happens before"
338 * @param curr is the current ModelAction that we are exploring; it must be a
341 void ModelChecker::build_reads_from_past(ModelAction *curr)
343 std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
346 ASSERT(curr->is_read());
348 /* Track whether this object has been initialized */
349 bool initialized = false;
351 for (i = 0; i < thrd_lists->size(); i++) {
352 action_list_t *list = &(*thrd_lists)[i];
353 action_list_t::reverse_iterator rit;
354 for (rit = list->rbegin(); rit != list->rend(); rit++) {
355 ModelAction *act = *rit;
357 /* Only consider 'write' actions */
358 if (!act->is_write())
361 DEBUG("Adding action to may_read_from:\n");
366 curr->get_node()->add_read_from(act);
368 /* Include at most one act per-thread that "happens before" curr */
369 if (act->happens_before(curr)) {
377 /* TODO: need a more informative way of reporting errors */
378 printf("ERROR: may read from uninitialized atomic\n");
381 if (DBG_ENABLED() || !initialized) {
382 printf("Reached read action:\n");
384 printf("Printing may_read_from\n");
385 curr->get_node()->print_may_read_from();
386 printf("End printing may_read_from\n");
392 static void print_list(action_list_t *list)
394 action_list_t::iterator it;
396 printf("---------------------------------------------------------------------\n");
399 for (it = list->begin(); it != list->end(); it++) {
402 printf("---------------------------------------------------------------------\n");
405 void ModelChecker::print_summary(void)
408 printf("Number of executions: %d\n", num_executions);
409 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
413 print_list(action_trace);
417 int ModelChecker::add_thread(Thread *t)
419 (*thread_map)[id_to_int(t->get_id())] = t;
420 scheduler->add_thread(t);
424 void ModelChecker::remove_thread(Thread *t)
426 scheduler->remove_thread(t);
429 int ModelChecker::switch_to_master(ModelAction *act)
434 old = thread_current();
435 set_current_action(act);
436 old->set_state(THREAD_READY);
437 return Thread::swap(old, get_system_context());