model: add sequence numbers to 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 ModelChecker *model;
12
13 ModelChecker::ModelChecker()
14 {
15         /* First thread created will have id (INITIAL_THREAD_ID + 1) */
16         this->used_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         used_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 ++used_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         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[id_to_int(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         act->seq_number = model->get_next_seq_num();
261 }
262
263 bool ModelAction::is_read()
264 {
265         return type == ATOMIC_READ;
266 }
267
268 bool ModelAction::is_write()
269 {
270         return type == ATOMIC_WRITE;
271 }
272
273 bool ModelAction::is_acquire()
274 {
275         switch (order) {
276         case memory_order_acquire:
277         case memory_order_acq_rel:
278         case memory_order_seq_cst:
279                 return true;
280         default:
281                 return false;
282         }
283 }
284
285 bool ModelAction::is_release()
286 {
287         switch (order) {
288         case memory_order_release:
289         case memory_order_acq_rel:
290         case memory_order_seq_cst:
291                 return true;
292         default:
293                 return false;
294         }
295 }
296
297 bool ModelAction::same_var(ModelAction *act)
298 {
299         return location == act->location;
300 }
301
302 bool ModelAction::same_thread(ModelAction *act)
303 {
304         return tid == act->tid;
305 }
306
307 bool ModelAction::is_dependent(ModelAction *act)
308 {
309         if (!is_read() && !is_write())
310                 return false;
311         if (!act->is_read() && !act->is_write())
312                 return false;
313         if (same_var(act) && !same_thread(act) &&
314                         (is_write() || act->is_write()))
315                 return true;
316         return false;
317 }
318
319 void ModelAction::print(void)
320 {
321         const char *type_str;
322         switch (this->type) {
323         case THREAD_CREATE:
324                 type_str = "thread create";
325                 break;
326         case THREAD_YIELD:
327                 type_str = "thread yield";
328                 break;
329         case THREAD_JOIN:
330                 type_str = "thread join";
331                 break;
332         case ATOMIC_READ:
333                 type_str = "atomic read";
334                 break;
335         case ATOMIC_WRITE:
336                 type_str = "atomic write";
337                 break;
338         default:
339                 type_str = "unknown type";
340         }
341
342         printf("(%4d) Thread: %d\tAction: %s\tMO: %d\tLoc: %14p\tValue: %d\n",
343                         seq_number, id_to_int(tid), type_str, order, location, value);
344 }