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