include: add multiple-inclusion guards
[model-checker.git] / nodestack.cc
1 #include <string.h>
2
3 #include "nodestack.h"
4 #include "action.h"
5 #include "common.h"
6 #include "model.h"
7 #include "threads-model.h"
8
9 /**
10  * @brief Node constructor
11  *
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
15  * parent.
16  *
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
19  * parent.
20  * @param nthreads The number of threads which exist at this point in the
21  * execution trace.
22  */
23 Node::Node(ModelAction *act, Node *par, int nthreads, Node *prevfairness)
24         : action(act),
25         parent(par),
26         num_threads(nthreads),
27         explored_children(num_threads),
28         backtrack(num_threads),
29         fairness(num_threads),
30         numBacktracks(0),
31         enabled_array(NULL),
32         may_read_from(),
33         read_from_index(0),
34         future_values(),
35         future_index(-1),
36         relseq_break_writes(),
37         relseq_break_index(0)
38 {
39         if (act) {
40                 act->set_node(this);
41                 int currtid=id_to_int(act->get_tid());
42                 int prevtid=(prevfairness != NULL)?id_to_int(prevfairness->action->get_tid()):0;
43                 
44                 if ( model->params.fairwindow != 0 ) {
45                         for(int i=0;i<nthreads;i++) {
46                                 ASSERT(i<((int)fairness.size()));
47                                 struct fairness_info * fi=& fairness[i];
48                                 struct fairness_info * prevfi=(par!=NULL)&&(i<par->get_num_threads())?&par->fairness[i]:NULL;
49                                 if (prevfi) {
50                                         *fi=*prevfi;
51                                 }
52                                 if (parent->enabled_array[i]==THREAD_ENABLED) {
53                                         fi->enabled_count++;
54                                 }
55                                 if (i==currtid) {
56                                         fi->turns++;
57                                         fi->priority = false;
58                                 }
59                                 //Do window processing
60                                 if (prevfairness != NULL) {
61                                         if (prevfairness -> parent->enabled_array[i] == THREAD_ENABLED)
62                                                 fi->enabled_count--;
63                                         if (i==prevtid) {
64                                                 fi->turns--;
65                                         }
66                                         //Need full window to start evaluating conditions
67                                         //If we meet the enabled count and have no turns, give us priority
68                                         if ((fi->enabled_count >= model->params.enabledcount) &&
69                                                         (fi->turns == 0))
70                                                 fi->priority = true;
71                                 }
72                         }
73                 }
74         }
75 }
76
77 /** @brief Node desctructor */
78 Node::~Node()
79 {
80         if (action)
81                 delete action;
82         if (enabled_array)
83                 model_free(enabled_array);
84 }
85
86 /** Prints debugging info for the ModelAction associated with this Node */
87 void Node::print()
88 {
89         if (action)
90                 action->print();
91         else
92                 printf("******** empty action ********\n");
93 }
94
95 /** @brief Prints info about may_read_from set */
96 void Node::print_may_read_from()
97 {
98         for (unsigned int i = 0; i < may_read_from.size(); i++)
99                 may_read_from[i]->print();
100 }
101
102 /**
103  * Sets a promise to explore meeting with the given node.
104  * @param i is the promise index.
105  */
106 void Node::set_promise(unsigned int i) {
107         if (i >= promises.size())
108                 promises.resize(i + 1, PROMISE_IGNORE);
109         if (promises[i] == PROMISE_IGNORE)
110                 promises[i] = PROMISE_UNFULFILLED;
111 }
112
113 /**
114  * Looks up whether a given promise should be satisfied by this node.
115  * @param i The promise index.
116  * @return true if the promise should be satisfied by the given model action.
117  */
118 bool Node::get_promise(unsigned int i) {
119         return (i < promises.size()) && (promises[i] == PROMISE_FULFILLED);
120 }
121
122 /**
123  * Increments to the next combination of promises.
124  * @return true if we have a valid combination.
125  */
126 bool Node::increment_promise() {
127         DBG();
128
129         for (unsigned int i = 0; i < promises.size(); i++) {
130                 if (promises[i] == PROMISE_UNFULFILLED) {
131                         promises[i] = PROMISE_FULFILLED;
132                         while (i > 0) {
133                                 i--;
134                                 if (promises[i] == PROMISE_FULFILLED)
135                                         promises[i] = PROMISE_UNFULFILLED;
136                         }
137                         return true;
138                 }
139         }
140         return false;
141 }
142
143 /**
144  * Returns whether the promise set is empty.
145  * @return true if we have explored all promise combinations.
146  */
147 bool Node::promise_empty() {
148         for (unsigned int i = 0; i < promises.size();i++)
149                 if (promises[i] == PROMISE_UNFULFILLED)
150                         return false;
151         return true;
152 }
153
154 /**
155  * Adds a value from a weakly ordered future write to backtrack to.
156  * @param value is the value to backtrack to.
157  */
158 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
159         int suitableindex=-1;
160         for (unsigned int i = 0; i < future_values.size(); i++) {
161                 if (future_values[i].value == value) {
162                         if (future_values[i].expiration>=expiration)
163                                 return false;
164                         if (future_index < ((int) i)) {
165                                 suitableindex=i;
166                         }
167                 }
168         }
169
170         if (suitableindex!=-1) {
171                 future_values[suitableindex].expiration=expiration;
172                 return true;
173         }
174         struct future_value newfv={value, expiration};
175         future_values.push_back(newfv);
176         return true;
177 }
178
179 /**
180  * Checks whether the future_values set for this node is empty.
181  * @return true if the future_values set is empty.
182  */
183 bool Node::future_value_empty() {
184         return ((future_index + 1) >= ((int)future_values.size()));
185 }
186
187 /**
188  * Checks if the Thread associated with this thread ID has been explored from
189  * this Node already.
190  * @param tid is the thread ID to check
191  * @return true if this thread choice has been explored already, false
192  * otherwise
193  */
194 bool Node::has_been_explored(thread_id_t tid)
195 {
196         int id = id_to_int(tid);
197         return explored_children[id];
198 }
199
200 /**
201  * Checks if the backtracking set is empty.
202  * @return true if the backtracking set is empty
203  */
204 bool Node::backtrack_empty()
205 {
206         return (numBacktracks == 0);
207 }
208
209 /**
210  * Checks whether the readsfrom set for this node is empty.
211  * @return true if the readsfrom set is empty.
212  */
213 bool Node::read_from_empty() {
214         return ((read_from_index+1) >= may_read_from.size());
215 }
216
217 /**
218  * Mark the appropriate backtracking information for exploring a thread choice.
219  * @param act The ModelAction to explore
220  */
221 void Node::explore_child(ModelAction *act, enabled_type_t * is_enabled)
222 {
223         if ( ! enabled_array )
224                 enabled_array=(enabled_type_t *)model_malloc(sizeof(enabled_type_t)*num_threads);
225         if (is_enabled != NULL)
226                 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t)*num_threads);
227         else {
228                 for(int i=0;i<num_threads;i++)
229                         enabled_array[i]=THREAD_DISABLED;
230         }
231
232         explore(act->get_tid());
233 }
234
235 /**
236  * Records a backtracking reference for a thread choice within this Node.
237  * Provides feedback as to whether this thread choice is already set for
238  * backtracking.
239  * @return false if the thread was already set to be backtracked, true
240  * otherwise
241  */
242 bool Node::set_backtrack(thread_id_t id)
243 {
244         int i = id_to_int(id);
245         if (backtrack[i])
246                 return false;
247         backtrack[i] = true;
248         numBacktracks++;
249         return true;
250 }
251
252 thread_id_t Node::get_next_backtrack()
253 {
254         /** @todo Find next backtrack */
255         unsigned int i;
256         for (i = 0; i < backtrack.size(); i++)
257                 if (backtrack[i] == true)
258                         break;
259         /* Backtrack set was empty? */
260         ASSERT(i != backtrack.size());
261
262         backtrack[i] = false;
263         numBacktracks--;
264         return int_to_id(i);
265 }
266
267 bool Node::is_enabled(Thread *t)
268 {
269         int thread_id=id_to_int(t->get_id());
270         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
271 }
272
273 bool Node::is_enabled(thread_id_t tid)
274 {
275         int thread_id=id_to_int(tid);
276         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
277 }
278
279 bool Node::has_priority(thread_id_t tid)
280 {
281         return fairness[id_to_int(tid)].priority;
282 }
283
284 /**
285  * Add an action to the may_read_from set.
286  * @param act is the action to add
287  */
288 void Node::add_read_from(const ModelAction *act)
289 {
290         may_read_from.push_back(act);
291 }
292
293 /**
294  * Gets the next 'future_value' value from this Node. Only valid for a node
295  * where this->action is a 'read'.
296  * @return The first element in future_values
297  */
298 uint64_t Node::get_future_value() {
299         ASSERT(future_index<((int)future_values.size()));
300         return future_values[future_index].value;
301 }
302
303 modelclock_t Node::get_future_value_expiration() {
304         ASSERT(future_index<((int)future_values.size()));
305         return future_values[future_index].expiration;
306 }
307
308
309 int Node::get_read_from_size() {
310         return may_read_from.size();
311 }
312
313 const ModelAction * Node::get_read_from_at(int i) {
314         return may_read_from[i];
315 }
316
317 /**
318  * Gets the next 'may_read_from' action from this Node. Only valid for a node
319  * where this->action is a 'read'.
320  * @return The first element in may_read_from
321  */
322 const ModelAction * Node::get_read_from() {
323         if (read_from_index < may_read_from.size())
324                 return may_read_from[read_from_index];
325         else
326                 return NULL;
327 }
328
329 /**
330  * Increments the index into the readsfrom set to explore the next item.
331  * @return Returns false if we have explored all items.
332  */
333 bool Node::increment_read_from() {
334         DBG();
335         promises.clear();
336         if (read_from_index < may_read_from.size()) {
337                 read_from_index++;
338                 return read_from_index < may_read_from.size();
339         }
340         return false;
341 }
342
343 /**
344  * Increments the index into the future_values set to explore the next item.
345  * @return Returns false if we have explored all values.
346  */
347 bool Node::increment_future_value() {
348         DBG();
349         promises.clear();
350         if (future_index < ((int)future_values.size())) {
351                 future_index++;
352                 return (future_index < ((int)future_values.size()));
353         }
354         return false;
355 }
356
357 /**
358  * Add a write ModelAction to the set of writes that may break the release
359  * sequence. This is used during replay exploration of pending release
360  * sequences. This Node must correspond to a release sequence fixup action.
361  *
362  * @param write The write that may break the release sequence. NULL means we
363  * allow the release sequence to synchronize.
364  */
365 void Node::add_relseq_break(const ModelAction *write)
366 {
367         relseq_break_writes.push_back(write);
368 }
369
370 /**
371  * Get the write that may break the current pending release sequence,
372  * according to the replay / divergence pattern.
373  *
374  * @return A write that may break the release sequence. If NULL, that means
375  * the release sequence should not be broken.
376  */
377 const ModelAction * Node::get_relseq_break()
378 {
379         if (relseq_break_index < (int)relseq_break_writes.size())
380                 return relseq_break_writes[relseq_break_index];
381         else
382                 return NULL;
383 }
384
385 /**
386  * Increments the index into the relseq_break_writes set to explore the next
387  * item.
388  * @return Returns false if we have explored all values.
389  */
390 bool Node::increment_relseq_break()
391 {
392         DBG();
393         promises.clear();
394         if (relseq_break_index < ((int)relseq_break_writes.size())) {
395                 relseq_break_index++;
396                 return (relseq_break_index < ((int)relseq_break_writes.size()));
397         }
398         return false;
399 }
400
401 /**
402  * @return True if all writes that may break the release sequence have been
403  * explored
404  */
405 bool Node::relseq_break_empty() {
406         return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
407 }
408
409 void Node::explore(thread_id_t tid)
410 {
411         int i = id_to_int(tid);
412         if (backtrack[i]) {
413                 backtrack[i] = false;
414                 numBacktracks--;
415         }
416         explored_children[i] = true;
417 }
418
419 NodeStack::NodeStack() :
420         node_list(1, new Node()),
421         iter(0),
422         total_nodes(0)
423 {
424         total_nodes++;
425 }
426
427 NodeStack::~NodeStack()
428 {
429         for (unsigned int i = 0; i < node_list.size(); i++)
430                 delete node_list[i];
431 }
432
433 void NodeStack::print()
434 {
435         printf("............................................\n");
436         printf("NodeStack printing node_list:\n");
437         for (unsigned int it = 0; it < node_list.size(); it++) {
438                 if (it == this->iter)
439                         printf("vvv following action is the current iterator vvv\n");
440                 node_list[it]->print();
441         }
442         printf("............................................\n");
443 }
444
445 /** Note: The is_enabled set contains what actions were enabled when
446  *  act was chosen. */
447
448 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
449 {
450         DBG();
451
452         ASSERT(!node_list.empty());
453
454         if ((iter+1) < node_list.size()) {
455                 iter++;
456                 return node_list[iter]->get_action();
457         }
458
459         /* Record action */
460         get_head()->explore_child(act, is_enabled);
461         Node *prevfairness = NULL;
462         if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
463                 prevfairness = node_list[iter-model->params.fairwindow];
464         }
465         node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
466         total_nodes++;
467         iter++;
468         return NULL;
469 }
470
471 /**
472  * Empties the stack of all trailing nodes after a given position and calls the
473  * destructor for each. This function is provided an offset which determines
474  * how many nodes (relative to the current replay state) to save before popping
475  * the stack.
476  * @param numAhead gives the number of Nodes (including this Node) to skip over
477  * before removing nodes.
478  */
479 void NodeStack::pop_restofstack(int numAhead)
480 {
481         /* Diverging from previous execution; clear out remainder of list */
482         unsigned int it=iter+numAhead;
483         for(unsigned int i=it;i<node_list.size();i++)
484                 delete node_list[i];
485         node_list.resize(it);
486 }
487
488 Node * NodeStack::get_head()
489 {
490         if (node_list.empty())
491                 return NULL;
492         return node_list[iter];
493 }
494
495 Node * NodeStack::get_next()
496 {
497         if (node_list.empty()) {
498                 DEBUG("Empty\n");
499                 return NULL;
500         }
501         unsigned int it=iter+1;
502         if (it == node_list.size()) {
503                 DEBUG("At end\n");
504                 return NULL;
505         }
506         return node_list[it];
507 }
508
509 void NodeStack::reset_execution()
510 {
511         iter = 0;
512 }