1 #define __STDC_FORMAT_MACROS
10 #include "threads-model.h"
11 #include "modeltypes.h"
14 * @brief Node constructor
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
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
24 * @param nthreads The number of threads which exist at this point in the
27 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness) :
28 read_from_status(READ_FROM_PAST),
31 num_threads(nthreads),
32 explored_children(num_threads),
33 backtrack(num_threads),
34 fairness(num_threads),
38 read_from_past_idx(0),
40 read_from_promise_idx(-1),
43 relseq_break_writes(),
44 relseq_break_index(0),
50 int currtid = id_to_int(act->get_tid());
51 int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
53 if (model->params.fairwindow != 0) {
54 for (int i = 0; i < num_threads; i++) {
55 ASSERT(i < ((int)fairness.size()));
56 struct fairness_info *fi = &fairness[i];
57 struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
61 if (parent && parent->is_enabled(int_to_id(i))) {
68 /* Do window processing */
69 if (prevfairness != NULL) {
70 if (prevfairness->parent->is_enabled(int_to_id(i)))
75 /* Need full window to start evaluating
77 * If we meet the enabled count and have no
78 * turns, give us priority */
79 if ((fi->enabled_count >= model->params.enabledcount) &&
87 /** @brief Node desctructor */
92 model_free(enabled_array);
95 /** Prints debugging info for the ModelAction associated with this Node */
96 void Node::print() const
99 model_print(" backtrack: %s", backtrack_empty() ? "empty" : "non-empty ");
100 for (int i = 0; i < (int)backtrack.size(); i++)
101 if (backtrack[i] == true)
102 model_print("[%d]", i);
105 model_print(" read from past: %s", read_from_past_empty() ? "empty" : "non-empty ");
106 for (int i = read_from_past_idx + 1; i < (int)read_from_past.size(); i++)
107 model_print("[%d]", read_from_past[i]->get_seq_number());
110 model_print(" read-from promises: %s", read_from_promise_empty() ? "empty" : "non-empty ");
111 for (int i = read_from_promise_idx + 1; i < (int)read_from_promises.size(); i++)
112 model_print("[%d]", read_from_promises[i]->get_seq_number());
115 model_print(" future values: %s", future_value_empty() ? "empty" : "non-empty ");
116 for (int i = future_index + 1; i < (int)future_values.size(); i++)
117 model_print("[%#" PRIx64 "]", future_values[i].value);
120 model_print(" promises: %s\n", promise_empty() ? "empty" : "non-empty");
121 model_print(" misc: %s\n", misc_empty() ? "empty" : "non-empty");
122 model_print(" rel seq break: %s\n", relseq_break_empty() ? "empty" : "non-empty");
126 * Sets a promise to explore meeting with the given node.
127 * @param i is the promise index.
129 void Node::set_promise(unsigned int i, bool is_rmw)
131 if (i >= promises.size())
132 promises.resize(i + 1, PROMISE_IGNORE);
133 if (promises[i] == PROMISE_IGNORE) {
134 promises[i] = PROMISE_UNFULFILLED;
136 promises[i] |= PROMISE_RMW;
141 * Looks up whether a given promise should be satisfied by this node.
142 * @param i The promise index.
143 * @return true if the promise should be satisfied by the given model action.
145 bool Node::get_promise(unsigned int i) const
147 return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
151 * Increments to the next combination of promises.
152 * @return true if we have a valid combination.
154 bool Node::increment_promise()
157 unsigned int rmw_count = 0;
158 for (unsigned int i = 0; i < promises.size(); i++) {
159 if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED))
163 for (unsigned int i = 0; i < promises.size(); i++) {
164 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
165 if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
166 //sending our value to two rmws... not going to work..try next combination
169 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_FULFILLED;
172 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
173 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
176 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
184 * Returns whether the promise set is empty.
185 * @return true if we have explored all promise combinations.
187 bool Node::promise_empty() const
189 bool fulfilledrmw = false;
190 for (int i = promises.size() - 1; i >= 0; i--) {
191 if (promises[i] == PROMISE_UNFULFILLED)
193 if (!fulfilledrmw && ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED))
195 if (promises[i] == (PROMISE_FULFILLED | PROMISE_RMW))
201 void Node::set_misc_max(int i)
206 int Node::get_misc() const
211 bool Node::increment_misc()
213 return (misc_index < misc_max) && ((++misc_index) < misc_max);
216 bool Node::misc_empty() const
218 return (misc_index + 1) >= misc_max;
222 * Checks if the Thread associated with this thread ID has been explored from
224 * @param tid is the thread ID to check
225 * @return true if this thread choice has been explored already, false
228 bool Node::has_been_explored(thread_id_t tid) const
230 int id = id_to_int(tid);
231 return explored_children[id];
235 * Checks if the backtracking set is empty.
236 * @return true if the backtracking set is empty
238 bool Node::backtrack_empty() const
240 return (numBacktracks == 0);
244 * Mark the appropriate backtracking information for exploring a thread choice.
245 * @param act The ModelAction to explore
247 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
250 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
251 if (is_enabled != NULL)
252 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
254 for (int i = 0; i < num_threads; i++)
255 enabled_array[i] = THREAD_DISABLED;
258 explore(act->get_tid());
262 * Records a backtracking reference for a thread choice within this Node.
263 * Provides feedback as to whether this thread choice is already set for
265 * @return false if the thread was already set to be backtracked, true
268 bool Node::set_backtrack(thread_id_t id)
270 int i = id_to_int(id);
271 ASSERT(i < ((int)backtrack.size()));
279 thread_id_t Node::get_next_backtrack()
281 /** @todo Find next backtrack */
283 for (i = 0; i < backtrack.size(); i++)
284 if (backtrack[i] == true)
286 /* Backtrack set was empty? */
287 ASSERT(i != backtrack.size());
289 backtrack[i] = false;
294 void Node::clear_backtracking()
296 for (unsigned int i = 0; i < backtrack.size(); i++)
297 backtrack[i] = false;
298 for (unsigned int i = 0; i < explored_children.size(); i++)
299 explored_children[i] = false;
302 bool Node::is_enabled(Thread *t) const
304 int thread_id = id_to_int(t->get_id());
305 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
308 enabled_type_t Node::enabled_status(thread_id_t tid) const
310 int thread_id = id_to_int(tid);
311 if (thread_id < num_threads)
312 return enabled_array[thread_id];
314 return THREAD_DISABLED;
317 bool Node::is_enabled(thread_id_t tid) const
319 int thread_id = id_to_int(tid);
320 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
323 bool Node::has_priority(thread_id_t tid) const
325 return fairness[id_to_int(tid)].priority;
328 /*********************************** read from ********************************/
331 * Get the current state of the may-read-from set iteration
332 * @return The read-from type we should currently be checking (past or future)
334 read_from_type_t Node::get_read_from_status()
336 if (read_from_status == READ_FROM_PAST && read_from_past.empty())
337 increment_read_from();
338 return read_from_status;
342 * Iterate one step in the may-read-from iteration. This includes a step in
343 * reading from the either the past or the future.
344 * @return True if there is a new read-from to explore; false otherwise
346 bool Node::increment_read_from()
349 if (increment_read_from_past()) {
350 read_from_status = READ_FROM_PAST;
352 } else if (increment_read_from_promise()) {
353 read_from_status = READ_FROM_PROMISE;
355 } else if (increment_future_value()) {
356 read_from_status = READ_FROM_FUTURE;
359 read_from_status = READ_FROM_NONE;
364 * @return True if there are any new read-froms to explore
366 bool Node::read_from_empty() const
368 return read_from_past_empty() && future_value_empty();
372 * Get the total size of the may-read-from set, including both past and future
374 * @return The size of may-read-from
376 unsigned int Node::read_from_size() const
378 return read_from_past.size() + future_values.size();
381 /******************************* end read from ********************************/
383 /****************************** read from past ********************************/
385 /** @brief Prints info about read_from_past set */
386 void Node::print_read_from_past()
388 for (unsigned int i = 0; i < read_from_past.size(); i++)
389 read_from_past[i]->print();
393 * Add an action to the read_from_past set.
394 * @param act is the action to add
396 void Node::add_read_from_past(const ModelAction *act)
398 read_from_past.push_back(act);
402 * Gets the next 'read_from_past' action from this Node. Only valid for a node
403 * where this->action is a 'read'.
404 * @return The first element in read_from_past
406 const ModelAction * Node::get_read_from_past() const
408 if (read_from_past_idx < read_from_past.size())
409 return read_from_past[read_from_past_idx];
414 const ModelAction * Node::get_read_from_past(int i) const
416 return read_from_past[i];
419 int Node::get_read_from_past_size() const
421 return read_from_past.size();
425 * Checks whether the readsfrom set for this node is empty.
426 * @return true if the readsfrom set is empty.
428 bool Node::read_from_past_empty() const
430 return ((read_from_past_idx + 1) >= read_from_past.size());
434 * Increments the index into the readsfrom set to explore the next item.
435 * @return Returns false if we have explored all items.
437 bool Node::increment_read_from_past()
440 if (read_from_past_idx < read_from_past.size()) {
441 read_from_past_idx++;
442 return read_from_past_idx < read_from_past.size();
447 /************************** end read from past ********************************/
449 /***************************** read_from_promises *****************************/
452 * Add an action to the read_from_promises set.
453 * @param reader The read which generated the Promise; we use the ModelAction
454 * instead of the Promise because the Promise does not last across executions
456 void Node::add_read_from_promise(const ModelAction *reader)
458 read_from_promises.push_back(reader);
462 * Gets the next 'read-from-promise' from this Node. Only valid for a node
463 * where this->action is a 'read'.
464 * @return The current element in read_from_promises
466 const Promise * Node::get_read_from_promise() const
468 if (read_from_promise_idx < 0 || read_from_promise_idx >= ((int)read_from_promises.size()))
470 return read_from_promises[read_from_promise_idx]->get_reads_from_promise();
474 * Checks whether the read_from_promises set for this node is empty.
475 * @return true if the read_from_promises set is empty.
477 bool Node::read_from_promise_empty() const
479 return ((read_from_promise_idx + 1) >= ((int)read_from_promises.size()));
483 * Increments the index into the read_from_promises set to explore the next item.
484 * @return Returns false if we have explored all promises.
486 bool Node::increment_read_from_promise()
489 if (read_from_promise_idx < ((int)read_from_promises.size())) {
490 read_from_promise_idx++;
491 return (read_from_promise_idx < ((int)read_from_promises.size()));
496 /************************* end read_from_promises *****************************/
498 /****************************** future values *********************************/
501 * Adds a value from a weakly ordered future write to backtrack to. This
502 * operation may "fail" if the future value has already been run (within some
503 * sloppiness window of this expiration), or if the futurevalues set has
504 * reached its maximum.
505 * @see model_params.maxfuturevalues
507 * @param value is the value to backtrack to.
508 * @return True if the future value was successully added; false otherwise
510 bool Node::add_future_value(struct future_value fv)
512 uint64_t value = fv.value;
513 modelclock_t expiration = fv.expiration;
514 thread_id_t tid = fv.tid;
515 int idx = -1; /* Highest index where value is found */
516 for (unsigned int i = 0; i < future_values.size(); i++) {
517 if (future_values[i].value == value && future_values[i].tid == tid) {
518 if (expiration <= future_values[i].expiration)
523 if (idx > future_index) {
524 /* Future value hasn't been explored; update expiration */
525 future_values[idx].expiration = expiration;
527 } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
528 /* Future value has been explored and is within the "sloppy" window */
532 /* Limit the size of the future-values set */
533 if (model->params.maxfuturevalues > 0 &&
534 (int)future_values.size() >= model->params.maxfuturevalues)
537 future_values.push_back(fv);
542 * Gets the next 'future_value' from this Node. Only valid for a node where
543 * this->action is a 'read'.
544 * @return The first element in future_values
546 struct future_value Node::get_future_value() const
548 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
549 return future_values[future_index];
553 * Checks whether the future_values set for this node is empty.
554 * @return true if the future_values set is empty.
556 bool Node::future_value_empty() const
558 return ((future_index + 1) >= ((int)future_values.size()));
562 * Increments the index into the future_values set to explore the next item.
563 * @return Returns false if we have explored all values.
565 bool Node::increment_future_value()
568 if (future_index < ((int)future_values.size())) {
570 return (future_index < ((int)future_values.size()));
575 /************************** end future values *********************************/
578 * Add a write ModelAction to the set of writes that may break the release
579 * sequence. This is used during replay exploration of pending release
580 * sequences. This Node must correspond to a release sequence fixup action.
582 * @param write The write that may break the release sequence. NULL means we
583 * allow the release sequence to synchronize.
585 void Node::add_relseq_break(const ModelAction *write)
587 relseq_break_writes.push_back(write);
591 * Get the write that may break the current pending release sequence,
592 * according to the replay / divergence pattern.
594 * @return A write that may break the release sequence. If NULL, that means
595 * the release sequence should not be broken.
597 const ModelAction * Node::get_relseq_break() const
599 if (relseq_break_index < (int)relseq_break_writes.size())
600 return relseq_break_writes[relseq_break_index];
606 * Increments the index into the relseq_break_writes set to explore the next
608 * @return Returns false if we have explored all values.
610 bool Node::increment_relseq_break()
614 if (relseq_break_index < ((int)relseq_break_writes.size())) {
615 relseq_break_index++;
616 return (relseq_break_index < ((int)relseq_break_writes.size()));
622 * @return True if all writes that may break the release sequence have been
625 bool Node::relseq_break_empty() const
627 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
630 void Node::explore(thread_id_t tid)
632 int i = id_to_int(tid);
633 ASSERT(i < ((int)backtrack.size()));
635 backtrack[i] = false;
638 explored_children[i] = true;
641 NodeStack::NodeStack() :
649 NodeStack::~NodeStack()
651 for (unsigned int i = 0; i < node_list.size(); i++)
655 void NodeStack::print() const
657 model_print("............................................\n");
658 model_print("NodeStack printing node_list:\n");
659 for (unsigned int it = 0; it < node_list.size(); it++) {
660 if ((int)it == this->head_idx)
661 model_print("vvv following action is the current iterator vvv\n");
662 node_list[it]->print();
664 model_print("............................................\n");
667 /** Note: The is_enabled set contains what actions were enabled when
669 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
673 if ((head_idx + 1) < (int)node_list.size()) {
675 return node_list[head_idx]->get_action();
679 Node *head = get_head();
680 Node *prevfairness = NULL;
682 head->explore_child(act, is_enabled);
683 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
684 prevfairness = node_list[head_idx - model->params.fairwindow];
687 int next_threads = model->get_num_threads();
688 if (act->get_type() == THREAD_CREATE)
690 node_list.push_back(new Node(act, head, next_threads, prevfairness));
697 * Empties the stack of all trailing nodes after a given position and calls the
698 * destructor for each. This function is provided an offset which determines
699 * how many nodes (relative to the current replay state) to save before popping
701 * @param numAhead gives the number of Nodes (including this Node) to skip over
702 * before removing nodes.
704 void NodeStack::pop_restofstack(int numAhead)
706 /* Diverging from previous execution; clear out remainder of list */
707 unsigned int it = head_idx + numAhead;
708 for (unsigned int i = it; i < node_list.size(); i++)
710 node_list.resize(it);
711 node_list.back()->clear_backtracking();
714 Node * NodeStack::get_head() const
716 if (node_list.empty() || head_idx < 0)
718 return node_list[head_idx];
721 Node * NodeStack::get_next() const
723 if (node_list.empty()) {
727 unsigned int it = head_idx + 1;
728 if (it == node_list.size()) {
732 return node_list[it];
735 void NodeStack::reset_execution()