1 #define __STDC_FORMAT_MACROS
10 #include "threads-model.h"
11 #include "modeltypes.h"
12 #include "execution.h"
16 * @brief Node constructor
18 * Constructs a single Node for use in a NodeStack. Each Node is associated
19 * with exactly one ModelAction (exception: the first Node should be created
20 * as an empty stub, to represent the first thread "choice") and up to one
23 * @param params The model-checker parameters
24 * @param act The ModelAction to associate with this Node. May be NULL.
25 * @param par The parent Node in the NodeStack. May be NULL if there is no
27 * @param nthreads The number of threads which exist at this point in the
30 Node::Node(const struct model_params *params, ModelAction *act, Node *par,
31 int nthreads, Node *prevfairness) :
32 read_from_status(READ_FROM_PAST),
37 num_threads(nthreads),
38 explored_children(num_threads),
39 backtrack(num_threads),
40 fairness(num_threads),
44 read_from_past_idx(0),
46 read_from_promise_idx(-1),
50 resolve_promise_idx(-1),
51 relseq_break_writes(),
52 relseq_break_index(0),
59 int currtid = id_to_int(act->get_tid());
60 int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
62 if (get_params()->fairwindow != 0) {
63 for (int i = 0; i < num_threads; i++) {
64 ASSERT(i < ((int)fairness.size()));
65 struct fairness_info *fi = &fairness[i];
66 struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
70 if (parent && parent->is_enabled(int_to_id(i))) {
77 /* Do window processing */
78 if (prevfairness != NULL) {
79 if (prevfairness->parent->is_enabled(int_to_id(i)))
84 /* Need full window to start evaluating
86 * If we meet the enabled count and have no
87 * turns, give us priority */
88 if ((fi->enabled_count >= get_params()->enabledcount) &&
96 int Node::get_yield_data(int tid1, int tid2) const {
97 if (tid1<num_threads && tid2 < num_threads)
98 return yield_data[YIELD_INDEX(tid1,tid2,num_threads)];
100 return YIELD_S | YIELD_D;
103 void Node::update_yield(Scheduler * scheduler) {
104 if (yield_data==NULL)
105 yield_data=(int *) model_calloc(1, sizeof(int)*num_threads*num_threads);
107 if (parent == NULL) {
108 for(int i = 0; i < num_threads*num_threads; i++) {
109 yield_data[i] = YIELD_S | YIELD_D;
113 int curr_tid=id_to_int(action->get_tid());
115 for(int u = 0; u < num_threads; u++) {
116 for(int v = 0; v < num_threads; v++) {
117 int yield_state=parent->get_yield_data(u, v);
118 bool next_enabled=scheduler->is_enabled(int_to_id(v));
119 bool curr_enabled=parent->is_enabled(int_to_id(v));
121 //Compute intersection of ES and E
122 yield_state&=~YIELD_E;
123 //Check to see if we disabled the thread
124 if (u==curr_tid && curr_enabled)
125 yield_state|=YIELD_D;
127 yield_data[YIELD_INDEX(u, v, num_threads)]=yield_state;
129 yield_data[YIELD_INDEX(u, curr_tid, num_threads)]=(yield_data[YIELD_INDEX(u, curr_tid, num_threads)]&~YIELD_P)|YIELD_S;
131 //handle curr.yield(t) part of computation
132 if (action->is_yield()) {
133 for(int v = 0; v < num_threads; v++) {
134 int yield_state=yield_data[YIELD_INDEX(curr_tid, v, num_threads)];
135 if ((yield_state & (YIELD_E | YIELD_D)) && (!(yield_state & YIELD_S)))
136 yield_state |= YIELD_P;
137 yield_state &= YIELD_P;
138 if (scheduler->is_enabled(int_to_id(v))) {
139 yield_state|=YIELD_E;
141 yield_data[YIELD_INDEX(curr_tid, v, num_threads)]=yield_state;
146 /** @brief Node desctructor */
151 delete uninit_action;
153 model_free(enabled_array);
155 model_free(yield_data);
158 /** Prints debugging info for the ModelAction associated with this Node */
159 void Node::print() const
162 model_print(" thread status: ");
164 for (int i = 0; i < num_threads; i++) {
166 enabled_type_to_string(enabled_array[i], str);
167 model_print("[%d: %s]", i, str);
171 model_print("(info not available)\n");
172 model_print(" backtrack: %s", backtrack_empty() ? "empty" : "non-empty ");
173 for (int i = 0; i < (int)backtrack.size(); i++)
174 if (backtrack[i] == true)
175 model_print("[%d]", i);
178 model_print(" read from past: %s", read_from_past_empty() ? "empty" : "non-empty ");
179 for (int i = read_from_past_idx + 1; i < (int)read_from_past.size(); i++)
180 model_print("[%d]", read_from_past[i]->get_seq_number());
183 model_print(" read-from promises: %s", read_from_promise_empty() ? "empty" : "non-empty ");
184 for (int i = read_from_promise_idx + 1; i < (int)read_from_promises.size(); i++)
185 model_print("[%d]", read_from_promises[i]->get_seq_number());
188 model_print(" future values: %s", future_value_empty() ? "empty" : "non-empty ");
189 for (int i = future_index + 1; i < (int)future_values.size(); i++)
190 model_print("[%#" PRIx64 "]", future_values[i].value);
193 model_print(" promises: %s\n", promise_empty() ? "empty" : "non-empty");
194 model_print(" misc: %s\n", misc_empty() ? "empty" : "non-empty");
195 model_print(" rel seq break: %s\n", relseq_break_empty() ? "empty" : "non-empty");
198 /****************************** threads backtracking **************************/
201 * Checks if the Thread associated with this thread ID has been explored from
203 * @param tid is the thread ID to check
204 * @return true if this thread choice has been explored already, false
207 bool Node::has_been_explored(thread_id_t tid) const
209 int id = id_to_int(tid);
210 return explored_children[id];
214 * Checks if the backtracking set is empty.
215 * @return true if the backtracking set is empty
217 bool Node::backtrack_empty() const
219 return (numBacktracks == 0);
222 void Node::explore(thread_id_t tid)
224 int i = id_to_int(tid);
225 ASSERT(i < ((int)backtrack.size()));
227 backtrack[i] = false;
230 explored_children[i] = true;
234 * Mark the appropriate backtracking information for exploring a thread choice.
235 * @param act The ModelAction to explore
237 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
240 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
241 if (is_enabled != NULL)
242 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
244 for (int i = 0; i < num_threads; i++)
245 enabled_array[i] = THREAD_DISABLED;
248 explore(act->get_tid());
252 * Records a backtracking reference for a thread choice within this Node.
253 * Provides feedback as to whether this thread choice is already set for
255 * @return false if the thread was already set to be backtracked, true
258 bool Node::set_backtrack(thread_id_t id)
260 int i = id_to_int(id);
261 ASSERT(i < ((int)backtrack.size()));
269 thread_id_t Node::get_next_backtrack()
271 /** @todo Find next backtrack */
273 for (i = 0; i < backtrack.size(); i++)
274 if (backtrack[i] == true)
276 /* Backtrack set was empty? */
277 ASSERT(i != backtrack.size());
279 backtrack[i] = false;
284 void Node::clear_backtracking()
286 for (unsigned int i = 0; i < backtrack.size(); i++)
287 backtrack[i] = false;
288 for (unsigned int i = 0; i < explored_children.size(); i++)
289 explored_children[i] = false;
293 /************************** end threads backtracking **************************/
295 /*********************************** promise **********************************/
298 * Sets a promise to explore meeting with the given node.
299 * @param i is the promise index.
301 void Node::set_promise(unsigned int i)
303 if (i >= resolve_promise.size())
304 resolve_promise.resize(i + 1, false);
305 resolve_promise[i] = true;
309 * Looks up whether a given promise should be satisfied by this node.
310 * @param i The promise index.
311 * @return true if the promise should be satisfied by the given ModelAction.
313 bool Node::get_promise(unsigned int i) const
315 return (i < resolve_promise.size()) && (int)i == resolve_promise_idx;
319 * Increments to the next promise to resolve.
320 * @return true if we have a valid combination.
322 bool Node::increment_promise()
325 if (resolve_promise.empty())
327 int prev_idx = resolve_promise_idx;
328 resolve_promise_idx++;
329 for ( ; resolve_promise_idx < (int)resolve_promise.size(); resolve_promise_idx++)
330 if (resolve_promise[resolve_promise_idx])
332 resolve_promise_idx = prev_idx;
337 * Returns whether the promise set is empty.
338 * @return true if we have explored all promise combinations.
340 bool Node::promise_empty() const
342 for (int i = resolve_promise_idx + 1; i < (int)resolve_promise.size(); i++)
343 if (i >= 0 && resolve_promise[i])
348 /** @brief Clear any promise-resolution information for this Node */
349 void Node::clear_promise_resolutions()
351 resolve_promise.clear();
352 resolve_promise_idx = -1;
355 /******************************* end promise **********************************/
357 void Node::set_misc_max(int i)
362 int Node::get_misc() const
367 bool Node::increment_misc()
369 return (misc_index < misc_max) && ((++misc_index) < misc_max);
372 bool Node::misc_empty() const
374 return (misc_index + 1) >= misc_max;
377 bool Node::is_enabled(Thread *t) const
379 int thread_id = id_to_int(t->get_id());
380 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
383 enabled_type_t Node::enabled_status(thread_id_t tid) const
385 int thread_id = id_to_int(tid);
386 if (thread_id < num_threads)
387 return enabled_array[thread_id];
389 return THREAD_DISABLED;
392 bool Node::is_enabled(thread_id_t tid) const
394 int thread_id = id_to_int(tid);
395 return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
398 bool Node::has_priority(thread_id_t tid) const
400 return fairness[id_to_int(tid)].priority;
403 bool Node::has_priority_over(thread_id_t tid1, thread_id_t tid2) const
405 return get_yield_data(id_to_int(tid1), id_to_int(tid2)) & YIELD_P;
408 /*********************************** read from ********************************/
411 * Get the current state of the may-read-from set iteration
412 * @return The read-from type we should currently be checking (past or future)
414 read_from_type_t Node::get_read_from_status()
416 if (read_from_status == READ_FROM_PAST && read_from_past.empty())
417 increment_read_from();
418 return read_from_status;
422 * Iterate one step in the may-read-from iteration. This includes a step in
423 * reading from the either the past or the future.
424 * @return True if there is a new read-from to explore; false otherwise
426 bool Node::increment_read_from()
428 clear_promise_resolutions();
429 if (increment_read_from_past()) {
430 read_from_status = READ_FROM_PAST;
432 } else if (increment_read_from_promise()) {
433 read_from_status = READ_FROM_PROMISE;
435 } else if (increment_future_value()) {
436 read_from_status = READ_FROM_FUTURE;
439 read_from_status = READ_FROM_NONE;
444 * @return True if there are any new read-froms to explore
446 bool Node::read_from_empty() const
448 return read_from_past_empty() &&
449 read_from_promise_empty() &&
450 future_value_empty();
454 * Get the total size of the may-read-from set, including both past and future
456 * @return The size of may-read-from
458 unsigned int Node::read_from_size() const
460 return read_from_past.size() +
461 read_from_promises.size() +
462 future_values.size();
465 /******************************* end read from ********************************/
467 /****************************** read from past ********************************/
469 /** @brief Prints info about read_from_past set */
470 void Node::print_read_from_past()
472 for (unsigned int i = 0; i < read_from_past.size(); i++)
473 read_from_past[i]->print();
477 * Add an action to the read_from_past set.
478 * @param act is the action to add
480 void Node::add_read_from_past(const ModelAction *act)
482 read_from_past.push_back(act);
486 * Gets the next 'read_from_past' action from this Node. Only valid for a node
487 * where this->action is a 'read'.
488 * @return The first element in read_from_past
490 const ModelAction * Node::get_read_from_past() const
492 if (read_from_past_idx < read_from_past.size()) {
493 int random_index = rand() % read_from_past.size();
494 return read_from_past[random_index];
496 // return read_from_past[read_from_past_idx];
501 const ModelAction * Node::get_read_from_past(int i) const
503 return read_from_past[i];
506 int Node::get_read_from_past_size() const
508 return read_from_past.size();
512 * Checks whether the readsfrom set for this node is empty.
513 * @return true if the readsfrom set is empty.
515 bool Node::read_from_past_empty() const
517 return ((read_from_past_idx + 1) >= read_from_past.size());
521 * Increments the index into the readsfrom set to explore the next item.
522 * @return Returns false if we have explored all items.
524 bool Node::increment_read_from_past()
527 if (read_from_past_idx < read_from_past.size()) {
528 read_from_past_idx++;
529 return read_from_past_idx < read_from_past.size();
534 /************************** end read from past ********************************/
536 /***************************** read_from_promises *****************************/
539 * Add an action to the read_from_promises set.
540 * @param reader The read which generated the Promise; we use the ModelAction
541 * instead of the Promise because the Promise does not last across executions
543 void Node::add_read_from_promise(const ModelAction *reader)
545 read_from_promises.push_back(reader);
549 * Gets the next 'read-from-promise' from this Node. Only valid for a node
550 * where this->action is a 'read'.
551 * @return The current element in read_from_promises
553 Promise * Node::get_read_from_promise() const
555 ASSERT(read_from_promise_idx >= 0 && read_from_promise_idx < ((int)read_from_promises.size()));
556 return read_from_promises[read_from_promise_idx]->get_reads_from_promise();
560 * Gets a particular 'read-from-promise' form this Node. Only vlaid for a node
561 * where this->action is a 'read'.
562 * @param i The index of the Promise to get
563 * @return The Promise at index i, if the Promise is still available; NULL
566 Promise * Node::get_read_from_promise(int i) const
568 return read_from_promises[i]->get_reads_from_promise();
571 /** @return The size of the read-from-promise set */
572 int Node::get_read_from_promise_size() const
574 return read_from_promises.size();
578 * Checks whether the read_from_promises set for this node is empty.
579 * @return true if the read_from_promises set is empty.
581 bool Node::read_from_promise_empty() const
583 return ((read_from_promise_idx + 1) >= ((int)read_from_promises.size()));
587 * Increments the index into the read_from_promises set to explore the next item.
588 * @return Returns false if we have explored all promises.
590 bool Node::increment_read_from_promise()
593 if (read_from_promise_idx < ((int)read_from_promises.size())) {
594 read_from_promise_idx++;
595 return (read_from_promise_idx < ((int)read_from_promises.size()));
600 /************************* end read_from_promises *****************************/
602 /****************************** future values *********************************/
605 * Adds a value from a weakly ordered future write to backtrack to. This
606 * operation may "fail" if the future value has already been run (within some
607 * sloppiness window of this expiration), or if the futurevalues set has
608 * reached its maximum.
609 * @see model_params.maxfuturevalues
611 * @param value is the value to backtrack to.
612 * @return True if the future value was successully added; false otherwise
614 bool Node::add_future_value(struct future_value fv)
616 uint64_t value = fv.value;
617 modelclock_t expiration = fv.expiration;
618 thread_id_t tid = fv.tid;
619 int idx = -1; /* Highest index where value is found */
620 for (unsigned int i = 0; i < future_values.size(); i++) {
621 if (future_values[i].value == value && future_values[i].tid == tid) {
622 if (expiration <= future_values[i].expiration)
627 if (idx > future_index) {
628 /* Future value hasn't been explored; update expiration */
629 future_values[idx].expiration = expiration;
631 } else if (idx >= 0 && expiration <= future_values[idx].expiration + get_params()->expireslop) {
632 /* Future value has been explored and is within the "sloppy" window */
636 /* Limit the size of the future-values set */
637 if (get_params()->maxfuturevalues > 0 &&
638 (int)future_values.size() >= get_params()->maxfuturevalues)
641 future_values.push_back(fv);
646 * Gets the next 'future_value' from this Node. Only valid for a node where
647 * this->action is a 'read'.
648 * @return The first element in future_values
650 struct future_value Node::get_future_value() const
652 ASSERT(future_index >= 0 && future_index < ((int)future_values.size()));
653 return future_values[future_index];
657 * Checks whether the future_values set for this node is empty.
658 * @return true if the future_values set is empty.
660 bool Node::future_value_empty() const
662 return ((future_index + 1) >= ((int)future_values.size()));
666 * Increments the index into the future_values set to explore the next item.
667 * @return Returns false if we have explored all values.
669 bool Node::increment_future_value()
672 if (future_index < ((int)future_values.size())) {
674 return (future_index < ((int)future_values.size()));
679 /************************** end future values *********************************/
681 /*********************** breaking release sequences ***************************/
684 * Add a write ModelAction to the set of writes that may break the release
685 * sequence. This is used during replay exploration of pending release
686 * sequences. This Node must correspond to a release sequence fixup action.
688 * @param write The write that may break the release sequence. NULL means we
689 * allow the release sequence to synchronize.
691 void Node::add_relseq_break(const ModelAction *write)
693 relseq_break_writes.push_back(write);
697 * Get the write that may break the current pending release sequence,
698 * according to the replay / divergence pattern.
700 * @return A write that may break the release sequence. If NULL, that means
701 * the release sequence should not be broken.
703 const ModelAction * Node::get_relseq_break() const
705 if (relseq_break_index < (int)relseq_break_writes.size())
706 return relseq_break_writes[relseq_break_index];
712 * Increments the index into the relseq_break_writes set to explore the next
714 * @return Returns false if we have explored all values.
716 bool Node::increment_relseq_break()
719 if (relseq_break_index < ((int)relseq_break_writes.size())) {
720 relseq_break_index++;
721 return (relseq_break_index < ((int)relseq_break_writes.size()));
727 * @return True if all writes that may break the release sequence have been
730 bool Node::relseq_break_empty() const
732 return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
735 /******************* end breaking release sequences ***************************/
738 * Increments some behavior's index, if a new behavior is available
739 * @return True if there is a new behavior available; otherwise false
741 bool Node::increment_behaviors()
743 /* satisfy a different misc_index values */
744 if (increment_misc())
746 /* satisfy a different set of promises */
747 if (increment_promise())
749 /* read from a different value */
750 if (increment_read_from())
752 /* resolve a release sequence differently */
753 if (increment_relseq_break())
758 NodeStack::NodeStack() :
766 NodeStack::~NodeStack()
768 for (unsigned int i = 0; i < node_list.size(); i++)
773 * @brief Register the model-checker object with this NodeStack
774 * @param exec The execution structure for the ModelChecker
776 void NodeStack::register_engine(const ModelExecution *exec)
778 this->execution = exec;
781 const struct model_params * NodeStack::get_params() const
783 return execution->get_params();
786 void NodeStack::print() const
788 model_print("............................................\n");
789 model_print("NodeStack printing node_list:\n");
790 for (unsigned int it = 0; it < node_list.size(); it++) {
791 if ((int)it == this->head_idx)
792 model_print("vvv following action is the current iterator vvv\n");
793 node_list[it]->print();
795 model_print("............................................\n");
798 /** Note: The is_enabled set contains what actions were enabled when
800 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
804 if ((head_idx + 1) < (int)node_list.size()) {
806 return node_list[head_idx]->get_action();
810 Node *head = get_head();
811 Node *prevfairness = NULL;
813 head->explore_child(act, is_enabled);
814 if (get_params()->fairwindow != 0 && head_idx > (int)get_params()->fairwindow)
815 prevfairness = node_list[head_idx - get_params()->fairwindow];
818 int next_threads = execution->get_num_threads();
819 if (act->get_type() == THREAD_CREATE || act->get_type() == PTHREAD_CREATE ) // may need to be changed
821 node_list.push_back(new Node(get_params(), act, head, next_threads, prevfairness));
828 * Empties the stack of all trailing nodes after a given position and calls the
829 * destructor for each. This function is provided an offset which determines
830 * how many nodes (relative to the current replay state) to save before popping
832 * @param numAhead gives the number of Nodes (including this Node) to skip over
833 * before removing nodes.
835 void NodeStack::pop_restofstack(int numAhead)
837 /* Diverging from previous execution; clear out remainder of list */
838 unsigned int it = head_idx + numAhead;
839 for (unsigned int i = it; i < node_list.size(); i++)
841 node_list.resize(it);
842 node_list.back()->clear_backtracking();
845 /** Reset the node stack. */
846 void NodeStack::full_reset()
848 for (unsigned int i = 0; i < node_list.size(); i++)
855 Node * NodeStack::get_head() const
857 if (node_list.empty() || head_idx < 0)
859 return node_list[head_idx];
862 Node * NodeStack::get_next() const
864 if (node_list.empty()) {
868 unsigned int it = head_idx + 1;
869 if (it == node_list.size()) {
873 return node_list[it];
876 void NodeStack::reset_execution()