7 #define INITIAL_THREAD_ID 0
11 ModelChecker::ModelChecker()
13 /* First thread created will have id (INITIAL_THREAD_ID + 1) */
14 this->used_thread_id = INITIAL_THREAD_ID;
15 /* Initialize default scheduler */
16 this->scheduler = new Scheduler();
19 this->current_action = NULL;
20 this->exploring = NULL;
21 this->nextThread = THREAD_ID_T_NONE;
23 rootNode = new TreeNode(NULL);
24 currentNode = rootNode;
25 action_trace = new action_list_t();
28 ModelChecker::~ModelChecker()
31 delete this->scheduler;
35 void ModelChecker::reset_to_initial_state()
37 DEBUG("+++ Resetting to initial state +++\n");
38 std::map<thread_id_t, class Thread *>::iterator it;
39 for (it = thread_map.begin(); it != thread_map.end(); it++) {
43 action_trace = new action_list_t();
44 currentNode = rootNode;
45 current_action = NULL;
46 used_thread_id = INITIAL_THREAD_ID;
47 /* scheduler reset ? */
50 int ModelChecker::get_next_id()
52 return ++used_thread_id;
55 Thread * ModelChecker::schedule_next_thread()
58 if (nextThread == THREAD_ID_T_NONE)
60 t = thread_map[nextThread];
62 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
67 * get_next_replay_thread() - Choose the next thread in the replay sequence
69 * If we've reached the 'diverge' point, then we pick a thread from the
71 * Otherwise, we simply return the next thread in the sequence.
73 thread_id_t ModelChecker::get_next_replay_thread()
78 next = exploring->get_state();
80 if (next == exploring->get_diverge()) {
81 TreeNode *node = next->get_node();
83 /* Reached divergence point; discard our current 'exploring' */
84 DEBUG("*** Discard 'Backtrack' object ***\n");
85 tid = node->getNextBacktrack();
89 tid = next->get_tid();
91 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
95 thread_id_t ModelChecker::advance_backtracking_state()
97 /* Have we completed exploring the preselected path? */
98 if (exploring == NULL)
99 return THREAD_ID_T_NONE;
101 /* Else, we are trying to replay an execution */
102 exploring->advance_state();
103 if (exploring->get_state() == NULL)
104 DEBUG("*** error: reached end of backtrack trace\n");
106 return get_next_replay_thread();
109 bool ModelChecker::next_execution()
113 if ((exploring = model->get_next_backtrack()) == NULL)
115 model->reset_to_initial_state();
116 nextThread = get_next_replay_thread();
120 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
122 void *loc = act->get_location();
123 action_type type = act->get_type();
124 thread_id_t id = act->get_tid();
136 /* linear search: from most recent to oldest */
137 action_list_t::reverse_iterator rit;
138 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
139 ModelAction *prev = *rit;
140 if (prev->get_location() != loc)
142 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
144 /* Conflict from the same thread is not really a conflict */
145 if (id == prev->get_tid())
152 void ModelChecker::set_backtracking(ModelAction *act)
157 prev = get_last_conflict(act);
161 node = prev->get_node();
163 /* Check if this has been explored already */
164 if (node->hasBeenExplored(act->get_tid()))
166 /* If this is a new backtracking point, mark the tree */
167 if (node->setBacktrack(act->get_tid()) != 0)
170 printf("Setting backtrack: conflict = %d, instead tid = %d\n",
171 prev->get_tid(), act->get_tid());
175 Backtrack *back = new Backtrack(prev, action_trace);
176 backtrack_list.push_back(back);
179 Backtrack * ModelChecker::get_next_backtrack()
182 if (backtrack_list.empty())
184 next = backtrack_list.back();
185 backtrack_list.pop_back();
189 void ModelChecker::check_current_action(void)
191 ModelAction *next = this->current_action;
194 DEBUG("trying to push NULL action...\n");
197 current_action = NULL;
198 nextThread = advance_backtracking_state();
199 next->set_node(currentNode);
200 set_backtracking(next);
201 currentNode = currentNode->exploreChild(next->get_tid());
202 this->action_trace->push_back(next);
205 void ModelChecker::print_summary(void)
207 action_list_t::iterator it;
210 printf("---------------------------------------------------------------------\n");
211 printf("Number of executions: %d\n", num_executions);
212 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
216 printf("Trace:\n\n");
218 for (it = action_trace->begin(); it != action_trace->end(); it++) {
222 printf("---------------------------------------------------------------------\n");
225 int ModelChecker::add_thread(Thread *t)
227 thread_map[t->get_id()] = t;
228 scheduler->add_thread(t);
232 void ModelChecker::remove_thread(Thread *t)
234 scheduler->remove_thread(t);
237 int ModelChecker::switch_to_master(ModelAction *act)
242 old = thread_current();
243 set_current_action(act);
244 old->set_state(THREAD_READY);
245 return Thread::swap(old, get_system_context());
248 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
250 Thread *t = thread_current();
251 ModelAction *act = this;
256 act->tid = t->get_id();
260 void ModelAction::print(void)
262 const char *type_str;
263 switch (this->type) {
265 type_str = "thread create";
268 type_str = "thread yield";
271 type_str = "thread join";
274 type_str = "atomic read";
277 type_str = "atomic write";
280 type_str = "unknown type";
283 printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);