f0d79245cd8aa2e648a5071f50e66e48f5bdc3d9
[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                 model_print("******** 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, bool is_rmw) {
109         if (i >= promises.size())
110                 promises.resize(i + 1, PROMISE_IGNORE);
111         if (promises[i] == PROMISE_IGNORE) {
112                 promises[i] = PROMISE_UNFULFILLED;
113                 if (is_rmw)
114                         promises[i] |= PROMISE_RMW;
115         }
116 }
117
118 /**
119  * Looks up whether a given promise should be satisfied by this node.
120  * @param i The promise index.
121  * @return true if the promise should be satisfied by the given model action.
122  */
123 bool Node::get_promise(unsigned int i) const
124 {
125         return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
126 }
127
128 /**
129  * Increments to the next combination of promises.
130  * @return true if we have a valid combination.
131  */
132 bool Node::increment_promise() {
133         DBG();
134         unsigned int rmw_count=0;
135         for (unsigned int i = 0; i < promises.size(); i++) {
136                 if (promises[i]==(PROMISE_RMW|PROMISE_FULFILLED))
137                         rmw_count++;
138         }
139         
140         for (unsigned int i = 0; i < promises.size(); i++) {
141                 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
142                         if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
143                                 //sending our value to two rmws... not going to work..try next combination
144                                 continue;
145                         }
146                         promises[i] = (promises[i] & PROMISE_RMW) |PROMISE_FULFILLED;
147                         while (i > 0) {
148                                 i--;
149                                 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
150                                         promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
151                         }
152                         return true;
153                 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
154                         rmw_count--;
155                 }
156         }
157         return false;
158 }
159
160 /**
161  * Returns whether the promise set is empty.
162  * @return true if we have explored all promise combinations.
163  */
164 bool Node::promise_empty() const
165 {
166         bool fulfilledrmw=false;
167         for (int i = promises.size()-1 ; i>=0; i--) {
168                 if (promises[i]==PROMISE_UNFULFILLED)
169                         return false;
170                 if (!fulfilledrmw && ((promises[i]&PROMISE_MASK)==PROMISE_UNFULFILLED))
171                         return false;
172                 if (promises[i]==(PROMISE_FULFILLED|PROMISE_RMW))
173                         fulfilledrmw=true;
174         }
175         return true;
176 }
177
178
179 void Node::set_misc_max(int i) {
180         misc_max=i;
181 }
182
183 int Node::get_misc() const
184 {
185         return misc_index;
186 }
187
188 bool Node::increment_misc() {
189         return (misc_index<misc_max)&&((++misc_index)<misc_max);
190 }
191
192 bool Node::misc_empty() const
193 {
194         return (misc_index+1)>=misc_max;
195 }
196
197
198 /**
199  * Adds a value from a weakly ordered future write to backtrack to. This
200  * operation may "fail" if the future value has already been run (within some
201  * sloppiness window of this expiration), or if the futurevalues set has
202  * reached its maximum.
203  * @see model_params.maxfuturevalues
204  *
205  * @param value is the value to backtrack to.
206  * @return True if the future value was successully added; false otherwise
207  */
208 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
209         int idx = -1; /* Highest index where value is found */
210         for (unsigned int i = 0; i < future_values.size(); i++) {
211                 if (future_values[i].value == value) {
212                         if (expiration <= future_values[i].expiration)
213                                 return false;
214                         idx = i;
215                 }
216         }
217         if (idx > future_index) {
218                 /* Future value hasn't been explored; update expiration */
219                 future_values[idx].expiration = expiration;
220                 return true;
221         } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
222                 /* Future value has been explored and is within the "sloppy" window */
223                 return false;
224         }
225
226         /* Limit the size of the future-values set */
227         if (model->params.maxfuturevalues > 0 &&
228                         (int)future_values.size() >= model->params.maxfuturevalues)
229                 return false;
230
231         struct future_value newfv = {value, expiration};
232         future_values.push_back(newfv);
233         return true;
234 }
235
236 /**
237  * Checks whether the future_values set for this node is empty.
238  * @return true if the future_values set is empty.
239  */
240 bool Node::future_value_empty() const
241 {
242         return ((future_index + 1) >= ((int)future_values.size()));
243 }
244
245 /**
246  * Checks if the Thread associated with this thread ID has been explored from
247  * this Node already.
248  * @param tid is the thread ID to check
249  * @return true if this thread choice has been explored already, false
250  * otherwise
251  */
252 bool Node::has_been_explored(thread_id_t tid) const
253 {
254         int id = id_to_int(tid);
255         return explored_children[id];
256 }
257
258 /**
259  * Checks if the backtracking set is empty.
260  * @return true if the backtracking set is empty
261  */
262 bool Node::backtrack_empty() const
263 {
264         return (numBacktracks == 0);
265 }
266
267 /**
268  * Checks whether the readsfrom set for this node is empty.
269  * @return true if the readsfrom set is empty.
270  */
271 bool Node::read_from_empty() const
272 {
273         return ((read_from_index+1) >= may_read_from.size());
274 }
275
276 /**
277  * Mark the appropriate backtracking information for exploring a thread choice.
278  * @param act The ModelAction to explore
279  */
280 void Node::explore_child(ModelAction *act, enabled_type_t * is_enabled)
281 {
282         if ( ! enabled_array )
283                 enabled_array=(enabled_type_t *)model_malloc(sizeof(enabled_type_t)*num_threads);
284         if (is_enabled != NULL)
285                 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t)*num_threads);
286         else {
287                 for(int i=0;i<num_threads;i++)
288                         enabled_array[i]=THREAD_DISABLED;
289         }
290
291         explore(act->get_tid());
292 }
293
294 /**
295  * Records a backtracking reference for a thread choice within this Node.
296  * Provides feedback as to whether this thread choice is already set for
297  * backtracking.
298  * @return false if the thread was already set to be backtracked, true
299  * otherwise
300  */
301 bool Node::set_backtrack(thread_id_t id)
302 {
303         int i = id_to_int(id);
304         ASSERT(i<((int)backtrack.size()));
305         if (backtrack[i])
306                 return false;
307         backtrack[i] = true;
308         numBacktracks++;
309         return true;
310 }
311
312 thread_id_t Node::get_next_backtrack()
313 {
314         /** @todo Find next backtrack */
315         unsigned int i;
316         for (i = 0; i < backtrack.size(); i++)
317                 if (backtrack[i] == true)
318                         break;
319         /* Backtrack set was empty? */
320         ASSERT(i != backtrack.size());
321
322         backtrack[i] = false;
323         numBacktracks--;
324         return int_to_id(i);
325 }
326
327 bool Node::is_enabled(Thread *t) const
328 {
329         int thread_id=id_to_int(t->get_id());
330         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
331 }
332
333 enabled_type_t Node::enabled_status(thread_id_t tid) const
334 {
335         int thread_id = id_to_int(tid);
336         if (thread_id < num_threads)
337                 return enabled_array[thread_id];
338         else
339                 return THREAD_DISABLED;
340 }
341
342 bool Node::is_enabled(thread_id_t tid) const
343 {
344         int thread_id=id_to_int(tid);
345         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
346 }
347
348 bool Node::has_priority(thread_id_t tid) const
349 {
350         return fairness[id_to_int(tid)].priority;
351 }
352
353 /**
354  * Add an action to the may_read_from set.
355  * @param act is the action to add
356  */
357 void Node::add_read_from(const ModelAction *act)
358 {
359         may_read_from.push_back(act);
360 }
361
362 /**
363  * Gets the next 'future_value' value from this Node. Only valid for a node
364  * where this->action is a 'read'.
365  * @return The first element in future_values
366  */
367 uint64_t Node::get_future_value() const
368 {
369         ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
370         return future_values[future_index].value;
371 }
372
373 modelclock_t Node::get_future_value_expiration() const
374 {
375         ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
376         return future_values[future_index].expiration;
377 }
378
379
380 int Node::get_read_from_size() const
381 {
382         return may_read_from.size();
383 }
384
385 const ModelAction * Node::get_read_from_at(int i) {
386         return may_read_from[i];
387 }
388
389 /**
390  * Gets the next 'may_read_from' action from this Node. Only valid for a node
391  * where this->action is a 'read'.
392  * @return The first element in may_read_from
393  */
394 const ModelAction * Node::get_read_from() const
395 {
396         if (read_from_index < may_read_from.size())
397                 return may_read_from[read_from_index];
398         else
399                 return NULL;
400 }
401
402 /**
403  * Increments the index into the readsfrom set to explore the next item.
404  * @return Returns false if we have explored all items.
405  */
406 bool Node::increment_read_from() {
407         DBG();
408         promises.clear();
409         if (read_from_index < may_read_from.size()) {
410                 read_from_index++;
411                 return read_from_index < may_read_from.size();
412         }
413         return false;
414 }
415
416 /**
417  * Increments the index into the future_values set to explore the next item.
418  * @return Returns false if we have explored all values.
419  */
420 bool Node::increment_future_value() {
421         DBG();
422         promises.clear();
423         if (future_index < ((int)future_values.size())) {
424                 future_index++;
425                 return (future_index < ((int)future_values.size()));
426         }
427         return false;
428 }
429
430 /**
431  * Add a write ModelAction to the set of writes that may break the release
432  * sequence. This is used during replay exploration of pending release
433  * sequences. This Node must correspond to a release sequence fixup action.
434  *
435  * @param write The write that may break the release sequence. NULL means we
436  * allow the release sequence to synchronize.
437  */
438 void Node::add_relseq_break(const ModelAction *write)
439 {
440         relseq_break_writes.push_back(write);
441 }
442
443 /**
444  * Get the write that may break the current pending release sequence,
445  * according to the replay / divergence pattern.
446  *
447  * @return A write that may break the release sequence. If NULL, that means
448  * the release sequence should not be broken.
449  */
450 const ModelAction * Node::get_relseq_break() const
451 {
452         if (relseq_break_index < (int)relseq_break_writes.size())
453                 return relseq_break_writes[relseq_break_index];
454         else
455                 return NULL;
456 }
457
458 /**
459  * Increments the index into the relseq_break_writes set to explore the next
460  * item.
461  * @return Returns false if we have explored all values.
462  */
463 bool Node::increment_relseq_break()
464 {
465         DBG();
466         promises.clear();
467         if (relseq_break_index < ((int)relseq_break_writes.size())) {
468                 relseq_break_index++;
469                 return (relseq_break_index < ((int)relseq_break_writes.size()));
470         }
471         return false;
472 }
473
474 /**
475  * @return True if all writes that may break the release sequence have been
476  * explored
477  */
478 bool Node::relseq_break_empty() const
479 {
480         return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
481 }
482
483 void Node::explore(thread_id_t tid)
484 {
485         int i = id_to_int(tid);
486         ASSERT(i<((int)backtrack.size()));
487         if (backtrack[i]) {
488                 backtrack[i] = false;
489                 numBacktracks--;
490         }
491         explored_children[i] = true;
492 }
493
494 NodeStack::NodeStack() :
495         node_list(1, new Node()),
496         iter(0),
497         total_nodes(0)
498 {
499         total_nodes++;
500 }
501
502 NodeStack::~NodeStack()
503 {
504         for (unsigned int i = 0; i < node_list.size(); i++)
505                 delete node_list[i];
506 }
507
508 void NodeStack::print() const
509 {
510         model_print("............................................\n");
511         model_print("NodeStack printing node_list:\n");
512         for (unsigned int it = 0; it < node_list.size(); it++) {
513                 if (it == this->iter)
514                         model_print("vvv following action is the current iterator vvv\n");
515                 node_list[it]->print();
516         }
517         model_print("............................................\n");
518 }
519
520 /** Note: The is_enabled set contains what actions were enabled when
521  *  act was chosen. */
522
523 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
524 {
525         DBG();
526
527         ASSERT(!node_list.empty());
528
529         if ((iter+1) < node_list.size()) {
530                 iter++;
531                 return node_list[iter]->get_action();
532         }
533
534         /* Record action */
535         get_head()->explore_child(act, is_enabled);
536         Node *prevfairness = NULL;
537         if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
538                 prevfairness = node_list[iter-model->params.fairwindow];
539         }
540         node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
541         total_nodes++;
542         iter++;
543         return NULL;
544 }
545
546 /**
547  * Empties the stack of all trailing nodes after a given position and calls the
548  * destructor for each. This function is provided an offset which determines
549  * how many nodes (relative to the current replay state) to save before popping
550  * the stack.
551  * @param numAhead gives the number of Nodes (including this Node) to skip over
552  * before removing nodes.
553  */
554 void NodeStack::pop_restofstack(int numAhead)
555 {
556         /* Diverging from previous execution; clear out remainder of list */
557         unsigned int it=iter+numAhead;
558         for(unsigned int i=it;i<node_list.size();i++)
559                 delete node_list[i];
560         node_list.resize(it);
561 }
562
563 Node * NodeStack::get_head() const
564 {
565         if (node_list.empty())
566                 return NULL;
567         return node_list[iter];
568 }
569
570 Node * NodeStack::get_next() const
571 {
572         if (node_list.empty()) {
573                 DEBUG("Empty\n");
574                 return NULL;
575         }
576         unsigned int it=iter+1;
577         if (it == node_list.size()) {
578                 DEBUG("At end\n");
579                 return NULL;
580         }
581         return node_list[it];
582 }
583
584 void NodeStack::reset_execution()
585 {
586         iter = 0;
587 }