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