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