fix page alignment issue...
[c11tester.git] / model.cc
1 #include <stdio.h>
2
3 #include "model.h"
4 #include "action.h"
5 #include "nodestack.h"
6 #include "schedule.h"
7 #include "snapshot-interface.h"
8 #include "common.h"
9
10 #define INITIAL_THREAD_ID       0
11
12 ModelChecker *model;
13
14 ModelChecker::ModelChecker()
15         :
16         /* Initialize default scheduler */
17         scheduler(new Scheduler()),
18         /* First thread created will have id INITIAL_THREAD_ID */
19         next_thread_id(INITIAL_THREAD_ID),
20         used_sequence_numbers(0),
21
22         num_executions(0),
23         current_action(NULL),
24         diverge(NULL),
25         nextThread(THREAD_ID_T_NONE),
26         action_trace(new action_list_t()),
27         node_stack(new NodeStack()),
28         next_backtrack(NULL)
29 {
30 }
31
32 ModelChecker::~ModelChecker()
33 {
34         std::map<int, class Thread *, std::less< int >, MyAlloc< std::pair< int, class Thread * > > >::iterator it;
35         for (it = thread_map.begin(); it != thread_map.end(); it++)
36                 delete (*it).second;
37         thread_map.clear();
38
39         delete action_trace;
40
41         delete node_stack;
42         delete scheduler;
43 }
44
45 void ModelChecker::reset_to_initial_state()
46 {
47         DEBUG("+++ Resetting to initial state +++\n");
48         std::map<int, class Thread *, std::less< int >, MyAlloc< std::pair< int, class Thread * > > >::iterator it;
49         for (it = thread_map.begin(); it != thread_map.end(); it++)
50                 delete (*it).second;
51         thread_map.clear();
52         delete action_trace;
53         action_trace = new action_list_t();
54         node_stack->reset_execution();
55         current_action = NULL;
56         next_thread_id = INITIAL_THREAD_ID;
57         used_sequence_numbers = 0;
58         nextThread = 0;
59         next_backtrack = NULL;
60         /* scheduler reset ? */
61 }
62
63 thread_id_t ModelChecker::get_next_id()
64 {
65         return next_thread_id++;
66 }
67
68 int ModelChecker::get_next_seq_num()
69 {
70         return ++used_sequence_numbers;
71 }
72
73 Thread * ModelChecker::schedule_next_thread()
74 {
75         Thread *t;
76         if (nextThread == THREAD_ID_T_NONE)
77                 return NULL;
78         t = thread_map[id_to_int(nextThread)];
79
80         ASSERT(t != NULL);
81
82         return t;
83 }
84
85 /*
86  * get_next_replay_thread() - Choose the next thread in the replay sequence
87  *
88  * If we've reached the 'diverge' point, then we pick a thread from the
89  *   backtracking set.
90  * Otherwise, we simply return the next thread in the sequence.
91  */
92 thread_id_t ModelChecker::get_next_replay_thread()
93 {
94         ModelAction *next;
95         thread_id_t tid;
96
97         /* Have we completed exploring the preselected path? */
98         if (diverge == NULL)
99                 return THREAD_ID_T_NONE;
100
101         /* Else, we are trying to replay an execution */
102         next = node_stack->get_next()->get_action();
103
104         if (next == diverge) {
105                 Node *node = next->get_node();
106
107                 /* Reached divergence point */
108                 DEBUG("*** Divergence point ***\n");
109                 tid = node->get_next_backtrack();
110                 diverge = NULL;
111         } else {
112                 tid = next->get_tid();
113         }
114         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
115         return tid;
116 }
117
118 bool ModelChecker::next_execution()
119 {
120         DBG();
121
122         num_executions++;
123         print_summary();
124         if ((diverge = model->get_next_backtrack()) == NULL)
125                 return false;
126
127         if (DBG_ENABLED()) {
128                 printf("Next execution will diverge at:\n");
129                 diverge->print();
130         }
131
132         model->reset_to_initial_state();
133         return true;
134 }
135
136 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
137 {
138         action_type type = act->get_type();
139
140         switch (type) {
141                 case THREAD_CREATE:
142                 case THREAD_YIELD:
143                 case THREAD_JOIN:
144                         return NULL;
145                 case ATOMIC_READ:
146                 case ATOMIC_WRITE:
147                 default:
148                         break;
149         }
150         /* linear search: from most recent to oldest */
151         action_list_t::reverse_iterator rit;
152         for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
153                 ModelAction *prev = *rit;
154                 if (act->is_dependent(prev))
155                         return prev;
156         }
157         return NULL;
158 }
159
160 void ModelChecker::set_backtracking(ModelAction *act)
161 {
162         ModelAction *prev;
163         Node *node;
164         Thread *t = get_thread(act->get_tid());
165
166         prev = get_last_conflict(act);
167         if (prev == NULL)
168                 return;
169
170         node = prev->get_node();
171
172         while (!node->is_enabled(t))
173                 t = t->get_parent();
174
175         /* Check if this has been explored already */
176         if (node->has_been_explored(t->get_id()))
177                 return;
178
179         if (!next_backtrack || *prev > *next_backtrack)
180                 next_backtrack = prev;
181
182         /* If this is a new backtracking point, mark the tree */
183         if (!node->set_backtrack(t->get_id()))
184                 return;
185         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
186                         prev->get_tid(), t->get_id());
187         if (DBG_ENABLED()) {
188                 prev->print();
189                 act->print();
190         }
191 }
192
193 ModelAction * ModelChecker::get_next_backtrack()
194 {
195         ModelAction *next = next_backtrack;
196         next_backtrack = NULL;
197         return next;
198 }
199
200 void ModelChecker::check_current_action(void)
201 {
202         Node *currnode;
203
204         ModelAction *curr = this->current_action;
205         current_action = NULL;
206         if (!curr) {
207                 DEBUG("trying to push NULL action...\n");
208                 return;
209         }
210
211         curr = node_stack->explore_action(curr);
212         nextThread = get_next_replay_thread();
213
214         currnode = curr->get_node();
215
216         if (!currnode->backtrack_empty())
217                 if (!next_backtrack || *curr > *next_backtrack)
218                         next_backtrack = curr;
219
220         set_backtracking(curr);
221         this->action_trace->push_back(curr);
222 }
223
224 void ModelChecker::print_summary(void)
225 {
226         printf("\n");
227         printf("Number of executions: %d\n", num_executions);
228         printf("Total nodes created: %d\n", Node::get_total_nodes());
229
230         scheduler->print();
231
232         print_list(action_trace);
233         printf("\n");
234 }
235
236 void ModelChecker::print_list(action_list_t *list)
237 {
238         action_list_t::iterator it;
239
240         printf("---------------------------------------------------------------------\n");
241         printf("Trace:\n");
242
243         for (it = list->begin(); it != list->end(); it++) {
244                 (*it)->print();
245         }
246         printf("---------------------------------------------------------------------\n");
247 }
248
249 int ModelChecker::add_thread(Thread *t)
250 {
251         thread_map[id_to_int(t->get_id())] = t;
252         scheduler->add_thread(t);
253         return 0;
254 }
255
256 void ModelChecker::remove_thread(Thread *t)
257 {
258         scheduler->remove_thread(t);
259 }
260
261 int ModelChecker::switch_to_master(ModelAction *act)
262 {
263         Thread *old;
264
265         DBG();
266         old = thread_current();
267         set_current_action(act);
268         old->set_state(THREAD_READY);
269         return Thread::swap(old, get_system_context());
270 }