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