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_thrd_map(new std::map<void *, std::vector<action_list_t> >()),
31 thrd_last_action(new std::vector<ModelAction *>(1)),
32 node_stack(new NodeStack()),
37 /** @brief Destructor */
38 ModelChecker::~ModelChecker()
40 std::map<int, Thread *>::iterator it;
41 for (it = thread_map->begin(); it != thread_map->end(); it++)
47 delete thrd_last_action;
53 * Restores user program to initial state and resets all model-checker data
56 void ModelChecker::reset_to_initial_state()
58 DEBUG("+++ Resetting to initial state +++\n");
59 node_stack->reset_execution();
60 current_action = NULL;
61 next_thread_id = INITIAL_THREAD_ID;
62 used_sequence_numbers = 0;
64 next_backtrack = NULL;
65 snapshotObject->backTrackBeforeStep(0);
68 /** @returns a thread ID for a new Thread */
69 thread_id_t ModelChecker::get_next_id()
71 return next_thread_id++;
74 /** @returns the number of user threads created during this execution */
75 int ModelChecker::get_num_threads()
77 return next_thread_id;
80 /** @returns a sequence number for a new ModelAction */
81 modelclock_t ModelChecker::get_next_seq_num()
83 return ++used_sequence_numbers;
87 * Performs the "scheduling" for the model-checker. That is, it checks if the
88 * model-checker has selected a "next thread to run" and returns it, if
89 * available. This function should be called from the Scheduler routine, where
90 * the Scheduler falls back to a default scheduling routine if needed.
92 * @return The next thread chosen by the model-checker. If the model-checker
93 * makes no selection, retuns NULL.
95 Thread * ModelChecker::schedule_next_thread()
98 if (nextThread == THREAD_ID_T_NONE)
100 t = (*thread_map)[id_to_int(nextThread)];
108 * Choose the next thread in the replay sequence.
110 * If the replay sequence has reached the 'diverge' point, returns a thread
111 * from the backtracking set. Otherwise, simply returns the next thread in the
112 * sequence that is being replayed.
114 thread_id_t ModelChecker::get_next_replay_thread()
119 /* Have we completed exploring the preselected path? */
121 return THREAD_ID_T_NONE;
123 /* Else, we are trying to replay an execution */
124 next = node_stack->get_next()->get_action();
126 if (next == diverge) {
127 Node *node = next->get_node()->get_parent();
129 /* Reached divergence point */
130 DEBUG("*** Divergence point ***\n");
131 tid = node->get_next_backtrack();
134 tid = next->get_tid();
136 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
141 * Queries the model-checker for more executions to explore and, if one
142 * exists, resets the model-checker state to execute a new execution.
144 * @return If there are more executions to explore, return true. Otherwise,
147 bool ModelChecker::next_execution()
153 if ((diverge = model->get_next_backtrack()) == NULL)
157 printf("Next execution will diverge at:\n");
161 model->reset_to_initial_state();
165 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
167 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);
278 * Performs various bookkeeping operations for the current ModelAction. For
279 * instance, adds action to the per-object, per-thread action vector and to the
280 * action trace list of all thread actions.
282 * @param act is the ModelAction to add.
284 void ModelChecker::add_action_to_lists(ModelAction *act)
286 action_trace->push_back(act);
288 std::vector<action_list_t> *vec = &(*obj_thrd_map)[act->get_location()];
289 if (id_to_int(act->get_tid()) >= (int)vec->size())
290 vec->resize(next_thread_id);
291 (*vec)[id_to_int(act->get_tid())].push_back(act);
293 (*thrd_last_action)[id_to_int(act->get_tid())] = act;
296 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
298 int nthreads = get_num_threads();
299 if ((int)thrd_last_action->size() < nthreads)
300 thrd_last_action->resize(nthreads);
301 return (*thrd_last_action)[id_to_int(tid)];
304 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
306 ModelAction *parent = get_last_action(tid);
308 parent = get_thread(tid)->get_creation();
312 ClockVector * ModelChecker::get_cv(thread_id_t tid) {
313 return get_parent_action(tid)->get_cv();
317 * Build up an initial set of all past writes that this 'read' action may read
318 * from. This set is determined by the clock vector's "happens before"
320 * @param curr is the current ModelAction that we are exploring; it must be a
323 void ModelChecker::build_reads_from_past(ModelAction *curr)
325 std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
328 ASSERT(curr->is_read());
330 /* Track whether this object has been initialized */
331 bool initialized = false;
333 for (i = 0; i < thrd_lists->size(); i++) {
334 action_list_t *list = &(*thrd_lists)[i];
335 action_list_t::reverse_iterator rit;
336 for (rit = list->rbegin(); rit != list->rend(); rit++) {
337 ModelAction *act = *rit;
339 /* Only consider 'write' actions */
340 if (!act->is_write())
343 DEBUG("Adding action to may_read_from:\n");
348 curr->get_node()->add_read_from(act);
350 /* Include at most one act per-thread that "happens before" curr */
351 if (act->happens_before(curr)) {
359 /* TODO: need a more informative way of reporting errors */
360 printf("ERROR: may read from uninitialized atomic\n");
363 if (DBG_ENABLED() || !initialized) {
364 printf("Reached read action:\n");
366 printf("Printing may_read_from\n");
367 curr->get_node()->print_may_read_from();
368 printf("End printing may_read_from\n");
374 static void print_list(action_list_t *list)
376 action_list_t::iterator it;
378 printf("---------------------------------------------------------------------\n");
381 for (it = list->begin(); it != list->end(); it++) {
384 printf("---------------------------------------------------------------------\n");
387 void ModelChecker::print_summary(void)
390 printf("Number of executions: %d\n", num_executions);
391 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
395 print_list(action_trace);
399 int ModelChecker::add_thread(Thread *t)
401 (*thread_map)[id_to_int(t->get_id())] = t;
402 scheduler->add_thread(t);
406 void ModelChecker::remove_thread(Thread *t)
408 scheduler->remove_thread(t);
411 int ModelChecker::switch_to_master(ModelAction *act)
416 old = thread_current();
417 set_current_action(act);
418 old->set_state(THREAD_READY);
419 return Thread::swap(old, get_system_context());