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