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