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 conditions
69 //If we meet the enabled count and have no turns, give us priority
70 if ((fi->enabled_count >= model->params.enabledcount) &&
79 /** @brief Node desctructor */
85 model_free(enabled_array);
88 /** Prints debugging info for the ModelAction associated with this Node */
94 model_print("******** empty action ********\n");
97 /** @brief Prints info about may_read_from set */
98 void Node::print_may_read_from()
100 for (unsigned int i = 0; i < may_read_from.size(); i++)
101 may_read_from[i]->print();
105 * Sets a promise to explore meeting with the given node.
106 * @param i is the promise index.
108 void Node::set_promise(unsigned int i, bool is_rmw) {
109 if (i >= promises.size())
110 promises.resize(i + 1, PROMISE_IGNORE);
111 if (promises[i] == PROMISE_IGNORE) {
112 promises[i] = PROMISE_UNFULFILLED;
114 promises[i] |= PROMISE_RMW;
119 * Looks up whether a given promise should be satisfied by this node.
120 * @param i The promise index.
121 * @return true if the promise should be satisfied by the given model action.
123 bool Node::get_promise(unsigned int i) const
125 return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
129 * Increments to the next combination of promises.
130 * @return true if we have a valid combination.
132 bool Node::increment_promise() {
134 unsigned int rmw_count=0;
135 for (unsigned int i = 0; i < promises.size(); i++) {
136 if (promises[i]==(PROMISE_RMW|PROMISE_FULFILLED))
140 for (unsigned int i = 0; i < promises.size(); i++) {
141 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
142 if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
143 //sending our value to two rmws... not going to work..try next combination
146 promises[i] = (promises[i] & PROMISE_RMW) |PROMISE_FULFILLED;
149 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
150 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
153 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
161 * Returns whether the promise set is empty.
162 * @return true if we have explored all promise combinations.
164 bool Node::promise_empty() const
166 bool fulfilledrmw=false;
167 for (int i = promises.size()-1 ; i>=0; i--) {
168 if (promises[i]==PROMISE_UNFULFILLED)
170 if (!fulfilledrmw && ((promises[i]&PROMISE_MASK)==PROMISE_UNFULFILLED))
172 if (promises[i]==(PROMISE_FULFILLED|PROMISE_RMW))
179 void Node::set_misc_max(int i) {
183 int Node::get_misc() const
188 bool Node::increment_misc() {
189 return (misc_index<misc_max)&&((++misc_index)<misc_max);
192 bool Node::misc_empty() const
194 return (misc_index+1)>=misc_max;
199 * Adds a value from a weakly ordered future write to backtrack to. This
200 * operation may "fail" if the future value has already been run (within some
201 * sloppiness window of this expiration), or if the futurevalues set has
202 * reached its maximum.
203 * @see model_params.maxfuturevalues
205 * @param value is the value to backtrack to.
206 * @return True if the future value was successully added; false otherwise
208 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
209 int idx = -1; /* Highest index where value is found */
210 for (unsigned int i = 0; i < future_values.size(); i++) {
211 if (future_values[i].value == value) {
212 if (expiration <= future_values[i].expiration)
217 if (idx > future_index) {
218 /* Future value hasn't been explored; update expiration */
219 future_values[idx].expiration = expiration;
221 } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
222 /* Future value has been explored and is within the "sloppy" window */
226 /* Limit the size of the future-values set */
227 if (model->params.maxfuturevalues > 0 &&
228 (int)future_values.size() >= model->params.maxfuturevalues)
231 struct future_value newfv = {value, expiration};
232 future_values.push_back(newfv);
237 * Checks whether the future_values set for this node is empty.
238 * @return true if the future_values set is empty.
240 bool Node::future_value_empty() const
242 return ((future_index + 1) >= ((int)future_values.size()));
246 * Checks if the Thread associated with this thread ID has been explored from
248 * @param tid is the thread ID to check
249 * @return true if this thread choice has been explored already, false
252 bool Node::has_been_explored(thread_id_t tid) const
254 int id = id_to_int(tid);
255 return explored_children[id];
259 * Checks if the backtracking set is empty.
260 * @return true if the backtracking set is empty
262 bool Node::backtrack_empty() const
264 return (numBacktracks == 0);
268 * Checks whether the readsfrom set for this node is empty.
269 * @return true if the readsfrom set is empty.
271 bool Node::read_from_empty() const
273 return ((read_from_index+1) >= may_read_from.size());
277 * Mark the appropriate backtracking information for exploring a thread choice.
278 * @param act The ModelAction to explore
280 void Node::explore_child(ModelAction *act, enabled_type_t * is_enabled)
282 if ( ! enabled_array )
283 enabled_array=(enabled_type_t *)model_malloc(sizeof(enabled_type_t)*num_threads);
284 if (is_enabled != NULL)
285 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t)*num_threads);
287 for(int i=0;i<num_threads;i++)
288 enabled_array[i]=THREAD_DISABLED;
291 explore(act->get_tid());
295 * Records a backtracking reference for a thread choice within this Node.
296 * Provides feedback as to whether this thread choice is already set for
298 * @return false if the thread was already set to be backtracked, true
301 bool Node::set_backtrack(thread_id_t id)
303 int i = id_to_int(id);
304 ASSERT(i<((int)backtrack.size()));
312 thread_id_t Node::get_next_backtrack()
314 /** @todo Find next backtrack */
316 for (i = 0; i < backtrack.size(); i++)
317 if (backtrack[i] == true)
319 /* Backtrack set was empty? */
320 ASSERT(i != backtrack.size());
322 backtrack[i] = false;
327 bool Node::is_enabled(Thread *t) const
329 int thread_id=id_to_int(t->get_id());
330 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
333 enabled_type_t Node::enabled_status(thread_id_t tid) const
335 int thread_id = id_to_int(tid);
336 if (thread_id < num_threads)
337 return enabled_array[thread_id];
339 return THREAD_DISABLED;
342 bool Node::is_enabled(thread_id_t tid) const
344 int thread_id=id_to_int(tid);
345 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
348 bool Node::has_priority(thread_id_t tid) const
350 return fairness[id_to_int(tid)].priority;
354 * Add an action to the may_read_from set.
355 * @param act is the action to add
357 void Node::add_read_from(const ModelAction *act)
359 may_read_from.push_back(act);
363 * Gets the next 'future_value' value from this Node. Only valid for a node
364 * where this->action is a 'read'.
365 * @return The first element in future_values
367 uint64_t Node::get_future_value() const
369 ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
370 return future_values[future_index].value;
373 modelclock_t Node::get_future_value_expiration() const
375 ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
376 return future_values[future_index].expiration;
380 int Node::get_read_from_size() const
382 return may_read_from.size();
385 const ModelAction * Node::get_read_from_at(int i) {
386 return may_read_from[i];
390 * Gets the next 'may_read_from' action from this Node. Only valid for a node
391 * where this->action is a 'read'.
392 * @return The first element in may_read_from
394 const ModelAction * Node::get_read_from() const
396 if (read_from_index < may_read_from.size())
397 return may_read_from[read_from_index];
403 * Increments the index into the readsfrom set to explore the next item.
404 * @return Returns false if we have explored all items.
406 bool Node::increment_read_from() {
409 if (read_from_index < may_read_from.size()) {
411 return read_from_index < may_read_from.size();
417 * Increments the index into the future_values set to explore the next item.
418 * @return Returns false if we have explored all values.
420 bool Node::increment_future_value() {
423 if (future_index < ((int)future_values.size())) {
425 return (future_index < ((int)future_values.size()));
431 * Add a write ModelAction to the set of writes that may break the release
432 * sequence. This is used during replay exploration of pending release
433 * sequences. This Node must correspond to a release sequence fixup action.
435 * @param write The write that may break the release sequence. NULL means we
436 * allow the release sequence to synchronize.
438 void Node::add_relseq_break(const ModelAction *write)
440 relseq_break_writes.push_back(write);
444 * Get the write that may break the current pending release sequence,
445 * according to the replay / divergence pattern.
447 * @return A write that may break the release sequence. If NULL, that means
448 * the release sequence should not be broken.
450 const ModelAction * Node::get_relseq_break() const
452 if (relseq_break_index < (int)relseq_break_writes.size())
453 return relseq_break_writes[relseq_break_index];
459 * Increments the index into the relseq_break_writes set to explore the next
461 * @return Returns false if we have explored all values.
463 bool Node::increment_relseq_break()
467 if (relseq_break_index < ((int)relseq_break_writes.size())) {
468 relseq_break_index++;
469 return (relseq_break_index < ((int)relseq_break_writes.size()));
475 * @return True if all writes that may break the release sequence have been
478 bool Node::relseq_break_empty() const
480 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
483 void Node::explore(thread_id_t tid)
485 int i = id_to_int(tid);
486 ASSERT(i<((int)backtrack.size()));
488 backtrack[i] = false;
491 explored_children[i] = true;
494 NodeStack::NodeStack() :
495 node_list(1, new Node()),
502 NodeStack::~NodeStack()
504 for (unsigned int i = 0; i < node_list.size(); i++)
508 void NodeStack::print() const
510 model_print("............................................\n");
511 model_print("NodeStack printing node_list:\n");
512 for (unsigned int it = 0; it < node_list.size(); it++) {
513 if (it == this->iter)
514 model_print("vvv following action is the current iterator vvv\n");
515 node_list[it]->print();
517 model_print("............................................\n");
520 /** Note: The is_enabled set contains what actions were enabled when
523 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
527 ASSERT(!node_list.empty());
529 if ((iter+1) < node_list.size()) {
531 return node_list[iter]->get_action();
535 get_head()->explore_child(act, is_enabled);
536 Node *prevfairness = NULL;
537 if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
538 prevfairness = node_list[iter-model->params.fairwindow];
540 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
547 * Empties the stack of all trailing nodes after a given position and calls the
548 * destructor for each. This function is provided an offset which determines
549 * how many nodes (relative to the current replay state) to save before popping
551 * @param numAhead gives the number of Nodes (including this Node) to skip over
552 * before removing nodes.
554 void NodeStack::pop_restofstack(int numAhead)
556 /* Diverging from previous execution; clear out remainder of list */
557 unsigned int it=iter+numAhead;
558 for(unsigned int i=it;i<node_list.size();i++)
560 node_list.resize(it);
563 Node * NodeStack::get_head() const
565 if (node_list.empty())
567 return node_list[iter];
570 Node * NodeStack::get_next() const
572 if (node_list.empty()) {
576 unsigned int it=iter+1;
577 if (it == node_list.size()) {
581 return node_list[it];
584 void NodeStack::reset_execution()