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