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 *list = &(*obj_map)[act->get_location()];
181 action_list_t::reverse_iterator rit;
182 for (rit = list->rbegin(); rit != list->rend(); rit++) {
183 ModelAction *prev = *rit;
184 if (act->is_synchronizing(prev))
190 void ModelChecker::set_backtracking(ModelAction *act)
194 Thread *t = get_thread(act->get_tid());
196 prev = get_last_conflict(act);
200 node = prev->get_node()->get_parent();
202 while (!node->is_enabled(t))
205 /* Check if this has been explored already */
206 if (node->has_been_explored(t->get_id()))
209 /* Cache the latest backtracking point */
210 if (!next_backtrack || *prev > *next_backtrack)
211 next_backtrack = prev;
213 /* If this is a new backtracking point, mark the tree */
214 if (!node->set_backtrack(t->get_id()))
216 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
217 prev->get_tid(), t->get_id());
224 ModelAction * ModelChecker::get_next_backtrack()
226 ModelAction *next = next_backtrack;
227 next_backtrack = NULL;
231 void ModelChecker::check_current_action(void)
235 ModelAction *curr = this->current_action;
237 current_action = NULL;
239 DEBUG("trying to push NULL action...\n");
243 tmp = node_stack->explore_action(curr);
245 /* Discard duplicate ModelAction; use action from NodeStack */
250 * Perform one-time actions when pushing new ModelAction onto
253 curr->create_cv(get_parent_action(curr->get_tid()));
254 /* Build may_read_from set */
256 build_reads_from_past(curr);
259 /* Assign 'creation' parent */
260 if (curr->get_type() == THREAD_CREATE) {
261 Thread *th = (Thread *)curr->get_location();
262 th->set_creation(curr);
265 nextThread = get_next_replay_thread();
267 currnode = curr->get_node()->get_parent();
269 if (!currnode->backtrack_empty())
270 if (!next_backtrack || *curr > *next_backtrack)
271 next_backtrack = curr;
273 set_backtracking(curr);
275 add_action_to_lists(curr);
277 /* Assign reads_from values */
278 /* TODO: perform release/acquire synchronization here; include
279 * reads_from as ModelAction member? */
280 Thread *th = get_thread(curr->get_tid());
281 int value = VALUE_NONE;
282 if (curr->is_read()) {
283 const ModelAction *reads_from = curr->get_node()->get_next_read_from();
284 value = reads_from->get_value();
285 /* Assign reads_from, perform release/acquire synchronization */
286 curr->read_from(reads_from);
288 th->set_return_value(value);
292 * Performs various bookkeeping operations for the current ModelAction. For
293 * instance, adds action to the per-object, per-thread action vector and to the
294 * action trace list of all thread actions.
296 * @param act is the ModelAction to add.
298 void ModelChecker::add_action_to_lists(ModelAction *act)
300 int tid = id_to_int(act->get_tid());
301 action_trace->push_back(act);
303 (*obj_map)[act->get_location()].push_back(act);
305 std::vector<action_list_t> *vec = &(*obj_thrd_map)[act->get_location()];
306 if (tid >= (int)vec->size())
307 vec->resize(next_thread_id);
308 (*vec)[tid].push_back(act);
310 if ((int)thrd_last_action->size() <= tid)
311 thrd_last_action->resize(get_num_threads());
312 (*thrd_last_action)[tid] = act;
315 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
317 int nthreads = get_num_threads();
318 if ((int)thrd_last_action->size() < nthreads)
319 thrd_last_action->resize(nthreads);
320 return (*thrd_last_action)[id_to_int(tid)];
324 * Gets the last memory_order_seq_cst action (in the total global sequence)
325 * performed on a particular object (i.e., memory location).
326 * @param location The object location to check
327 * @return The last seq_cst action performed
329 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
331 action_list_t *list = &(*obj_map)[location];
332 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
333 action_list_t::reverse_iterator rit;
334 for (rit = list->rbegin(); rit != list->rend(); rit++)
335 if ((*rit)->is_write() && (*rit)->is_seqcst())
340 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
342 ModelAction *parent = get_last_action(tid);
344 parent = get_thread(tid)->get_creation();
348 ClockVector * ModelChecker::get_cv(thread_id_t tid) {
349 return get_parent_action(tid)->get_cv();
353 * Build up an initial set of all past writes that this 'read' action may read
354 * from. This set is determined by the clock vector's "happens before"
356 * @param curr is the current ModelAction that we are exploring; it must be a
359 void ModelChecker::build_reads_from_past(ModelAction *curr)
361 std::vector<action_list_t> *thrd_lists = &(*obj_thrd_map)[curr->get_location()];
364 ASSERT(curr->is_read());
366 ModelAction *last_seq_cst = NULL;
368 if (curr->is_seqcst())
369 last_seq_cst = get_last_seq_cst(curr->get_location());
371 /* Track whether this object has been initialized */
372 bool initialized = false;
373 /* Would each action synchronize if we read from it? */
374 bool all_synch = true;
375 /* Is the may_read_from set empty? (tracked locally) */
378 for (i = 0; i < thrd_lists->size(); i++) {
379 action_list_t *list = &(*thrd_lists)[i];
380 action_list_t::reverse_iterator rit;
381 for (rit = list->rbegin(); rit != list->rend(); rit++) {
382 ModelAction *act = *rit;
384 /* Only consider 'write' actions */
385 if (!act->is_write())
388 /* Don't consider more than one seq_cst write */
389 if (!act->is_seqcst() || act == last_seq_cst) {
390 DEBUG("Adding action to may_read_from:\n");
395 curr->get_node()->add_read_from(act);
398 if (!(act->is_release() && curr->is_acquire())
399 && !act->same_thread(curr))
403 /* Include at most one act per-thread that "happens before" curr */
404 if (act->happens_before(curr)) {
411 if (!empty && all_synch)
415 /* TODO: need a more informative way of reporting errors */
416 printf("ERROR: may read from uninitialized atomic\n");
419 if (DBG_ENABLED() || !initialized) {
420 printf("Reached read action:\n");
422 printf("Printing may_read_from\n");
423 curr->get_node()->print_may_read_from();
424 printf("End printing may_read_from\n");
430 static void print_list(action_list_t *list)
432 action_list_t::iterator it;
434 printf("---------------------------------------------------------------------\n");
437 for (it = list->begin(); it != list->end(); it++) {
440 printf("---------------------------------------------------------------------\n");
443 void ModelChecker::print_summary(void)
446 printf("Number of executions: %d\n", num_executions);
447 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
451 print_list(action_trace);
455 int ModelChecker::add_thread(Thread *t)
457 (*thread_map)[id_to_int(t->get_id())] = t;
458 scheduler->add_thread(t);
462 void ModelChecker::remove_thread(Thread *t)
464 scheduler->remove_thread(t);
467 int ModelChecker::switch_to_master(ModelAction *act)
472 old = thread_current();
473 set_current_action(act);
474 old->set_state(THREAD_READY);
475 return Thread::swap(old, get_system_context());