Merge branch 'norris'
[model-checker.git] / nodestack.cc
1 #include <string.h>
2
3 #include "nodestack.h"
4 #include "action.h"
5 #include "common.h"
6 #include "model.h"
7
8 /**
9  * @brief Node constructor
10  *
11  * Constructs a single Node for use in a NodeStack. Each Node is associated
12  * with exactly one ModelAction (exception: the first Node should be created
13  * as an empty stub, to represent the first thread "choice") and up to one
14  * parent.
15  *
16  * @param act The ModelAction to associate with this Node. May be NULL.
17  * @param par The parent Node in the NodeStack. May be NULL if there is no
18  * parent.
19  * @param nthreads The number of threads which exist at this point in the
20  * execution trace.
21  */
22 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness)
23         : action(act),
24         parent(par),
25         num_threads(nthreads),
26         explored_children(num_threads),
27         backtrack(num_threads),
28         fairness(num_threads),
29         numBacktracks(0),
30         enabled_array(NULL),
31         may_read_from(),
32         read_from_index(0),
33         future_values(),
34         future_index(-1)
35 {
36         if (act) {
37                 act->set_node(this);
38                 int currtid=id_to_int(act->get_tid());
39                 int prevtid=(prevfairness != NULL)?id_to_int(prevfairness->action->get_tid()):0;
40                 
41                 if ( model->params.fairwindow != 0 ) {
42                         for(int i=0;i<nthreads;i++) {
43                                 ASSERT(i<((int)fairness.size()));
44                                 struct fairness_info * fi=& fairness[i];
45                                 struct fairness_info * prevfi=(par!=NULL)&&(i<par->get_num_threads())?&par->fairness[i]:NULL;
46                                 if (prevfi) {
47                                         *fi=*prevfi;
48                                 }
49                                 if (parent->enabled_array[i]) {
50                                         fi->enabled_count++;
51                                 }
52                                 if (i==currtid) {
53                                         fi->turns++;
54                                         fi->priority = false;
55                                 }
56                                 //Do window processing
57                                 if (prevfairness != NULL) {
58                                         if (prevfairness -> parent->enabled_array[i])
59                                                 fi->enabled_count--;
60                                         if (i==prevtid) {
61                                                 fi->turns--;
62                                         }
63                                         //Need full window to start evaluating conditions
64                                         //If we meet the enabled count and have no turns, give us priority
65                                         if ((fi->enabled_count >= model->params.enabledcount) &&
66                                                         (fi->turns == 0))
67                                                 fi->priority = true;
68                                 }
69                         }
70                 }
71         }
72 }
73
74 /** @brief Node desctructor */
75 Node::~Node()
76 {
77         if (action)
78                 delete action;
79         if (enabled_array)
80                 MYFREE(enabled_array);
81 }
82
83 /** Prints debugging info for the ModelAction associated with this Node */
84 void Node::print()
85 {
86         if (action)
87                 action->print();
88         else
89                 printf("******** empty action ********\n");
90 }
91
92 /** @brief Prints info about may_read_from set */
93 void Node::print_may_read_from()
94 {
95         for (unsigned int i = 0; i < may_read_from.size(); i++)
96                 may_read_from[i]->print();
97 }
98
99 /**
100  * Sets a promise to explore meeting with the given node.
101  * @param i is the promise index.
102  */
103 void Node::set_promise(unsigned int i) {
104         if (i >= promises.size())
105                 promises.resize(i + 1, PROMISE_IGNORE);
106         if (promises[i] == PROMISE_IGNORE)
107                 promises[i] = PROMISE_UNFULFILLED;
108 }
109
110 /**
111  * Looks up whether a given promise should be satisfied by this node.
112  * @param i The promise index.
113  * @return true if the promise should be satisfied by the given model action.
114  */
115 bool Node::get_promise(unsigned int i) {
116         return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
117 }
118
119 /**
120  * Increments to the next combination of promises.
121  * @return true if we have a valid combination.
122  */
123 bool Node::increment_promise() {
124         for (unsigned int i = 0; i < promises.size(); i++) {
125                 if (promises[i] == PROMISE_UNFULFILLED) {
126                         promises[i] = PROMISE_FULFILLED;
127                         while (i > 0) {
128                                 i--;
129                                 if (promises[i] == PROMISE_FULFILLED)
130                                         promises[i] = PROMISE_UNFULFILLED;
131                         }
132                         return true;
133                 }
134         }
135         return false;
136 }
137
138 /**
139  * Returns whether the promise set is empty.
140  * @return true if we have explored all promise combinations.
141  */
142 bool Node::promise_empty() {
143         for (unsigned int i = 0; i < promises.size();i++)
144                 if (promises[i] == PROMISE_UNFULFILLED)
145                         return false;
146         return true;
147 }
148
149 /**
150  * Adds a value from a weakly ordered future write to backtrack to.
151  * @param value is the value to backtrack to.
152  */
153 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
154         int suitableindex=-1;
155         for (unsigned int i = 0; i < future_values.size(); i++) {
156                 if (future_values[i].value == value) {
157                         if (future_values[i].expiration>=expiration)
158                                 return false;
159                         if (future_index < i) {
160                                 suitableindex=i;
161                         }
162                 }
163         }
164
165         if (suitableindex!=-1) {
166                 future_values[suitableindex].expiration=expiration;
167                 return true;
168         }
169         struct future_value newfv={value, expiration};
170         future_values.push_back(newfv);
171         return true;
172 }
173
174 /**
175  * Checks whether the future_values set for this node is empty.
176  * @return true if the future_values set is empty.
177  */
178 bool Node::future_value_empty() {
179         return ((future_index + 1) >= future_values.size());
180 }
181
182 /**
183  * Checks if the Thread associated with this thread ID has been explored from
184  * this Node already.
185  * @param tid is the thread ID to check
186  * @return true if this thread choice has been explored already, false
187  * otherwise
188  */
189 bool Node::has_been_explored(thread_id_t tid)
190 {
191         int id = id_to_int(tid);
192         return explored_children[id];
193 }
194
195 /**
196  * Checks if the backtracking set is empty.
197  * @return true if the backtracking set is empty
198  */
199 bool Node::backtrack_empty()
200 {
201         return (numBacktracks == 0);
202 }
203
204 /**
205  * Checks whether the readsfrom set for this node is empty.
206  * @return true if the readsfrom set is empty.
207  */
208 bool Node::read_from_empty() {
209         return ((read_from_index+1) >= may_read_from.size());
210 }
211
212 /**
213  * Mark the appropriate backtracking information for exploring a thread choice.
214  * @param act The ModelAction to explore
215  */
216 void Node::explore_child(ModelAction *act, bool * is_enabled)
217 {
218         if ( ! enabled_array )
219                 enabled_array=(bool *)MYMALLOC(sizeof(bool)*num_threads);
220         if (is_enabled != NULL)
221                 memcpy(enabled_array, is_enabled, sizeof(bool)*num_threads);
222         else {
223                 for(int i=0;i<num_threads;i++)
224                         enabled_array[i]=false;
225         }
226
227         explore(act->get_tid());
228 }
229
230 /**
231  * Records a backtracking reference for a thread choice within this Node.
232  * Provides feedback as to whether this thread choice is already set for
233  * backtracking.
234  * @return false if the thread was already set to be backtracked, true
235  * otherwise
236  */
237 bool Node::set_backtrack(thread_id_t id)
238 {
239         int i = id_to_int(id);
240         if (backtrack[i])
241                 return false;
242         backtrack[i] = true;
243         numBacktracks++;
244         return true;
245 }
246
247 thread_id_t Node::get_next_backtrack()
248 {
249         /** @todo Find next backtrack */
250         unsigned int i;
251         for (i = 0; i < backtrack.size(); i++)
252                 if (backtrack[i] == true)
253                         break;
254         /* Backtrack set was empty? */
255         ASSERT(i != backtrack.size());
256
257         backtrack[i] = false;
258         numBacktracks--;
259         return int_to_id(i);
260 }
261
262 bool Node::is_enabled(Thread *t)
263 {
264         int thread_id=id_to_int(t->get_id());
265         return thread_id < num_threads && enabled_array[thread_id];
266 }
267
268 bool Node::is_enabled(thread_id_t tid)
269 {
270         int thread_id=id_to_int(tid);
271         return thread_id < num_threads && enabled_array[thread_id];
272 }
273
274 /**
275  * Add an action to the may_read_from set.
276  * @param act is the action to add
277  */
278 void Node::add_read_from(const ModelAction *act)
279 {
280         may_read_from.push_back(act);
281 }
282
283 /**
284  * Gets the next 'future_value' value from this Node. Only valid for a node
285  * where this->action is a 'read'.
286  * @return The first element in future_values
287  */
288 uint64_t Node::get_future_value() {
289         ASSERT(future_index<future_values.size());
290         return future_values[future_index].value;
291 }
292
293 modelclock_t Node::get_future_value_expiration() {
294         ASSERT(future_index<future_values.size());
295         return future_values[future_index].expiration;
296 }
297
298
299 int Node::get_read_from_size() {
300         return may_read_from.size();
301 }
302
303 const ModelAction * Node::get_read_from_at(int i) {
304         return may_read_from[i];
305 }
306
307 /**
308  * Gets the next 'may_read_from' action from this Node. Only valid for a node
309  * where this->action is a 'read'.
310  * @return The first element in may_read_from
311  */
312 const ModelAction * Node::get_read_from() {
313         if (read_from_index < may_read_from.size())
314                 return may_read_from[read_from_index];
315         else
316                 return NULL;
317 }
318
319 /**
320  * Increments the index into the readsfrom set to explore the next item.
321  * @return Returns false if we have explored all items.
322  */
323 bool Node::increment_read_from() {
324         read_from_index++;
325         return (read_from_index < may_read_from.size());
326 }
327
328 /**
329  * Increments the index into the future_values set to explore the next item.
330  * @return Returns false if we have explored all values.
331  */
332 bool Node::increment_future_value() {
333         future_index++;
334         return (future_index < future_values.size());
335 }
336
337 void Node::explore(thread_id_t tid)
338 {
339         int i = id_to_int(tid);
340         if (backtrack[i]) {
341                 backtrack[i] = false;
342                 numBacktracks--;
343         }
344         explored_children[i] = true;
345 }
346
347 NodeStack::NodeStack()
348         : total_nodes(0)
349 {
350         node_list.push_back(new Node());
351         total_nodes++;
352         iter = 0;
353 }
354
355 NodeStack::~NodeStack()
356 {
357 }
358
359 void NodeStack::print()
360 {
361         printf("............................................\n");
362         printf("NodeStack printing node_list:\n");
363         for (unsigned int it = 0; it < node_list.size(); it++) {
364                 if (it == this->iter)
365                         printf("vvv following action is the current iterator vvv\n");
366                 node_list[it]->print();
367         }
368         printf("............................................\n");
369 }
370
371 /** Note: The is_enabled set contains what actions were enabled when
372  *  act was chosen. */
373
374 ModelAction * NodeStack::explore_action(ModelAction *act, bool * is_enabled)
375 {
376         DBG();
377
378         ASSERT(!node_list.empty());
379
380         if ((iter+1) < node_list.size()) {
381                 iter++;
382                 return node_list[iter]->get_action();
383         }
384
385         /* Record action */
386         get_head()->explore_child(act, is_enabled);
387         Node *prevfairness = NULL;
388         if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
389                 prevfairness = node_list[iter-model->params.fairwindow];
390         }
391         node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
392         total_nodes++;
393         iter++;
394         return NULL;
395 }
396
397 /**
398  * Empties the stack of all trailing nodes after a given position and calls the
399  * destructor for each. This function is provided an offset which determines
400  * how many nodes (relative to the current replay state) to save before popping
401  * the stack.
402  * @param numAhead gives the number of Nodes (including this Node) to skip over
403  * before removing nodes.
404  */
405 void NodeStack::pop_restofstack(int numAhead)
406 {
407         /* Diverging from previous execution; clear out remainder of list */
408         unsigned int it=iter+numAhead;
409         node_list.resize(it);
410 }
411
412 Node * NodeStack::get_head()
413 {
414         if (node_list.empty())
415                 return NULL;
416         return node_list[iter];
417 }
418
419 Node * NodeStack::get_next()
420 {
421         if (node_list.empty()) {
422                 DEBUG("Empty\n");
423                 return NULL;
424         }
425         unsigned int it=iter+1;
426         if (it == node_list.size()) {
427                 DEBUG("At end\n");
428                 return NULL;
429         }
430         return node_list[it];
431 }
432
433 void NodeStack::reset_execution()
434 {
435         iter = 0;
436 }