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