this is the bad line...
[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         ASSERT(i<((int)backtrack.size()));
266         if (backtrack[i])
267                 return false;
268         backtrack[i] = true;
269         numBacktracks++;
270         return true;
271 }
272
273 thread_id_t Node::get_next_backtrack()
274 {
275         /** @todo Find next backtrack */
276         unsigned int i;
277         for (i = 0; i < backtrack.size(); i++)
278                 if (backtrack[i] == true)
279                         break;
280         /* Backtrack set was empty? */
281         ASSERT(i != backtrack.size());
282
283         backtrack[i] = false;
284         numBacktracks--;
285         return int_to_id(i);
286 }
287
288 bool Node::is_enabled(Thread *t)
289 {
290         int thread_id=id_to_int(t->get_id());
291         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
292 }
293
294 enabled_type_t Node::enabled_status(thread_id_t tid) {
295         int thread_id=id_to_int(tid);
296         if (thread_id < num_threads)
297                 return enabled_array[thread_id];
298         else
299                 return THREAD_DISABLED;
300 }
301
302 bool Node::is_enabled(thread_id_t tid)
303 {
304         int thread_id=id_to_int(tid);
305         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
306 }
307
308 bool Node::has_priority(thread_id_t tid)
309 {
310         return fairness[id_to_int(tid)].priority;
311 }
312
313 /**
314  * Add an action to the may_read_from set.
315  * @param act is the action to add
316  */
317 void Node::add_read_from(const ModelAction *act)
318 {
319         may_read_from.push_back(act);
320 }
321
322 /**
323  * Gets the next 'future_value' value from this Node. Only valid for a node
324  * where this->action is a 'read'.
325  * @return The first element in future_values
326  */
327 uint64_t Node::get_future_value() {
328         ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
329         return future_values[future_index].value;
330 }
331
332 modelclock_t Node::get_future_value_expiration() {
333         ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
334         return future_values[future_index].expiration;
335 }
336
337
338 int Node::get_read_from_size() {
339         return may_read_from.size();
340 }
341
342 const ModelAction * Node::get_read_from_at(int i) {
343         return may_read_from[i];
344 }
345
346 /**
347  * Gets the next 'may_read_from' action from this Node. Only valid for a node
348  * where this->action is a 'read'.
349  * @return The first element in may_read_from
350  */
351 const ModelAction * Node::get_read_from() {
352         if (read_from_index < may_read_from.size())
353                 return may_read_from[read_from_index];
354         else
355                 return NULL;
356 }
357
358 /**
359  * Increments the index into the readsfrom set to explore the next item.
360  * @return Returns false if we have explored all items.
361  */
362 bool Node::increment_read_from() {
363         DBG();
364         promises.clear();
365         if (read_from_index < may_read_from.size()) {
366                 read_from_index++;
367                 return read_from_index < may_read_from.size();
368         }
369         return false;
370 }
371
372 /**
373  * Increments the index into the future_values set to explore the next item.
374  * @return Returns false if we have explored all values.
375  */
376 bool Node::increment_future_value() {
377         DBG();
378         promises.clear();
379         if (future_index < ((int)future_values.size())) {
380                 future_index++;
381                 return (future_index < ((int)future_values.size()));
382         }
383         return false;
384 }
385
386 /**
387  * Add a write ModelAction to the set of writes that may break the release
388  * sequence. This is used during replay exploration of pending release
389  * sequences. This Node must correspond to a release sequence fixup action.
390  *
391  * @param write The write that may break the release sequence. NULL means we
392  * allow the release sequence to synchronize.
393  */
394 void Node::add_relseq_break(const ModelAction *write)
395 {
396         relseq_break_writes.push_back(write);
397 }
398
399 /**
400  * Get the write that may break the current pending release sequence,
401  * according to the replay / divergence pattern.
402  *
403  * @return A write that may break the release sequence. If NULL, that means
404  * the release sequence should not be broken.
405  */
406 const ModelAction * Node::get_relseq_break()
407 {
408         if (relseq_break_index < (int)relseq_break_writes.size())
409                 return relseq_break_writes[relseq_break_index];
410         else
411                 return NULL;
412 }
413
414 /**
415  * Increments the index into the relseq_break_writes set to explore the next
416  * item.
417  * @return Returns false if we have explored all values.
418  */
419 bool Node::increment_relseq_break()
420 {
421         DBG();
422         promises.clear();
423         if (relseq_break_index < ((int)relseq_break_writes.size())) {
424                 relseq_break_index++;
425                 return (relseq_break_index < ((int)relseq_break_writes.size()));
426         }
427         return false;
428 }
429
430 /**
431  * @return True if all writes that may break the release sequence have been
432  * explored
433  */
434 bool Node::relseq_break_empty() {
435         return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
436 }
437
438 void Node::explore(thread_id_t tid)
439 {
440         int i = id_to_int(tid);
441         ASSERT(i<((int)backtrack.size()));
442         if (backtrack[i]) {
443                 backtrack[i] = false;
444                 numBacktracks--;
445         }
446         explored_children[i] = true;
447 }
448
449 NodeStack::NodeStack() :
450         node_list(1, new Node()),
451         iter(0),
452         total_nodes(0)
453 {
454         total_nodes++;
455 }
456
457 NodeStack::~NodeStack()
458 {
459         for (unsigned int i = 0; i < node_list.size(); i++)
460                 delete node_list[i];
461 }
462
463 void NodeStack::print()
464 {
465         printf("............................................\n");
466         printf("NodeStack printing node_list:\n");
467         for (unsigned int it = 0; it < node_list.size(); it++) {
468                 if (it == this->iter)
469                         printf("vvv following action is the current iterator vvv\n");
470                 node_list[it]->print();
471         }
472         printf("............................................\n");
473 }
474
475 /** Note: The is_enabled set contains what actions were enabled when
476  *  act was chosen. */
477
478 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
479 {
480         DBG();
481
482         ASSERT(!node_list.empty());
483
484         if ((iter+1) < node_list.size()) {
485                 iter++;
486                 return node_list[iter]->get_action();
487         }
488
489         /* Record action */
490         get_head()->explore_child(act, is_enabled);
491         Node *prevfairness = NULL;
492         if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
493                 prevfairness = node_list[iter-model->params.fairwindow];
494         }
495         node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
496         total_nodes++;
497         iter++;
498         return NULL;
499 }
500
501 /**
502  * Empties the stack of all trailing nodes after a given position and calls the
503  * destructor for each. This function is provided an offset which determines
504  * how many nodes (relative to the current replay state) to save before popping
505  * the stack.
506  * @param numAhead gives the number of Nodes (including this Node) to skip over
507  * before removing nodes.
508  */
509 void NodeStack::pop_restofstack(int numAhead)
510 {
511         /* Diverging from previous execution; clear out remainder of list */
512         unsigned int it=iter+numAhead;
513         for(unsigned int i=it;i<node_list.size();i++)
514                 delete node_list[i];
515         node_list.resize(it);
516 }
517
518 Node * NodeStack::get_head()
519 {
520         if (node_list.empty())
521                 return NULL;
522         return node_list[iter];
523 }
524
525 Node * NodeStack::get_next()
526 {
527         if (node_list.empty()) {
528                 DEBUG("Empty\n");
529                 return NULL;
530         }
531         unsigned int it=iter+1;
532         if (it == node_list.size()) {
533                 DEBUG("At end\n");
534                 return NULL;
535         }
536         return node_list[it];
537 }
538
539 void NodeStack::reset_execution()
540 {
541         iter = 0;
542 }