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()
123 if ((exploring = model->get_next_backtrack()) == NULL)
125 model->reset_to_initial_state();
126 nextThread = get_next_replay_thread();
129 printf("Next execution will diverge at:\n");
130 exploring->get_diverge()->print();
131 print_list(exploring->get_trace());
136 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
138 action_type type = act->get_type();
150 /* linear search: from most recent to oldest */
151 action_list_t::reverse_iterator rit;
152 for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
153 ModelAction *prev = *rit;
154 if (act->is_dependent(prev))
160 void ModelChecker::set_backtracking(ModelAction *act)
165 prev = get_last_conflict(act);
169 node = prev->get_node();
171 /* Check if this has been explored already */
172 if (node->hasBeenExplored(act->get_tid()))
174 /* If this is a new backtracking point, mark the tree */
175 if (node->setBacktrack(act->get_tid()) != 0)
178 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
179 prev->get_tid(), act->get_tid());
185 Backtrack *back = new Backtrack(prev, action_trace);
186 backtrack_list.push_back(back);
189 Backtrack * ModelChecker::get_next_backtrack()
192 if (backtrack_list.empty())
194 next = backtrack_list.back();
195 backtrack_list.pop_back();
199 void ModelChecker::check_current_action(void)
201 ModelAction *next = this->current_action;
204 DEBUG("trying to push NULL action...\n");
207 current_action = NULL;
208 nextThread = advance_backtracking_state();
209 next->set_node(currentNode);
210 set_backtracking(next);
211 currentNode = currentNode->exploreChild(next->get_tid());
212 this->action_trace->push_back(next);
215 void ModelChecker::print_summary(void)
218 printf("Number of executions: %d\n", num_executions);
219 printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
223 print_list(action_trace);
228 void ModelChecker::print_list(action_list_t *list)
230 action_list_t::iterator it;
232 printf("---------------------------------------------------------------------\n");
235 for (it = list->begin(); it != list->end(); it++) {
238 printf("---------------------------------------------------------------------\n");
241 int ModelChecker::add_thread(Thread *t)
243 thread_map[id_to_int(t->get_id())] = t;
244 scheduler->add_thread(t);
248 void ModelChecker::remove_thread(Thread *t)
250 scheduler->remove_thread(t);
253 int ModelChecker::switch_to_master(ModelAction *act)
258 old = thread_current();
259 set_current_action(act);
260 old->set_state(THREAD_READY);
261 return Thread::swap(old, get_system_context());
264 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
266 Thread *t = thread_current();
267 ModelAction *act = this;
272 act->tid = t->get_id();
274 act->seq_number = model->get_next_seq_num();
277 bool ModelAction::is_read()
279 return type == ATOMIC_READ;
282 bool ModelAction::is_write()
284 return type == ATOMIC_WRITE;
287 bool ModelAction::is_acquire()
290 case memory_order_acquire:
291 case memory_order_acq_rel:
292 case memory_order_seq_cst:
299 bool ModelAction::is_release()
302 case memory_order_release:
303 case memory_order_acq_rel:
304 case memory_order_seq_cst:
311 bool ModelAction::same_var(ModelAction *act)
313 return location == act->location;
316 bool ModelAction::same_thread(ModelAction *act)
318 return tid == act->tid;
321 bool ModelAction::is_dependent(ModelAction *act)
323 if (!is_read() && !is_write())
325 if (!act->is_read() && !act->is_write())
327 if (same_var(act) && !same_thread(act) &&
328 (is_write() || act->is_write()))
333 void ModelAction::print(void)
335 const char *type_str;
336 switch (this->type) {
338 type_str = "thread create";
341 type_str = "thread yield";
344 type_str = "thread join";
347 type_str = "atomic read";
350 type_str = "atomic write";
353 type_str = "unknown type";
356 printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
357 seq_number, id_to_int(tid), type_str, order, location, value);