7 #include "snapshot-interface.h"
10 #define INITIAL_THREAD_ID 0
14 ModelChecker::ModelChecker()
16 /* Initialize default scheduler */
17 scheduler(new Scheduler()),
18 /* First thread created will have id INITIAL_THREAD_ID */
19 next_thread_id(INITIAL_THREAD_ID),
20 used_sequence_numbers(0),
25 nextThread(THREAD_ID_T_NONE),
26 action_trace(new action_list_t()),
27 thread_map(new std::map<int, class Thread *>),
28 node_stack(new NodeStack()),
33 ModelChecker::~ModelChecker()
35 std::map<int, class Thread *>::iterator it;
36 for (it = thread_map->begin(); it != thread_map->end(); it++)
46 void ModelChecker::reset_to_initial_state()
48 DEBUG("+++ Resetting to initial state +++\n");
49 std::map<int, class Thread *>::iterator it;
50 for (it = thread_map->begin(); it != thread_map->end(); it++)
54 action_trace = new action_list_t();
55 node_stack->reset_execution();
56 current_action = NULL;
57 next_thread_id = INITIAL_THREAD_ID;
58 used_sequence_numbers = 0;
60 next_backtrack = NULL;
61 /* scheduler reset ? */
64 thread_id_t ModelChecker::get_next_id()
66 return next_thread_id++;
69 int ModelChecker::get_next_seq_num()
71 return ++used_sequence_numbers;
74 Thread * ModelChecker::schedule_next_thread()
77 if (nextThread == THREAD_ID_T_NONE)
79 t = (*thread_map)[id_to_int(nextThread)];
87 * get_next_replay_thread() - Choose the next thread in the replay sequence
89 * If we've reached the 'diverge' point, then we pick a thread from the
91 * Otherwise, we simply return the next thread in the sequence.
93 thread_id_t ModelChecker::get_next_replay_thread()
98 /* Have we completed exploring the preselected path? */
100 return THREAD_ID_T_NONE;
102 /* Else, we are trying to replay an execution */
103 next = node_stack->get_next()->get_action();
105 if (next == diverge) {
106 Node *node = next->get_node();
108 /* Reached divergence point */
109 DEBUG("*** Divergence point ***\n");
110 tid = node->get_next_backtrack();
113 tid = next->get_tid();
115 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
119 bool ModelChecker::next_execution()
125 if ((diverge = model->get_next_backtrack()) == NULL)
129 printf("Next execution will diverge at:\n");
133 model->reset_to_initial_state();
137 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
139 action_type type = act->get_type();
151 /* linear search: from most recent to oldest */
152 action_list_t::reverse_iterator rit;
153 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
154 ModelAction *prev = *rit;
155 if (act->is_dependent(prev))
161 void ModelChecker::set_backtracking(ModelAction *act)
165 Thread *t = get_thread(act->get_tid());
167 prev = get_last_conflict(act);
171 node = prev->get_node();
173 while (!node->is_enabled(t))
176 /* Check if this has been explored already */
177 if (node->has_been_explored(t->get_id()))
180 if (!next_backtrack || *prev > *next_backtrack)
181 next_backtrack = prev;
183 /* If this is a new backtracking point, mark the tree */
184 if (!node->set_backtrack(t->get_id()))
186 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
187 prev->get_tid(), t->get_id());
194 ModelAction * ModelChecker::get_next_backtrack()
196 ModelAction *next = next_backtrack;
197 next_backtrack = NULL;
201 void ModelChecker::check_current_action(void)
205 ModelAction *curr = this->current_action;
206 current_action = NULL;
208 DEBUG("trying to push NULL action...\n");
212 curr = node_stack->explore_action(curr);
213 nextThread = get_next_replay_thread();
215 currnode = curr->get_node();
217 if (!currnode->backtrack_empty())
218 if (!next_backtrack || *curr > *next_backtrack)
219 next_backtrack = curr;
221 set_backtracking(curr);
222 this->action_trace->push_back(curr);
225 void ModelChecker::print_summary(void)
228 printf("Number of executions: %d\n", num_executions);
229 printf("Total nodes created: %d\n", Node::get_total_nodes());
233 print_list(action_trace);
237 void ModelChecker::print_list(action_list_t *list)
239 action_list_t::iterator it;
241 printf("---------------------------------------------------------------------\n");
244 for (it = list->begin(); it != list->end(); it++) {
247 printf("---------------------------------------------------------------------\n");
250 int ModelChecker::add_thread(Thread *t)
252 (*thread_map)[id_to_int(t->get_id())] = t;
253 scheduler->add_thread(t);
257 void ModelChecker::remove_thread(Thread *t)
259 scheduler->remove_thread(t);
262 int ModelChecker::switch_to_master(ModelAction *act)
267 old = thread_current();
268 set_current_action(act);
269 old->set_state(THREAD_READY);
270 return Thread::swap(old, get_system_context());