7 #include "snapshot-interface.h"
10 #define INITIAL_THREAD_ID 0
14 ModelChecker::ModelChecker()
16 /* Initialize default scheduler */
17 scheduler(new Scheduler()),
18 /* First thread created will have id INITIAL_THREAD_ID */
19 next_thread_id(INITIAL_THREAD_ID),
20 used_sequence_numbers(0),
25 nextThread(THREAD_ID_T_NONE),
26 action_trace(new action_list_t()),
27 thread_map(new std::map<int, class Thread *>),
28 node_stack(new NodeStack()),
33 ModelChecker::~ModelChecker()
35 std::map<int, class Thread *>::iterator it;
36 for (it = thread_map->begin(); it != thread_map->end(); it++)
46 void ModelChecker::reset_to_initial_state()
48 DEBUG("+++ Resetting to initial state +++\n");
49 std::map<int, class Thread *>::iterator it;
50 for (it = thread_map->begin(); it != thread_map->end(); it++)
54 action_trace = new action_list_t();
55 node_stack->reset_execution();
56 current_action = NULL;
57 next_thread_id = INITIAL_THREAD_ID;
58 used_sequence_numbers = 0;
60 next_backtrack = NULL;
61 /* scheduler reset ? */
62 snapshotObject->backTrackBeforeStep(0);
65 thread_id_t ModelChecker::get_next_id()
67 return next_thread_id++;
70 int ModelChecker::get_next_seq_num()
72 return ++used_sequence_numbers;
75 Thread * ModelChecker::schedule_next_thread()
78 if (nextThread == THREAD_ID_T_NONE)
80 t = (*thread_map)[id_to_int(nextThread)];
88 * get_next_replay_thread() - Choose the next thread in the replay sequence
90 * If we've reached the 'diverge' point, then we pick a thread from the
92 * Otherwise, we simply return the next thread in the sequence.
94 thread_id_t ModelChecker::get_next_replay_thread()
99 /* Have we completed exploring the preselected path? */
101 return THREAD_ID_T_NONE;
103 /* Else, we are trying to replay an execution */
104 next = node_stack->get_next()->get_action();
106 if (next == diverge) {
107 Node *node = next->get_node();
109 /* Reached divergence point */
110 DEBUG("*** Divergence point ***\n");
111 tid = node->get_next_backtrack();
114 tid = next->get_tid();
116 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
120 bool ModelChecker::next_execution()
126 if ((diverge = model->get_next_backtrack()) == NULL)
130 printf("Next execution will diverge at:\n");
134 model->reset_to_initial_state();
138 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
140 action_type type = act->get_type();
152 /* linear search: from most recent to oldest */
153 action_list_t::reverse_iterator rit;
154 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
155 ModelAction *prev = *rit;
156 if (act->is_dependent(prev))
162 void ModelChecker::set_backtracking(ModelAction *act)
166 Thread *t = get_thread(act->get_tid());
168 prev = get_last_conflict(act);
172 node = prev->get_node();
174 while (!node->is_enabled(t))
177 /* Check if this has been explored already */
178 if (node->has_been_explored(t->get_id()))
181 if (!next_backtrack || *prev > *next_backtrack)
182 next_backtrack = prev;
184 /* If this is a new backtracking point, mark the tree */
185 if (!node->set_backtrack(t->get_id()))
187 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
188 prev->get_tid(), t->get_id());
195 ModelAction * ModelChecker::get_next_backtrack()
197 ModelAction *next = next_backtrack;
198 next_backtrack = NULL;
202 void ModelChecker::check_current_action(void)
206 ModelAction *curr = this->current_action;
207 current_action = NULL;
209 DEBUG("trying to push NULL action...\n");
213 curr = node_stack->explore_action(curr);
214 nextThread = get_next_replay_thread();
216 currnode = curr->get_node();
218 if (!currnode->backtrack_empty())
219 if (!next_backtrack || *curr > *next_backtrack)
220 next_backtrack = curr;
222 set_backtracking(curr);
223 this->action_trace->push_back(curr);
226 void ModelChecker::print_summary(void)
229 printf("Number of executions: %d\n", num_executions);
230 printf("Total nodes created: %d\n", Node::get_total_nodes());
234 print_list(action_trace);
238 void ModelChecker::print_list(action_list_t *list)
240 action_list_t::iterator it;
242 printf("---------------------------------------------------------------------\n");
245 for (it = list->begin(); it != list->end(); it++) {
248 printf("---------------------------------------------------------------------\n");
251 int ModelChecker::add_thread(Thread *t)
253 (*thread_map)[id_to_int(t->get_id())] = t;
254 scheduler->add_thread(t);
258 void ModelChecker::remove_thread(Thread *t)
260 scheduler->remove_thread(t);
263 int ModelChecker::switch_to_master(ModelAction *act)
268 old = thread_current();
269 set_current_action(act);
270 old->set_state(THREAD_READY);
271 return Thread::swap(old, get_system_context());