malloc: add exception info to function header
[c11tester.git] / model.cc
1 #include <stdio.h>
2
3 #include "model.h"
4 #include "schedule.h"
5 #include "common.h"
6
7 #define INITIAL_THREAD_ID       0
8
9 ModelChecker *model;
10
11 ModelChecker::ModelChecker()
12 {
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();
17
18         num_executions = 0;
19         this->current_action = NULL;
20         this->exploring = NULL;
21         this->nextThread = THREAD_ID_T_NONE;
22
23         rootNode = new TreeNode(NULL);
24         currentNode = rootNode;
25         action_trace = new action_list_t();
26 }
27
28 ModelChecker::~ModelChecker()
29 {
30         delete action_trace;
31         delete this->scheduler;
32         delete rootNode;
33 }
34
35 void ModelChecker::reset_to_initial_state()
36 {
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++) {
40                 delete (*it).second;
41         }
42         thread_map.clear();
43         action_trace = new action_list_t();
44         currentNode = rootNode;
45         current_action = NULL;
46         used_thread_id = INITIAL_THREAD_ID;
47         /* scheduler reset ? */
48 }
49
50 int ModelChecker::get_next_id()
51 {
52         return ++used_thread_id;
53 }
54
55 Thread * ModelChecker::schedule_next_thread()
56 {
57         Thread *t;
58         if (nextThread == THREAD_ID_T_NONE)
59                 return NULL;
60         t = thread_map[nextThread];
61         if (t == NULL)
62                 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
63         return t;
64 }
65
66 /*
67  * get_next_replay_thread() - Choose the next thread in the replay sequence
68  *
69  * If we've reached the 'diverge' point, then we pick a thread from the
70  *   backtracking set.
71  * Otherwise, we simply return the next thread in the sequence.
72  */
73 thread_id_t ModelChecker::get_next_replay_thread()
74 {
75         ModelAction *next;
76         thread_id_t tid;
77
78         next = exploring->get_state();
79
80         if (next == exploring->get_diverge()) {
81                 TreeNode *node = next->get_node();
82
83                 /* Reached divergence point; discard our current 'exploring' */
84                 DEBUG("*** Discard 'Backtrack' object ***\n");
85                 tid = node->getNextBacktrack();
86                 delete exploring;
87                 exploring = NULL;
88         } else {
89                 tid = next->get_tid();
90         }
91         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
92         return tid;
93 }
94
95 thread_id_t ModelChecker::advance_backtracking_state()
96 {
97         /* Have we completed exploring the preselected path? */
98         if (exploring == NULL)
99                 return THREAD_ID_T_NONE;
100
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");
105
106         return get_next_replay_thread();
107 }
108
109 bool ModelChecker::next_execution()
110 {
111         num_executions++;
112         print_summary();
113         if ((exploring = model->get_next_backtrack()) == NULL)
114                 return false;
115         model->reset_to_initial_state();
116         nextThread = get_next_replay_thread();
117         return true;
118 }
119
120 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
121 {
122         void *loc = act->get_location();
123         action_type type = act->get_type();
124         thread_id_t id = act->get_tid();
125
126         switch (type) {
127                 case THREAD_CREATE:
128                 case THREAD_YIELD:
129                 case THREAD_JOIN:
130                         return NULL;
131                 case ATOMIC_READ:
132                 case ATOMIC_WRITE:
133                 default:
134                         break;
135         }
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)
141                         continue;
142                 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
143                         continue;
144                 /* Conflict from the same thread is not really a conflict */
145                 if (id == prev->get_tid())
146                         continue;
147                 return prev;
148         }
149         return NULL;
150 }
151
152 void ModelChecker::set_backtracking(ModelAction *act)
153 {
154         ModelAction *prev;
155         TreeNode *node;
156
157         prev = get_last_conflict(act);
158         if (prev == NULL)
159                 return;
160
161         node = prev->get_node();
162
163         /* Check if this has been explored already */
164         if (node->hasBeenExplored(act->get_tid()))
165                 return;
166         /* If this is a new backtracking point, mark the tree */
167         if (node->setBacktrack(act->get_tid()) != 0)
168                 return;
169
170         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
171                         prev->get_tid(), act->get_tid());
172         if (DBG_ENABLED()) {
173                 prev->print();
174                 act->print();
175         }
176
177         Backtrack *back = new Backtrack(prev, action_trace);
178         backtrack_list.push_back(back);
179 }
180
181 Backtrack * ModelChecker::get_next_backtrack()
182 {
183         Backtrack *next;
184         if (backtrack_list.empty())
185                 return NULL;
186         next = backtrack_list.back();
187         backtrack_list.pop_back();
188         return next;
189 }
190
191 void ModelChecker::check_current_action(void)
192 {
193         ModelAction *next = this->current_action;
194
195         if (!next) {
196                 DEBUG("trying to push NULL action...\n");
197                 return;
198         }
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);
205 }
206
207 void ModelChecker::print_summary(void)
208 {
209         action_list_t::iterator it;
210
211         printf("\n");
212         printf("---------------------------------------------------------------------\n");
213         printf("Number of executions: %d\n", num_executions);
214         printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
215
216         scheduler->print();
217
218         printf("Trace:\n\n");
219
220         for (it = action_trace->begin(); it != action_trace->end(); it++) {
221                 DBG();
222                 (*it)->print();
223         }
224         printf("---------------------------------------------------------------------\n");
225 }
226
227 int ModelChecker::add_thread(Thread *t)
228 {
229         thread_map[t->get_id()] = t;
230         scheduler->add_thread(t);
231         return 0;
232 }
233
234 void ModelChecker::remove_thread(Thread *t)
235 {
236         scheduler->remove_thread(t);
237 }
238
239 int ModelChecker::switch_to_master(ModelAction *act)
240 {
241         Thread *old;
242
243         DBG();
244         old = thread_current();
245         set_current_action(act);
246         old->set_state(THREAD_READY);
247         return Thread::swap(old, get_system_context());
248 }
249
250 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
251 {
252         Thread *t = thread_current();
253         ModelAction *act = this;
254
255         act->type = type;
256         act->order = order;
257         act->location = loc;
258         act->tid = t->get_id();
259         act->value = value;
260 }
261
262 void ModelAction::print(void)
263 {
264         const char *type_str;
265         switch (this->type) {
266         case THREAD_CREATE:
267                 type_str = "thread create";
268                 break;
269         case THREAD_YIELD:
270                 type_str = "thread yield";
271                 break;
272         case THREAD_JOIN:
273                 type_str = "thread join";
274                 break;
275         case ATOMIC_READ:
276                 type_str = "atomic read";
277                 break;
278         case ATOMIC_WRITE:
279                 type_str = "atomic write";
280                 break;
281         default:
282                 type_str = "unknown type";
283         }
284
285         printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);
286 }