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(i)) {
61 //Do window processing
62 if (prevfairness != NULL) {
63 if (prevfairness -> parent->is_enabled(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) {
109 if (i >= promises.size())
110 promises.resize(i + 1, PROMISE_IGNORE);
111 if (promises[i] == PROMISE_IGNORE)
112 promises[i] = PROMISE_UNFULFILLED;
116 * Looks up whether a given promise should be satisfied by this node.
117 * @param i The promise index.
118 * @return true if the promise should be satisfied by the given model action.
120 bool Node::get_promise(unsigned int i) {
121 return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
125 * Increments to the next combination of promises.
126 * @return true if we have a valid combination.
128 bool Node::increment_promise() {
131 for (unsigned int i = 0; i < promises.size(); i++) {
132 if (promises[i] == PROMISE_UNFULFILLED) {
133 promises[i] = PROMISE_FULFILLED;
136 if (promises[i] == PROMISE_FULFILLED)
137 promises[i] = PROMISE_UNFULFILLED;
146 * Returns whether the promise set is empty.
147 * @return true if we have explored all promise combinations.
149 bool Node::promise_empty() {
150 for (unsigned int i = 0; i < promises.size();i++)
151 if (promises[i] == PROMISE_UNFULFILLED)
157 void Node::set_misc_max(int i) {
161 int Node::get_misc() {
165 bool Node::increment_misc() {
166 return (misc_index<misc_max)&&((++misc_index)<misc_max);
169 bool Node::misc_empty() {
170 return (misc_index+1)>=misc_max;
175 * Adds a value from a weakly ordered future write to backtrack to.
176 * @param value is the value to backtrack to.
178 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
179 int suitableindex=-1;
180 for (unsigned int i = 0; i < future_values.size(); i++) {
181 if (future_values[i].value == value) {
182 if (future_values[i].expiration>=expiration)
184 if (future_index < ((int) i)) {
190 if (suitableindex!=-1) {
191 future_values[suitableindex].expiration=expiration;
194 struct future_value newfv={value, expiration};
195 future_values.push_back(newfv);
200 * Checks whether the future_values set for this node is empty.
201 * @return true if the future_values set is empty.
203 bool Node::future_value_empty() {
204 return ((future_index + 1) >= ((int)future_values.size()));
208 * Checks if the Thread associated with this thread ID has been explored from
210 * @param tid is the thread ID to check
211 * @return true if this thread choice has been explored already, false
214 bool Node::has_been_explored(thread_id_t tid)
216 int id = id_to_int(tid);
217 return explored_children[id];
221 * Checks if the backtracking set is empty.
222 * @return true if the backtracking set is empty
224 bool Node::backtrack_empty()
226 return (numBacktracks == 0);
230 * Checks whether the readsfrom set for this node is empty.
231 * @return true if the readsfrom set is empty.
233 bool Node::read_from_empty() {
234 return ((read_from_index+1) >= may_read_from.size());
238 * Mark the appropriate backtracking information for exploring a thread choice.
239 * @param act The ModelAction to explore
241 void Node::explore_child(ModelAction *act, enabled_type_t * is_enabled)
243 if ( ! enabled_array )
244 enabled_array=(enabled_type_t *)model_malloc(sizeof(enabled_type_t)*num_threads);
245 if (is_enabled != NULL)
246 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t)*num_threads);
248 for(int i=0;i<num_threads;i++)
249 enabled_array[i]=THREAD_DISABLED;
252 explore(act->get_tid());
256 * Records a backtracking reference for a thread choice within this Node.
257 * Provides feedback as to whether this thread choice is already set for
259 * @return false if the thread was already set to be backtracked, true
262 bool Node::set_backtrack(thread_id_t id)
264 int i = id_to_int(id);
272 thread_id_t Node::get_next_backtrack()
274 /** @todo Find next backtrack */
276 for (i = 0; i < backtrack.size(); i++)
277 if (backtrack[i] == true)
279 /* Backtrack set was empty? */
280 ASSERT(i != backtrack.size());
282 backtrack[i] = false;
287 bool Node::is_enabled(Thread *t)
289 int thread_id=id_to_int(t->get_id());
290 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
293 bool Node::is_enabled(thread_id_t tid)
295 int thread_id=id_to_int(tid);
296 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
299 bool Node::has_priority(thread_id_t tid)
301 return fairness[id_to_int(tid)].priority;
305 * Add an action to the may_read_from set.
306 * @param act is the action to add
308 void Node::add_read_from(const ModelAction *act)
310 may_read_from.push_back(act);
314 * Gets the next 'future_value' value from this Node. Only valid for a node
315 * where this->action is a 'read'.
316 * @return The first element in future_values
318 uint64_t Node::get_future_value() {
319 ASSERT(future_index<((int)future_values.size()));
320 return future_values[future_index].value;
323 modelclock_t Node::get_future_value_expiration() {
324 ASSERT(future_index<((int)future_values.size()));
325 return future_values[future_index].expiration;
329 int Node::get_read_from_size() {
330 return may_read_from.size();
333 const ModelAction * Node::get_read_from_at(int i) {
334 return may_read_from[i];
338 * Gets the next 'may_read_from' action from this Node. Only valid for a node
339 * where this->action is a 'read'.
340 * @return The first element in may_read_from
342 const ModelAction * Node::get_read_from() {
343 if (read_from_index < may_read_from.size())
344 return may_read_from[read_from_index];
350 * Increments the index into the readsfrom set to explore the next item.
351 * @return Returns false if we have explored all items.
353 bool Node::increment_read_from() {
356 if (read_from_index < may_read_from.size()) {
358 return read_from_index < may_read_from.size();
364 * Increments the index into the future_values set to explore the next item.
365 * @return Returns false if we have explored all values.
367 bool Node::increment_future_value() {
370 if (future_index < ((int)future_values.size())) {
372 return (future_index < ((int)future_values.size()));
378 * Add a write ModelAction to the set of writes that may break the release
379 * sequence. This is used during replay exploration of pending release
380 * sequences. This Node must correspond to a release sequence fixup action.
382 * @param write The write that may break the release sequence. NULL means we
383 * allow the release sequence to synchronize.
385 void Node::add_relseq_break(const ModelAction *write)
387 relseq_break_writes.push_back(write);
391 * Get the write that may break the current pending release sequence,
392 * according to the replay / divergence pattern.
394 * @return A write that may break the release sequence. If NULL, that means
395 * the release sequence should not be broken.
397 const ModelAction * Node::get_relseq_break()
399 if (relseq_break_index < (int)relseq_break_writes.size())
400 return relseq_break_writes[relseq_break_index];
406 * Increments the index into the relseq_break_writes set to explore the next
408 * @return Returns false if we have explored all values.
410 bool Node::increment_relseq_break()
414 if (relseq_break_index < ((int)relseq_break_writes.size())) {
415 relseq_break_index++;
416 return (relseq_break_index < ((int)relseq_break_writes.size()));
422 * @return True if all writes that may break the release sequence have been
425 bool Node::relseq_break_empty() {
426 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
429 void Node::explore(thread_id_t tid)
431 int i = id_to_int(tid);
433 backtrack[i] = false;
436 explored_children[i] = true;
439 NodeStack::NodeStack() :
440 node_list(1, new Node()),
447 NodeStack::~NodeStack()
449 for (unsigned int i = 0; i < node_list.size(); i++)
453 void NodeStack::print()
455 printf("............................................\n");
456 printf("NodeStack printing node_list:\n");
457 for (unsigned int it = 0; it < node_list.size(); it++) {
458 if (it == this->iter)
459 printf("vvv following action is the current iterator vvv\n");
460 node_list[it]->print();
462 printf("............................................\n");
465 /** Note: The is_enabled set contains what actions were enabled when
468 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
472 ASSERT(!node_list.empty());
474 if ((iter+1) < node_list.size()) {
476 return node_list[iter]->get_action();
480 get_head()->explore_child(act, is_enabled);
481 Node *prevfairness = NULL;
482 if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
483 prevfairness = node_list[iter-model->params.fairwindow];
485 node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
492 * Empties the stack of all trailing nodes after a given position and calls the
493 * destructor for each. This function is provided an offset which determines
494 * how many nodes (relative to the current replay state) to save before popping
496 * @param numAhead gives the number of Nodes (including this Node) to skip over
497 * before removing nodes.
499 void NodeStack::pop_restofstack(int numAhead)
501 /* Diverging from previous execution; clear out remainder of list */
502 unsigned int it=iter+numAhead;
503 for(unsigned int i=it;i<node_list.size();i++)
505 node_list.resize(it);
508 Node * NodeStack::get_head()
510 if (node_list.empty())
512 return node_list[iter];
515 Node * NodeStack::get_next()
517 if (node_list.empty()) {
521 unsigned int it=iter+1;
522 if (it == node_list.size()) {
526 return node_list[it];
529 void NodeStack::reset_execution()