7a29e00b6e2be472dd519e7674fa43769f998499
[model-checker.git] / model.cc
1 #include <stdio.h>
2
3 #include "model.h"
4 #include "action.h"
5 #include "tree.h"
6 #include "schedule.h"
7 #include "common.h"
8
9 #define INITIAL_THREAD_ID       0
10
11 class Backtrack {
12 public:
13         Backtrack(ModelAction *d, action_list_t *t) {
14                 diverge = d;
15                 actionTrace = t;
16                 iter = actionTrace->begin();
17         }
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;
23         }
24 private:
25         ModelAction *diverge;
26         action_list_t *actionTrace;
27         /* points to position in actionTrace as we replay */
28         action_list_t::iterator iter;
29 };
30
31 ModelChecker *model;
32
33 ModelChecker::ModelChecker()
34 {
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();
40
41         num_executions = 0;
42         this->current_action = NULL;
43         this->exploring = NULL;
44         this->nextThread = THREAD_ID_T_NONE;
45
46         rootNode = new TreeNode(NULL);
47         currentNode = rootNode;
48         action_trace = new action_list_t();
49 }
50
51 ModelChecker::~ModelChecker()
52 {
53         delete action_trace;
54         delete this->scheduler;
55         delete rootNode;
56 }
57
58 void ModelChecker::reset_to_initial_state()
59 {
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++)
63                 delete (*it).second;
64         thread_map.clear();
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 ? */
71 }
72
73 thread_id_t ModelChecker::get_next_id()
74 {
75         return next_thread_id++;
76 }
77
78 int ModelChecker::get_next_seq_num()
79 {
80         return ++used_sequence_numbers;
81 }
82
83 Thread * ModelChecker::schedule_next_thread()
84 {
85         Thread *t;
86         if (nextThread == THREAD_ID_T_NONE)
87                 return NULL;
88         t = thread_map[id_to_int(nextThread)];
89         if (t == NULL)
90                 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
91         return t;
92 }
93
94 /*
95  * get_next_replay_thread() - Choose the next thread in the replay sequence
96  *
97  * If we've reached the 'diverge' point, then we pick a thread from the
98  *   backtracking set.
99  * Otherwise, we simply return the next thread in the sequence.
100  */
101 thread_id_t ModelChecker::get_next_replay_thread()
102 {
103         ModelAction *next;
104         thread_id_t tid;
105
106         next = exploring->get_state();
107
108         if (next == exploring->get_diverge()) {
109                 TreeNode *node = next->get_node();
110
111                 /* Reached divergence point; discard our current 'exploring' */
112                 DEBUG("*** Discard 'Backtrack' object ***\n");
113                 tid = node->getNextBacktrack();
114                 delete exploring;
115                 exploring = NULL;
116         } else {
117                 tid = next->get_tid();
118         }
119         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
120         return tid;
121 }
122
123 thread_id_t ModelChecker::advance_backtracking_state()
124 {
125         /* Have we completed exploring the preselected path? */
126         if (exploring == NULL)
127                 return THREAD_ID_T_NONE;
128
129         /* Else, we are trying to replay an execution */
130         exploring->advance_state();
131
132         ASSERT(exploring->get_state() != NULL);
133
134         return get_next_replay_thread();
135 }
136
137 bool ModelChecker::next_execution()
138 {
139         DBG();
140
141         num_executions++;
142         print_summary();
143         if ((exploring = model->get_next_backtrack()) == NULL)
144                 return false;
145         model->reset_to_initial_state();
146         nextThread = get_next_replay_thread();
147
148         if (DBG_ENABLED()) {
149                 printf("Next execution will diverge at:\n");
150                 exploring->get_diverge()->print();
151                 print_list(exploring->get_trace());
152         }
153         return true;
154 }
155
156 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
157 {
158         action_type type = act->get_type();
159
160         switch (type) {
161                 case THREAD_CREATE:
162                 case THREAD_YIELD:
163                 case THREAD_JOIN:
164                         return NULL;
165                 case ATOMIC_READ:
166                 case ATOMIC_WRITE:
167                 default:
168                         break;
169         }
170         /* linear search: from most recent to oldest */
171         action_list_t::reverse_iterator rit;
172         for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
173                 ModelAction *prev = *rit;
174                 if (act->is_dependent(prev))
175                         return prev;
176         }
177         return NULL;
178 }
179
180 void ModelChecker::set_backtracking(ModelAction *act)
181 {
182         ModelAction *prev;
183         TreeNode *node;
184
185         prev = get_last_conflict(act);
186         if (prev == NULL)
187                 return;
188
189         node = prev->get_node();
190
191         /* Check if this has been explored already */
192         if (node->hasBeenExplored(act->get_tid()))
193                 return;
194         /* If this is a new backtracking point, mark the tree */
195         if (node->setBacktrack(act->get_tid()) != 0)
196                 return;
197
198         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
199                         prev->get_tid(), act->get_tid());
200         if (DBG_ENABLED()) {
201                 prev->print();
202                 act->print();
203         }
204
205         Backtrack *back = new Backtrack(prev, action_trace);
206         backtrack_list.push_back(back);
207 }
208
209 Backtrack * ModelChecker::get_next_backtrack()
210 {
211         Backtrack *next;
212         if (backtrack_list.empty())
213                 return NULL;
214         next = backtrack_list.back();
215         backtrack_list.pop_back();
216         return next;
217 }
218
219 void ModelChecker::check_current_action(void)
220 {
221         ModelAction *next = this->current_action;
222
223         if (!next) {
224                 DEBUG("trying to push NULL action...\n");
225                 return;
226         }
227         current_action = NULL;
228         nextThread = advance_backtracking_state();
229         next->set_node(currentNode);
230         set_backtracking(next);
231         currentNode = currentNode->exploreChild(next->get_tid());
232         this->action_trace->push_back(next);
233 }
234
235 void ModelChecker::print_summary(void)
236 {
237         printf("\n");
238         printf("Number of executions: %d\n", num_executions);
239         printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
240
241         scheduler->print();
242
243         print_list(action_trace);
244         printf("\n");
245
246 }
247
248 void ModelChecker::print_list(action_list_t *list)
249 {
250         action_list_t::iterator it;
251
252         printf("---------------------------------------------------------------------\n");
253         printf("Trace:\n");
254
255         for (it = list->begin(); it != list->end(); it++) {
256                 (*it)->print();
257         }
258         printf("---------------------------------------------------------------------\n");
259 }
260
261 int ModelChecker::add_thread(Thread *t)
262 {
263         thread_map[id_to_int(t->get_id())] = t;
264         scheduler->add_thread(t);
265         return 0;
266 }
267
268 void ModelChecker::remove_thread(Thread *t)
269 {
270         scheduler->remove_thread(t);
271 }
272
273 int ModelChecker::switch_to_master(ModelAction *act)
274 {
275         Thread *old;
276
277         DBG();
278         old = thread_current();
279         set_current_action(act);
280         old->set_state(THREAD_READY);
281         return Thread::swap(old, get_system_context());
282 }
283
284 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
285 {
286         Thread *t = thread_current();
287         ModelAction *act = this;
288
289         act->type = type;
290         act->order = order;
291         act->location = loc;
292         act->tid = t->get_id();
293         act->value = value;
294         act->seq_number = model->get_next_seq_num();
295 }
296
297 bool ModelAction::is_read()
298 {
299         return type == ATOMIC_READ;
300 }
301
302 bool ModelAction::is_write()
303 {
304         return type == ATOMIC_WRITE;
305 }
306
307 bool ModelAction::is_acquire()
308 {
309         switch (order) {
310         case memory_order_acquire:
311         case memory_order_acq_rel:
312         case memory_order_seq_cst:
313                 return true;
314         default:
315                 return false;
316         }
317 }
318
319 bool ModelAction::is_release()
320 {
321         switch (order) {
322         case memory_order_release:
323         case memory_order_acq_rel:
324         case memory_order_seq_cst:
325                 return true;
326         default:
327                 return false;
328         }
329 }
330
331 bool ModelAction::same_var(ModelAction *act)
332 {
333         return location == act->location;
334 }
335
336 bool ModelAction::same_thread(ModelAction *act)
337 {
338         return tid == act->tid;
339 }
340
341 bool ModelAction::is_dependent(ModelAction *act)
342 {
343         if (!is_read() && !is_write())
344                 return false;
345         if (!act->is_read() && !act->is_write())
346                 return false;
347         if (same_var(act) && !same_thread(act) &&
348                         (is_write() || act->is_write()))
349                 return true;
350         return false;
351 }
352
353 void ModelAction::print(void)
354 {
355         const char *type_str;
356         switch (this->type) {
357         case THREAD_CREATE:
358                 type_str = "thread create";
359                 break;
360         case THREAD_YIELD:
361                 type_str = "thread yield";
362                 break;
363         case THREAD_JOIN:
364                 type_str = "thread join";
365                 break;
366         case ATOMIC_READ:
367                 type_str = "atomic read";
368                 break;
369         case ATOMIC_WRITE:
370                 type_str = "atomic write";
371                 break;
372         default:
373                 type_str = "unknown type";
374         }
375
376         printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
377                         seq_number, id_to_int(tid), type_str, order, location, value);
378 }