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)
30 num_threads(nthreads),
31 explored_children(num_threads),
32 backtrack(num_threads),
33 fairness(num_threads),
40 relseq_break_writes(),
41 relseq_break_index(0),
47 int currtid = id_to_int(act->get_tid());
48 int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
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;
58 if (parent && parent->is_enabled(int_to_id(i))) {
65 /* Do window processing */
66 if (prevfairness != NULL) {
67 if (prevfairness->parent->is_enabled(int_to_id(i)))
72 /* Need full window to start evaluating
74 * If we meet the enabled count and have no
75 * turns, give us priority */
76 if ((fi->enabled_count >= model->params.enabledcount) &&
84 /** @brief Node desctructor */
89 model_free(enabled_array);
92 /** Prints debugging info for the ModelAction associated with this Node */
93 void Node::print() const
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);
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);
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());
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");
116 /** @brief Prints info about may_read_from set */
117 void Node::print_may_read_from()
119 for (unsigned int i = 0; i < may_read_from.size(); i++)
120 may_read_from[i]->print();
124 * Sets a promise to explore meeting with the given node.
125 * @param i is the promise index.
127 void Node::set_promise(unsigned int i, bool is_rmw)
129 if (i >= promises.size())
130 promises.resize(i + 1, PROMISE_IGNORE);
131 if (promises[i] == PROMISE_IGNORE) {
132 promises[i] = PROMISE_UNFULFILLED;
134 promises[i] |= PROMISE_RMW;
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.
143 bool Node::get_promise(unsigned int i) const
145 return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
149 * Increments to the next combination of promises.
150 * @return true if we have a valid combination.
152 bool Node::increment_promise()
155 unsigned int rmw_count = 0;
156 for (unsigned int i = 0; i < promises.size(); i++) {
157 if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED))
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
167 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_FULFILLED;
170 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
171 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
174 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
182 * Returns whether the promise set is empty.
183 * @return true if we have explored all promise combinations.
185 bool Node::promise_empty() const
187 bool fulfilledrmw = false;
188 for (int i = promises.size() - 1; i >= 0; i--) {
189 if (promises[i] == PROMISE_UNFULFILLED)
191 if (!fulfilledrmw && ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED))
193 if (promises[i] == (PROMISE_FULFILLED | PROMISE_RMW))
199 void Node::set_misc_max(int i)
204 int Node::get_misc() const
209 bool Node::increment_misc()
211 return (misc_index < misc_max) && ((++misc_index) < misc_max);
214 bool Node::misc_empty() const
216 return (misc_index + 1) >= misc_max;
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
226 * @param value is the value to backtrack to.
227 * @return True if the future value was successully added; false otherwise
229 bool Node::add_future_value(struct future_value& fv)
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)
242 if (idx > future_index) {
243 /* Future value hasn't been explored; update expiration */
244 future_values[idx].expiration = expiration;
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 */
251 /* Limit the size of the future-values set */
252 if (model->params.maxfuturevalues > 0 &&
253 (int)future_values.size() >= model->params.maxfuturevalues)
256 future_values.push_back(fv);
261 * Checks whether the future_values set for this node is empty.
262 * @return true if the future_values set is empty.
264 bool Node::future_value_empty() const
266 return ((future_index + 1) >= ((int)future_values.size()));
270 * Checks if the Thread associated with this thread ID has been explored from
272 * @param tid is the thread ID to check
273 * @return true if this thread choice has been explored already, false
276 bool Node::has_been_explored(thread_id_t tid) const
278 int id = id_to_int(tid);
279 return explored_children[id];
283 * Checks if the backtracking set is empty.
284 * @return true if the backtracking set is empty
286 bool Node::backtrack_empty() const
288 return (numBacktracks == 0);
292 * Checks whether the readsfrom set for this node is empty.
293 * @return true if the readsfrom set is empty.
295 bool Node::read_from_empty() const
297 return ((read_from_index + 1) >= may_read_from.size());
301 * Mark the appropriate backtracking information for exploring a thread choice.
302 * @param act The ModelAction to explore
304 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
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);
311 for (int i = 0; i < num_threads; i++)
312 enabled_array[i] = THREAD_DISABLED;
315 explore(act->get_tid());
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
322 * @return false if the thread was already set to be backtracked, true
325 bool Node::set_backtrack(thread_id_t id)
327 int i = id_to_int(id);
328 ASSERT(i < ((int)backtrack.size()));
336 thread_id_t Node::get_next_backtrack()
338 /** @todo Find next backtrack */
340 for (i = 0; i < backtrack.size(); i++)
341 if (backtrack[i] == true)
343 /* Backtrack set was empty? */
344 ASSERT(i != backtrack.size());
346 backtrack[i] = false;
351 bool Node::is_enabled(Thread *t) const
353 int thread_id = id_to_int(t->get_id());
354 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
357 enabled_type_t Node::enabled_status(thread_id_t tid) const
359 int thread_id = id_to_int(tid);
360 if (thread_id < num_threads)
361 return enabled_array[thread_id];
363 return THREAD_DISABLED;
366 bool Node::is_enabled(thread_id_t tid) const
368 int thread_id = id_to_int(tid);
369 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
372 bool Node::has_priority(thread_id_t tid) const
374 return fairness[id_to_int(tid)].priority;
378 * Add an action to the may_read_from set.
379 * @param act is the action to add
381 void Node::add_read_from(const ModelAction *act)
383 may_read_from.push_back(act);
387 * Gets the next 'future_value' from this Node. Only valid for a node where
388 * this->action is a 'read'.
389 * @return The first element in future_values
391 struct future_value Node::get_future_value() const
393 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
394 return future_values[future_index];
397 int Node::get_read_from_size() const
399 return may_read_from.size();
402 const ModelAction * Node::get_read_from_at(int i) const
404 return may_read_from[i];
408 * Gets the next 'may_read_from' action from this Node. Only valid for a node
409 * where this->action is a 'read'.
410 * @return The first element in may_read_from
412 const ModelAction * Node::get_read_from() const
414 if (read_from_index < may_read_from.size())
415 return may_read_from[read_from_index];
421 * Increments the index into the readsfrom set to explore the next item.
422 * @return Returns false if we have explored all items.
424 bool Node::increment_read_from()
428 if (read_from_index < may_read_from.size()) {
430 return read_from_index < may_read_from.size();
436 * Increments the index into the future_values set to explore the next item.
437 * @return Returns false if we have explored all values.
439 bool Node::increment_future_value()
443 if (future_index < ((int)future_values.size())) {
445 return (future_index < ((int)future_values.size()));
451 * Add a write ModelAction to the set of writes that may break the release
452 * sequence. This is used during replay exploration of pending release
453 * sequences. This Node must correspond to a release sequence fixup action.
455 * @param write The write that may break the release sequence. NULL means we
456 * allow the release sequence to synchronize.
458 void Node::add_relseq_break(const ModelAction *write)
460 relseq_break_writes.push_back(write);
464 * Get the write that may break the current pending release sequence,
465 * according to the replay / divergence pattern.
467 * @return A write that may break the release sequence. If NULL, that means
468 * the release sequence should not be broken.
470 const ModelAction * Node::get_relseq_break() const
472 if (relseq_break_index < (int)relseq_break_writes.size())
473 return relseq_break_writes[relseq_break_index];
479 * Increments the index into the relseq_break_writes set to explore the next
481 * @return Returns false if we have explored all values.
483 bool Node::increment_relseq_break()
487 if (relseq_break_index < ((int)relseq_break_writes.size())) {
488 relseq_break_index++;
489 return (relseq_break_index < ((int)relseq_break_writes.size()));
495 * @return True if all writes that may break the release sequence have been
498 bool Node::relseq_break_empty() const
500 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
503 void Node::explore(thread_id_t tid)
505 int i = id_to_int(tid);
506 ASSERT(i < ((int)backtrack.size()));
508 backtrack[i] = false;
511 explored_children[i] = true;
514 NodeStack::NodeStack() :
522 NodeStack::~NodeStack()
524 for (unsigned int i = 0; i < node_list.size(); i++)
528 void NodeStack::print() const
530 model_print("............................................\n");
531 model_print("NodeStack printing node_list:\n");
532 for (unsigned int it = 0; it < node_list.size(); it++) {
533 if ((int)it == this->head_idx)
534 model_print("vvv following action is the current iterator vvv\n");
535 node_list[it]->print();
537 model_print("............................................\n");
540 /** Note: The is_enabled set contains what actions were enabled when
542 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
546 if ((head_idx + 1) < (int)node_list.size()) {
548 return node_list[head_idx]->get_action();
552 Node *head = get_head();
553 Node *prevfairness = NULL;
555 head->explore_child(act, is_enabled);
556 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
557 prevfairness = node_list[head_idx - model->params.fairwindow];
559 node_list.push_back(new Node(act, head, model->get_num_threads(), prevfairness));
566 * Empties the stack of all trailing nodes after a given position and calls the
567 * destructor for each. This function is provided an offset which determines
568 * how many nodes (relative to the current replay state) to save before popping
570 * @param numAhead gives the number of Nodes (including this Node) to skip over
571 * before removing nodes.
573 void NodeStack::pop_restofstack(int numAhead)
575 /* Diverging from previous execution; clear out remainder of list */
576 unsigned int it = head_idx + numAhead;
577 for (unsigned int i = it; i < node_list.size(); i++)
579 node_list.resize(it);
582 Node * NodeStack::get_head() const
584 if (node_list.empty() || head_idx < 0)
586 return node_list[head_idx];
589 Node * NodeStack::get_next() const
591 if (node_list.empty()) {
595 unsigned int it = head_idx + 1;
596 if (it == node_list.size()) {
600 return node_list[it];
603 void NodeStack::reset_execution()