tmp (model)
[model-checker.git] / model.cc
1 #include <stdio.h>
2
3 #include "model.h"
4 #include "schedule.h"
5 #include "common.h"
6
7 ModelChecker *model;
8
9 ModelChecker::ModelChecker()
10 {
11         /* First thread created (system_thread) will have id 1 */
12         this->used_thread_id = 0;
13         /* Initialize default scheduler */
14         this->scheduler = new Scheduler();
15
16         num_executions = 0;
17         this->current_action = NULL;
18         this->exploring = NULL;
19         this->nextThread = THREAD_ID_T_NONE;
20
21         rootNode = new TreeNode(NULL);
22         currentNode = rootNode;
23         action_trace = new action_list_t();
24 }
25
26 ModelChecker::~ModelChecker()
27 {
28         delete action_trace;
29         delete this->scheduler;
30         delete rootNode;
31 }
32
33 void ModelChecker::reset_to_initial_state()
34 {
35         DEBUG("+++ Resetting to initial state +++\n");
36         std::map<thread_id_t, class Thread *>::iterator it;
37         for (it = thread_map.begin(); it != thread_map.end(); it++) {
38                 delete (*it).second;
39         }
40         thread_map.clear();
41         action_trace = new action_list_t();
42         currentNode = rootNode;
43         current_action = NULL;
44         used_thread_id = 1; // ?
45         /* scheduler reset ? */
46 }
47
48 int ModelChecker::get_next_id()
49 {
50         return ++used_thread_id;
51 }
52
53 void ModelChecker::add_system_thread(Thread *t)
54 {
55         this->system_thread = t;
56 }
57
58 Thread * ModelChecker::schedule_next_thread()
59 {
60         Thread *t;
61         if (nextThread == THREAD_ID_T_NONE)
62                 return NULL;
63         t = thread_map[nextThread];
64         if (t == NULL)
65                 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
66         return t;
67 }
68
69 /*
70  * get_next_replay_thread() - Choose the next thread in the replay sequence
71  *
72  * If we've reached the 'diverge' point, then we pick a thread from the
73  *   backtracking set.
74  * Otherwise, we simply return the next thread in the sequence.
75  */
76 thread_id_t ModelChecker::get_next_replay_thread()
77 {
78         ModelAction *next;
79         thread_id_t tid;
80
81         next = exploring->get_state();
82
83         if (next == exploring->get_diverge()) {
84                 TreeNode *node = next->get_node();
85
86                 /* Reached divergence point; discard our current 'exploring' */
87                 DEBUG("*** Discard 'Backtrack' object ***\n");
88                 tid = node->getNextBacktrack();
89                 delete exploring;
90                 exploring = NULL;
91         } else {
92                 tid = next->get_tid();
93         }
94         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
95         return tid;
96 }
97
98 thread_id_t ModelChecker::advance_backtracking_state()
99 {
100         /* Have we completed exploring the preselected path? */
101         if (exploring == NULL)
102                 return THREAD_ID_T_NONE;
103
104         /* Else, we are trying to replay an execution */
105         exploring->advance_state();
106         if (exploring->get_state() == NULL)
107                 DEBUG("*** error: reached end of backtrack trace\n");
108
109         return get_next_replay_thread();
110 }
111
112 bool ModelChecker::next_execution()
113 {
114         num_executions++;
115         if ((exploring = model->get_next_backtrack()) == NULL)
116                 return false;
117         model->reset_to_initial_state();
118         nextThread = get_next_replay_thread();
119         return true;
120 }
121
122 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
123 {
124         void *loc = act->get_location();
125         action_type type = act->get_type();
126         thread_id_t id = act->get_tid();
127
128         switch (type) {
129                 case THREAD_CREATE:
130                 case THREAD_YIELD:
131                 case THREAD_JOIN:
132                         return NULL;
133                 case ATOMIC_READ:
134                 case ATOMIC_WRITE:
135                 default:
136                         break;
137         }
138         /* linear search: from most recent to oldest */
139         action_list_t::reverse_iterator rit;
140         for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
141                 ModelAction *prev = *rit;
142                 if (prev->get_location() != loc)
143                         continue;
144                 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
145                         continue;
146                 /* Conflict from the same thread is not really a conflict */
147                 if (id == prev->get_tid())
148                         return NULL;
149                 return prev;
150         }
151         return NULL;
152 }
153
154 void ModelChecker::set_backtracking(ModelAction *act)
155 {
156         ModelAction *prev;
157         TreeNode *node;
158
159         prev = get_last_conflict(act);
160         if (prev == NULL)
161                 return;
162
163         node = prev->get_node();
164
165         /* Check if this has been explored already */
166         if (node->hasBeenExplored(act->get_tid()))
167                 return;
168         /* If this is a new backtracking point, mark the tree */
169         if (node->setBacktrack(act->get_tid()) != 0)
170                 return;
171
172         printf("Setting backtrack: conflict = %d, instead tid = %d\n",
173                         prev->get_tid(), act->get_tid());
174         prev->print();
175         act->print();
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         nextThread = advance_backtracking_state();
200         next->set_node(currentNode);
201         set_backtracking(next);
202         currentNode = currentNode->exploreChild(next->get_tid());
203         this->action_trace->push_back(next);
204 }
205
206 void ModelChecker::print_trace(void)
207 {
208         action_list_t::iterator it;
209
210         printf("\n");
211         printf("---------------------------------------------------------------------\n");
212         printf("Number of executions: %d\n", num_executions);
213         printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
214
215         scheduler->print();
216
217         printf("\nTrace:\n\n");
218
219         for (it = action_trace->begin(); it != action_trace->end(); it++) {
220                 DBG();
221                 (*it)->print();
222         }
223         printf("---------------------------------------------------------------------\n");
224 }
225
226 int ModelChecker::add_thread(Thread *t)
227 {
228         thread_map[t->get_id()] = t;
229         scheduler->add_thread(t);
230         return 0;
231 }
232
233 void ModelChecker::remove_thread(Thread *t)
234 {
235         scheduler->remove_thread(t);
236 }
237
238 int ModelChecker::switch_to_master(ModelAction *act)
239 {
240         Thread *old, *next;
241
242         DBG();
243         old = thread_current();
244         set_current_action(act);
245         old->set_state(THREAD_READY);
246         next = system_thread;
247         return old->swap(next);
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 }
261
262 void ModelAction::print(void)
263 {
264         const char *type_str;
265         switch (this->type) {
266         case THREAD_CREATE:
267                 type_str = "thread create";
268                 break;
269         case THREAD_YIELD:
270                 type_str = "thread yield";
271                 break;
272         case THREAD_JOIN:
273                 type_str = "thread join";
274                 break;
275         case ATOMIC_READ:
276                 type_str = "atomic read";
277                 break;
278         case ATOMIC_WRITE:
279                 type_str = "atomic write";
280                 break;
281         default:
282                 type_str = "unknown type";
283         }
284
285         printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);
286 }