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 rootNode(new TreeNode()),
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);
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;
85 /* scheduler reset ? */
88 thread_id_t ModelChecker::get_next_id()
90 return next_thread_id++;
93 int ModelChecker::get_next_seq_num()
95 return ++used_sequence_numbers;
98 Thread * ModelChecker::schedule_next_thread()
101 if (nextThread == THREAD_ID_T_NONE)
103 t = thread_map[id_to_int(nextThread)];
111 * get_next_replay_thread() - Choose the next thread in the replay sequence
113 * If we've reached the 'diverge' point, then we pick a thread from the
115 * Otherwise, we simply return the next thread in the sequence.
117 thread_id_t ModelChecker::get_next_replay_thread()
122 /* Have we completed exploring the preselected path? */
123 if (exploring == NULL)
124 return THREAD_ID_T_NONE;
126 /* Else, we are trying to replay an execution */
127 exploring->advance_state();
129 ASSERT(exploring->get_state() != NULL);
131 next = exploring->get_state();
133 if (next == exploring->get_diverge()) {
134 TreeNode *node = next->get_treenode();
136 /* Reached divergence point; discard our current 'exploring' */
137 DEBUG("*** Discard 'Backtrack' object ***\n");
138 tid = node->getNextBacktrack();
142 tid = next->get_tid();
144 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
148 bool ModelChecker::next_execution()
154 if ((exploring = model->get_next_backtrack()) == NULL)
158 printf("Next execution will diverge at:\n");
159 exploring->get_diverge()->print();
160 print_list(exploring->get_trace());
163 model->reset_to_initial_state();
167 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
169 action_type type = act->get_type();
181 /* linear search: from most recent to oldest */
182 action_list_t::reverse_iterator rit;
183 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
184 ModelAction *prev = *rit;
185 if (act->is_dependent(prev))
191 void ModelChecker::set_backtracking(ModelAction *act)
195 Thread *t = get_thread(act->get_tid());
197 prev = get_last_conflict(act);
201 node = prev->get_treenode();
203 while (t && !node->is_enabled(t))
206 /* Check if this has been explored already */
207 if (node->hasBeenExplored(t->get_id()))
209 /* If this is a new backtracking point, mark the tree */
210 if (node->setBacktrack(t->get_id()) != 0)
213 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
214 prev->get_tid(), t->get_id());
220 Backtrack *back = new Backtrack(prev, action_trace);
221 backtrack_list.push_back(back);
224 Backtrack * ModelChecker::get_next_backtrack()
227 if (backtrack_list.empty())
229 next = backtrack_list.back();
230 backtrack_list.pop_back();
234 void ModelChecker::check_current_action(void)
236 ModelAction *curr = this->current_action;
237 current_action = NULL;
239 DEBUG("trying to push NULL action...\n");
243 nextThread = get_next_replay_thread();
244 curr->set_node(currentNode);
245 set_backtracking(curr);
246 currentNode = currentNode->explore_child(curr);
247 this->action_trace->push_back(curr);
250 void ModelChecker::print_summary(void)
253 printf("Number of executions: %d\n", num_executions);
254 printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
258 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());