model: implement get_next_replay() and advance_backtracking_state()
[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         this->current_action = NULL;
17         this->exploring = NULL;
18         this->nextThread = THREAD_ID_T_NONE;
19
20         rootNode = new TreeNode(NULL);
21         currentNode = rootNode;
22         action_trace = new action_list_t();
23 }
24
25 ModelChecker::~ModelChecker()
26 {
27         delete action_trace;
28         delete this->scheduler;
29         delete rootNode;
30 }
31
32 void ModelChecker::assign_id(Thread *t)
33 {
34         t->set_id(++used_thread_id);
35 }
36
37 void ModelChecker::add_system_thread(Thread *t)
38 {
39         this->system_thread = t;
40 }
41
42 Thread *ModelChecker::schedule_next_thread()
43 {
44         Thread *t;
45         if (nextThread == THREAD_ID_T_NONE)
46                 return NULL;
47         t = thread_map[nextThread];
48         if (t == NULL)
49                 DEBUG("*** error: thread not in thread_map: id = %d\n", nextThread);
50         return t;
51 }
52
53 /*
54  * get_next_replay_thread() - Choose the next thread in the replay sequence
55  *
56  * If we've reached the 'diverge' point, then we pick a thread from the
57  *   backtracking set.
58  * Otherwise, we simply return the next thread in the sequence.
59  */
60 thread_id_t ModelChecker::get_next_replay_thread()
61 {
62         ModelAction *next;
63         thread_id_t tid;
64
65         next = exploring->get_state();
66
67         if (next == exploring->get_diverge()) {
68                 TreeNode *node = next->get_node();
69
70                 /* Reached divergence point; discard our current 'exploring' */
71                 DEBUG("*** Discard 'Backtrack' object ***\n");
72                 tid = node->getNextBacktrack();
73                 delete exploring;
74                 exploring = NULL;
75         } else {
76                 tid = next->get_tid();
77         }
78         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
79         return tid;
80 }
81
82 thread_id_t ModelChecker::advance_backtracking_state()
83 {
84         /* Have we completed exploring the preselected path? */
85         if (exploring == NULL)
86                 return THREAD_ID_T_NONE;
87
88         /* Else, we are trying to replay an execution */
89         exploring->advance_state();
90         if (exploring->get_state() == NULL)
91                 DEBUG("*** error: reached end of backtrack trace\n");
92
93         return get_next_replay_thread();
94 }
95
96 ModelAction *ModelChecker::get_last_conflict(ModelAction *act)
97 {
98         void *loc = act->get_location();
99         action_type type = act->get_type();
100         thread_id_t id = act->get_tid();
101
102         switch (type) {
103                 case THREAD_CREATE:
104                 case THREAD_YIELD:
105                 case THREAD_JOIN:
106                         return NULL;
107                 case ATOMIC_READ:
108                 case ATOMIC_WRITE:
109                 default:
110                         break;
111         }
112         action_list_t::reverse_iterator rit;
113         for (rit = action_trace->rbegin(); rit != action_trace->rend(); rit++) {
114                 ModelAction *prev = *rit;
115                 if (prev->get_location() != loc)
116                         continue;
117                 if (type == ATOMIC_READ && prev->get_type() != ATOMIC_WRITE)
118                         continue;
119                 /* Conflict from the same thread is not really a conflict */
120                 if (id == prev->get_tid())
121                         return NULL;
122                 return prev;
123         }
124         return NULL;
125 }
126
127 void ModelChecker::set_backtracking(ModelAction *act)
128 {
129         ModelAction *prev;
130         TreeNode *node;
131
132         prev = get_last_conflict(act);
133         if (prev == NULL)
134                 return;
135
136         node = prev->get_node();
137
138         /* Check if this has been explored already */
139         if (node->hasBeenExplored(act->get_tid()))
140                 return;
141         /* If this is a new backtracking point, mark the tree */
142         if (node->setBacktrack(act->get_tid()) != 0)
143                 return;
144
145         printf("Setting backtrack: conflict = %d, instead tid = %d\n",
146                         prev->get_tid(), act->get_tid());
147         prev->print();
148         act->print();
149
150         Backtrack *back = new Backtrack(prev, action_trace);
151         backtrack_list.push_back(back);
152 }
153
154 void ModelChecker::check_current_action(void)
155 {
156         ModelAction *next = this->current_action;
157
158         if (!next) {
159                 DEBUG("trying to push NULL action...\n");
160                 return;
161         }
162         nextThread = advance_backtracking_state();
163         next->set_node(currentNode);
164         set_backtracking(next);
165         currentNode = currentNode->exploreChild(next->get_tid());
166         this->action_trace->push_back(next);
167 }
168
169 void ModelChecker::print_trace(void)
170 {
171         action_list_t::iterator it;
172
173         printf("\n");
174         printf("---------------------------------------------------------------------\n");
175         printf("Total nodes created: %d\n\n", TreeNode::getTotalNodes());
176
177         for (it = action_trace->begin(); it != action_trace->end(); it++) {
178                 DBG();
179                 (*it)->print();
180         }
181         printf("---------------------------------------------------------------------\n");
182 }
183
184 int ModelChecker::add_thread(Thread *t)
185 {
186         thread_map[t->get_id()] = t;
187         return 0;
188 }
189
190 int ModelChecker::switch_to_master(ModelAction *act)
191 {
192         Thread *old, *next;
193
194         DBG();
195         old = thread_current();
196         set_current_action(act);
197         old->set_state(THREAD_READY);
198         next = system_thread;
199         return old->swap(next);
200 }
201
202 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc, int value)
203 {
204         Thread *t = thread_current();
205         ModelAction *act = this;
206
207         act->type = type;
208         act->order = order;
209         act->location = loc;
210         act->tid = t->get_id();
211         act->value = value;
212 }
213
214 void ModelAction::print(void)
215 {
216         const char *type_str;
217         switch (this->type) {
218         case THREAD_CREATE:
219                 type_str = "thread create";
220                 break;
221         case THREAD_YIELD:
222                 type_str = "thread yield";
223                 break;
224         case THREAD_JOIN:
225                 type_str = "thread join";
226                 break;
227         case ATOMIC_READ:
228                 type_str = "atomic read";
229                 break;
230         case ATOMIC_WRITE:
231                 type_str = "atomic write";
232                 break;
233         default:
234                 type_str = "unknown type";
235         }
236
237         printf("Thread: %d\tAction: %s\tMO: %d\tLoc: %#014zx\tValue: %d\n", tid, type_str, order, (size_t)location, value);
238 }