9 #define INITIAL_THREAD_ID 0
13 Backtrack(ModelAction *d, action_list_t *t) {
16 iter = actionTrace->begin();
18 ModelAction * get_diverge() { return diverge; }
19 action_list_t * get_trace() { return actionTrace; }
20 void advance_state() { iter++; }
21 ModelAction * get_state() {
22 return iter == actionTrace->end() ? NULL : *iter;
26 action_list_t *actionTrace;
27 /* points to position in actionTrace as we replay */
28 action_list_t::iterator iter;
33 ModelChecker::ModelChecker()
35 /* First thread created will have id INITIAL_THREAD_ID */
36 this->next_thread_id = INITIAL_THREAD_ID;
37 used_sequence_numbers = 0;
38 /* Initialize default scheduler */
39 this->scheduler = new Scheduler();
42 this->current_action = NULL;
43 this->exploring = NULL;
44 this->nextThread = THREAD_ID_T_NONE;
46 rootNode = new TreeNode();
47 currentNode = rootNode;
48 action_trace = new action_list_t();
51 ModelChecker::~ModelChecker()
54 delete this->scheduler;
58 void ModelChecker::reset_to_initial_state()
60 DEBUG("+++ Resetting to initial state +++\n");
61 std::map<int, class Thread *>::iterator it;
62 for (it = thread_map.begin(); it != thread_map.end(); it++)
65 action_trace = new action_list_t();
66 currentNode = rootNode;
67 current_action = NULL;
68 next_thread_id = INITIAL_THREAD_ID;
69 used_sequence_numbers = 0;
70 /* scheduler reset ? */
73 thread_id_t ModelChecker::get_next_id()
75 return next_thread_id++;
78 int ModelChecker::get_next_seq_num()
80 return ++used_sequence_numbers;
83 Thread * ModelChecker::schedule_next_thread()
86 if (nextThread == THREAD_ID_T_NONE)
88 t = thread_map[id_to_int(nextThread)];
90 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
95 * get_next_replay_thread() - Choose the next thread in the replay sequence
97 * If we've reached the 'diverge' point, then we pick a thread from the
99 * Otherwise, we simply return the next thread in the sequence.
101 thread_id_t ModelChecker::get_next_replay_thread()
106 next = exploring->get_state();
108 if (next == exploring->get_diverge()) {
109 TreeNode *node = next->get_node();
111 /* Reached divergence point; discard our current 'exploring' */
112 DEBUG("*** Discard 'Backtrack' object ***\n");
113 tid = node->getNextBacktrack();
117 tid = next->get_tid();
119 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
123 thread_id_t ModelChecker::advance_backtracking_state()
125 /* Have we completed exploring the preselected path? */
126 if (exploring == NULL)
127 return THREAD_ID_T_NONE;
129 /* Else, we are trying to replay an execution */
130 exploring->advance_state();
132 ASSERT(exploring->get_state() != NULL);
134 return get_next_replay_thread();
137 bool ModelChecker::next_execution()
143 if ((exploring = model->get_next_backtrack()) == NULL)
147 printf("Next execution will diverge at:\n");
148 exploring->get_diverge()->print();
149 print_list(exploring->get_trace());
152 model->reset_to_initial_state();
153 nextThread = get_next_replay_thread();
157 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
159 action_type type = act->get_type();
171 /* linear search: from most recent to oldest */
172 action_list_t::reverse_iterator rit;
173 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
174 ModelAction *prev = *rit;
175 if (act->is_dependent(prev))
181 void ModelChecker::set_backtracking(ModelAction *act)
185 Thread *t = get_thread(act->get_tid());
187 prev = get_last_conflict(act);
191 node = prev->get_node();
193 /* Check if this has been explored already */
194 if (node->hasBeenExplored(t->get_id()))
196 /* If this is a new backtracking point, mark the tree */
197 if (node->setBacktrack(t->get_id()) != 0)
200 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
201 prev->get_tid(), t->get_id());
207 Backtrack *back = new Backtrack(prev, action_trace);
208 backtrack_list.push_back(back);
211 Backtrack * ModelChecker::get_next_backtrack()
214 if (backtrack_list.empty())
216 next = backtrack_list.back();
217 backtrack_list.pop_back();
221 void ModelChecker::check_current_action(void)
223 ModelAction *next = this->current_action;
226 DEBUG("trying to push NULL action...\n");
229 current_action = NULL;
230 nextThread = advance_backtracking_state();
231 next->set_node(currentNode);
232 set_backtracking(next);
233 currentNode = currentNode->explore_child(next);
234 this->action_trace->push_back(next);
237 void ModelChecker::print_summary(void)
240 printf("Number of executions: %d\n", num_executions);
241 printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
245 print_list(action_trace);
250 void ModelChecker::print_list(action_list_t *list)
252 action_list_t::iterator it;
254 printf("---------------------------------------------------------------------\n");
257 for (it = list->begin(); it != list->end(); it++) {
260 printf("---------------------------------------------------------------------\n");
263 int ModelChecker::add_thread(Thread *t)
265 thread_map[id_to_int(t->get_id())] = t;
266 scheduler->add_thread(t);
270 void ModelChecker::remove_thread(Thread *t)
272 scheduler->remove_thread(t);
275 int ModelChecker::switch_to_master(ModelAction *act)
280 old = thread_current();
281 set_current_action(act);
282 old->set_state(THREAD_READY);
283 return Thread::swap(old, get_system_context());
286 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
288 Thread *t = thread_current();
289 ModelAction *act = this;
294 act->tid = t->get_id();
296 act->seq_number = model->get_next_seq_num();
299 bool ModelAction::is_read()
301 return type == ATOMIC_READ;
304 bool ModelAction::is_write()
306 return type == ATOMIC_WRITE;
309 bool ModelAction::is_acquire()
312 case memory_order_acquire:
313 case memory_order_acq_rel:
314 case memory_order_seq_cst:
321 bool ModelAction::is_release()
324 case memory_order_release:
325 case memory_order_acq_rel:
326 case memory_order_seq_cst:
333 bool ModelAction::same_var(ModelAction *act)
335 return location == act->location;
338 bool ModelAction::same_thread(ModelAction *act)
340 return tid == act->tid;
343 bool ModelAction::is_dependent(ModelAction *act)
345 if (!is_read() && !is_write())
347 if (!act->is_read() && !act->is_write())
349 if (same_var(act) && !same_thread(act) &&
350 (is_write() || act->is_write()))
355 void ModelAction::print(void)
357 const char *type_str;
358 switch (this->type) {
360 type_str = "thread create";
363 type_str = "thread yield";
366 type_str = "thread join";
369 type_str = "atomic read";
372 type_str = "atomic write";
375 type_str = "unknown type";
378 printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
379 seq_number, id_to_int(tid), type_str, order, location, value);