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();
61 ModelChecker::~ModelChecker()
63 std::map<int, class Thread *, std::less< int >, MyAlloc< std::pair< int, class Thread * > > >::iterator it;
64 for (it = thread_map.begin(); it != thread_map.end(); it++)
68 free_action_list(action_trace);
70 delete this->scheduler;
74 void ModelChecker::reset_to_initial_state()
76 DEBUG("+++ Resetting to initial state +++\n");
77 std::map<int, class Thread *, std::less< int >, MyAlloc< std::pair< int, class Thread * > > >::iterator it;
78 for (it = thread_map.begin(); it != thread_map.end(); it++)
81 action_trace = new action_list_t();
82 currentNode = rootNode;
83 current_action = NULL;
84 next_thread_id = INITIAL_THREAD_ID;
85 used_sequence_numbers = 0;
86 /* scheduler reset ? */
89 thread_id_t ModelChecker::get_next_id()
91 return next_thread_id++;
94 int ModelChecker::get_next_seq_num()
96 return ++used_sequence_numbers;
99 Thread * ModelChecker::schedule_next_thread()
102 if (nextThread == THREAD_ID_T_NONE)
104 t = thread_map[id_to_int(nextThread)];
112 * get_next_replay_thread() - Choose the next thread in the replay sequence
114 * If we've reached the 'diverge' point, then we pick a thread from the
116 * Otherwise, we simply return the next thread in the sequence.
118 thread_id_t ModelChecker::get_next_replay_thread()
123 next = exploring->get_state();
125 if (next == exploring->get_diverge()) {
126 TreeNode *node = next->get_node();
128 /* Reached divergence point; discard our current 'exploring' */
129 DEBUG("*** Discard 'Backtrack' object ***\n");
130 tid = node->getNextBacktrack();
134 tid = next->get_tid();
136 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
140 thread_id_t ModelChecker::advance_backtracking_state()
142 /* Have we completed exploring the preselected path? */
143 if (exploring == NULL)
144 return THREAD_ID_T_NONE;
146 /* Else, we are trying to replay an execution */
147 exploring->advance_state();
149 ASSERT(exploring->get_state() != NULL);
151 return get_next_replay_thread();
154 bool ModelChecker::next_execution()
160 if ((exploring = model->get_next_backtrack()) == NULL)
164 printf("Next execution will diverge at:\n");
165 exploring->get_diverge()->print();
166 print_list(exploring->get_trace());
169 model->reset_to_initial_state();
170 nextThread = get_next_replay_thread();
174 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
176 action_type type = act->get_type();
188 /* linear search: from most recent to oldest */
189 action_list_t::reverse_iterator rit;
190 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
191 ModelAction *prev = *rit;
192 if (act->is_dependent(prev))
198 void ModelChecker::set_backtracking(ModelAction *act)
202 Thread *t = get_thread(act->get_tid());
204 prev = get_last_conflict(act);
208 node = prev->get_node();
210 while (t && !node->is_enabled(t))
213 /* Check if this has been explored already */
214 if (node->hasBeenExplored(t->get_id()))
216 /* If this is a new backtracking point, mark the tree */
217 if (node->setBacktrack(t->get_id()) != 0)
220 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
221 prev->get_tid(), t->get_id());
227 Backtrack *back = new Backtrack(prev, action_trace);
228 backtrack_list.push_back(back);
231 Backtrack * ModelChecker::get_next_backtrack()
234 if (backtrack_list.empty())
236 next = backtrack_list.back();
237 backtrack_list.pop_back();
241 void ModelChecker::check_current_action(void)
243 ModelAction *next = this->current_action;
246 DEBUG("trying to push NULL action...\n");
249 current_action = NULL;
250 nextThread = advance_backtracking_state();
251 next->set_node(currentNode);
252 set_backtracking(next);
253 currentNode = currentNode->explore_child(next);
254 this->action_trace->push_back(next);
257 void ModelChecker::print_summary(void)
260 printf("Number of executions: %d\n", num_executions);
261 printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
265 print_list(action_trace);
270 void ModelChecker::print_list(action_list_t *list)
272 action_list_t::iterator it;
274 printf("---------------------------------------------------------------------\n");
277 for (it = list->begin(); it != list->end(); it++) {
280 printf("---------------------------------------------------------------------\n");
283 int ModelChecker::add_thread(Thread *t)
285 thread_map[id_to_int(t->get_id())] = t;
286 scheduler->add_thread(t);
290 void ModelChecker::remove_thread(Thread *t)
292 scheduler->remove_thread(t);
295 int ModelChecker::switch_to_master(ModelAction *act)
300 old = thread_current();
301 set_current_action(act);
302 old->set_state(THREAD_READY);
303 return Thread::swap(old, get_system_context());