7 #include "snapshot-interface.h"
11 #define INITIAL_THREAD_ID 0
15 Backtrack(ModelAction *d, action_list_t *t) {
18 iter = actionTrace->begin();
20 ModelAction * get_diverge() { return diverge; }
21 action_list_t * get_trace() { return actionTrace; }
22 void advance_state() { iter++; }
23 ModelAction * get_state() {
24 return iter == actionTrace->end() ? NULL : *iter;
28 action_list_t *actionTrace;
29 /* points to position in actionTrace as we replay */
30 action_list_t::iterator iter;
35 void free_action_list(action_list_t *list)
37 action_list_t::iterator it;
38 for (it = list->begin(); it != list->end(); it++)
43 ModelChecker::ModelChecker()
45 /* First thread created will have id INITIAL_THREAD_ID */
46 this->next_thread_id = INITIAL_THREAD_ID;
47 used_sequence_numbers = 0;
48 /* Initialize default scheduler */
49 this->scheduler = new Scheduler();
52 this->current_action = NULL;
53 this->exploring = NULL;
54 this->nextThread = THREAD_ID_T_NONE;
56 rootNode = new TreeNode();
57 currentNode = rootNode;
58 action_trace = new action_list_t();
59 global_vec = snapshot_utils::ReturnGlobalSegmentsToSnapshot();
62 ModelChecker::~ModelChecker()
64 std::map<int, class Thread *, std::less< int >, MyAlloc< std::pair< int, class Thread * > > >::iterator it;
65 for (it = thread_map.begin(); it != thread_map.end(); it++)
69 free_action_list(action_trace);
71 delete this->scheduler;
75 void ModelChecker::reset_to_initial_state()
77 DEBUG("+++ Resetting to initial state +++\n");
78 std::map<int, class Thread *, std::less< int >, MyAlloc< std::pair< int, class Thread * > > >::iterator it;
79 for (it = thread_map.begin(); it != thread_map.end(); it++)
82 action_trace = new action_list_t();
83 currentNode = rootNode;
84 current_action = NULL;
85 next_thread_id = INITIAL_THREAD_ID;
86 used_sequence_numbers = 0;
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 next = exploring->get_state();
126 if (next == exploring->get_diverge()) {
127 TreeNode *node = next->get_node();
129 /* Reached divergence point; discard our current 'exploring' */
130 DEBUG("*** Discard 'Backtrack' object ***\n");
131 tid = node->getNextBacktrack();
135 tid = next->get_tid();
137 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
141 thread_id_t ModelChecker::advance_backtracking_state()
143 /* Have we completed exploring the preselected path? */
144 if (exploring == NULL)
145 return THREAD_ID_T_NONE;
147 /* Else, we are trying to replay an execution */
148 exploring->advance_state();
150 ASSERT(exploring->get_state() != NULL);
152 return get_next_replay_thread();
155 bool ModelChecker::next_execution()
161 if ((exploring = model->get_next_backtrack()) == NULL)
165 printf("Next execution will diverge at:\n");
166 exploring->get_diverge()->print();
167 print_list(exploring->get_trace());
170 model->reset_to_initial_state();
171 nextThread = get_next_replay_thread();
175 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
177 action_type type = act->get_type();
189 /* linear search: from most recent to oldest */
190 action_list_t::reverse_iterator rit;
191 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
192 ModelAction *prev = *rit;
193 if (act->is_dependent(prev))
199 void ModelChecker::set_backtracking(ModelAction *act)
203 Thread *t = get_thread(act->get_tid());
205 prev = get_last_conflict(act);
209 node = prev->get_node();
211 while (t && !node->is_enabled(t))
214 /* Check if this has been explored already */
215 if (node->hasBeenExplored(t->get_id()))
217 /* If this is a new backtracking point, mark the tree */
218 if (node->setBacktrack(t->get_id()) != 0)
221 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
222 prev->get_tid(), t->get_id());
228 Backtrack *back = new Backtrack(prev, action_trace);
229 backtrack_list.push_back(back);
232 Backtrack * ModelChecker::get_next_backtrack()
235 if (backtrack_list.empty())
237 next = backtrack_list.back();
238 backtrack_list.pop_back();
242 void ModelChecker::check_current_action(void)
244 ModelAction *next = this->current_action;
247 DEBUG("trying to push NULL action...\n");
250 current_action = NULL;
251 nextThread = advance_backtracking_state();
252 next->set_node(currentNode);
253 set_backtracking(next);
254 currentNode = currentNode->explore_child(next);
255 this->action_trace->push_back(next);
258 void ModelChecker::print_summary(void)
261 printf("Number of executions: %d\n", num_executions);
262 printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
266 print_list(action_trace);
271 void ModelChecker::print_list(action_list_t *list)
273 action_list_t::iterator it;
275 printf("---------------------------------------------------------------------\n");
278 for (it = list->begin(); it != list->end(); it++) {
281 printf("---------------------------------------------------------------------\n");
284 int ModelChecker::add_thread(Thread *t)
286 thread_map[id_to_int(t->get_id())] = t;
287 scheduler->add_thread(t);
291 void ModelChecker::remove_thread(Thread *t)
293 scheduler->remove_thread(t);
296 int ModelChecker::switch_to_master(ModelAction *act)
301 old = thread_current();
302 set_current_action(act);
303 old->set_state(THREAD_READY);
304 return Thread::swap(old, get_system_context());