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