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 */
96 model_print("******** empty action ********\n");
99 /** @brief Prints info about may_read_from set */
100 void Node::print_may_read_from()
102 for (unsigned int i = 0; i < may_read_from.size(); i++)
103 may_read_from[i]->print();
107 * Sets a promise to explore meeting with the given node.
108 * @param i is the promise index.
110 void Node::set_promise(unsigned int i, bool is_rmw) {
111 if (i >= promises.size())
112 promises.resize(i + 1, PROMISE_IGNORE);
113 if (promises[i] == PROMISE_IGNORE) {
114 promises[i] = PROMISE_UNFULFILLED;
116 promises[i] |= PROMISE_RMW;
121 * Looks up whether a given promise should be satisfied by this node.
122 * @param i The promise index.
123 * @return true if the promise should be satisfied by the given model action.
125 bool Node::get_promise(unsigned int i) const
127 return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
131 * Increments to the next combination of promises.
132 * @return true if we have a valid combination.
134 bool Node::increment_promise() {
136 unsigned int rmw_count = 0;
137 for (unsigned int i = 0; i < promises.size(); i++) {
138 if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED))
142 for (unsigned int i = 0; i < promises.size(); i++) {
143 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
144 if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
145 //sending our value to two rmws... not going to work..try next combination
148 promises[i] = (promises[i] & PROMISE_RMW) |PROMISE_FULFILLED;
151 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
152 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
155 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
163 * Returns whether the promise set is empty.
164 * @return true if we have explored all promise combinations.
166 bool Node::promise_empty() const
168 bool fulfilledrmw = false;
169 for (int i = promises.size() - 1; i >= 0; i--) {
170 if (promises[i] == PROMISE_UNFULFILLED)
172 if (!fulfilledrmw && ((promises[i]&PROMISE_MASK) == PROMISE_UNFULFILLED))
174 if (promises[i] == (PROMISE_FULFILLED|PROMISE_RMW))
181 void Node::set_misc_max(int i)
186 int Node::get_misc() const
191 bool Node::increment_misc() {
192 return (misc_index < misc_max) && ((++misc_index) < misc_max);
195 bool Node::misc_empty() const
197 return (misc_index + 1) >= misc_max;
202 * Adds a value from a weakly ordered future write to backtrack to. This
203 * operation may "fail" if the future value has already been run (within some
204 * sloppiness window of this expiration), or if the futurevalues set has
205 * reached its maximum.
206 * @see model_params.maxfuturevalues
208 * @param value is the value to backtrack to.
209 * @return True if the future value was successully added; false otherwise
211 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
212 int idx = -1; /* Highest index where value is found */
213 for (unsigned int i = 0; i < future_values.size(); i++) {
214 if (future_values[i].value == value) {
215 if (expiration <= future_values[i].expiration)
220 if (idx > future_index) {
221 /* Future value hasn't been explored; update expiration */
222 future_values[idx].expiration = expiration;
224 } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
225 /* Future value has been explored and is within the "sloppy" window */
229 /* Limit the size of the future-values set */
230 if (model->params.maxfuturevalues > 0 &&
231 (int)future_values.size() >= model->params.maxfuturevalues)
234 struct future_value newfv = {value, expiration};
235 future_values.push_back(newfv);
240 * Checks whether the future_values set for this node is empty.
241 * @return true if the future_values set is empty.
243 bool Node::future_value_empty() const
245 return ((future_index + 1) >= ((int)future_values.size()));
249 * Checks if the Thread associated with this thread ID has been explored from
251 * @param tid is the thread ID to check
252 * @return true if this thread choice has been explored already, false
255 bool Node::has_been_explored(thread_id_t tid) const
257 int id = id_to_int(tid);
258 return explored_children[id];
262 * Checks if the backtracking set is empty.
263 * @return true if the backtracking set is empty
265 bool Node::backtrack_empty() const
267 return (numBacktracks == 0);
271 * Checks whether the readsfrom set for this node is empty.
272 * @return true if the readsfrom set is empty.
274 bool Node::read_from_empty() const
276 return ((read_from_index + 1) >= may_read_from.size());
280 * Mark the appropriate backtracking information for exploring a thread choice.
281 * @param act The ModelAction to explore
283 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
286 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
287 if (is_enabled != NULL)
288 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
290 for (int i = 0; i < num_threads; i++)
291 enabled_array[i] = THREAD_DISABLED;
294 explore(act->get_tid());
298 * Records a backtracking reference for a thread choice within this Node.
299 * Provides feedback as to whether this thread choice is already set for
301 * @return false if the thread was already set to be backtracked, true
304 bool Node::set_backtrack(thread_id_t id)
306 int i = id_to_int(id);
307 ASSERT(i < ((int)backtrack.size()));
315 thread_id_t Node::get_next_backtrack()
317 /** @todo Find next backtrack */
319 for (i = 0; i < backtrack.size(); i++)
320 if (backtrack[i] == true)
322 /* Backtrack set was empty? */
323 ASSERT(i != backtrack.size());
325 backtrack[i] = false;
330 bool Node::is_enabled(Thread *t) const
332 int thread_id = id_to_int(t->get_id());
333 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
336 enabled_type_t Node::enabled_status(thread_id_t tid) const
338 int thread_id = id_to_int(tid);
339 if (thread_id < num_threads)
340 return enabled_array[thread_id];
342 return THREAD_DISABLED;
345 bool Node::is_enabled(thread_id_t tid) const
347 int thread_id = id_to_int(tid);
348 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
351 bool Node::has_priority(thread_id_t tid) const
353 return fairness[id_to_int(tid)].priority;
357 * Add an action to the may_read_from set.
358 * @param act is the action to add
360 void Node::add_read_from(const ModelAction *act)
362 may_read_from.push_back(act);
366 * Gets the next 'future_value' value from this Node. Only valid for a node
367 * where this->action is a 'read'.
368 * @return The first element in future_values
370 uint64_t Node::get_future_value() const
372 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
373 return future_values[future_index].value;
376 modelclock_t Node::get_future_value_expiration() const
378 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
379 return future_values[future_index].expiration;
383 int Node::get_read_from_size() const
385 return may_read_from.size();
388 const ModelAction * Node::get_read_from_at(int i) {
389 return may_read_from[i];
393 * Gets the next 'may_read_from' action from this Node. Only valid for a node
394 * where this->action is a 'read'.
395 * @return The first element in may_read_from
397 const ModelAction * Node::get_read_from() const
399 if (read_from_index < may_read_from.size())
400 return may_read_from[read_from_index];
406 * Increments the index into the readsfrom set to explore the next item.
407 * @return Returns false if we have explored all items.
409 bool Node::increment_read_from() {
412 if (read_from_index < may_read_from.size()) {
414 return read_from_index < may_read_from.size();
420 * Increments the index into the future_values set to explore the next item.
421 * @return Returns false if we have explored all values.
423 bool Node::increment_future_value() {
426 if (future_index < ((int)future_values.size())) {
428 return (future_index < ((int)future_values.size()));
434 * Add a write ModelAction to the set of writes that may break the release
435 * sequence. This is used during replay exploration of pending release
436 * sequences. This Node must correspond to a release sequence fixup action.
438 * @param write The write that may break the release sequence. NULL means we
439 * allow the release sequence to synchronize.
441 void Node::add_relseq_break(const ModelAction *write)
443 relseq_break_writes.push_back(write);
447 * Get the write that may break the current pending release sequence,
448 * according to the replay / divergence pattern.
450 * @return A write that may break the release sequence. If NULL, that means
451 * the release sequence should not be broken.
453 const ModelAction * Node::get_relseq_break() const
455 if (relseq_break_index < (int)relseq_break_writes.size())
456 return relseq_break_writes[relseq_break_index];
462 * Increments the index into the relseq_break_writes set to explore the next
464 * @return Returns false if we have explored all values.
466 bool Node::increment_relseq_break()
470 if (relseq_break_index < ((int)relseq_break_writes.size())) {
471 relseq_break_index++;
472 return (relseq_break_index < ((int)relseq_break_writes.size()));
478 * @return True if all writes that may break the release sequence have been
481 bool Node::relseq_break_empty() const
483 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
486 void Node::explore(thread_id_t tid)
488 int i = id_to_int(tid);
489 ASSERT(i < ((int)backtrack.size()));
491 backtrack[i] = false;
494 explored_children[i] = true;
497 NodeStack::NodeStack() :
498 node_list(1, new Node()),
505 NodeStack::~NodeStack()
507 for (unsigned int i = 0; i < node_list.size(); i++)
511 void NodeStack::print() const
513 model_print("............................................\n");
514 model_print("NodeStack printing node_list:\n");
515 for (unsigned int it = 0; it < node_list.size(); it++) {
516 if ((int)it == this->head_idx)
517 model_print("vvv following action is the current iterator vvv\n");
518 node_list[it]->print();
520 model_print("............................................\n");
523 /** Note: The is_enabled set contains what actions were enabled when
525 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
529 ASSERT(!node_list.empty());
531 if ((head_idx + 1) < (int)node_list.size()) {
533 return node_list[head_idx]->get_action();
537 get_head()->explore_child(act, is_enabled);
538 Node *prevfairness = NULL;
539 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
540 prevfairness = node_list[head_idx - model->params.fairwindow];
541 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
548 * Empties the stack of all trailing nodes after a given position and calls the
549 * destructor for each. This function is provided an offset which determines
550 * how many nodes (relative to the current replay state) to save before popping
552 * @param numAhead gives the number of Nodes (including this Node) to skip over
553 * before removing nodes.
555 void NodeStack::pop_restofstack(int numAhead)
557 /* Diverging from previous execution; clear out remainder of list */
558 unsigned int it = head_idx + numAhead;
559 for (unsigned int i = it; i < node_list.size(); i++)
561 node_list.resize(it);
564 Node * NodeStack::get_head() const
566 if (node_list.empty())
568 return node_list[head_idx];
571 Node * NodeStack::get_next() const
573 if (node_list.empty()) {
577 unsigned int it = head_idx + 1;
578 if (it == node_list.size()) {
582 return node_list[it];
585 void NodeStack::reset_execution()