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. This
196 * operation may "fail" if the future value is already set (with a later
197 * expiration), or if the futurevalues set has reached its maximum.
198 * @see model_params.maxfuturevalues
200 * @param value is the value to backtrack to.
201 * @return True if the future value was successully added; false otherwise
203 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
204 int suitableindex=-1;
205 for (unsigned int i = 0; i < future_values.size(); i++) {
206 if (future_values[i].value == value) {
207 if (future_values[i].expiration>=expiration)
209 if (future_index < ((int) i)) {
215 if (suitableindex!=-1) {
216 future_values[suitableindex].expiration=expiration;
220 if (model->params.maxfuturevalues > 0 &&
221 (int)future_values.size() >= model->params.maxfuturevalues)
224 struct future_value newfv = {value, expiration};
225 future_values.push_back(newfv);
230 * Checks whether the future_values set for this node is empty.
231 * @return true if the future_values set is empty.
233 bool Node::future_value_empty() {
234 return ((future_index + 1) >= ((int)future_values.size()));
238 * Checks if the Thread associated with this thread ID has been explored from
240 * @param tid is the thread ID to check
241 * @return true if this thread choice has been explored already, false
244 bool Node::has_been_explored(thread_id_t tid)
246 int id = id_to_int(tid);
247 return explored_children[id];
251 * Checks if the backtracking set is empty.
252 * @return true if the backtracking set is empty
254 bool Node::backtrack_empty()
256 return (numBacktracks == 0);
260 * Checks whether the readsfrom set for this node is empty.
261 * @return true if the readsfrom set is empty.
263 bool Node::read_from_empty() {
264 return ((read_from_index+1) >= may_read_from.size());
268 * Mark the appropriate backtracking information for exploring a thread choice.
269 * @param act The ModelAction to explore
271 void Node::explore_child(ModelAction *act, enabled_type_t * is_enabled)
273 if ( ! enabled_array )
274 enabled_array=(enabled_type_t *)model_malloc(sizeof(enabled_type_t)*num_threads);
275 if (is_enabled != NULL)
276 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t)*num_threads);
278 for(int i=0;i<num_threads;i++)
279 enabled_array[i]=THREAD_DISABLED;
282 explore(act->get_tid());
286 * Records a backtracking reference for a thread choice within this Node.
287 * Provides feedback as to whether this thread choice is already set for
289 * @return false if the thread was already set to be backtracked, true
292 bool Node::set_backtrack(thread_id_t id)
294 int i = id_to_int(id);
295 ASSERT(i<((int)backtrack.size()));
303 thread_id_t Node::get_next_backtrack()
305 /** @todo Find next backtrack */
307 for (i = 0; i < backtrack.size(); i++)
308 if (backtrack[i] == true)
310 /* Backtrack set was empty? */
311 ASSERT(i != backtrack.size());
313 backtrack[i] = false;
318 bool Node::is_enabled(Thread *t)
320 int thread_id=id_to_int(t->get_id());
321 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
324 enabled_type_t Node::enabled_status(thread_id_t tid) {
325 int thread_id=id_to_int(tid);
326 if (thread_id < num_threads)
327 return enabled_array[thread_id];
329 return THREAD_DISABLED;
332 bool Node::is_enabled(thread_id_t tid)
334 int thread_id=id_to_int(tid);
335 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
338 bool Node::has_priority(thread_id_t tid)
340 return fairness[id_to_int(tid)].priority;
344 * Add an action to the may_read_from set.
345 * @param act is the action to add
347 void Node::add_read_from(const ModelAction *act)
349 may_read_from.push_back(act);
353 * Gets the next 'future_value' value from this Node. Only valid for a node
354 * where this->action is a 'read'.
355 * @return The first element in future_values
357 uint64_t Node::get_future_value() {
358 ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
359 return future_values[future_index].value;
362 modelclock_t Node::get_future_value_expiration() {
363 ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
364 return future_values[future_index].expiration;
368 int Node::get_read_from_size() {
369 return may_read_from.size();
372 const ModelAction * Node::get_read_from_at(int i) {
373 return may_read_from[i];
377 * Gets the next 'may_read_from' action from this Node. Only valid for a node
378 * where this->action is a 'read'.
379 * @return The first element in may_read_from
381 const ModelAction * Node::get_read_from() {
382 if (read_from_index < may_read_from.size())
383 return may_read_from[read_from_index];
389 * Increments the index into the readsfrom set to explore the next item.
390 * @return Returns false if we have explored all items.
392 bool Node::increment_read_from() {
395 if (read_from_index < may_read_from.size()) {
397 return read_from_index < may_read_from.size();
403 * Increments the index into the future_values set to explore the next item.
404 * @return Returns false if we have explored all values.
406 bool Node::increment_future_value() {
409 if (future_index < ((int)future_values.size())) {
411 return (future_index < ((int)future_values.size()));
417 * Add a write ModelAction to the set of writes that may break the release
418 * sequence. This is used during replay exploration of pending release
419 * sequences. This Node must correspond to a release sequence fixup action.
421 * @param write The write that may break the release sequence. NULL means we
422 * allow the release sequence to synchronize.
424 void Node::add_relseq_break(const ModelAction *write)
426 relseq_break_writes.push_back(write);
430 * Get the write that may break the current pending release sequence,
431 * according to the replay / divergence pattern.
433 * @return A write that may break the release sequence. If NULL, that means
434 * the release sequence should not be broken.
436 const ModelAction * Node::get_relseq_break()
438 if (relseq_break_index < (int)relseq_break_writes.size())
439 return relseq_break_writes[relseq_break_index];
445 * Increments the index into the relseq_break_writes set to explore the next
447 * @return Returns false if we have explored all values.
449 bool Node::increment_relseq_break()
453 if (relseq_break_index < ((int)relseq_break_writes.size())) {
454 relseq_break_index++;
455 return (relseq_break_index < ((int)relseq_break_writes.size()));
461 * @return True if all writes that may break the release sequence have been
464 bool Node::relseq_break_empty() {
465 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
468 void Node::explore(thread_id_t tid)
470 int i = id_to_int(tid);
471 ASSERT(i<((int)backtrack.size()));
473 backtrack[i] = false;
476 explored_children[i] = true;
479 NodeStack::NodeStack() :
480 node_list(1, new Node()),
487 NodeStack::~NodeStack()
489 for (unsigned int i = 0; i < node_list.size(); i++)
493 void NodeStack::print()
495 printf("............................................\n");
496 printf("NodeStack printing node_list:\n");
497 for (unsigned int it = 0; it < node_list.size(); it++) {
498 if (it == this->iter)
499 printf("vvv following action is the current iterator vvv\n");
500 node_list[it]->print();
502 printf("............................................\n");
505 /** Note: The is_enabled set contains what actions were enabled when
508 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
512 ASSERT(!node_list.empty());
514 if ((iter+1) < node_list.size()) {
516 return node_list[iter]->get_action();
520 get_head()->explore_child(act, is_enabled);
521 Node *prevfairness = NULL;
522 if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
523 prevfairness = node_list[iter-model->params.fairwindow];
525 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
532 * Empties the stack of all trailing nodes after a given position and calls the
533 * destructor for each. This function is provided an offset which determines
534 * how many nodes (relative to the current replay state) to save before popping
536 * @param numAhead gives the number of Nodes (including this Node) to skip over
537 * before removing nodes.
539 void NodeStack::pop_restofstack(int numAhead)
541 /* Diverging from previous execution; clear out remainder of list */
542 unsigned int it=iter+numAhead;
543 for(unsigned int i=it;i<node_list.size();i++)
545 node_list.resize(it);
548 Node * NodeStack::get_head()
550 if (node_list.empty())
552 return node_list[iter];
555 Node * NodeStack::get_next()
557 if (node_list.empty()) {
561 unsigned int it=iter+1;
562 if (it == node_list.size()) {
566 return node_list[it];
569 void NodeStack::reset_execution()