tree: revise arguments (use Thread, ModelAction)
[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 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();
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
146         if (DBG_ENABLED()) {
147                 printf("Next execution will diverge at:\n");
148                 exploring->get_diverge()->print();
149                 print_list(exploring->get_trace());
150         }
151
152         model->reset_to_initial_state();
153         nextThread = get_next_replay_thread();
154         return true;
155 }
156
157 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
158 {
159         action_type type = act->get_type();
160
161         switch (type) {
162                 case THREAD_CREATE:
163                 case THREAD_YIELD:
164                 case THREAD_JOIN:
165                         return NULL;
166                 case ATOMIC_READ:
167                 case ATOMIC_WRITE:
168                 default:
169                         break;
170         }
171         /* linear search: from most recent to oldest */
172         action_list_t::reverse_iterator rit;
173         for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
174                 ModelAction *prev = *rit;
175                 if (act->is_dependent(prev))
176                         return prev;
177         }
178         return NULL;
179 }
180
181 void ModelChecker::set_backtracking(ModelAction *act)
182 {
183         ModelAction *prev;
184         TreeNode *node;
185         Thread *t = get_thread(act->get_tid());
186
187         prev = get_last_conflict(act);
188         if (prev == NULL)
189                 return;
190
191         node = prev->get_node();
192
193         /* Check if this has been explored already */
194         if (node->hasBeenExplored(t->get_id()))
195                 return;
196         /* If this is a new backtracking point, mark the tree */
197         if (node->setBacktrack(t->get_id()) != 0)
198                 return;
199
200         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
201                         prev->get_tid(), t->get_id());
202         if (DBG_ENABLED()) {
203                 prev->print();
204                 act->print();
205         }
206
207         Backtrack *back = new Backtrack(prev, action_trace);
208         backtrack_list.push_back(back);
209 }
210
211 Backtrack * ModelChecker::get_next_backtrack()
212 {
213         Backtrack *next;
214         if (backtrack_list.empty())
215                 return NULL;
216         next = backtrack_list.back();
217         backtrack_list.pop_back();
218         return next;
219 }
220
221 void ModelChecker::check_current_action(void)
222 {
223         ModelAction *next = this->current_action;
224
225         if (!next) {
226                 DEBUG("trying to push NULL action...\n");
227                 return;
228         }
229         current_action = NULL;
230         nextThread = advance_backtracking_state();
231         next->set_node(currentNode);
232         set_backtracking(next);
233         currentNode = currentNode->explore_child(next);
234         this->action_trace->push_back(next);
235 }
236
237 void ModelChecker::print_summary(void)
238 {
239         printf("\n");
240         printf("Number of executions: %d\n", num_executions);
241         printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
242
243         scheduler->print();
244
245         print_list(action_trace);
246         printf("\n");
247
248 }
249
250 void ModelChecker::print_list(action_list_t *list)
251 {
252         action_list_t::iterator it;
253
254         printf("---------------------------------------------------------------------\n");
255         printf("Trace:\n");
256
257         for (it = list->begin(); it != list->end(); it++) {
258                 (*it)->print();
259         }
260         printf("---------------------------------------------------------------------\n");
261 }
262
263 int ModelChecker::add_thread(Thread *t)
264 {
265         thread_map[id_to_int(t->get_id())] = t;
266         scheduler->add_thread(t);
267         return 0;
268 }
269
270 void ModelChecker::remove_thread(Thread *t)
271 {
272         scheduler->remove_thread(t);
273 }
274
275 int ModelChecker::switch_to_master(ModelAction *act)
276 {
277         Thread *old;
278
279         DBG();
280         old = thread_current();
281         set_current_action(act);
282         old->set_state(THREAD_READY);
283         return Thread::swap(old, get_system_context());
284 }
285
286 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
287 {
288         Thread *t = thread_current();
289         ModelAction *act = this;
290
291         act->type = type;
292         act->order = order;
293         act->location = loc;
294         act->tid = t->get_id();
295         act->value = value;
296         act->seq_number = model->get_next_seq_num();
297 }
298
299 bool ModelAction::is_read()
300 {
301         return type == ATOMIC_READ;
302 }
303
304 bool ModelAction::is_write()
305 {
306         return type == ATOMIC_WRITE;
307 }
308
309 bool ModelAction::is_acquire()
310 {
311         switch (order) {
312         case memory_order_acquire:
313         case memory_order_acq_rel:
314         case memory_order_seq_cst:
315                 return true;
316         default:
317                 return false;
318         }
319 }
320
321 bool ModelAction::is_release()
322 {
323         switch (order) {
324         case memory_order_release:
325         case memory_order_acq_rel:
326         case memory_order_seq_cst:
327                 return true;
328         default:
329                 return false;
330         }
331 }
332
333 bool ModelAction::same_var(ModelAction *act)
334 {
335         return location == act->location;
336 }
337
338 bool ModelAction::same_thread(ModelAction *act)
339 {
340         return tid == act->tid;
341 }
342
343 bool ModelAction::is_dependent(ModelAction *act)
344 {
345         if (!is_read() && !is_write())
346                 return false;
347         if (!act->is_read() && !act->is_write())
348                 return false;
349         if (same_var(act) && !same_thread(act) &&
350                         (is_write() || act->is_write()))
351                 return true;
352         return false;
353 }
354
355 void ModelAction::print(void)
356 {
357         const char *type_str;
358         switch (this->type) {
359         case THREAD_CREATE:
360                 type_str = "thread create";
361                 break;
362         case THREAD_YIELD:
363                 type_str = "thread yield";
364                 break;
365         case THREAD_JOIN:
366                 type_str = "thread join";
367                 break;
368         case ATOMIC_READ:
369                 type_str = "atomic read";
370                 break;
371         case ATOMIC_WRITE:
372                 type_str = "atomic write";
373                 break;
374         default:
375                 type_str = "unknown type";
376         }
377
378         printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
379                         seq_number, id_to_int(tid), type_str, order, location, value);
380 }