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 printf("******** 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) {
124 return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
128 * Increments to the next combination of promises.
129 * @return true if we have a valid combination.
131 bool Node::increment_promise() {
133 unsigned int rmw_count=0;
134 for (unsigned int i = 0; i < promises.size(); i++) {
135 if (promises[i]==(PROMISE_RMW|PROMISE_FULFILLED))
139 for (unsigned int i = 0; i < promises.size(); i++) {
140 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
141 if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
142 //sending our value to two rmws... not going to work..try next combination
145 promises[i] = (promises[i] & PROMISE_RMW) |PROMISE_FULFILLED;
148 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
149 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
152 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
160 * Returns whether the promise set is empty.
161 * @return true if we have explored all promise combinations.
163 bool Node::promise_empty() {
164 bool fulfilledrmw=false;
165 for (int i = promises.size()-1 ; i>=0; i--) {
166 if (promises[i]==PROMISE_UNFULFILLED)
168 if (!fulfilledrmw && ((promises[i]&PROMISE_MASK)==PROMISE_UNFULFILLED))
170 if (promises[i]==(PROMISE_FULFILLED|PROMISE_RMW))
177 void Node::set_misc_max(int i) {
181 int Node::get_misc() {
185 bool Node::increment_misc() {
186 return (misc_index<misc_max)&&((++misc_index)<misc_max);
189 bool Node::misc_empty() {
190 return (misc_index+1)>=misc_max;
195 * Adds a value from a weakly ordered future write to backtrack to.
196 * @param value is the value to backtrack to.
198 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
199 int suitableindex=-1;
200 for (unsigned int i = 0; i < future_values.size(); i++) {
201 if (future_values[i].value == value) {
202 if (future_values[i].expiration>=expiration)
204 if (future_index < ((int) i)) {
210 if (suitableindex!=-1) {
211 future_values[suitableindex].expiration=expiration;
214 struct future_value newfv={value, expiration};
215 future_values.push_back(newfv);
220 * Checks whether the future_values set for this node is empty.
221 * @return true if the future_values set is empty.
223 bool Node::future_value_empty() {
224 return ((future_index + 1) >= ((int)future_values.size()));
228 * Checks if the Thread associated with this thread ID has been explored from
230 * @param tid is the thread ID to check
231 * @return true if this thread choice has been explored already, false
234 bool Node::has_been_explored(thread_id_t tid)
236 int id = id_to_int(tid);
237 return explored_children[id];
241 * Checks if the backtracking set is empty.
242 * @return true if the backtracking set is empty
244 bool Node::backtrack_empty()
246 return (numBacktracks == 0);
250 * Checks whether the readsfrom set for this node is empty.
251 * @return true if the readsfrom set is empty.
253 bool Node::read_from_empty() {
254 return ((read_from_index+1) >= may_read_from.size());
258 * Mark the appropriate backtracking information for exploring a thread choice.
259 * @param act The ModelAction to explore
261 void Node::explore_child(ModelAction *act, enabled_type_t * is_enabled)
263 if ( ! enabled_array )
264 enabled_array=(enabled_type_t *)model_malloc(sizeof(enabled_type_t)*num_threads);
265 if (is_enabled != NULL)
266 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t)*num_threads);
268 for(int i=0;i<num_threads;i++)
269 enabled_array[i]=THREAD_DISABLED;
272 explore(act->get_tid());
276 * Records a backtracking reference for a thread choice within this Node.
277 * Provides feedback as to whether this thread choice is already set for
279 * @return false if the thread was already set to be backtracked, true
282 bool Node::set_backtrack(thread_id_t id)
284 int i = id_to_int(id);
285 ASSERT(i<((int)backtrack.size()));
293 thread_id_t Node::get_next_backtrack()
295 /** @todo Find next backtrack */
297 for (i = 0; i < backtrack.size(); i++)
298 if (backtrack[i] == true)
300 /* Backtrack set was empty? */
301 ASSERT(i != backtrack.size());
303 backtrack[i] = false;
308 bool Node::is_enabled(Thread *t)
310 int thread_id=id_to_int(t->get_id());
311 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
314 enabled_type_t Node::enabled_status(thread_id_t tid) {
315 int thread_id=id_to_int(tid);
316 if (thread_id < num_threads)
317 return enabled_array[thread_id];
319 return THREAD_DISABLED;
322 bool Node::is_enabled(thread_id_t tid)
324 int thread_id=id_to_int(tid);
325 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
328 bool Node::has_priority(thread_id_t tid)
330 return fairness[id_to_int(tid)].priority;
334 * Add an action to the may_read_from set.
335 * @param act is the action to add
337 void Node::add_read_from(const ModelAction *act)
339 may_read_from.push_back(act);
343 * Gets the next 'future_value' value from this Node. Only valid for a node
344 * where this->action is a 'read'.
345 * @return The first element in future_values
347 uint64_t Node::get_future_value() {
348 ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
349 return future_values[future_index].value;
352 modelclock_t Node::get_future_value_expiration() {
353 ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
354 return future_values[future_index].expiration;
358 int Node::get_read_from_size() {
359 return may_read_from.size();
362 const ModelAction * Node::get_read_from_at(int i) {
363 return may_read_from[i];
367 * Gets the next 'may_read_from' action from this Node. Only valid for a node
368 * where this->action is a 'read'.
369 * @return The first element in may_read_from
371 const ModelAction * Node::get_read_from() {
372 if (read_from_index < may_read_from.size())
373 return may_read_from[read_from_index];
379 * Increments the index into the readsfrom set to explore the next item.
380 * @return Returns false if we have explored all items.
382 bool Node::increment_read_from() {
385 if (read_from_index < may_read_from.size()) {
387 return read_from_index < may_read_from.size();
393 * Increments the index into the future_values set to explore the next item.
394 * @return Returns false if we have explored all values.
396 bool Node::increment_future_value() {
399 if (future_index < ((int)future_values.size())) {
401 return (future_index < ((int)future_values.size()));
407 * Add a write ModelAction to the set of writes that may break the release
408 * sequence. This is used during replay exploration of pending release
409 * sequences. This Node must correspond to a release sequence fixup action.
411 * @param write The write that may break the release sequence. NULL means we
412 * allow the release sequence to synchronize.
414 void Node::add_relseq_break(const ModelAction *write)
416 relseq_break_writes.push_back(write);
420 * Get the write that may break the current pending release sequence,
421 * according to the replay / divergence pattern.
423 * @return A write that may break the release sequence. If NULL, that means
424 * the release sequence should not be broken.
426 const ModelAction * Node::get_relseq_break()
428 if (relseq_break_index < (int)relseq_break_writes.size())
429 return relseq_break_writes[relseq_break_index];
435 * Increments the index into the relseq_break_writes set to explore the next
437 * @return Returns false if we have explored all values.
439 bool Node::increment_relseq_break()
443 if (relseq_break_index < ((int)relseq_break_writes.size())) {
444 relseq_break_index++;
445 return (relseq_break_index < ((int)relseq_break_writes.size()));
451 * @return True if all writes that may break the release sequence have been
454 bool Node::relseq_break_empty() {
455 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
458 void Node::explore(thread_id_t tid)
460 int i = id_to_int(tid);
461 ASSERT(i<((int)backtrack.size()));
463 backtrack[i] = false;
466 explored_children[i] = true;
469 NodeStack::NodeStack() :
470 node_list(1, new Node()),
477 NodeStack::~NodeStack()
479 for (unsigned int i = 0; i < node_list.size(); i++)
483 void NodeStack::print()
485 printf("............................................\n");
486 printf("NodeStack printing node_list:\n");
487 for (unsigned int it = 0; it < node_list.size(); it++) {
488 if (it == this->iter)
489 printf("vvv following action is the current iterator vvv\n");
490 node_list[it]->print();
492 printf("............................................\n");
495 /** Note: The is_enabled set contains what actions were enabled when
498 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
502 ASSERT(!node_list.empty());
504 if ((iter+1) < node_list.size()) {
506 return node_list[iter]->get_action();
510 get_head()->explore_child(act, is_enabled);
511 Node *prevfairness = NULL;
512 if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
513 prevfairness = node_list[iter-model->params.fairwindow];
515 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
522 * Empties the stack of all trailing nodes after a given position and calls the
523 * destructor for each. This function is provided an offset which determines
524 * how many nodes (relative to the current replay state) to save before popping
526 * @param numAhead gives the number of Nodes (including this Node) to skip over
527 * before removing nodes.
529 void NodeStack::pop_restofstack(int numAhead)
531 /* Diverging from previous execution; clear out remainder of list */
532 unsigned int it=iter+numAhead;
533 for(unsigned int i=it;i<node_list.size();i++)
535 node_list.resize(it);
538 Node * NodeStack::get_head()
540 if (node_list.empty())
542 return node_list[iter];
545 Node * NodeStack::get_next()
547 if (node_list.empty()) {
551 unsigned int it=iter+1;
552 if (it == node_list.size()) {
556 return node_list[it];
559 void NodeStack::reset_execution()