9 #define INITIAL_THREAD_ID 0
13 ModelChecker::ModelChecker()
15 /* First thread created will have id INITIAL_THREAD_ID */
16 this->next_thread_id = INITIAL_THREAD_ID;
17 used_sequence_numbers = 0;
18 /* Initialize default scheduler */
19 this->scheduler = new Scheduler();
22 this->current_action = NULL;
23 this->exploring = NULL;
24 this->nextThread = THREAD_ID_T_NONE;
26 rootNode = new TreeNode(NULL);
27 currentNode = rootNode;
28 action_trace = new action_list_t();
31 ModelChecker::~ModelChecker()
34 delete this->scheduler;
38 void ModelChecker::reset_to_initial_state()
40 DEBUG("+++ Resetting to initial state +++\n");
41 std::map<int, class Thread *>::iterator it;
42 for (it = thread_map.begin(); it != thread_map.end(); it++)
45 action_trace = new action_list_t();
46 currentNode = rootNode;
47 current_action = NULL;
48 next_thread_id = INITIAL_THREAD_ID;
49 used_sequence_numbers = 0;
50 /* scheduler reset ? */
53 thread_id_t ModelChecker::get_next_id()
55 return next_thread_id++;
58 int ModelChecker::get_next_seq_num()
60 return ++used_sequence_numbers;
63 Thread * ModelChecker::schedule_next_thread()
66 if (nextThread == THREAD_ID_T_NONE)
68 t = thread_map[id_to_int(nextThread)];
70 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
75 * get_next_replay_thread() - Choose the next thread in the replay sequence
77 * If we've reached the 'diverge' point, then we pick a thread from the
79 * Otherwise, we simply return the next thread in the sequence.
81 thread_id_t ModelChecker::get_next_replay_thread()
86 next = exploring->get_state();
88 if (next == exploring->get_diverge()) {
89 TreeNode *node = next->get_node();
91 /* Reached divergence point; discard our current 'exploring' */
92 DEBUG("*** Discard 'Backtrack' object ***\n");
93 tid = node->getNextBacktrack();
97 tid = next->get_tid();
99 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
103 thread_id_t ModelChecker::advance_backtracking_state()
105 /* Have we completed exploring the preselected path? */
106 if (exploring == NULL)
107 return THREAD_ID_T_NONE;
109 /* Else, we are trying to replay an execution */
110 exploring->advance_state();
111 if (exploring->get_state() == NULL)
112 DEBUG("*** error: reached end of backtrack trace\n");
114 return get_next_replay_thread();
117 bool ModelChecker::next_execution()
121 if ((exploring = model->get_next_backtrack()) == NULL)
123 model->reset_to_initial_state();
124 nextThread = get_next_replay_thread();
128 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
130 action_type type = act->get_type();
142 /* linear search: from most recent to oldest */
143 action_list_t::reverse_iterator rit;
144 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
145 ModelAction *prev = *rit;
146 if (act->is_dependent(prev))
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 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
171 prev->get_tid(), act->get_tid());
177 Backtrack *back = new Backtrack(prev, action_trace);
178 backtrack_list.push_back(back);
181 Backtrack * ModelChecker::get_next_backtrack()
184 if (backtrack_list.empty())
186 next = backtrack_list.back();
187 backtrack_list.pop_back();
191 void ModelChecker::check_current_action(void)
193 ModelAction *next = this->current_action;
196 DEBUG("trying to push NULL action...\n");
199 current_action = NULL;
200 nextThread = advance_backtracking_state();
201 next->set_node(currentNode);
202 set_backtracking(next);
203 currentNode = currentNode->exploreChild(next->get_tid());
204 this->action_trace->push_back(next);
207 void ModelChecker::print_summary(void)
209 action_list_t::iterator it;
212 printf("---------------------------------------------------------------------\n");
213 printf("Number of executions: %d\n", num_executions);
214 printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
218 printf("Trace:\n\n");
220 for (it = action_trace->begin(); it != action_trace->end(); it++) {
224 printf("---------------------------------------------------------------------\n");
227 int ModelChecker::add_thread(Thread *t)
229 thread_map[id_to_int(t->get_id())] = t;
230 scheduler->add_thread(t);
234 void ModelChecker::remove_thread(Thread *t)
236 scheduler->remove_thread(t);
239 int ModelChecker::switch_to_master(ModelAction *act)
244 old = thread_current();
245 set_current_action(act);
246 old->set_state(THREAD_READY);
247 return Thread::swap(old, get_system_context());
250 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
252 Thread *t = thread_current();
253 ModelAction *act = this;
258 act->tid = t->get_id();
260 act->seq_number = model->get_next_seq_num();
263 bool ModelAction::is_read()
265 return type == ATOMIC_READ;
268 bool ModelAction::is_write()
270 return type == ATOMIC_WRITE;
273 bool ModelAction::is_acquire()
276 case memory_order_acquire:
277 case memory_order_acq_rel:
278 case memory_order_seq_cst:
285 bool ModelAction::is_release()
288 case memory_order_release:
289 case memory_order_acq_rel:
290 case memory_order_seq_cst:
297 bool ModelAction::same_var(ModelAction *act)
299 return location == act->location;
302 bool ModelAction::same_thread(ModelAction *act)
304 return tid == act->tid;
307 bool ModelAction::is_dependent(ModelAction *act)
309 if (!is_read() && !is_write())
311 if (!act->is_read() && !act->is_write())
313 if (same_var(act) && !same_thread(act) &&
314 (is_write() || act->is_write()))
319 void ModelAction::print(void)
321 const char *type_str;
322 switch (this->type) {
324 type_str = "thread create";
327 type_str = "thread yield";
330 type_str = "thread join";
333 type_str = "atomic read";
336 type_str = "atomic write";
339 type_str = "unknown type";
342 printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
343 seq_number, id_to_int(tid), type_str, order, location, value);