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()
53 std::map<int, class Thread *>::iterator it;
54 for (it = thread_map.begin(); it != thread_map.end(); it++)
59 delete this->scheduler;
63 void ModelChecker::reset_to_initial_state()
65 DEBUG("+++ Resetting to initial state +++\n");
66 std::map<int, class Thread *>::iterator it;
67 for (it = thread_map.begin(); it != thread_map.end(); it++)
70 action_trace = new action_list_t();
71 currentNode = rootNode;
72 current_action = NULL;
73 next_thread_id = INITIAL_THREAD_ID;
74 used_sequence_numbers = 0;
75 /* scheduler reset ? */
78 thread_id_t ModelChecker::get_next_id()
80 return next_thread_id++;
83 int ModelChecker::get_next_seq_num()
85 return ++used_sequence_numbers;
88 Thread * ModelChecker::schedule_next_thread()
91 if (nextThread == THREAD_ID_T_NONE)
93 t = thread_map[id_to_int(nextThread)];
101 * get_next_replay_thread() - Choose the next thread in the replay sequence
103 * If we've reached the 'diverge' point, then we pick a thread from the
105 * Otherwise, we simply return the next thread in the sequence.
107 thread_id_t ModelChecker::get_next_replay_thread()
112 next = exploring->get_state();
114 if (next == exploring->get_diverge()) {
115 TreeNode *node = next->get_node();
117 /* Reached divergence point; discard our current 'exploring' */
118 DEBUG("*** Discard 'Backtrack' object ***\n");
119 tid = node->getNextBacktrack();
123 tid = next->get_tid();
125 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
129 thread_id_t ModelChecker::advance_backtracking_state()
131 /* Have we completed exploring the preselected path? */
132 if (exploring == NULL)
133 return THREAD_ID_T_NONE;
135 /* Else, we are trying to replay an execution */
136 exploring->advance_state();
138 ASSERT(exploring->get_state() != NULL);
140 return get_next_replay_thread();
143 bool ModelChecker::next_execution()
149 if ((exploring = model->get_next_backtrack()) == NULL)
153 printf("Next execution will diverge at:\n");
154 exploring->get_diverge()->print();
155 print_list(exploring->get_trace());
158 model->reset_to_initial_state();
159 nextThread = get_next_replay_thread();
163 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
165 action_type type = act->get_type();
177 /* linear search: from most recent to oldest */
178 action_list_t::reverse_iterator rit;
179 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
180 ModelAction *prev = *rit;
181 if (act->is_dependent(prev))
187 void ModelChecker::set_backtracking(ModelAction *act)
191 Thread *t = get_thread(act->get_tid());
193 prev = get_last_conflict(act);
197 node = prev->get_node();
199 while (t && !node->is_enabled(t))
202 /* Check if this has been explored already */
203 if (node->hasBeenExplored(t->get_id()))
205 /* If this is a new backtracking point, mark the tree */
206 if (node->setBacktrack(t->get_id()) != 0)
209 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
210 prev->get_tid(), t->get_id());
216 Backtrack *back = new Backtrack(prev, action_trace);
217 backtrack_list.push_back(back);
220 Backtrack * ModelChecker::get_next_backtrack()
223 if (backtrack_list.empty())
225 next = backtrack_list.back();
226 backtrack_list.pop_back();
230 void ModelChecker::check_current_action(void)
232 ModelAction *next = this->current_action;
235 DEBUG("trying to push NULL action...\n");
238 current_action = NULL;
239 nextThread = advance_backtracking_state();
240 next->set_node(currentNode);
241 set_backtracking(next);
242 currentNode = currentNode->explore_child(next);
243 this->action_trace->push_back(next);
246 void ModelChecker::print_summary(void)
249 printf("Number of executions: %d\n", num_executions);
250 printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
254 print_list(action_trace);
259 void ModelChecker::print_list(action_list_t *list)
261 action_list_t::iterator it;
263 printf("---------------------------------------------------------------------\n");
266 for (it = list->begin(); it != list->end(); it++) {
269 printf("---------------------------------------------------------------------\n");
272 int ModelChecker::add_thread(Thread *t)
274 thread_map[id_to_int(t->get_id())] = t;
275 scheduler->add_thread(t);
279 void ModelChecker::remove_thread(Thread *t)
281 scheduler->remove_thread(t);
284 int ModelChecker::switch_to_master(ModelAction *act)
289 old = thread_current();
290 set_current_action(act);
291 old->set_state(THREAD_READY);
292 return Thread::swap(old, get_system_context());
295 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
297 Thread *t = thread_current();
298 ModelAction *act = this;
303 act->tid = t->get_id();
305 act->seq_number = model->get_next_seq_num();
308 bool ModelAction::is_read()
310 return type == ATOMIC_READ;
313 bool ModelAction::is_write()
315 return type == ATOMIC_WRITE;
318 bool ModelAction::is_acquire()
321 case memory_order_acquire:
322 case memory_order_acq_rel:
323 case memory_order_seq_cst:
330 bool ModelAction::is_release()
333 case memory_order_release:
334 case memory_order_acq_rel:
335 case memory_order_seq_cst:
342 bool ModelAction::same_var(ModelAction *act)
344 return location == act->location;
347 bool ModelAction::same_thread(ModelAction *act)
349 return tid == act->tid;
352 bool ModelAction::is_dependent(ModelAction *act)
354 if (!is_read() && !is_write())
356 if (!act->is_read() && !act->is_write())
358 if (same_var(act) && !same_thread(act) &&
359 (is_write() || act->is_write()))
364 void ModelAction::print(void)
366 const char *type_str;
367 switch (this->type) {
369 type_str = "thread create";
372 type_str = "thread yield";
375 type_str = "thread join";
378 type_str = "atomic read";
381 type_str = "atomic write";
384 type_str = "unknown type";
387 printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
388 seq_number, id_to_int(tid), type_str, order, location, value);