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