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