7 #include "threads-model.h"
10 * @brief Node constructor
12 * Constructs a single Node for use in a NodeStack. Each Node is associated
13 * with exactly one ModelAction (exception: the first Node should be created
14 * as an empty stub, to represent the first thread "choice") and up to one
17 * @param act The ModelAction to associate with this Node. May be NULL.
18 * @param par The parent Node in the NodeStack. May be NULL if there is no
20 * @param nthreads The number of threads which exist at this point in the
23 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness)
26 num_threads(nthreads),
27 explored_children(num_threads),
28 backtrack(num_threads),
29 fairness(num_threads),
36 relseq_break_writes(),
37 relseq_break_index(0),
43 int currtid = id_to_int(act->get_tid());
44 int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
46 if (model->params.fairwindow != 0) {
47 for (int i = 0; i < num_threads; i++) {
48 ASSERT(i < ((int)fairness.size()));
49 struct fairness_info *fi = &fairness[i];
50 struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
54 if (parent && parent->is_enabled(int_to_id(i))) {
61 /* Do window processing */
62 if (prevfairness != NULL) {
63 if (prevfairness->parent->is_enabled(int_to_id(i)))
68 /* Need full window to start evaluating
70 * If we meet the enabled count and have no
71 * turns, give us priority */
72 if ((fi->enabled_count >= model->params.enabledcount) &&
80 /** @brief Node desctructor */
85 model_free(enabled_array);
88 /** Prints debugging info for the ModelAction associated with this Node */
92 model_print(" backtrack: %s", backtrack_empty() ? "empty" : "non-empty");
93 for (int i = 0; i < (int)backtrack.size(); i++)
94 if (backtrack[i] == true)
95 model_print("[%d]", i);
97 model_print(" future values: %s\n", future_value_empty() ? "empty" : "non-empty");
98 model_print(" read-from: %s\n", read_from_empty() ? "empty" : "non-empty");
99 model_print(" promises: %s\n", promise_empty() ? "empty" : "non-empty");
100 model_print(" misc: %s\n", misc_empty() ? "empty" : "non-empty");
101 model_print(" rel seq break: %s\n", relseq_break_empty() ? "empty" : "non-empty");
104 /** @brief Prints info about may_read_from set */
105 void Node::print_may_read_from()
107 for (unsigned int i = 0; i < may_read_from.size(); i++)
108 may_read_from[i]->print();
112 * Sets a promise to explore meeting with the given node.
113 * @param i is the promise index.
115 void Node::set_promise(unsigned int i, bool is_rmw)
117 if (i >= promises.size())
118 promises.resize(i + 1, PROMISE_IGNORE);
119 if (promises[i] == PROMISE_IGNORE) {
120 promises[i] = PROMISE_UNFULFILLED;
122 promises[i] |= PROMISE_RMW;
127 * Looks up whether a given promise should be satisfied by this node.
128 * @param i The promise index.
129 * @return true if the promise should be satisfied by the given model action.
131 bool Node::get_promise(unsigned int i) const
133 return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
137 * Increments to the next combination of promises.
138 * @return true if we have a valid combination.
140 bool Node::increment_promise()
143 unsigned int rmw_count = 0;
144 for (unsigned int i = 0; i < promises.size(); i++) {
145 if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED))
149 for (unsigned int i = 0; i < promises.size(); i++) {
150 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
151 if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
152 //sending our value to two rmws... not going to work..try next combination
155 promises[i] = (promises[i] & PROMISE_RMW) |PROMISE_FULFILLED;
158 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
159 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
162 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
170 * Returns whether the promise set is empty.
171 * @return true if we have explored all promise combinations.
173 bool Node::promise_empty() const
175 bool fulfilledrmw = false;
176 for (int i = promises.size() - 1; i >= 0; i--) {
177 if (promises[i] == PROMISE_UNFULFILLED)
179 if (!fulfilledrmw && ((promises[i]&PROMISE_MASK) == PROMISE_UNFULFILLED))
181 if (promises[i] == (PROMISE_FULFILLED|PROMISE_RMW))
188 void Node::set_misc_max(int i)
193 int Node::get_misc() const
198 bool Node::increment_misc()
200 return (misc_index < misc_max) && ((++misc_index) < misc_max);
203 bool Node::misc_empty() const
205 return (misc_index + 1) >= misc_max;
210 * Adds a value from a weakly ordered future write to backtrack to. This
211 * operation may "fail" if the future value has already been run (within some
212 * sloppiness window of this expiration), or if the futurevalues set has
213 * reached its maximum.
214 * @see model_params.maxfuturevalues
216 * @param value is the value to backtrack to.
217 * @return True if the future value was successully added; false otherwise
219 bool Node::add_future_value(uint64_t value, modelclock_t expiration)
221 int idx = -1; /* Highest index where value is found */
222 for (unsigned int i = 0; i < future_values.size(); i++) {
223 if (future_values[i].value == value) {
224 if (expiration <= future_values[i].expiration)
229 if (idx > future_index) {
230 /* Future value hasn't been explored; update expiration */
231 future_values[idx].expiration = expiration;
233 } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
234 /* Future value has been explored and is within the "sloppy" window */
238 /* Limit the size of the future-values set */
239 if (model->params.maxfuturevalues > 0 &&
240 (int)future_values.size() >= model->params.maxfuturevalues)
243 struct future_value newfv = {value, expiration};
244 future_values.push_back(newfv);
249 * Checks whether the future_values set for this node is empty.
250 * @return true if the future_values set is empty.
252 bool Node::future_value_empty() const
254 return ((future_index + 1) >= ((int)future_values.size()));
258 * Checks if the Thread associated with this thread ID has been explored from
260 * @param tid is the thread ID to check
261 * @return true if this thread choice has been explored already, false
264 bool Node::has_been_explored(thread_id_t tid) const
266 int id = id_to_int(tid);
267 return explored_children[id];
271 * Checks if the backtracking set is empty.
272 * @return true if the backtracking set is empty
274 bool Node::backtrack_empty() const
276 return (numBacktracks == 0);
280 * Checks whether the readsfrom set for this node is empty.
281 * @return true if the readsfrom set is empty.
283 bool Node::read_from_empty() const
285 return ((read_from_index + 1) >= may_read_from.size());
289 * Mark the appropriate backtracking information for exploring a thread choice.
290 * @param act The ModelAction to explore
292 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
295 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
296 if (is_enabled != NULL)
297 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
299 for (int i = 0; i < num_threads; i++)
300 enabled_array[i] = THREAD_DISABLED;
303 explore(act->get_tid());
307 * Records a backtracking reference for a thread choice within this Node.
308 * Provides feedback as to whether this thread choice is already set for
310 * @return false if the thread was already set to be backtracked, true
313 bool Node::set_backtrack(thread_id_t id)
315 int i = id_to_int(id);
316 ASSERT(i < ((int)backtrack.size()));
324 thread_id_t Node::get_next_backtrack()
326 /** @todo Find next backtrack */
328 for (i = 0; i < backtrack.size(); i++)
329 if (backtrack[i] == true)
331 /* Backtrack set was empty? */
332 ASSERT(i != backtrack.size());
334 backtrack[i] = false;
339 bool Node::is_enabled(Thread *t) const
341 int thread_id = id_to_int(t->get_id());
342 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
345 enabled_type_t Node::enabled_status(thread_id_t tid) const
347 int thread_id = id_to_int(tid);
348 if (thread_id < num_threads)
349 return enabled_array[thread_id];
351 return THREAD_DISABLED;
354 bool Node::is_enabled(thread_id_t tid) const
356 int thread_id = id_to_int(tid);
357 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
360 bool Node::has_priority(thread_id_t tid) const
362 return fairness[id_to_int(tid)].priority;
366 * Add an action to the may_read_from set.
367 * @param act is the action to add
369 void Node::add_read_from(const ModelAction *act)
371 may_read_from.push_back(act);
375 * Gets the next 'future_value' value from this Node. Only valid for a node
376 * where this->action is a 'read'.
377 * @return The first element in future_values
379 uint64_t Node::get_future_value() const
381 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
382 return future_values[future_index].value;
385 modelclock_t Node::get_future_value_expiration() const
387 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
388 return future_values[future_index].expiration;
392 int Node::get_read_from_size() const
394 return may_read_from.size();
397 const ModelAction * Node::get_read_from_at(int i) const
399 return may_read_from[i];
403 * Gets the next 'may_read_from' action from this Node. Only valid for a node
404 * where this->action is a 'read'.
405 * @return The first element in may_read_from
407 const ModelAction * Node::get_read_from() const
409 if (read_from_index < may_read_from.size())
410 return may_read_from[read_from_index];
416 * Increments the index into the readsfrom set to explore the next item.
417 * @return Returns false if we have explored all items.
419 bool Node::increment_read_from()
423 if (read_from_index < may_read_from.size()) {
425 return read_from_index < may_read_from.size();
431 * Increments the index into the future_values set to explore the next item.
432 * @return Returns false if we have explored all values.
434 bool Node::increment_future_value()
438 if (future_index < ((int)future_values.size())) {
440 return (future_index < ((int)future_values.size()));
446 * Add a write ModelAction to the set of writes that may break the release
447 * sequence. This is used during replay exploration of pending release
448 * sequences. This Node must correspond to a release sequence fixup action.
450 * @param write The write that may break the release sequence. NULL means we
451 * allow the release sequence to synchronize.
453 void Node::add_relseq_break(const ModelAction *write)
455 relseq_break_writes.push_back(write);
459 * Get the write that may break the current pending release sequence,
460 * according to the replay / divergence pattern.
462 * @return A write that may break the release sequence. If NULL, that means
463 * the release sequence should not be broken.
465 const ModelAction * Node::get_relseq_break() const
467 if (relseq_break_index < (int)relseq_break_writes.size())
468 return relseq_break_writes[relseq_break_index];
474 * Increments the index into the relseq_break_writes set to explore the next
476 * @return Returns false if we have explored all values.
478 bool Node::increment_relseq_break()
482 if (relseq_break_index < ((int)relseq_break_writes.size())) {
483 relseq_break_index++;
484 return (relseq_break_index < ((int)relseq_break_writes.size()));
490 * @return True if all writes that may break the release sequence have been
493 bool Node::relseq_break_empty() const
495 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
498 void Node::explore(thread_id_t tid)
500 int i = id_to_int(tid);
501 ASSERT(i < ((int)backtrack.size()));
503 backtrack[i] = false;
506 explored_children[i] = true;
509 NodeStack::NodeStack() :
517 NodeStack::~NodeStack()
519 for (unsigned int i = 0; i < node_list.size(); i++)
523 void NodeStack::print() const
525 model_print("............................................\n");
526 model_print("NodeStack printing node_list:\n");
527 for (unsigned int it = 0; it < node_list.size(); it++) {
528 if ((int)it == this->head_idx)
529 model_print("vvv following action is the current iterator vvv\n");
530 node_list[it]->print();
532 model_print("............................................\n");
535 /** Note: The is_enabled set contains what actions were enabled when
537 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
541 if ((head_idx + 1) < (int)node_list.size()) {
543 return node_list[head_idx]->get_action();
547 Node *head = get_head();
548 Node *prevfairness = NULL;
550 head->explore_child(act, is_enabled);
551 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
552 prevfairness = node_list[head_idx - model->params.fairwindow];
554 node_list.push_back(new Node(act, head, model->get_num_threads(), prevfairness));
561 * Empties the stack of all trailing nodes after a given position and calls the
562 * destructor for each. This function is provided an offset which determines
563 * how many nodes (relative to the current replay state) to save before popping
565 * @param numAhead gives the number of Nodes (including this Node) to skip over
566 * before removing nodes.
568 void NodeStack::pop_restofstack(int numAhead)
570 /* Diverging from previous execution; clear out remainder of list */
571 unsigned int it = head_idx + numAhead;
572 for (unsigned int i = it; i < node_list.size(); i++)
574 node_list.resize(it);
577 Node * NodeStack::get_head() const
579 if (node_list.empty() || head_idx < 0)
581 return node_list[head_idx];
584 Node * NodeStack::get_next() const
586 if (node_list.empty()) {
590 unsigned int it = head_idx + 1;
591 if (it == node_list.size()) {
595 return node_list[it];
598 void NodeStack::reset_execution()