976b395db7889a123465d6ba89533503aa99a749
[c11tester.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 ModelChecker *model;
12
13 ModelChecker::ModelChecker()
14 {
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();
20
21         num_executions = 0;
22         this->current_action = NULL;
23         this->exploring = NULL;
24         this->nextThread = THREAD_ID_T_NONE;
25
26         rootNode = new TreeNode(NULL);
27         currentNode = rootNode;
28         action_trace = new action_list_t();
29 }
30
31 ModelChecker::~ModelChecker()
32 {
33         delete action_trace;
34         delete this->scheduler;
35         delete rootNode;
36 }
37
38 void ModelChecker::reset_to_initial_state()
39 {
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++)
43                 delete (*it).second;
44         thread_map.clear();
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 ? */
51 }
52
53 thread_id_t ModelChecker::get_next_id()
54 {
55         return next_thread_id++;
56 }
57
58 int ModelChecker::get_next_seq_num()
59 {
60         return ++used_sequence_numbers;
61 }
62
63 Thread * ModelChecker::schedule_next_thread()
64 {
65         Thread *t;
66         if (nextThread == THREAD_ID_T_NONE)
67                 return NULL;
68         t = thread_map[id_to_int(nextThread)];
69         if (t == NULL)
70                 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
71         return t;
72 }
73
74 /*
75  * get_next_replay_thread() - Choose the next thread in the replay sequence
76  *
77  * If we've reached the 'diverge' point, then we pick a thread from the
78  *   backtracking set.
79  * Otherwise, we simply return the next thread in the sequence.
80  */
81 thread_id_t ModelChecker::get_next_replay_thread()
82 {
83         ModelAction *next;
84         thread_id_t tid;
85
86         next = exploring->get_state();
87
88         if (next == exploring->get_diverge()) {
89                 TreeNode *node = next->get_node();
90
91                 /* Reached divergence point; discard our current 'exploring' */
92                 DEBUG("*** Discard 'Backtrack' object ***\n");
93                 tid = node->getNextBacktrack();
94                 delete exploring;
95                 exploring = NULL;
96         } else {
97                 tid = next->get_tid();
98         }
99         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
100         return tid;
101 }
102
103 thread_id_t ModelChecker::advance_backtracking_state()
104 {
105         /* Have we completed exploring the preselected path? */
106         if (exploring == NULL)
107                 return THREAD_ID_T_NONE;
108
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");
113
114         return get_next_replay_thread();
115 }
116
117 bool ModelChecker::next_execution()
118 {
119         num_executions++;
120         print_summary();
121         if ((exploring = model->get_next_backtrack()) == NULL)
122                 return false;
123         model->reset_to_initial_state();
124         nextThread = get_next_replay_thread();
125         return true;
126 }
127
128 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
129 {
130         action_type type = act->get_type();
131
132         switch (type) {
133                 case THREAD_CREATE:
134                 case THREAD_YIELD:
135                 case THREAD_JOIN:
136                         return NULL;
137                 case ATOMIC_READ:
138                 case ATOMIC_WRITE:
139                 default:
140                         break;
141         }
142         /* linear search: from most recent to oldest */
143         action_list_t::reverse_iterator rit;
144         for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
145                 ModelAction *prev = *rit;
146                 if (act->is_dependent(prev))
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         printf("\n");
210         printf("Number of executions: %d\n", num_executions);
211         printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
212
213         scheduler->print();
214
215         print_list(action_trace);
216         printf("\n");
217
218 }
219
220 void ModelChecker::print_list(action_list_t *list)
221 {
222         action_list_t::iterator it;
223
224         printf("---------------------------------------------------------------------\n");
225         printf("Trace:\n");
226
227         for (it = list->begin(); it != list->end(); it++) {
228                 (*it)->print();
229         }
230         printf("---------------------------------------------------------------------\n");
231 }
232
233 int ModelChecker::add_thread(Thread *t)
234 {
235         thread_map[id_to_int(t->get_id())] = t;
236         scheduler->add_thread(t);
237         return 0;
238 }
239
240 void ModelChecker::remove_thread(Thread *t)
241 {
242         scheduler->remove_thread(t);
243 }
244
245 int ModelChecker::switch_to_master(ModelAction *act)
246 {
247         Thread *old;
248
249         DBG();
250         old = thread_current();
251         set_current_action(act);
252         old->set_state(THREAD_READY);
253         return Thread::swap(old, get_system_context());
254 }
255
256 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
257 {
258         Thread *t = thread_current();
259         ModelAction *act = this;
260
261         act->type = type;
262         act->order = order;
263         act->location = loc;
264         act->tid = t->get_id();
265         act->value = value;
266         act->seq_number = model->get_next_seq_num();
267 }
268
269 bool ModelAction::is_read()
270 {
271         return type == ATOMIC_READ;
272 }
273
274 bool ModelAction::is_write()
275 {
276         return type == ATOMIC_WRITE;
277 }
278
279 bool ModelAction::is_acquire()
280 {
281         switch (order) {
282         case memory_order_acquire:
283         case memory_order_acq_rel:
284         case memory_order_seq_cst:
285                 return true;
286         default:
287                 return false;
288         }
289 }
290
291 bool ModelAction::is_release()
292 {
293         switch (order) {
294         case memory_order_release:
295         case memory_order_acq_rel:
296         case memory_order_seq_cst:
297                 return true;
298         default:
299                 return false;
300         }
301 }
302
303 bool ModelAction::same_var(ModelAction *act)
304 {
305         return location == act->location;
306 }
307
308 bool ModelAction::same_thread(ModelAction *act)
309 {
310         return tid == act->tid;
311 }
312
313 bool ModelAction::is_dependent(ModelAction *act)
314 {
315         if (!is_read() && !is_write())
316                 return false;
317         if (!act->is_read() && !act->is_write())
318                 return false;
319         if (same_var(act) && !same_thread(act) &&
320                         (is_write() || act->is_write()))
321                 return true;
322         return false;
323 }
324
325 void ModelAction::print(void)
326 {
327         const char *type_str;
328         switch (this->type) {
329         case THREAD_CREATE:
330                 type_str = "thread create";
331                 break;
332         case THREAD_YIELD:
333                 type_str = "thread yield";
334                 break;
335         case THREAD_JOIN:
336                 type_str = "thread join";
337                 break;
338         case ATOMIC_READ:
339                 type_str = "atomic read";
340                 break;
341         case ATOMIC_WRITE:
342                 type_str = "atomic write";
343                 break;
344         default:
345                 type_str = "unknown type";
346         }
347
348         printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
349                         seq_number, id_to_int(tid), type_str, order, location, value);
350 }