66869ab12b2f2c0e17ba7b7c740e62391ba0b99a
[c11tester.git] / nodestack.cc
1 #define __STDC_FORMAT_MACROS
2 #include <inttypes.h>
3
4 #include <string.h>
5
6 #include "nodestack.h"
7 #include "action.h"
8 #include "common.h"
9 #include "model.h"
10 #include "threads-model.h"
11 #include "modeltypes.h"
12
13 /**
14  * @brief Node constructor
15  *
16  * Constructs a single Node for use in a NodeStack. Each Node is associated
17  * with exactly one ModelAction (exception: the first Node should be created
18  * as an empty stub, to represent the first thread "choice") and up to one
19  * parent.
20  *
21  * @param act The ModelAction to associate with this Node. May be NULL.
22  * @param par The parent Node in the NodeStack. May be NULL if there is no
23  * parent.
24  * @param nthreads The number of threads which exist at this point in the
25  * execution trace.
26  */
27 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness) :
28         read_from_status(READ_FROM_PAST),
29         action(act),
30         parent(par),
31         num_threads(nthreads),
32         explored_children(num_threads),
33         backtrack(num_threads),
34         fairness(num_threads),
35         numBacktracks(0),
36         enabled_array(NULL),
37         read_from_past(),
38         read_from_past_idx(0),
39         read_from_promises(),
40         read_from_promise_idx(-1),
41         future_values(),
42         future_index(-1),
43         relseq_break_writes(),
44         relseq_break_index(0),
45         misc_index(0),
46         misc_max(0)
47 {
48         ASSERT(act);
49         act->set_node(this);
50         int currtid = id_to_int(act->get_tid());
51         int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
52
53         if (model->params.fairwindow != 0) {
54                 for (int i = 0; i < num_threads; i++) {
55                         ASSERT(i < ((int)fairness.size()));
56                         struct fairness_info *fi = &fairness[i];
57                         struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
58                         if (prevfi) {
59                                 *fi = *prevfi;
60                         }
61                         if (parent && parent->is_enabled(int_to_id(i))) {
62                                 fi->enabled_count++;
63                         }
64                         if (i == currtid) {
65                                 fi->turns++;
66                                 fi->priority = false;
67                         }
68                         /* Do window processing */
69                         if (prevfairness != NULL) {
70                                 if (prevfairness->parent->is_enabled(int_to_id(i)))
71                                         fi->enabled_count--;
72                                 if (i == prevtid) {
73                                         fi->turns--;
74                                 }
75                                 /* Need full window to start evaluating
76                                  * conditions
77                                  * If we meet the enabled count and have no
78                                  * turns, give us priority */
79                                 if ((fi->enabled_count >= model->params.enabledcount) &&
80                                                 (fi->turns == 0))
81                                         fi->priority = true;
82                         }
83                 }
84         }
85 }
86
87 /** @brief Node desctructor */
88 Node::~Node()
89 {
90         delete action;
91         if (enabled_array)
92                 model_free(enabled_array);
93 }
94
95 /** Prints debugging info for the ModelAction associated with this Node */
96 void Node::print() const
97 {
98         action->print();
99         model_print("          backtrack: %s", backtrack_empty() ? "empty" : "non-empty ");
100         for (int i = 0; i < (int)backtrack.size(); i++)
101                 if (backtrack[i] == true)
102                         model_print("[%d]", i);
103         model_print("\n");
104
105         model_print("          read from past: %s", read_from_past_empty() ? "empty" : "non-empty ");
106         for (int i = read_from_past_idx + 1; i < (int)read_from_past.size(); i++)
107                 model_print("[%d]", read_from_past[i]->get_seq_number());
108         model_print("\n");
109
110         model_print("          read-from promises: %s", read_from_promise_empty() ? "empty" : "non-empty ");
111         for (int i = read_from_promise_idx + 1; i < (int)read_from_promises.size(); i++)
112                 model_print("[%d]", read_from_promises[i]->get_seq_number());
113         model_print("\n");
114
115         model_print("          future values: %s", future_value_empty() ? "empty" : "non-empty ");
116         for (int i = future_index + 1; i < (int)future_values.size(); i++)
117                 model_print("[%#" PRIx64 "]", future_values[i].value);
118         model_print("\n");
119
120         model_print("          promises: %s\n", promise_empty() ? "empty" : "non-empty");
121         model_print("          misc: %s\n", misc_empty() ? "empty" : "non-empty");
122         model_print("          rel seq break: %s\n", relseq_break_empty() ? "empty" : "non-empty");
123 }
124
125 /**
126  * Sets a promise to explore meeting with the given node.
127  * @param i is the promise index.
128  */
129 void Node::set_promise(unsigned int i, bool is_rmw)
130 {
131         if (i >= promises.size())
132                 promises.resize(i + 1, PROMISE_IGNORE);
133         if (promises[i] == PROMISE_IGNORE) {
134                 promises[i] = PROMISE_UNFULFILLED;
135                 if (is_rmw)
136                         promises[i] |= PROMISE_RMW;
137         }
138 }
139
140 /**
141  * Looks up whether a given promise should be satisfied by this node.
142  * @param i The promise index.
143  * @return true if the promise should be satisfied by the given model action.
144  */
145 bool Node::get_promise(unsigned int i) const
146 {
147         return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
148 }
149
150 /**
151  * Increments to the next combination of promises.
152  * @return true if we have a valid combination.
153  */
154 bool Node::increment_promise()
155 {
156         DBG();
157         unsigned int rmw_count = 0;
158         for (unsigned int i = 0; i < promises.size(); i++) {
159                 if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED))
160                         rmw_count++;
161         }
162
163         for (unsigned int i = 0; i < promises.size(); i++) {
164                 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
165                         if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
166                                 //sending our value to two rmws... not going to work..try next combination
167                                 continue;
168                         }
169                         promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_FULFILLED;
170                         while (i > 0) {
171                                 i--;
172                                 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
173                                         promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
174                         }
175                         return true;
176                 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
177                         rmw_count--;
178                 }
179         }
180         return false;
181 }
182
183 /**
184  * Returns whether the promise set is empty.
185  * @return true if we have explored all promise combinations.
186  */
187 bool Node::promise_empty() const
188 {
189         bool fulfilledrmw = false;
190         for (int i = promises.size() - 1; i >= 0; i--) {
191                 if (promises[i] == PROMISE_UNFULFILLED)
192                         return false;
193                 if (!fulfilledrmw && ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED))
194                         return false;
195                 if (promises[i] == (PROMISE_FULFILLED | PROMISE_RMW))
196                         fulfilledrmw = true;
197         }
198         return true;
199 }
200
201 void Node::set_misc_max(int i)
202 {
203         misc_max = i;
204 }
205
206 int Node::get_misc() const
207 {
208         return misc_index;
209 }
210
211 bool Node::increment_misc()
212 {
213         return (misc_index < misc_max) && ((++misc_index) < misc_max);
214 }
215
216 bool Node::misc_empty() const
217 {
218         return (misc_index + 1) >= misc_max;
219 }
220
221 /**
222  * Checks if the Thread associated with this thread ID has been explored from
223  * this Node already.
224  * @param tid is the thread ID to check
225  * @return true if this thread choice has been explored already, false
226  * otherwise
227  */
228 bool Node::has_been_explored(thread_id_t tid) const
229 {
230         int id = id_to_int(tid);
231         return explored_children[id];
232 }
233
234 /**
235  * Checks if the backtracking set is empty.
236  * @return true if the backtracking set is empty
237  */
238 bool Node::backtrack_empty() const
239 {
240         return (numBacktracks == 0);
241 }
242
243 /**
244  * Mark the appropriate backtracking information for exploring a thread choice.
245  * @param act The ModelAction to explore
246  */
247 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
248 {
249         if (!enabled_array)
250                 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
251         if (is_enabled != NULL)
252                 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
253         else {
254                 for (int i = 0; i < num_threads; i++)
255                         enabled_array[i] = THREAD_DISABLED;
256         }
257
258         explore(act->get_tid());
259 }
260
261 /**
262  * Records a backtracking reference for a thread choice within this Node.
263  * Provides feedback as to whether this thread choice is already set for
264  * backtracking.
265  * @return false if the thread was already set to be backtracked, true
266  * otherwise
267  */
268 bool Node::set_backtrack(thread_id_t id)
269 {
270         int i = id_to_int(id);
271         ASSERT(i < ((int)backtrack.size()));
272         if (backtrack[i])
273                 return false;
274         backtrack[i] = true;
275         numBacktracks++;
276         return true;
277 }
278
279 thread_id_t Node::get_next_backtrack()
280 {
281         /** @todo Find next backtrack */
282         unsigned int i;
283         for (i = 0; i < backtrack.size(); i++)
284                 if (backtrack[i] == true)
285                         break;
286         /* Backtrack set was empty? */
287         ASSERT(i != backtrack.size());
288
289         backtrack[i] = false;
290         numBacktracks--;
291         return int_to_id(i);
292 }
293
294 void Node::clear_backtracking()
295 {
296         for (unsigned int i = 0; i < backtrack.size(); i++)
297                 backtrack[i] = false;
298         for (unsigned int i = 0; i < explored_children.size(); i++)
299                 explored_children[i] = false;
300 }
301
302 bool Node::is_enabled(Thread *t) const
303 {
304         int thread_id = id_to_int(t->get_id());
305         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
306 }
307
308 enabled_type_t Node::enabled_status(thread_id_t tid) const
309 {
310         int thread_id = id_to_int(tid);
311         if (thread_id < num_threads)
312                 return enabled_array[thread_id];
313         else
314                 return THREAD_DISABLED;
315 }
316
317 bool Node::is_enabled(thread_id_t tid) const
318 {
319         int thread_id = id_to_int(tid);
320         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
321 }
322
323 bool Node::has_priority(thread_id_t tid) const
324 {
325         return fairness[id_to_int(tid)].priority;
326 }
327
328 /*********************************** read from ********************************/
329
330 /**
331  * Get the current state of the may-read-from set iteration
332  * @return The read-from type we should currently be checking (past or future)
333  */
334 read_from_type_t Node::get_read_from_status()
335 {
336         if (read_from_status == READ_FROM_PAST && read_from_past.empty())
337                 increment_read_from();
338         return read_from_status;
339 }
340
341 /**
342  * Iterate one step in the may-read-from iteration. This includes a step in
343  * reading from the either the past or the future.
344  * @return True if there is a new read-from to explore; false otherwise
345  */
346 bool Node::increment_read_from()
347 {
348         promises.clear();
349         if (increment_read_from_past()) {
350                read_from_status = READ_FROM_PAST;
351                return true;
352         } else if (increment_read_from_promise()) {
353                 read_from_status = READ_FROM_PROMISE;
354                 return true;
355         } else if (increment_future_value()) {
356                 read_from_status = READ_FROM_FUTURE;
357                 return true;
358         }
359         read_from_status = READ_FROM_NONE;
360         return false;
361 }
362
363 /**
364  * @return True if there are any new read-froms to explore
365  */
366 bool Node::read_from_empty() const
367 {
368         return read_from_past_empty() &&
369                 read_from_promise_empty() &&
370                 future_value_empty();
371 }
372
373 /**
374  * Get the total size of the may-read-from set, including both past and future
375  * values
376  * @return The size of may-read-from
377  */
378 unsigned int Node::read_from_size() const
379 {
380         return read_from_past.size() +
381                 read_from_promises.size() +
382                 future_values.size();
383 }
384
385 /******************************* end read from ********************************/
386
387 /****************************** read from past ********************************/
388
389 /** @brief Prints info about read_from_past set */
390 void Node::print_read_from_past()
391 {
392         for (unsigned int i = 0; i < read_from_past.size(); i++)
393                 read_from_past[i]->print();
394 }
395
396 /**
397  * Add an action to the read_from_past set.
398  * @param act is the action to add
399  */
400 void Node::add_read_from_past(const ModelAction *act)
401 {
402         read_from_past.push_back(act);
403 }
404
405 /**
406  * Gets the next 'read_from_past' action from this Node. Only valid for a node
407  * where this->action is a 'read'.
408  * @return The first element in read_from_past
409  */
410 const ModelAction * Node::get_read_from_past() const
411 {
412         if (read_from_past_idx < read_from_past.size())
413                 return read_from_past[read_from_past_idx];
414         else
415                 return NULL;
416 }
417
418 const ModelAction * Node::get_read_from_past(int i) const
419 {
420         return read_from_past[i];
421 }
422
423 int Node::get_read_from_past_size() const
424 {
425         return read_from_past.size();
426 }
427
428 /**
429  * Checks whether the readsfrom set for this node is empty.
430  * @return true if the readsfrom set is empty.
431  */
432 bool Node::read_from_past_empty() const
433 {
434         return ((read_from_past_idx + 1) >= read_from_past.size());
435 }
436
437 /**
438  * Increments the index into the readsfrom set to explore the next item.
439  * @return Returns false if we have explored all items.
440  */
441 bool Node::increment_read_from_past()
442 {
443         DBG();
444         if (read_from_past_idx < read_from_past.size()) {
445                 read_from_past_idx++;
446                 return read_from_past_idx < read_from_past.size();
447         }
448         return false;
449 }
450
451 /************************** end read from past ********************************/
452
453 /***************************** read_from_promises *****************************/
454
455 /**
456  * Add an action to the read_from_promises set.
457  * @param reader The read which generated the Promise; we use the ModelAction
458  * instead of the Promise because the Promise does not last across executions
459  */
460 void Node::add_read_from_promise(const ModelAction *reader)
461 {
462         read_from_promises.push_back(reader);
463 }
464
465 /**
466  * Gets the next 'read-from-promise' from this Node. Only valid for a node
467  * where this->action is a 'read'.
468  * @return The current element in read_from_promises
469  */
470 const Promise * Node::get_read_from_promise() const
471 {
472         if (read_from_promise_idx < 0 || read_from_promise_idx >= ((int)read_from_promises.size()))
473                 return NULL;
474         return read_from_promises[read_from_promise_idx]->get_reads_from_promise();
475 }
476
477 /**
478  * Checks whether the read_from_promises set for this node is empty.
479  * @return true if the read_from_promises set is empty.
480  */
481 bool Node::read_from_promise_empty() const
482 {
483         return ((read_from_promise_idx + 1) >= ((int)read_from_promises.size()));
484 }
485
486 /**
487  * Increments the index into the read_from_promises set to explore the next item.
488  * @return Returns false if we have explored all promises.
489  */
490 bool Node::increment_read_from_promise()
491 {
492         DBG();
493         if (read_from_promise_idx < ((int)read_from_promises.size())) {
494                 read_from_promise_idx++;
495                 return (read_from_promise_idx < ((int)read_from_promises.size()));
496         }
497         return false;
498 }
499
500 /************************* end read_from_promises *****************************/
501
502 /****************************** future values *********************************/
503
504 /**
505  * Adds a value from a weakly ordered future write to backtrack to. This
506  * operation may "fail" if the future value has already been run (within some
507  * sloppiness window of this expiration), or if the futurevalues set has
508  * reached its maximum.
509  * @see model_params.maxfuturevalues
510  *
511  * @param value is the value to backtrack to.
512  * @return True if the future value was successully added; false otherwise
513  */
514 bool Node::add_future_value(struct future_value fv)
515 {
516         uint64_t value = fv.value;
517         modelclock_t expiration = fv.expiration;
518         thread_id_t tid = fv.tid;
519         int idx = -1; /* Highest index where value is found */
520         for (unsigned int i = 0; i < future_values.size(); i++) {
521                 if (future_values[i].value == value && future_values[i].tid == tid) {
522                         if (expiration <= future_values[i].expiration)
523                                 return false;
524                         idx = i;
525                 }
526         }
527         if (idx > future_index) {
528                 /* Future value hasn't been explored; update expiration */
529                 future_values[idx].expiration = expiration;
530                 return true;
531         } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
532                 /* Future value has been explored and is within the "sloppy" window */
533                 return false;
534         }
535
536         /* Limit the size of the future-values set */
537         if (model->params.maxfuturevalues > 0 &&
538                         (int)future_values.size() >= model->params.maxfuturevalues)
539                 return false;
540
541         future_values.push_back(fv);
542         return true;
543 }
544
545 /**
546  * Gets the next 'future_value' from this Node. Only valid for a node where
547  * this->action is a 'read'.
548  * @return The first element in future_values
549  */
550 struct future_value Node::get_future_value() const
551 {
552         ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
553         return future_values[future_index];
554 }
555
556 /**
557  * Checks whether the future_values set for this node is empty.
558  * @return true if the future_values set is empty.
559  */
560 bool Node::future_value_empty() const
561 {
562         return ((future_index + 1) >= ((int)future_values.size()));
563 }
564
565 /**
566  * Increments the index into the future_values set to explore the next item.
567  * @return Returns false if we have explored all values.
568  */
569 bool Node::increment_future_value()
570 {
571         DBG();
572         if (future_index < ((int)future_values.size())) {
573                 future_index++;
574                 return (future_index < ((int)future_values.size()));
575         }
576         return false;
577 }
578
579 /************************** end future values *********************************/
580
581 /**
582  * Add a write ModelAction to the set of writes that may break the release
583  * sequence. This is used during replay exploration of pending release
584  * sequences. This Node must correspond to a release sequence fixup action.
585  *
586  * @param write The write that may break the release sequence. NULL means we
587  * allow the release sequence to synchronize.
588  */
589 void Node::add_relseq_break(const ModelAction *write)
590 {
591         relseq_break_writes.push_back(write);
592 }
593
594 /**
595  * Get the write that may break the current pending release sequence,
596  * according to the replay / divergence pattern.
597  *
598  * @return A write that may break the release sequence. If NULL, that means
599  * the release sequence should not be broken.
600  */
601 const ModelAction * Node::get_relseq_break() const
602 {
603         if (relseq_break_index < (int)relseq_break_writes.size())
604                 return relseq_break_writes[relseq_break_index];
605         else
606                 return NULL;
607 }
608
609 /**
610  * Increments the index into the relseq_break_writes set to explore the next
611  * item.
612  * @return Returns false if we have explored all values.
613  */
614 bool Node::increment_relseq_break()
615 {
616         DBG();
617         promises.clear();
618         if (relseq_break_index < ((int)relseq_break_writes.size())) {
619                 relseq_break_index++;
620                 return (relseq_break_index < ((int)relseq_break_writes.size()));
621         }
622         return false;
623 }
624
625 /**
626  * @return True if all writes that may break the release sequence have been
627  * explored
628  */
629 bool Node::relseq_break_empty() const
630 {
631         return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
632 }
633
634 void Node::explore(thread_id_t tid)
635 {
636         int i = id_to_int(tid);
637         ASSERT(i < ((int)backtrack.size()));
638         if (backtrack[i]) {
639                 backtrack[i] = false;
640                 numBacktracks--;
641         }
642         explored_children[i] = true;
643 }
644
645 NodeStack::NodeStack() :
646         node_list(),
647         head_idx(-1),
648         total_nodes(0)
649 {
650         total_nodes++;
651 }
652
653 NodeStack::~NodeStack()
654 {
655         for (unsigned int i = 0; i < node_list.size(); i++)
656                 delete node_list[i];
657 }
658
659 void NodeStack::print() const
660 {
661         model_print("............................................\n");
662         model_print("NodeStack printing node_list:\n");
663         for (unsigned int it = 0; it < node_list.size(); it++) {
664                 if ((int)it == this->head_idx)
665                         model_print("vvv following action is the current iterator vvv\n");
666                 node_list[it]->print();
667         }
668         model_print("............................................\n");
669 }
670
671 /** Note: The is_enabled set contains what actions were enabled when
672  *  act was chosen. */
673 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
674 {
675         DBG();
676
677         if ((head_idx + 1) < (int)node_list.size()) {
678                 head_idx++;
679                 return node_list[head_idx]->get_action();
680         }
681
682         /* Record action */
683         Node *head = get_head();
684         Node *prevfairness = NULL;
685         if (head) {
686                 head->explore_child(act, is_enabled);
687                 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
688                         prevfairness = node_list[head_idx - model->params.fairwindow];
689         }
690
691         int next_threads = model->get_num_threads();
692         if (act->get_type() == THREAD_CREATE)
693                 next_threads++;
694         node_list.push_back(new Node(act, head, next_threads, prevfairness));
695         total_nodes++;
696         head_idx++;
697         return NULL;
698 }
699
700 /**
701  * Empties the stack of all trailing nodes after a given position and calls the
702  * destructor for each. This function is provided an offset which determines
703  * how many nodes (relative to the current replay state) to save before popping
704  * the stack.
705  * @param numAhead gives the number of Nodes (including this Node) to skip over
706  * before removing nodes.
707  */
708 void NodeStack::pop_restofstack(int numAhead)
709 {
710         /* Diverging from previous execution; clear out remainder of list */
711         unsigned int it = head_idx + numAhead;
712         for (unsigned int i = it; i < node_list.size(); i++)
713                 delete node_list[i];
714         node_list.resize(it);
715         node_list.back()->clear_backtracking();
716 }
717
718 Node * NodeStack::get_head() const
719 {
720         if (node_list.empty() || head_idx < 0)
721                 return NULL;
722         return node_list[head_idx];
723 }
724
725 Node * NodeStack::get_next() const
726 {
727         if (node_list.empty()) {
728                 DEBUG("Empty\n");
729                 return NULL;
730         }
731         unsigned int it = head_idx + 1;
732         if (it == node_list.size()) {
733                 DEBUG("At end\n");
734                 return NULL;
735         }
736         return node_list[it];
737 }
738
739 void NodeStack::reset_execution()
740 {
741         head_idx = -1;
742 }