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 != NULL) ? id_to_int(prevfairness->action->get_tid()) : 0;
46 if (model->params.fairwindow != 0) {
47 for (int i = 0; i < nthreads; i++) {
48 ASSERT(i < ((int)fairness.size()));
49 struct fairness_info *fi = &fairness[i];
50 struct fairness_info *prevfi = (par != NULL) && (i < par->get_num_threads()) ? &par->fairness[i] : NULL;
54 if (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
71 * have no turns, give us priority */
72 if ((fi->enabled_count >= model->params.enabledcount) &&
81 /** @brief Node desctructor */
87 model_free(enabled_array);
90 /** Prints debugging info for the ModelAction associated with this Node */
95 model_print(" backtrack: %s\n", backtrack_empty() ? "empty" : "non-empty");
96 model_print(" future values: %s\n", future_value_empty() ? "empty" : "non-empty");
97 model_print(" read-from: %s\n", read_from_empty() ? "empty" : "non-empty");
98 model_print(" promises: %s\n", promise_empty() ? "empty" : "non-empty");
99 model_print(" misc: %s\n", misc_empty() ? "empty" : "non-empty");
100 model_print(" rel seq break: %s\n", relseq_break_empty() ? "empty" : "non-empty");
102 model_print("******** empty action ********\n");
105 /** @brief Prints info about may_read_from set */
106 void Node::print_may_read_from()
108 for (unsigned int i = 0; i < may_read_from.size(); i++)
109 may_read_from[i]->print();
113 * Sets a promise to explore meeting with the given node.
114 * @param i is the promise index.
116 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() {
142 unsigned int rmw_count = 0;
143 for (unsigned int i = 0; i < promises.size(); i++) {
144 if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED))
148 for (unsigned int i = 0; i < promises.size(); i++) {
149 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
150 if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
151 //sending our value to two rmws... not going to work..try next combination
154 promises[i] = (promises[i] & PROMISE_RMW) |PROMISE_FULFILLED;
157 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
158 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
161 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
169 * Returns whether the promise set is empty.
170 * @return true if we have explored all promise combinations.
172 bool Node::promise_empty() const
174 bool fulfilledrmw = false;
175 for (int i = promises.size() - 1; i >= 0; i--) {
176 if (promises[i] == PROMISE_UNFULFILLED)
178 if (!fulfilledrmw && ((promises[i]&PROMISE_MASK) == PROMISE_UNFULFILLED))
180 if (promises[i] == (PROMISE_FULFILLED|PROMISE_RMW))
187 void Node::set_misc_max(int i)
192 int Node::get_misc() const
197 bool Node::increment_misc() {
198 return (misc_index < misc_max) && ((++misc_index) < misc_max);
201 bool Node::misc_empty() const
203 return (misc_index + 1) >= misc_max;
208 * Adds a value from a weakly ordered future write to backtrack to. This
209 * operation may "fail" if the future value has already been run (within some
210 * sloppiness window of this expiration), or if the futurevalues set has
211 * reached its maximum.
212 * @see model_params.maxfuturevalues
214 * @param value is the value to backtrack to.
215 * @return True if the future value was successully added; false otherwise
217 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
218 int idx = -1; /* Highest index where value is found */
219 for (unsigned int i = 0; i < future_values.size(); i++) {
220 if (future_values[i].value == value) {
221 if (expiration <= future_values[i].expiration)
226 if (idx > future_index) {
227 /* Future value hasn't been explored; update expiration */
228 future_values[idx].expiration = expiration;
230 } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
231 /* Future value has been explored and is within the "sloppy" window */
235 /* Limit the size of the future-values set */
236 if (model->params.maxfuturevalues > 0 &&
237 (int)future_values.size() >= model->params.maxfuturevalues)
240 struct future_value newfv = {value, expiration};
241 future_values.push_back(newfv);
246 * Checks whether the future_values set for this node is empty.
247 * @return true if the future_values set is empty.
249 bool Node::future_value_empty() const
251 return ((future_index + 1) >= ((int)future_values.size()));
255 * Checks if the Thread associated with this thread ID has been explored from
257 * @param tid is the thread ID to check
258 * @return true if this thread choice has been explored already, false
261 bool Node::has_been_explored(thread_id_t tid) const
263 int id = id_to_int(tid);
264 return explored_children[id];
268 * Checks if the backtracking set is empty.
269 * @return true if the backtracking set is empty
271 bool Node::backtrack_empty() const
273 return (numBacktracks == 0);
277 * Checks whether the readsfrom set for this node is empty.
278 * @return true if the readsfrom set is empty.
280 bool Node::read_from_empty() const
282 return ((read_from_index + 1) >= may_read_from.size());
286 * Mark the appropriate backtracking information for exploring a thread choice.
287 * @param act The ModelAction to explore
289 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
292 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
293 if (is_enabled != NULL)
294 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
296 for (int i = 0; i < num_threads; i++)
297 enabled_array[i] = THREAD_DISABLED;
300 explore(act->get_tid());
304 * Records a backtracking reference for a thread choice within this Node.
305 * Provides feedback as to whether this thread choice is already set for
307 * @return false if the thread was already set to be backtracked, true
310 bool Node::set_backtrack(thread_id_t id)
312 int i = id_to_int(id);
313 ASSERT(i < ((int)backtrack.size()));
321 thread_id_t Node::get_next_backtrack()
323 /** @todo Find next backtrack */
325 for (i = 0; i < backtrack.size(); i++)
326 if (backtrack[i] == true)
328 /* Backtrack set was empty? */
329 ASSERT(i != backtrack.size());
331 backtrack[i] = false;
336 bool Node::is_enabled(Thread *t) const
338 int thread_id = id_to_int(t->get_id());
339 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
342 enabled_type_t Node::enabled_status(thread_id_t tid) const
344 int thread_id = id_to_int(tid);
345 if (thread_id < num_threads)
346 return enabled_array[thread_id];
348 return THREAD_DISABLED;
351 bool Node::is_enabled(thread_id_t tid) const
353 int thread_id = id_to_int(tid);
354 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
357 bool Node::has_priority(thread_id_t tid) const
359 return fairness[id_to_int(tid)].priority;
363 * Add an action to the may_read_from set.
364 * @param act is the action to add
366 void Node::add_read_from(const ModelAction *act)
368 may_read_from.push_back(act);
372 * Gets the next 'future_value' value from this Node. Only valid for a node
373 * where this->action is a 'read'.
374 * @return The first element in future_values
376 uint64_t Node::get_future_value() const
378 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
379 return future_values[future_index].value;
382 modelclock_t Node::get_future_value_expiration() const
384 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
385 return future_values[future_index].expiration;
389 int Node::get_read_from_size() const
391 return may_read_from.size();
394 const ModelAction * Node::get_read_from_at(int i) {
395 return may_read_from[i];
399 * Gets the next 'may_read_from' action from this Node. Only valid for a node
400 * where this->action is a 'read'.
401 * @return The first element in may_read_from
403 const ModelAction * Node::get_read_from() const
405 if (read_from_index < may_read_from.size())
406 return may_read_from[read_from_index];
412 * Increments the index into the readsfrom set to explore the next item.
413 * @return Returns false if we have explored all items.
415 bool Node::increment_read_from() {
418 if (read_from_index < may_read_from.size()) {
420 return read_from_index < may_read_from.size();
426 * Increments the index into the future_values set to explore the next item.
427 * @return Returns false if we have explored all values.
429 bool Node::increment_future_value() {
432 if (future_index < ((int)future_values.size())) {
434 return (future_index < ((int)future_values.size()));
440 * Add a write ModelAction to the set of writes that may break the release
441 * sequence. This is used during replay exploration of pending release
442 * sequences. This Node must correspond to a release sequence fixup action.
444 * @param write The write that may break the release sequence. NULL means we
445 * allow the release sequence to synchronize.
447 void Node::add_relseq_break(const ModelAction *write)
449 relseq_break_writes.push_back(write);
453 * Get the write that may break the current pending release sequence,
454 * according to the replay / divergence pattern.
456 * @return A write that may break the release sequence. If NULL, that means
457 * the release sequence should not be broken.
459 const ModelAction * Node::get_relseq_break() const
461 if (relseq_break_index < (int)relseq_break_writes.size())
462 return relseq_break_writes[relseq_break_index];
468 * Increments the index into the relseq_break_writes set to explore the next
470 * @return Returns false if we have explored all values.
472 bool Node::increment_relseq_break()
476 if (relseq_break_index < ((int)relseq_break_writes.size())) {
477 relseq_break_index++;
478 return (relseq_break_index < ((int)relseq_break_writes.size()));
484 * @return True if all writes that may break the release sequence have been
487 bool Node::relseq_break_empty() const
489 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
492 void Node::explore(thread_id_t tid)
494 int i = id_to_int(tid);
495 ASSERT(i < ((int)backtrack.size()));
497 backtrack[i] = false;
500 explored_children[i] = true;
503 NodeStack::NodeStack() :
504 node_list(1, new Node()),
511 NodeStack::~NodeStack()
513 for (unsigned int i = 0; i < node_list.size(); i++)
517 void NodeStack::print() const
519 model_print("............................................\n");
520 model_print("NodeStack printing node_list:\n");
521 for (unsigned int it = 0; it < node_list.size(); it++) {
522 if ((int)it == this->head_idx)
523 model_print("vvv following action is the current iterator vvv\n");
524 node_list[it]->print();
526 model_print("............................................\n");
529 /** Note: The is_enabled set contains what actions were enabled when
531 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
535 ASSERT(!node_list.empty());
537 if ((head_idx + 1) < (int)node_list.size()) {
539 return node_list[head_idx]->get_action();
543 get_head()->explore_child(act, is_enabled);
544 Node *prevfairness = NULL;
545 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
546 prevfairness = node_list[head_idx - model->params.fairwindow];
547 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
554 * Empties the stack of all trailing nodes after a given position and calls the
555 * destructor for each. This function is provided an offset which determines
556 * how many nodes (relative to the current replay state) to save before popping
558 * @param numAhead gives the number of Nodes (including this Node) to skip over
559 * before removing nodes.
561 void NodeStack::pop_restofstack(int numAhead)
563 /* Diverging from previous execution; clear out remainder of list */
564 unsigned int it = head_idx + numAhead;
565 for (unsigned int i = it; i < node_list.size(); i++)
567 node_list.resize(it);
570 Node * NodeStack::get_head() const
572 if (node_list.empty())
574 return node_list[head_idx];
577 Node * NodeStack::get_next() const
579 if (node_list.empty()) {
583 unsigned int it = head_idx + 1;
584 if (it == node_list.size()) {
588 return node_list[it];
591 void NodeStack::reset_execution()