1 #define __STDC_FORMAT_MACROS
10 #include "threads-model.h"
13 * @brief Node constructor
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
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
23 * @param nthreads The number of threads which exist at this point in the
26 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness)
29 num_threads(nthreads),
30 explored_children(num_threads),
31 backtrack(num_threads),
32 fairness(num_threads),
39 relseq_break_writes(),
40 relseq_break_index(0),
46 int currtid = id_to_int(act->get_tid());
47 int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
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;
57 if (parent && parent->is_enabled(int_to_id(i))) {
64 /* Do window processing */
65 if (prevfairness != NULL) {
66 if (prevfairness->parent->is_enabled(int_to_id(i)))
71 /* Need full window to start evaluating
73 * If we meet the enabled count and have no
74 * turns, give us priority */
75 if ((fi->enabled_count >= model->params.enabledcount) &&
83 /** @brief Node desctructor */
88 model_free(enabled_array);
91 /** Prints debugging info for the ModelAction associated with this Node */
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);
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);
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());
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");
115 /** @brief Prints info about may_read_from set */
116 void Node::print_may_read_from()
118 for (unsigned int i = 0; i < may_read_from.size(); i++)
119 may_read_from[i]->print();
123 * Sets a promise to explore meeting with the given node.
124 * @param i is the promise index.
126 void Node::set_promise(unsigned int i, bool is_rmw)
128 if (i >= promises.size())
129 promises.resize(i + 1, PROMISE_IGNORE);
130 if (promises[i] == PROMISE_IGNORE) {
131 promises[i] = PROMISE_UNFULFILLED;
133 promises[i] |= PROMISE_RMW;
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.
142 bool Node::get_promise(unsigned int i) const
144 return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
148 * Increments to the next combination of promises.
149 * @return true if we have a valid combination.
151 bool Node::increment_promise()
154 unsigned int rmw_count = 0;
155 for (unsigned int i = 0; i < promises.size(); i++) {
156 if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED))
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
166 promises[i] = (promises[i] & PROMISE_RMW) |PROMISE_FULFILLED;
169 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
170 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
173 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
181 * Returns whether the promise set is empty.
182 * @return true if we have explored all promise combinations.
184 bool Node::promise_empty() const
186 bool fulfilledrmw = false;
187 for (int i = promises.size() - 1; i >= 0; i--) {
188 if (promises[i] == PROMISE_UNFULFILLED)
190 if (!fulfilledrmw && ((promises[i]&PROMISE_MASK) == PROMISE_UNFULFILLED))
192 if (promises[i] == (PROMISE_FULFILLED|PROMISE_RMW))
198 void Node::set_misc_max(int i)
203 int Node::get_misc() const
208 bool Node::increment_misc()
210 return (misc_index < misc_max) && ((++misc_index) < misc_max);
213 bool Node::misc_empty() const
215 return (misc_index + 1) >= misc_max;
219 * Adds a value from a weakly ordered future write to backtrack to. This
220 * operation may "fail" if the future value has already been run (within some
221 * sloppiness window of this expiration), or if the futurevalues set has
222 * reached its maximum.
223 * @see model_params.maxfuturevalues
225 * @param value is the value to backtrack to.
226 * @return True if the future value was successully added; false otherwise
228 bool Node::add_future_value(const ModelAction *writer, modelclock_t expiration)
230 uint64_t value = writer->get_value();
231 int idx = -1; /* Highest index where value is found */
232 for (unsigned int i = 0; i < future_values.size(); i++) {
233 if (future_values[i].value == value) {
234 if (expiration <= future_values[i].expiration)
239 if (idx > future_index) {
240 /* Future value hasn't been explored; update expiration */
241 future_values[idx].expiration = expiration;
243 } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
244 /* Future value has been explored and is within the "sloppy" window */
248 /* Limit the size of the future-values set */
249 if (model->params.maxfuturevalues > 0 &&
250 (int)future_values.size() >= model->params.maxfuturevalues)
253 struct future_value newfv = {value, expiration};
254 future_values.push_back(newfv);
259 * Checks whether the future_values set for this node is empty.
260 * @return true if the future_values set is empty.
262 bool Node::future_value_empty() const
264 return ((future_index + 1) >= ((int)future_values.size()));
268 * Checks if the Thread associated with this thread ID has been explored from
270 * @param tid is the thread ID to check
271 * @return true if this thread choice has been explored already, false
274 bool Node::has_been_explored(thread_id_t tid) const
276 int id = id_to_int(tid);
277 return explored_children[id];
281 * Checks if the backtracking set is empty.
282 * @return true if the backtracking set is empty
284 bool Node::backtrack_empty() const
286 return (numBacktracks == 0);
290 * Checks whether the readsfrom set for this node is empty.
291 * @return true if the readsfrom set is empty.
293 bool Node::read_from_empty() const
295 return ((read_from_index + 1) >= may_read_from.size());
299 * Mark the appropriate backtracking information for exploring a thread choice.
300 * @param act The ModelAction to explore
302 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
305 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
306 if (is_enabled != NULL)
307 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
309 for (int i = 0; i < num_threads; i++)
310 enabled_array[i] = THREAD_DISABLED;
313 explore(act->get_tid());
317 * Records a backtracking reference for a thread choice within this Node.
318 * Provides feedback as to whether this thread choice is already set for
320 * @return false if the thread was already set to be backtracked, true
323 bool Node::set_backtrack(thread_id_t id)
325 int i = id_to_int(id);
326 ASSERT(i < ((int)backtrack.size()));
334 thread_id_t Node::get_next_backtrack()
336 /** @todo Find next backtrack */
338 for (i = 0; i < backtrack.size(); i++)
339 if (backtrack[i] == true)
341 /* Backtrack set was empty? */
342 ASSERT(i != backtrack.size());
344 backtrack[i] = false;
349 bool Node::is_enabled(Thread *t) const
351 int thread_id = id_to_int(t->get_id());
352 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
355 enabled_type_t Node::enabled_status(thread_id_t tid) const
357 int thread_id = id_to_int(tid);
358 if (thread_id < num_threads)
359 return enabled_array[thread_id];
361 return THREAD_DISABLED;
364 bool Node::is_enabled(thread_id_t tid) const
366 int thread_id = id_to_int(tid);
367 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
370 bool Node::has_priority(thread_id_t tid) const
372 return fairness[id_to_int(tid)].priority;
376 * Add an action to the may_read_from set.
377 * @param act is the action to add
379 void Node::add_read_from(const ModelAction *act)
381 may_read_from.push_back(act);
385 * Gets the next 'future_value' from this Node. Only valid for a node where
386 * this->action is a 'read'.
387 * @return The first element in future_values
389 struct future_value Node::get_future_value() const
391 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
392 return future_values[future_index];
395 int Node::get_read_from_size() const
397 return may_read_from.size();
400 const ModelAction * Node::get_read_from_at(int i) const
402 return may_read_from[i];
406 * Gets the next 'may_read_from' action from this Node. Only valid for a node
407 * where this->action is a 'read'.
408 * @return The first element in may_read_from
410 const ModelAction * Node::get_read_from() const
412 if (read_from_index < may_read_from.size())
413 return may_read_from[read_from_index];
419 * Increments the index into the readsfrom set to explore the next item.
420 * @return Returns false if we have explored all items.
422 bool Node::increment_read_from()
426 if (read_from_index < may_read_from.size()) {
428 return read_from_index < may_read_from.size();
434 * Increments the index into the future_values set to explore the next item.
435 * @return Returns false if we have explored all values.
437 bool Node::increment_future_value()
441 if (future_index < ((int)future_values.size())) {
443 return (future_index < ((int)future_values.size()));
449 * Add a write ModelAction to the set of writes that may break the release
450 * sequence. This is used during replay exploration of pending release
451 * sequences. This Node must correspond to a release sequence fixup action.
453 * @param write The write that may break the release sequence. NULL means we
454 * allow the release sequence to synchronize.
456 void Node::add_relseq_break(const ModelAction *write)
458 relseq_break_writes.push_back(write);
462 * Get the write that may break the current pending release sequence,
463 * according to the replay / divergence pattern.
465 * @return A write that may break the release sequence. If NULL, that means
466 * the release sequence should not be broken.
468 const ModelAction * Node::get_relseq_break() const
470 if (relseq_break_index < (int)relseq_break_writes.size())
471 return relseq_break_writes[relseq_break_index];
477 * Increments the index into the relseq_break_writes set to explore the next
479 * @return Returns false if we have explored all values.
481 bool Node::increment_relseq_break()
485 if (relseq_break_index < ((int)relseq_break_writes.size())) {
486 relseq_break_index++;
487 return (relseq_break_index < ((int)relseq_break_writes.size()));
493 * @return True if all writes that may break the release sequence have been
496 bool Node::relseq_break_empty() const
498 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
501 void Node::explore(thread_id_t tid)
503 int i = id_to_int(tid);
504 ASSERT(i < ((int)backtrack.size()));
506 backtrack[i] = false;
509 explored_children[i] = true;
512 NodeStack::NodeStack() :
520 NodeStack::~NodeStack()
522 for (unsigned int i = 0; i < node_list.size(); i++)
526 void NodeStack::print() const
528 model_print("............................................\n");
529 model_print("NodeStack printing node_list:\n");
530 for (unsigned int it = 0; it < node_list.size(); it++) {
531 if ((int)it == this->head_idx)
532 model_print("vvv following action is the current iterator vvv\n");
533 node_list[it]->print();
535 model_print("............................................\n");
538 /** Note: The is_enabled set contains what actions were enabled when
540 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
544 if ((head_idx + 1) < (int)node_list.size()) {
546 return node_list[head_idx]->get_action();
550 Node *head = get_head();
551 Node *prevfairness = NULL;
553 head->explore_child(act, is_enabled);
554 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
555 prevfairness = node_list[head_idx - model->params.fairwindow];
557 node_list.push_back(new Node(act, head, model->get_num_threads(), prevfairness));
564 * Empties the stack of all trailing nodes after a given position and calls the
565 * destructor for each. This function is provided an offset which determines
566 * how many nodes (relative to the current replay state) to save before popping
568 * @param numAhead gives the number of Nodes (including this Node) to skip over
569 * before removing nodes.
571 void NodeStack::pop_restofstack(int numAhead)
573 /* Diverging from previous execution; clear out remainder of list */
574 unsigned int it = head_idx + numAhead;
575 for (unsigned int i = it; i < node_list.size(); i++)
577 node_list.resize(it);
580 Node * NodeStack::get_head() const
582 if (node_list.empty() || head_idx < 0)
584 return node_list[head_idx];
587 Node * NodeStack::get_next() const
589 if (node_list.empty()) {
593 unsigned int it = head_idx + 1;
594 if (it == node_list.size()) {
598 return node_list[it];
601 void NodeStack::reset_execution()