Adding STL stuff and operator news of snapshot to model-checker. Need to actuallly...
[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 void free_action_list(action_list_t *list)
34 {
35         action_list_t::iterator it;
36         for (it = list->begin(); it != list->end(); it++)
37                 delete (*it);
38         delete list;
39 }
40
41 ModelChecker::ModelChecker()
42 {
43         /* First thread created will have id INITIAL_THREAD_ID */
44         this->next_thread_id = INITIAL_THREAD_ID;
45         used_sequence_numbers = 0;
46         /* Initialize default scheduler */
47         this->scheduler = new Scheduler();
48
49         num_executions = 0;
50         this->current_action = NULL;
51         this->exploring = NULL;
52         this->nextThread = THREAD_ID_T_NONE;
53
54         rootNode = new TreeNode();
55         currentNode = rootNode;
56         action_trace = new action_list_t();
57 }
58
59 ModelChecker::~ModelChecker()
60 {
61         std::map<int, class Thread *, std::less< int >, MyAlloc< std::pair< 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
66         free_action_list(action_trace);
67
68         delete this->scheduler;
69         delete rootNode;
70 }
71
72 void ModelChecker::reset_to_initial_state()
73 {
74         DEBUG("+++ Resetting to initial state +++\n");
75         std::map<int, class Thread *, std::less< int >, MyAlloc< std::pair< int, class Thread * > > >::iterator it;
76         for (it = thread_map.begin(); it != thread_map.end(); it++)
77                 delete (*it).second;
78         thread_map.clear();
79         action_trace = new action_list_t();
80         currentNode = rootNode;
81         current_action = NULL;
82         next_thread_id = INITIAL_THREAD_ID;
83         used_sequence_numbers = 0;
84         /* scheduler reset ? */
85 }
86
87 thread_id_t ModelChecker::get_next_id()
88 {
89         return next_thread_id++;
90 }
91
92 int ModelChecker::get_next_seq_num()
93 {
94         return ++used_sequence_numbers;
95 }
96
97 Thread * ModelChecker::schedule_next_thread()
98 {
99         Thread *t;
100         if (nextThread == THREAD_ID_T_NONE)
101                 return NULL;
102         t = thread_map[id_to_int(nextThread)];
103
104         ASSERT(t != NULL);
105
106         return t;
107 }
108
109 /*
110  * get_next_replay_thread() - Choose the next thread in the replay sequence
111  *
112  * If we've reached the 'diverge' point, then we pick a thread from the
113  *   backtracking set.
114  * Otherwise, we simply return the next thread in the sequence.
115  */
116 thread_id_t ModelChecker::get_next_replay_thread()
117 {
118         ModelAction *next;
119         thread_id_t tid;
120
121         next = exploring->get_state();
122
123         if (next == exploring->get_diverge()) {
124                 TreeNode *node = next->get_node();
125
126                 /* Reached divergence point; discard our current 'exploring' */
127                 DEBUG("*** Discard 'Backtrack' object ***\n");
128                 tid = node->getNextBacktrack();
129                 delete exploring;
130                 exploring = NULL;
131         } else {
132                 tid = next->get_tid();
133         }
134         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
135         return tid;
136 }
137
138 thread_id_t ModelChecker::advance_backtracking_state()
139 {
140         /* Have we completed exploring the preselected path? */
141         if (exploring == NULL)
142                 return THREAD_ID_T_NONE;
143
144         /* Else, we are trying to replay an execution */
145         exploring->advance_state();
146
147         ASSERT(exploring->get_state() != NULL);
148
149         return get_next_replay_thread();
150 }
151
152 bool ModelChecker::next_execution()
153 {
154         DBG();
155
156         num_executions++;
157         print_summary();
158         if ((exploring = model->get_next_backtrack()) == NULL)
159                 return false;
160
161         if (DBG_ENABLED()) {
162                 printf("Next execution will diverge at:\n");
163                 exploring->get_diverge()->print();
164                 print_list(exploring->get_trace());
165         }
166
167         model->reset_to_initial_state();
168         nextThread = get_next_replay_thread();
169         return true;
170 }
171
172 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
173 {
174         action_type type = act->get_type();
175
176         switch (type) {
177                 case THREAD_CREATE:
178                 case THREAD_YIELD:
179                 case THREAD_JOIN:
180                         return NULL;
181                 case ATOMIC_READ:
182                 case ATOMIC_WRITE:
183                 default:
184                         break;
185         }
186         /* linear search: from most recent to oldest */
187         action_list_t::reverse_iterator rit;
188         for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
189                 ModelAction *prev = *rit;
190                 if (act->is_dependent(prev))
191                         return prev;
192         }
193         return NULL;
194 }
195
196 void ModelChecker::set_backtracking(ModelAction *act)
197 {
198         ModelAction *prev;
199         TreeNode *node;
200         Thread *t = get_thread(act->get_tid());
201
202         prev = get_last_conflict(act);
203         if (prev == NULL)
204                 return;
205
206         node = prev->get_node();
207
208         while (t && !node->is_enabled(t))
209                 t = t->get_parent();
210
211         /* Check if this has been explored already */
212         if (node->hasBeenExplored(t->get_id()))
213                 return;
214         /* If this is a new backtracking point, mark the tree */
215         if (node->setBacktrack(t->get_id()) != 0)
216                 return;
217
218         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
219                         prev->get_tid(), t->get_id());
220         if (DBG_ENABLED()) {
221                 prev->print();
222                 act->print();
223         }
224
225         Backtrack *back = new Backtrack(prev, action_trace);
226         backtrack_list.push_back(back);
227 }
228
229 Backtrack * ModelChecker::get_next_backtrack()
230 {
231         Backtrack *next;
232         if (backtrack_list.empty())
233                 return NULL;
234         next = backtrack_list.back();
235         backtrack_list.pop_back();
236         return next;
237 }
238
239 void ModelChecker::check_current_action(void)
240 {
241         ModelAction *next = this->current_action;
242
243         if (!next) {
244                 DEBUG("trying to push NULL action...\n");
245                 return;
246         }
247         current_action = NULL;
248         nextThread = advance_backtracking_state();
249         next->set_node(currentNode);
250         set_backtracking(next);
251         currentNode = currentNode->explore_child(next);
252         this->action_trace->push_back(next);
253 }
254
255 void ModelChecker::print_summary(void)
256 {
257         printf("\n");
258         printf("Number of executions: %d\n", num_executions);
259         printf("Total nodes created: %d\n", TreeNode::getTotalNodes());
260
261         scheduler->print();
262
263         print_list(action_trace);
264         printf("\n");
265
266 }
267
268 void ModelChecker::print_list(action_list_t *list)
269 {
270         action_list_t::iterator it;
271
272         printf("---------------------------------------------------------------------\n");
273         printf("Trace:\n");
274
275         for (it = list->begin(); it != list->end(); it++) {
276                 (*it)->print();
277         }
278         printf("---------------------------------------------------------------------\n");
279 }
280
281 int ModelChecker::add_thread(Thread *t)
282 {
283         thread_map[id_to_int(t->get_id())] = t;
284         scheduler->add_thread(t);
285         return 0;
286 }
287
288 void ModelChecker::remove_thread(Thread *t)
289 {
290         scheduler->remove_thread(t);
291 }
292
293 int ModelChecker::switch_to_master(ModelAction *act)
294 {
295         Thread *old;
296
297         DBG();
298         old = thread_current();
299         set_current_action(act);
300         old->set_state(THREAD_READY);
301         return Thread::swap(old, get_system_context());
302 }