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),
40 read_from_promise_idx(-1),
44 resolve_promise_idx(-1),
45 relseq_break_writes(),
46 relseq_break_index(0),
52 int currtid = id_to_int(act->get_tid());
53 int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
55 if (model->params.fairwindow != 0) {
56 for (int i = 0; i < num_threads; i++) {
57 ASSERT(i < ((int)fairness.size()));
58 struct fairness_info *fi = &fairness[i];
59 struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
63 if (parent && parent->is_enabled(int_to_id(i))) {
70 /* Do window processing */
71 if (prevfairness != NULL) {
72 if (prevfairness->parent->is_enabled(int_to_id(i)))
77 /* Need full window to start evaluating
79 * If we meet the enabled count and have no
80 * turns, give us priority */
81 if ((fi->enabled_count >= model->params.enabledcount) &&
89 /** @brief Node desctructor */
94 model_free(enabled_array);
97 /** Prints debugging info for the ModelAction associated with this Node */
98 void Node::print() const
101 model_print(" backtrack: %s", backtrack_empty() ? "empty" : "non-empty ");
102 for (int i = 0; i < (int)backtrack.size(); i++)
103 if (backtrack[i] == true)
104 model_print("[%d]", i);
107 model_print(" read from past: %s", read_from_past_empty() ? "empty" : "non-empty ");
108 for (int i = read_from_past_idx + 1; i < (int)read_from_past.size(); i++)
109 model_print("[%d]", read_from_past[i]->get_seq_number());
112 model_print(" read-from promises: %s", read_from_promise_empty() ? "empty" : "non-empty ");
113 for (int i = read_from_promise_idx + 1; i < (int)read_from_promises.size(); i++)
114 model_print("[%d]", read_from_promises[i]->get_seq_number());
117 model_print(" future values: %s", future_value_empty() ? "empty" : "non-empty ");
118 for (int i = future_index + 1; i < (int)future_values.size(); i++)
119 model_print("[%#" PRIx64 "]", future_values[i].value);
122 model_print(" promises: %s\n", promise_empty() ? "empty" : "non-empty");
123 model_print(" misc: %s\n", misc_empty() ? "empty" : "non-empty");
124 model_print(" rel seq break: %s\n", relseq_break_empty() ? "empty" : "non-empty");
127 /*********************************** promise **********************************/
130 * Sets a promise to explore meeting with the given node.
131 * @param i is the promise index.
133 void Node::set_promise(unsigned int i)
135 if (i >= resolve_promise.size())
136 resolve_promise.resize(i + 1, false);
137 resolve_promise[i] = true;
141 * Looks up whether a given promise should be satisfied by this node.
142 * @param i The promise index.
143 * @return true if the promise should be satisfied by the given ModelAction.
145 bool Node::get_promise(unsigned int i) const
147 return (i < resolve_promise.size()) && (int)i == resolve_promise_idx;
151 * Increments to the next promise to resolve.
152 * @return true if we have a valid combination.
154 bool Node::increment_promise()
157 if (resolve_promise.empty())
159 int prev_idx = resolve_promise_idx;
160 resolve_promise_idx++;
161 for ( ; resolve_promise_idx < (int)resolve_promise.size(); resolve_promise_idx++)
162 if (resolve_promise[resolve_promise_idx])
164 resolve_promise_idx = prev_idx;
169 * Returns whether the promise set is empty.
170 * @return true if we have explored all promise combinations.
172 bool Node::promise_empty() const
174 for (int i = resolve_promise_idx + 1; i < (int)resolve_promise.size(); i++)
175 if (i >= 0 && resolve_promise[i])
180 /** @brief Clear any promise-resolution information for this Node */
181 void Node::clear_promise_resolutions()
183 resolve_promise.clear();
184 resolve_promise_idx = -1;
187 /******************************* end promise **********************************/
189 void Node::set_misc_max(int i)
194 int Node::get_misc() const
199 bool Node::increment_misc()
201 return (misc_index < misc_max) && ((++misc_index) < misc_max);
204 bool Node::misc_empty() const
206 return (misc_index + 1) >= misc_max;
210 * Checks if the Thread associated with this thread ID has been explored from
212 * @param tid is the thread ID to check
213 * @return true if this thread choice has been explored already, false
216 bool Node::has_been_explored(thread_id_t tid) const
218 int id = id_to_int(tid);
219 return explored_children[id];
223 * Checks if the backtracking set is empty.
224 * @return true if the backtracking set is empty
226 bool Node::backtrack_empty() const
228 return (numBacktracks == 0);
232 * Mark the appropriate backtracking information for exploring a thread choice.
233 * @param act The ModelAction to explore
235 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
238 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
239 if (is_enabled != NULL)
240 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
242 for (int i = 0; i < num_threads; i++)
243 enabled_array[i] = THREAD_DISABLED;
246 explore(act->get_tid());
250 * Records a backtracking reference for a thread choice within this Node.
251 * Provides feedback as to whether this thread choice is already set for
253 * @return false if the thread was already set to be backtracked, true
256 bool Node::set_backtrack(thread_id_t id)
258 int i = id_to_int(id);
259 ASSERT(i < ((int)backtrack.size()));
267 thread_id_t Node::get_next_backtrack()
269 /** @todo Find next backtrack */
271 for (i = 0; i < backtrack.size(); i++)
272 if (backtrack[i] == true)
274 /* Backtrack set was empty? */
275 ASSERT(i != backtrack.size());
277 backtrack[i] = false;
282 void Node::clear_backtracking()
284 for (unsigned int i = 0; i < backtrack.size(); i++)
285 backtrack[i] = false;
286 for (unsigned int i = 0; i < explored_children.size(); i++)
287 explored_children[i] = false;
290 bool Node::is_enabled(Thread *t) const
292 int thread_id = id_to_int(t->get_id());
293 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
296 enabled_type_t Node::enabled_status(thread_id_t tid) const
298 int thread_id = id_to_int(tid);
299 if (thread_id < num_threads)
300 return enabled_array[thread_id];
302 return THREAD_DISABLED;
305 bool Node::is_enabled(thread_id_t tid) const
307 int thread_id = id_to_int(tid);
308 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
311 bool Node::has_priority(thread_id_t tid) const
313 return fairness[id_to_int(tid)].priority;
316 /*********************************** read from ********************************/
319 * Get the current state of the may-read-from set iteration
320 * @return The read-from type we should currently be checking (past or future)
322 read_from_type_t Node::get_read_from_status()
324 if (read_from_status == READ_FROM_PAST && read_from_past.empty())
325 increment_read_from();
326 return read_from_status;
330 * Iterate one step in the may-read-from iteration. This includes a step in
331 * reading from the either the past or the future.
332 * @return True if there is a new read-from to explore; false otherwise
334 bool Node::increment_read_from()
336 clear_promise_resolutions();
337 if (increment_read_from_past()) {
338 read_from_status = READ_FROM_PAST;
340 } else if (increment_read_from_promise()) {
341 read_from_status = READ_FROM_PROMISE;
343 } else if (increment_future_value()) {
344 read_from_status = READ_FROM_FUTURE;
347 read_from_status = READ_FROM_NONE;
352 * @return True if there are any new read-froms to explore
354 bool Node::read_from_empty() const
356 return read_from_past_empty() &&
357 read_from_promise_empty() &&
358 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() +
369 read_from_promises.size() +
370 future_values.size();
373 /******************************* end read from ********************************/
375 /****************************** read from past ********************************/
377 /** @brief Prints info about read_from_past set */
378 void Node::print_read_from_past()
380 for (unsigned int i = 0; i < read_from_past.size(); i++)
381 read_from_past[i]->print();
385 * Add an action to the read_from_past set.
386 * @param act is the action to add
388 void Node::add_read_from_past(const ModelAction *act)
390 read_from_past.push_back(act);
394 * Gets the next 'read_from_past' action from this Node. Only valid for a node
395 * where this->action is a 'read'.
396 * @return The first element in read_from_past
398 const ModelAction * Node::get_read_from_past() const
400 if (read_from_past_idx < read_from_past.size())
401 return read_from_past[read_from_past_idx];
406 const ModelAction * Node::get_read_from_past(int i) const
408 return read_from_past[i];
411 int Node::get_read_from_past_size() const
413 return read_from_past.size();
417 * Checks whether the readsfrom set for this node is empty.
418 * @return true if the readsfrom set is empty.
420 bool Node::read_from_past_empty() const
422 return ((read_from_past_idx + 1) >= read_from_past.size());
426 * Increments the index into the readsfrom set to explore the next item.
427 * @return Returns false if we have explored all items.
429 bool Node::increment_read_from_past()
432 if (read_from_past_idx < read_from_past.size()) {
433 read_from_past_idx++;
434 return read_from_past_idx < read_from_past.size();
439 /************************** end read from past ********************************/
441 /***************************** read_from_promises *****************************/
444 * Add an action to the read_from_promises set.
445 * @param reader The read which generated the Promise; we use the ModelAction
446 * instead of the Promise because the Promise does not last across executions
448 void Node::add_read_from_promise(const ModelAction *reader)
450 read_from_promises.push_back(reader);
454 * Gets the next 'read-from-promise' from this Node. Only valid for a node
455 * where this->action is a 'read'.
456 * @return The current element in read_from_promises
458 Promise * Node::get_read_from_promise() const
460 if (read_from_promise_idx < 0 || read_from_promise_idx >= ((int)read_from_promises.size()))
462 return read_from_promises[read_from_promise_idx]->get_reads_from_promise();
466 * Checks whether the read_from_promises set for this node is empty.
467 * @return true if the read_from_promises set is empty.
469 bool Node::read_from_promise_empty() const
471 return ((read_from_promise_idx + 1) >= ((int)read_from_promises.size()));
475 * Increments the index into the read_from_promises set to explore the next item.
476 * @return Returns false if we have explored all promises.
478 bool Node::increment_read_from_promise()
481 if (read_from_promise_idx < ((int)read_from_promises.size())) {
482 read_from_promise_idx++;
483 return (read_from_promise_idx < ((int)read_from_promises.size()));
488 /************************* end read_from_promises *****************************/
490 /****************************** future values *********************************/
493 * Adds a value from a weakly ordered future write to backtrack to. This
494 * operation may "fail" if the future value has already been run (within some
495 * sloppiness window of this expiration), or if the futurevalues set has
496 * reached its maximum.
497 * @see model_params.maxfuturevalues
499 * @param value is the value to backtrack to.
500 * @return True if the future value was successully added; false otherwise
502 bool Node::add_future_value(struct future_value fv)
504 uint64_t value = fv.value;
505 modelclock_t expiration = fv.expiration;
506 thread_id_t tid = fv.tid;
507 int idx = -1; /* Highest index where value is found */
508 for (unsigned int i = 0; i < future_values.size(); i++) {
509 if (future_values[i].value == value && future_values[i].tid == tid) {
510 if (expiration <= future_values[i].expiration)
515 if (idx > future_index) {
516 /* Future value hasn't been explored; update expiration */
517 future_values[idx].expiration = expiration;
519 } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
520 /* Future value has been explored and is within the "sloppy" window */
524 /* Limit the size of the future-values set */
525 if (model->params.maxfuturevalues > 0 &&
526 (int)future_values.size() >= model->params.maxfuturevalues)
529 future_values.push_back(fv);
534 * Gets the next 'future_value' from this Node. Only valid for a node where
535 * this->action is a 'read'.
536 * @return The first element in future_values
538 struct future_value Node::get_future_value() const
540 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
541 return future_values[future_index];
545 * Checks whether the future_values set for this node is empty.
546 * @return true if the future_values set is empty.
548 bool Node::future_value_empty() const
550 return ((future_index + 1) >= ((int)future_values.size()));
554 * Increments the index into the future_values set to explore the next item.
555 * @return Returns false if we have explored all values.
557 bool Node::increment_future_value()
560 if (future_index < ((int)future_values.size())) {
562 return (future_index < ((int)future_values.size()));
567 /************************** end future values *********************************/
570 * Add a write ModelAction to the set of writes that may break the release
571 * sequence. This is used during replay exploration of pending release
572 * sequences. This Node must correspond to a release sequence fixup action.
574 * @param write The write that may break the release sequence. NULL means we
575 * allow the release sequence to synchronize.
577 void Node::add_relseq_break(const ModelAction *write)
579 relseq_break_writes.push_back(write);
583 * Get the write that may break the current pending release sequence,
584 * according to the replay / divergence pattern.
586 * @return A write that may break the release sequence. If NULL, that means
587 * the release sequence should not be broken.
589 const ModelAction * Node::get_relseq_break() const
591 if (relseq_break_index < (int)relseq_break_writes.size())
592 return relseq_break_writes[relseq_break_index];
598 * Increments the index into the relseq_break_writes set to explore the next
600 * @return Returns false if we have explored all values.
602 bool Node::increment_relseq_break()
605 if (relseq_break_index < ((int)relseq_break_writes.size())) {
606 relseq_break_index++;
607 return (relseq_break_index < ((int)relseq_break_writes.size()));
613 * @return True if all writes that may break the release sequence have been
616 bool Node::relseq_break_empty() const
618 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
621 void Node::explore(thread_id_t tid)
623 int i = id_to_int(tid);
624 ASSERT(i < ((int)backtrack.size()));
626 backtrack[i] = false;
629 explored_children[i] = true;
632 NodeStack::NodeStack() :
640 NodeStack::~NodeStack()
642 for (unsigned int i = 0; i < node_list.size(); i++)
646 void NodeStack::print() const
648 model_print("............................................\n");
649 model_print("NodeStack printing node_list:\n");
650 for (unsigned int it = 0; it < node_list.size(); it++) {
651 if ((int)it == this->head_idx)
652 model_print("vvv following action is the current iterator vvv\n");
653 node_list[it]->print();
655 model_print("............................................\n");
658 /** Note: The is_enabled set contains what actions were enabled when
660 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
664 if ((head_idx + 1) < (int)node_list.size()) {
666 return node_list[head_idx]->get_action();
670 Node *head = get_head();
671 Node *prevfairness = NULL;
673 head->explore_child(act, is_enabled);
674 if (model->params.fairwindow != 0 && head_idx > (int)model->params.fairwindow)
675 prevfairness = node_list[head_idx - model->params.fairwindow];
678 int next_threads = model->get_num_threads();
679 if (act->get_type() == THREAD_CREATE)
681 node_list.push_back(new Node(act, head, next_threads, prevfairness));
688 * Empties the stack of all trailing nodes after a given position and calls the
689 * destructor for each. This function is provided an offset which determines
690 * how many nodes (relative to the current replay state) to save before popping
692 * @param numAhead gives the number of Nodes (including this Node) to skip over
693 * before removing nodes.
695 void NodeStack::pop_restofstack(int numAhead)
697 /* Diverging from previous execution; clear out remainder of list */
698 unsigned int it = head_idx + numAhead;
699 for (unsigned int i = it; i < node_list.size(); i++)
701 node_list.resize(it);
702 node_list.back()->clear_backtracking();
705 Node * NodeStack::get_head() const
707 if (node_list.empty() || head_idx < 0)
709 return node_list[head_idx];
712 Node * NodeStack::get_next() const
714 if (node_list.empty()) {
718 unsigned int it = head_idx + 1;
719 if (it == node_list.size()) {
723 return node_list[it];
726 void NodeStack::reset_execution()