1 #define __STDC_FORMAT_MACROS
10 #include "threads-model.h"
11 #include "modeltypes.h"
14 * @brief Node constructor
16 * Constructs a single Node for use in a NodeStack. Each Node is associated
17 * with exactly one ModelAction (exception: the first Node should be created
18 * as an empty stub, to represent the first thread "choice") and up to one
21 * @param act The ModelAction to associate with this Node. May be NULL.
22 * @param par The parent Node in the NodeStack. May be NULL if there is no
24 * @param nthreads The number of threads which exist at this point in the
27 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness) :
28 read_from_status(READ_FROM_PAST),
31 num_threads(nthreads),
32 explored_children(num_threads),
33 backtrack(num_threads),
34 fairness(num_threads),
38 read_from_past_idx(0),
41 relseq_break_writes(),
42 relseq_break_index(0),
48 int currtid = id_to_int(act->get_tid());
49 int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
51 if (model->params.fairwindow != 0) {
52 for (int i = 0; i < num_threads; i++) {
53 ASSERT(i < ((int)fairness.size()));
54 struct fairness_info *fi = &fairness[i];
55 struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
59 if (parent && parent->is_enabled(int_to_id(i))) {
66 /* Do window processing */
67 if (prevfairness != NULL) {
68 if (prevfairness->parent->is_enabled(int_to_id(i)))
73 /* Need full window to start evaluating
75 * If we meet the enabled count and have no
76 * turns, give us priority */
77 if ((fi->enabled_count >= model->params.enabledcount) &&
85 /** @brief Node desctructor */
90 model_free(enabled_array);
93 /** Prints debugging info for the ModelAction associated with this Node */
94 void Node::print() const
97 model_print(" backtrack: %s", backtrack_empty() ? "empty" : "non-empty ");
98 for (int i = 0; i < (int)backtrack.size(); i++)
99 if (backtrack[i] == true)
100 model_print("[%d]", i);
103 model_print(" read from past: %s", read_from_past_empty() ? "empty" : "non-empty ");
104 for (int i = read_from_past_idx + 1; i < (int)read_from_past.size(); i++)
105 model_print("[%d]", read_from_past[i]->get_seq_number());
108 model_print(" future values: %s", future_value_empty() ? "empty" : "non-empty ");
109 for (int i = future_index + 1; i < (int)future_values.size(); i++)
110 model_print("[%#" PRIx64 "]", future_values[i].value);
113 model_print(" promises: %s\n", promise_empty() ? "empty" : "non-empty");
114 model_print(" misc: %s\n", misc_empty() ? "empty" : "non-empty");
115 model_print(" rel seq break: %s\n", relseq_break_empty() ? "empty" : "non-empty");
119 * Sets a promise to explore meeting with the given node.
120 * @param i is the promise index.
122 void Node::set_promise(unsigned int i, bool is_rmw)
124 if (i >= promises.size())
125 promises.resize(i + 1, PROMISE_IGNORE);
126 if (promises[i] == PROMISE_IGNORE) {
127 promises[i] = PROMISE_UNFULFILLED;
129 promises[i] |= PROMISE_RMW;
134 * Looks up whether a given promise should be satisfied by this node.
135 * @param i The promise index.
136 * @return true if the promise should be satisfied by the given model action.
138 bool Node::get_promise(unsigned int i) const
140 return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
144 * Increments to the next combination of promises.
145 * @return true if we have a valid combination.
147 bool Node::increment_promise()
150 unsigned int rmw_count = 0;
151 for (unsigned int i = 0; i < promises.size(); i++) {
152 if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED))
156 for (unsigned int i = 0; i < promises.size(); i++) {
157 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
158 if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
159 //sending our value to two rmws... not going to work..try next combination
162 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_FULFILLED;
165 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
166 promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
169 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
177 * Returns whether the promise set is empty.
178 * @return true if we have explored all promise combinations.
180 bool Node::promise_empty() const
182 bool fulfilledrmw = false;
183 for (int i = promises.size() - 1; i >= 0; i--) {
184 if (promises[i] == PROMISE_UNFULFILLED)
186 if (!fulfilledrmw && ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED))
188 if (promises[i] == (PROMISE_FULFILLED | PROMISE_RMW))
194 void Node::set_misc_max(int i)
199 int Node::get_misc() const
204 bool Node::increment_misc()
206 return (misc_index < misc_max) && ((++misc_index) < misc_max);
209 bool Node::misc_empty() const
211 return (misc_index + 1) >= misc_max;
215 * Checks if the Thread associated with this thread ID has been explored from
217 * @param tid is the thread ID to check
218 * @return true if this thread choice has been explored already, false
221 bool Node::has_been_explored(thread_id_t tid) const
223 int id = id_to_int(tid);
224 return explored_children[id];
228 * Checks if the backtracking set is empty.
229 * @return true if the backtracking set is empty
231 bool Node::backtrack_empty() const
233 return (numBacktracks == 0);
237 * Mark the appropriate backtracking information for exploring a thread choice.
238 * @param act The ModelAction to explore
240 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
243 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
244 if (is_enabled != NULL)
245 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
247 for (int i = 0; i < num_threads; i++)
248 enabled_array[i] = THREAD_DISABLED;
251 explore(act->get_tid());
255 * Records a backtracking reference for a thread choice within this Node.
256 * Provides feedback as to whether this thread choice is already set for
258 * @return false if the thread was already set to be backtracked, true
261 bool Node::set_backtrack(thread_id_t id)
263 int i = id_to_int(id);
264 ASSERT(i < ((int)backtrack.size()));
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 void Node::clear_backtracking()
289 for (unsigned int i = 0; i < backtrack.size(); i++)
290 backtrack[i] = false;
291 for (unsigned int i = 0; i < explored_children.size(); i++)
292 explored_children[i] = false;
295 bool Node::is_enabled(Thread *t) const
297 int thread_id = id_to_int(t->get_id());
298 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
301 enabled_type_t Node::enabled_status(thread_id_t tid) const
303 int thread_id = id_to_int(tid);
304 if (thread_id < num_threads)
305 return enabled_array[thread_id];
307 return THREAD_DISABLED;
310 bool Node::is_enabled(thread_id_t tid) const
312 int thread_id = id_to_int(tid);
313 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
316 bool Node::has_priority(thread_id_t tid) const
318 return fairness[id_to_int(tid)].priority;
321 /*********************************** read from ********************************/
324 * Get the current state of the may-read-from set iteration
325 * @return The read-from type we should currently be checking (past or future)
327 read_from_type_t Node::get_read_from_status()
329 if (read_from_status == READ_FROM_PAST && read_from_past.empty())
330 increment_read_from();
331 return read_from_status;
335 * Iterate one step in the may-read-from iteration. This includes a step in
336 * reading from the either the past or the future.
337 * @return True if there is a new read-from to explore; false otherwise
339 bool Node::increment_read_from()
342 if (increment_read_from_past()) {
343 read_from_status = READ_FROM_PAST;
345 } else if (increment_future_value()) {
346 read_from_status = READ_FROM_FUTURE;
349 read_from_status = READ_FROM_NONE;
354 * @return True if there are any new read-froms to explore
356 bool Node::read_from_empty() const
358 return read_from_past_empty() && future_value_empty();
362 * Get the total size of the may-read-from set, including both past and future
364 * @return The size of may-read-from
366 unsigned int Node::read_from_size() const
368 return read_from_past.size() + future_values.size();
371 /******************************* end read from ********************************/
373 /****************************** read from past ********************************/
375 /** @brief Prints info about read_from_past set */
376 void Node::print_read_from_past()
378 for (unsigned int i = 0; i < read_from_past.size(); i++)
379 read_from_past[i]->print();
383 * Add an action to the read_from_past set.
384 * @param act is the action to add
386 void Node::add_read_from_past(const ModelAction *act)
388 read_from_past.push_back(act);
392 * Gets the next 'read_from_past' action from this Node. Only valid for a node
393 * where this->action is a 'read'.
394 * @return The first element in read_from_past
396 const ModelAction * Node::get_read_from_past() const
398 if (read_from_past_idx < read_from_past.size())
399 return read_from_past[read_from_past_idx];
404 const ModelAction * Node::get_read_from_past(int i) const
406 return read_from_past[i];
409 int Node::get_read_from_past_size() const
411 return read_from_past.size();
415 * Checks whether the readsfrom set for this node is empty.
416 * @return true if the readsfrom set is empty.
418 bool Node::read_from_past_empty() const
420 return ((read_from_past_idx + 1) >= read_from_past.size());
424 * Increments the index into the readsfrom set to explore the next item.
425 * @return Returns false if we have explored all items.
427 bool Node::increment_read_from_past()
430 if (read_from_past_idx < read_from_past.size()) {
431 read_from_past_idx++;
432 return read_from_past_idx < read_from_past.size();
437 /************************** end read from past ********************************/
439 /****************************** future values *********************************/
442 * Adds a value from a weakly ordered future write to backtrack to. This
443 * operation may "fail" if the future value has already been run (within some
444 * sloppiness window of this expiration), or if the futurevalues set has
445 * reached its maximum.
446 * @see model_params.maxfuturevalues
448 * @param value is the value to backtrack to.
449 * @return True if the future value was successully added; false otherwise
451 bool Node::add_future_value(struct future_value fv)
453 uint64_t value = fv.value;
454 modelclock_t expiration = fv.expiration;
455 thread_id_t tid = fv.tid;
456 int idx = -1; /* Highest index where value is found */
457 for (unsigned int i = 0; i < future_values.size(); i++) {
458 if (future_values[i].value == value && future_values[i].tid == tid) {
459 if (expiration <= future_values[i].expiration)
464 if (idx > future_index) {
465 /* Future value hasn't been explored; update expiration */
466 future_values[idx].expiration = expiration;
468 } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
469 /* Future value has been explored and is within the "sloppy" window */
473 /* Limit the size of the future-values set */
474 if (model->params.maxfuturevalues > 0 &&
475 (int)future_values.size() >= model->params.maxfuturevalues)
478 future_values.push_back(fv);
483 * Gets the next 'future_value' from this Node. Only valid for a node where
484 * this->action is a 'read'.
485 * @return The first element in future_values
487 struct future_value Node::get_future_value() const
489 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
490 return future_values[future_index];
494 * Checks whether the future_values set for this node is empty.
495 * @return true if the future_values set is empty.
497 bool Node::future_value_empty() const
499 return ((future_index + 1) >= ((int)future_values.size()));
503 * Increments the index into the future_values set to explore the next item.
504 * @return Returns false if we have explored all values.
506 bool Node::increment_future_value()
509 if (future_index < ((int)future_values.size())) {
511 return (future_index < ((int)future_values.size()));
516 /************************** end future values *********************************/
519 * Add a write ModelAction to the set of writes that may break the release
520 * sequence. This is used during replay exploration of pending release
521 * sequences. This Node must correspond to a release sequence fixup action.
523 * @param write The write that may break the release sequence. NULL means we
524 * allow the release sequence to synchronize.
526 void Node::add_relseq_break(const ModelAction *write)
528 relseq_break_writes.push_back(write);
532 * Get the write that may break the current pending release sequence,
533 * according to the replay / divergence pattern.
535 * @return A write that may break the release sequence. If NULL, that means
536 * the release sequence should not be broken.
538 const ModelAction * Node::get_relseq_break() const
540 if (relseq_break_index < (int)relseq_break_writes.size())
541 return relseq_break_writes[relseq_break_index];
547 * Increments the index into the relseq_break_writes set to explore the next
549 * @return Returns false if we have explored all values.
551 bool Node::increment_relseq_break()
555 if (relseq_break_index < ((int)relseq_break_writes.size())) {
556 relseq_break_index++;
557 return (relseq_break_index < ((int)relseq_break_writes.size()));
563 * @return True if all writes that may break the release sequence have been
566 bool Node::relseq_break_empty() const
568 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
571 void Node::explore(thread_id_t tid)
573 int i = id_to_int(tid);
574 ASSERT(i < ((int)backtrack.size()));
576 backtrack[i] = false;
579 explored_children[i] = true;
582 NodeStack::NodeStack() :
590 NodeStack::~NodeStack()
592 for (unsigned int i = 0; i < node_list.size(); i++)
596 void NodeStack::print() const
598 model_print("............................................\n");
599 model_print("NodeStack printing node_list:\n");
600 for (unsigned int it = 0; it < node_list.size(); it++) {
601 if ((int)it == this->head_idx)
602 model_print("vvv following action is the current iterator vvv\n");
603 node_list[it]->print();
605 model_print("............................................\n");
608 /** Note: The is_enabled set contains what actions were enabled when
610 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
614 if ((head_idx + 1) < (int)node_list.size()) {
616 return node_list[head_idx]->get_action();
620 Node *head = get_head();
621 Node *prevfairness = NULL;
623 head->explore_child(act, is_enabled);
624 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
625 prevfairness = node_list[head_idx - model->params.fairwindow];
628 int next_threads = model->get_num_threads();
629 if (act->get_type() == THREAD_CREATE)
631 node_list.push_back(new Node(act, head, next_threads, prevfairness));
638 * Empties the stack of all trailing nodes after a given position and calls the
639 * destructor for each. This function is provided an offset which determines
640 * how many nodes (relative to the current replay state) to save before popping
642 * @param numAhead gives the number of Nodes (including this Node) to skip over
643 * before removing nodes.
645 void NodeStack::pop_restofstack(int numAhead)
647 /* Diverging from previous execution; clear out remainder of list */
648 unsigned int it = head_idx + numAhead;
649 for (unsigned int i = it; i < node_list.size(); i++)
651 node_list.resize(it);
652 node_list.back()->clear_backtracking();
655 Node * NodeStack::get_head() const
657 if (node_list.empty() || head_idx < 0)
659 return node_list[head_idx];
662 Node * NodeStack::get_next() const
664 if (node_list.empty()) {
668 unsigned int it = head_idx + 1;
669 if (it == node_list.size()) {
673 return node_list[it];
676 void NodeStack::reset_execution()