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 /* First thread created will have id INITIAL_THREAD_ID */
44 this->next_thread_id = INITIAL_THREAD_ID;
45 used_sequence_numbers = 0;
46 /* Initialize default scheduler */
47 this->scheduler = new Scheduler();
50 this->current_action = NULL;
51 this->exploring = NULL;
52 this->nextThread = THREAD_ID_T_NONE;
54 rootNode = new TreeNode();
55 currentNode = rootNode;
56 action_trace = new action_list_t();
59 ModelChecker::~ModelChecker()
61 std::map<int, class Thread *>::iterator it;
62 for (it = thread_map.begin(); it != thread_map.end(); it++)
66 free_action_list(action_trace);
68 delete this->scheduler;
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++)
79 action_trace = new action_list_t();
80 currentNode = rootNode;
81 current_action = NULL;
82 next_thread_id = INITIAL_THREAD_ID;
83 used_sequence_numbers = 0;
84 /* scheduler reset ? */
87 thread_id_t ModelChecker::get_next_id()
89 return next_thread_id++;
92 int ModelChecker::get_next_seq_num()
94 return ++used_sequence_numbers;
97 Thread * ModelChecker::schedule_next_thread()
100 if (nextThread == THREAD_ID_T_NONE)
102 t = thread_map[id_to_int(nextThread)];
110 * get_next_replay_thread() - Choose the next thread in the replay sequence
112 * If we've reached the 'diverge' point, then we pick a thread from the
114 * Otherwise, we simply return the next thread in the sequence.
116 thread_id_t ModelChecker::get_next_replay_thread()
121 next = exploring->get_state();
123 if (next == exploring->get_diverge()) {
124 TreeNode *node = next->get_node();
126 /* Reached divergence point; discard our current 'exploring' */
127 DEBUG("*** Discard 'Backtrack' object ***\n");
128 tid = node->getNextBacktrack();
132 tid = next->get_tid();
134 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
138 thread_id_t ModelChecker::advance_backtracking_state()
140 /* Have we completed exploring the preselected path? */
141 if (exploring == NULL)
142 return THREAD_ID_T_NONE;
144 /* Else, we are trying to replay an execution */
145 exploring->advance_state();
147 ASSERT(exploring->get_state() != NULL);
149 return get_next_replay_thread();
152 bool ModelChecker::next_execution()
158 if ((exploring = model->get_next_backtrack()) == NULL)
162 printf("Next execution will diverge at:\n");
163 exploring->get_diverge()->print();
164 print_list(exploring->get_trace());
167 model->reset_to_initial_state();
168 nextThread = get_next_replay_thread();
172 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
174 action_type type = act->get_type();
186 /* linear search: from most recent to oldest */
187 action_list_t::reverse_iterator rit;
188 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
189 ModelAction *prev = *rit;
190 if (act->is_dependent(prev))
196 void ModelChecker::set_backtracking(ModelAction *act)
200 Thread *t = get_thread(act->get_tid());
202 prev = get_last_conflict(act);
206 node = prev->get_node();
208 while (t && !node->is_enabled(t))
211 /* Check if this has been explored already */
212 if (node->hasBeenExplored(t->get_id()))
214 /* If this is a new backtracking point, mark the tree */
215 if (node->setBacktrack(t->get_id()) != 0)
218 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
219 prev->get_tid(), t->get_id());
225 Backtrack *back = new Backtrack(prev, action_trace);
226 backtrack_list.push_back(back);
229 Backtrack * ModelChecker::get_next_backtrack()
232 if (backtrack_list.empty())
234 next = backtrack_list.back();
235 backtrack_list.pop_back();
239 void ModelChecker::check_current_action(void)
241 ModelAction *next = this->current_action;
244 DEBUG("trying to push NULL action...\n");
247 current_action = NULL;
248 nextThread = advance_backtracking_state();
249 next->set_node(currentNode);
250 set_backtracking(next);
251 currentNode = currentNode->explore_child(next);
252 this->action_trace->push_back(next);
255 void ModelChecker::print_summary(void)
258 printf("Number of executions: %d\n", num_executions);
259 printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
263 print_list(action_trace);
268 void ModelChecker::print_list(action_list_t *list)
270 action_list_t::iterator it;
272 printf("---------------------------------------------------------------------\n");
275 for (it = list->begin(); it != list->end(); it++) {
278 printf("---------------------------------------------------------------------\n");
281 int ModelChecker::add_thread(Thread *t)
283 thread_map[id_to_int(t->get_id())] = t;
284 scheduler->add_thread(t);
288 void ModelChecker::remove_thread(Thread *t)
290 scheduler->remove_thread(t);
293 int ModelChecker::switch_to_master(ModelAction *act)
298 old = thread_current();
299 set_current_action(act);
300 old->set_state(THREAD_READY);
301 return Thread::swap(old, get_system_context());