9 #define INITIAL_THREAD_ID 0
13 Backtrack(ModelAction *d, action_list_t *t) {
16 iter = actionTrace->begin();
18 ModelAction * get_diverge() { return diverge; }
19 action_list_t * get_trace() { return actionTrace; }
20 void advance_state() { iter++; }
21 ModelAction * get_state() {
22 return iter == actionTrace->end() ? NULL : *iter;
26 action_list_t *actionTrace;
27 /* points to position in actionTrace as we replay */
28 action_list_t::iterator iter;
33 void free_action_list(action_list_t *list)
35 action_list_t::iterator it;
36 for (it = list->begin(); it != list->end(); it++)
41 ModelChecker::ModelChecker()
43 /* Initialize default scheduler */
44 scheduler(new Scheduler()),
45 /* First thread created will have id INITIAL_THREAD_ID */
46 next_thread_id(INITIAL_THREAD_ID),
47 used_sequence_numbers(0),
52 nextThread(THREAD_ID_T_NONE),
53 action_trace(new action_list_t()),
54 node_stack(new NodeStack()),
59 ModelChecker::~ModelChecker()
61 std::map<int, class Thread *>::iterator it;
62 for (it = thread_map.begin(); it != thread_map.end(); it++)
72 void ModelChecker::reset_to_initial_state()
74 DEBUG("+++ Resetting to initial state +++\n");
75 std::map<int, class Thread *>::iterator it;
76 for (it = thread_map.begin(); it != thread_map.end(); it++)
80 action_trace = new action_list_t();
81 node_stack->reset_execution();
82 current_action = NULL;
83 next_thread_id = INITIAL_THREAD_ID;
84 used_sequence_numbers = 0;
86 next_backtrack = NULL;
87 /* scheduler reset ? */
90 thread_id_t ModelChecker::get_next_id()
92 return next_thread_id++;
95 int ModelChecker::get_next_seq_num()
97 return ++used_sequence_numbers;
100 Thread * ModelChecker::schedule_next_thread()
103 if (nextThread == THREAD_ID_T_NONE)
105 t = thread_map[id_to_int(nextThread)];
113 * get_next_replay_thread() - Choose the next thread in the replay sequence
115 * If we've reached the 'diverge' point, then we pick a thread from the
117 * Otherwise, we simply return the next thread in the sequence.
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 *node = next->get_node();
134 /* Reached divergence point */
135 DEBUG("*** Divergence point ***\n");
136 tid = node->get_next_backtrack();
139 tid = next->get_tid();
141 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
145 bool ModelChecker::next_execution()
151 if ((diverge = model->get_next_backtrack()) == NULL)
155 printf("Next execution will diverge at:\n");
159 model->reset_to_initial_state();
163 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
165 action_type type = act->get_type();
177 /* linear search: from most recent to oldest */
178 action_list_t::reverse_iterator rit;
179 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
180 ModelAction *prev = *rit;
181 if (act->is_dependent(prev))
187 void ModelChecker::set_backtracking(ModelAction *act)
191 Thread *t = get_thread(act->get_tid());
193 prev = get_last_conflict(act);
197 node = prev->get_node();
199 while (!node->is_enabled(t))
202 /* Check if this has been explored already */
203 if (node->has_been_explored(t->get_id()))
206 if (!next_backtrack || *prev > *next_backtrack)
207 next_backtrack = prev;
209 /* If this is a new backtracking point, mark the tree */
210 if (!node->set_backtrack(t->get_id()))
212 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
213 prev->get_tid(), t->get_id());
220 ModelAction * ModelChecker::get_next_backtrack()
222 ModelAction *next = next_backtrack;
223 next_backtrack = NULL;
227 void ModelChecker::check_current_action(void)
231 ModelAction *curr = this->current_action;
232 current_action = NULL;
234 DEBUG("trying to push NULL action...\n");
238 curr = node_stack->explore_action(curr);
239 nextThread = get_next_replay_thread();
241 currnode = curr->get_node();
243 if (!currnode->backtrack_empty())
244 if (!next_backtrack || *curr > *next_backtrack)
245 next_backtrack = curr;
247 set_backtracking(curr);
248 this->action_trace->push_back(curr);
251 void ModelChecker::print_summary(void)
254 printf("Number of executions: %d\n", num_executions);
255 printf("Total nodes created: %d\n", Node::get_total_nodes());
259 print_list(action_trace);
263 void ModelChecker::print_list(action_list_t *list)
265 action_list_t::iterator it;
267 printf("---------------------------------------------------------------------\n");
270 for (it = list->begin(); it != list->end(); it++) {
273 printf("---------------------------------------------------------------------\n");
276 int ModelChecker::add_thread(Thread *t)
278 thread_map[id_to_int(t->get_id())] = t;
279 scheduler->add_thread(t);
283 void ModelChecker::remove_thread(Thread *t)
285 scheduler->remove_thread(t);
288 int ModelChecker::switch_to_master(ModelAction *act)
293 old = thread_current();
294 set_current_action(act);
295 old->set_state(THREAD_READY);
296 return Thread::swap(old, get_system_context());