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