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),
51 int currtid = id_to_int(act->get_tid());
52 int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
54 if (get_params()->fairwindow != 0) {
55 for (int i = 0; i < num_threads; i++) {
56 ASSERT(i < ((int)fairness.size()));
57 struct fairness_info *fi = &fairness[i];
58 struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
62 if (parent && parent->is_enabled(int_to_id(i))) {
69 /* Do window processing */
70 if (prevfairness != NULL) {
71 if (prevfairness->parent->is_enabled(int_to_id(i)))
76 /* Need full window to start evaluating
78 * If we meet the enabled count and have no
79 * turns, give us priority */
80 if ((fi->enabled_count >= get_params()->enabledcount) &&
88 int Node::get_yield_data(int tid1, int tid2) const {
89 if (tid1<num_threads && tid2 < num_threads)
90 return yield_data[YIELD_INDEX(tid1,tid2,num_threads)];
92 return YIELD_S | YIELD_D;
95 void Node::update_yield(Scheduler * scheduler) {
97 yield_data=(int *) model_calloc(1, sizeof(int)*num_threads*num_threads);
100 for(int i = 0; i < num_threads*num_threads; i++) {
101 yield_data[i] = YIELD_S | YIELD_D;
105 int curr_tid=id_to_int(action->get_tid());
107 for(int u = 0; u < num_threads; u++) {
108 for(int v = 0; v < num_threads; v++) {
109 int yield_state=parent->get_yield_data(u, v);
110 bool next_enabled=scheduler->is_enabled(int_to_id(v));
111 bool curr_enabled=parent->is_enabled(int_to_id(v));
113 //Compute intersection of ES and E
114 yield_state&=~YIELD_E;
115 //Check to see if we disabled the thread
116 if (u==curr_tid && curr_enabled)
117 yield_state|=YIELD_D;
119 yield_data[YIELD_INDEX(u, v, num_threads)]=yield_state;
121 yield_data[YIELD_INDEX(u, curr_tid, num_threads)]=(yield_data[YIELD_INDEX(u, curr_tid, num_threads)]&~YIELD_P)|YIELD_S;
123 //handle curr.yield(t) part of computation
124 if (action->is_yield()) {
125 for(int v = 0; v < num_threads; v++) {
126 int yield_state=yield_data[YIELD_INDEX(curr_tid, v, num_threads)];
127 if ((yield_state & (YIELD_E | YIELD_D)) && (!(yield_state & YIELD_S)))
128 yield_state |= YIELD_P;
129 yield_state &= YIELD_P;
130 if (scheduler->is_enabled(int_to_id(v))) {
131 yield_state|=YIELD_E;
133 yield_data[YIELD_INDEX(curr_tid, v, num_threads)]=yield_state;
138 /** @brief Node desctructor */
143 delete uninit_action;
145 model_free(enabled_array);
147 model_free(yield_data);
150 /** Prints debugging info for the ModelAction associated with this Node */
151 void Node::print() const
154 model_print(" thread status: ");
156 for (int i = 0; i < num_threads; i++) {
158 enabled_type_to_string(enabled_array[i], str);
159 model_print("[%d: %s]", i, str);
163 model_print("(info not available)\n");
164 model_print(" backtrack: %s", backtrack_empty() ? "empty" : "non-empty ");
165 for (int i = 0; i < (int)backtrack.size(); i++)
166 if (backtrack[i] == true)
167 model_print("[%d]", i);
170 model_print(" read from past: %s", read_from_past_empty() ? "empty" : "non-empty ");
171 for (int i = read_from_past_idx + 1; i < (int)read_from_past.size(); i++)
172 model_print("[%d]", read_from_past[i]->get_seq_number());
175 model_print(" misc: %s\n", misc_empty() ? "empty" : "non-empty");
178 /****************************** threads backtracking **************************/
181 * Checks if the Thread associated with this thread ID has been explored from
183 * @param tid is the thread ID to check
184 * @return true if this thread choice has been explored already, false
187 bool Node::has_been_explored(thread_id_t tid) const
189 int id = id_to_int(tid);
190 return explored_children[id];
194 * Checks if the backtracking set is empty.
195 * @return true if the backtracking set is empty
197 bool Node::backtrack_empty() const
199 return (numBacktracks == 0);
202 void Node::explore(thread_id_t tid)
204 int i = id_to_int(tid);
205 ASSERT(i < ((int)backtrack.size()));
207 backtrack[i] = false;
210 explored_children[i] = true;
214 * Mark the appropriate backtracking information for exploring a thread choice.
215 * @param act The ModelAction to explore
217 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
220 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
221 if (is_enabled != NULL)
222 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
224 for (int i = 0; i < num_threads; i++)
225 enabled_array[i] = THREAD_DISABLED;
228 explore(act->get_tid());
232 * Records a backtracking reference for a thread choice within this Node.
233 * Provides feedback as to whether this thread choice is already set for
235 * @return false if the thread was already set to be backtracked, true
238 bool Node::set_backtrack(thread_id_t id)
240 int i = id_to_int(id);
241 ASSERT(i < ((int)backtrack.size()));
249 thread_id_t Node::get_next_backtrack()
251 /** @todo Find next backtrack */
253 for (i = 0; i < backtrack.size(); i++)
254 if (backtrack[i] == true)
256 /* Backtrack set was empty? */
257 ASSERT(i != backtrack.size());
259 backtrack[i] = false;
264 void Node::clear_backtracking()
266 for (unsigned int i = 0; i < backtrack.size(); i++)
267 backtrack[i] = false;
268 for (unsigned int i = 0; i < explored_children.size(); i++)
269 explored_children[i] = false;
273 /************************** end threads backtracking **************************/
275 void Node::set_misc_max(int i)
280 int Node::get_misc() const
285 bool Node::increment_misc()
287 return (misc_index < misc_max) && ((++misc_index) < misc_max);
290 bool Node::misc_empty() const
292 return (misc_index + 1) >= misc_max;
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 bool Node::has_priority_over(thread_id_t tid1, thread_id_t tid2) const
323 return get_yield_data(id_to_int(tid1), id_to_int(tid2)) & YIELD_P;
326 /*********************************** read from ********************************/
329 * Get the current state of the may-read-from set iteration
330 * @return The read-from type we should currently be checking (past)
332 read_from_type_t Node::get_read_from_status()
334 if (read_from_status == READ_FROM_PAST && read_from_past.empty())
335 increment_read_from();
336 return read_from_status;
340 * Iterate one step in the may-read-from iteration. This includes a step in
341 * reading from the either the past or the future.
342 * @return True if there is a new read-from to explore; false otherwise
344 bool Node::increment_read_from()
346 if (increment_read_from_past()) {
347 read_from_status = READ_FROM_PAST;
350 read_from_status = READ_FROM_NONE;
355 * @return True if there are any new read-froms to explore
357 bool Node::read_from_empty() const
359 return read_from_past_empty();
363 * Get the total size of the may-read-from set, including both past
364 * @return The size of may-read-from
366 unsigned int Node::read_from_size() const
368 return read_from_past.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 int random_index = rand() % read_from_past.size();
400 return read_from_past[random_index];
402 // return read_from_past[read_from_past_idx];
407 const ModelAction * Node::get_read_from_past(int i) const
409 return read_from_past[i];
412 int Node::get_read_from_past_size() const
414 return read_from_past.size();
418 * Checks whether the readsfrom set for this node is empty.
419 * @return true if the readsfrom set is empty.
421 bool Node::read_from_past_empty() const
423 return ((read_from_past_idx + 1) >= read_from_past.size());
427 * Increments the index into the readsfrom set to explore the next item.
428 * @return Returns false if we have explored all items.
430 bool Node::increment_read_from_past()
433 if (read_from_past_idx < read_from_past.size()) {
434 read_from_past_idx++;
435 return read_from_past_idx < read_from_past.size();
440 /************************** end read from past ********************************/
444 * Increments some behavior's index, if a new behavior is available
445 * @return True if there is a new behavior available; otherwise false
447 bool Node::increment_behaviors()
449 /* satisfy a different misc_index values */
450 if (increment_misc())
452 /* read from a different value */
453 if (increment_read_from())
458 NodeStack::NodeStack() :
466 NodeStack::~NodeStack()
468 for (unsigned int i = 0; i < node_list.size(); i++)
473 * @brief Register the model-checker object with this NodeStack
474 * @param exec The execution structure for the ModelChecker
476 void NodeStack::register_engine(const ModelExecution *exec)
478 this->execution = exec;
481 const struct model_params * NodeStack::get_params() const
483 return execution->get_params();
486 void NodeStack::print() const
488 model_print("............................................\n");
489 model_print("NodeStack printing node_list:\n");
490 for (unsigned int it = 0; it < node_list.size(); it++) {
491 if ((int)it == this->head_idx)
492 model_print("vvv following action is the current iterator vvv\n");
493 node_list[it]->print();
495 model_print("............................................\n");
498 /** Note: The is_enabled set contains what actions were enabled when
500 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
504 if ((head_idx + 1) < (int)node_list.size()) {
506 return node_list[head_idx]->get_action();
510 Node *head = get_head();
511 Node *prevfairness = NULL;
513 head->explore_child(act, is_enabled);
514 if (get_params()->fairwindow != 0 && head_idx > (int)get_params()->fairwindow)
515 prevfairness = node_list[head_idx - get_params()->fairwindow];
518 int next_threads = execution->get_num_threads();
519 if (act->get_type() == THREAD_CREATE || act->get_type() == PTHREAD_CREATE ) // may need to be changed
521 node_list.push_back(new Node(get_params(), act, head, next_threads, prevfairness));
528 * Empties the stack of all trailing nodes after a given position and calls the
529 * destructor for each. This function is provided an offset which determines
530 * how many nodes (relative to the current replay state) to save before popping
532 * @param numAhead gives the number of Nodes (including this Node) to skip over
533 * before removing nodes.
535 void NodeStack::pop_restofstack(int numAhead)
537 /* Diverging from previous execution; clear out remainder of list */
538 unsigned int it = head_idx + numAhead;
539 for (unsigned int i = it; i < node_list.size(); i++)
541 node_list.resize(it);
542 node_list.back()->clear_backtracking();
545 /** Reset the node stack. */
546 void NodeStack::full_reset()
548 for (unsigned int i = 0; i < node_list.size(); i++)
555 Node * NodeStack::get_head() const
557 if (node_list.empty() || head_idx < 0)
559 return node_list[head_idx];
562 Node * NodeStack::get_next() const
564 if (node_list.empty()) {
568 unsigned int it = head_idx + 1;
569 if (it == node_list.size()) {
573 return node_list[it];
576 void NodeStack::reset_execution()