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