main: remove #include's
[c11tester.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                 model_print("******** 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, bool is_rmw) {
109         if (i >= promises.size())
110                 promises.resize(i + 1, PROMISE_IGNORE);
111         if (promises[i] == PROMISE_IGNORE) {
112                 promises[i] = PROMISE_UNFULFILLED;
113                 if (is_rmw)
114                         promises[i] |= PROMISE_RMW;
115         }
116 }
117
118 /**
119  * Looks up whether a given promise should be satisfied by this node.
120  * @param i The promise index.
121  * @return true if the promise should be satisfied by the given model action.
122  */
123 bool Node::get_promise(unsigned int i) {
124         return (i < promises.size()) && ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED);
125 }
126
127 /**
128  * Increments to the next combination of promises.
129  * @return true if we have a valid combination.
130  */
131 bool Node::increment_promise() {
132         DBG();
133         unsigned int rmw_count=0;
134         for (unsigned int i = 0; i < promises.size(); i++) {
135                 if (promises[i]==(PROMISE_RMW|PROMISE_FULFILLED))
136                         rmw_count++;
137         }
138         
139         for (unsigned int i = 0; i < promises.size(); i++) {
140                 if ((promises[i] & PROMISE_MASK) == PROMISE_UNFULFILLED) {
141                         if ((rmw_count > 0) && (promises[i] & PROMISE_RMW)) {
142                                 //sending our value to two rmws... not going to work..try next combination
143                                 continue;
144                         }
145                         promises[i] = (promises[i] & PROMISE_RMW) |PROMISE_FULFILLED;
146                         while (i > 0) {
147                                 i--;
148                                 if ((promises[i] & PROMISE_MASK) == PROMISE_FULFILLED)
149                                         promises[i] = (promises[i] & PROMISE_RMW) | PROMISE_UNFULFILLED;
150                         }
151                         return true;
152                 } else if (promises[i] == (PROMISE_RMW|PROMISE_FULFILLED)) {
153                         rmw_count--;
154                 }
155         }
156         return false;
157 }
158
159 /**
160  * Returns whether the promise set is empty.
161  * @return true if we have explored all promise combinations.
162  */
163 bool Node::promise_empty() {
164         bool fulfilledrmw=false;
165         for (int i = promises.size()-1 ; i>=0; i--) {
166                 if (promises[i]==PROMISE_UNFULFILLED)
167                         return false;
168                 if (!fulfilledrmw && ((promises[i]&PROMISE_MASK)==PROMISE_UNFULFILLED))
169                         return false;
170                 if (promises[i]==(PROMISE_FULFILLED|PROMISE_RMW))
171                         fulfilledrmw=true;
172         }
173         return true;
174 }
175
176
177 void Node::set_misc_max(int i) {
178         misc_max=i;
179 }
180
181 int Node::get_misc() {
182         return misc_index;
183 }
184
185 bool Node::increment_misc() {
186         return (misc_index<misc_max)&&((++misc_index)<misc_max);
187 }
188
189 bool Node::misc_empty() {
190         return (misc_index+1)>=misc_max;
191 }
192
193
194 /**
195  * Adds a value from a weakly ordered future write to backtrack to. This
196  * operation may "fail" if the future value has already been run (within some
197  * sloppiness window of this expiration), or if the futurevalues set has
198  * reached its maximum.
199  * @see model_params.maxfuturevalues
200  *
201  * @param value is the value to backtrack to.
202  * @return True if the future value was successully added; false otherwise
203  */
204 bool Node::add_future_value(uint64_t value, modelclock_t expiration) {
205         int idx = -1; /* Highest index where value is found */
206         for (unsigned int i = 0; i < future_values.size(); i++) {
207                 if (future_values[i].value == value) {
208                         if (expiration <= future_values[i].expiration)
209                                 return false;
210                         idx = i;
211                 }
212         }
213         if (idx > future_index) {
214                 /* Future value hasn't been explored; update expiration */
215                 future_values[idx].expiration = expiration;
216                 return true;
217         } else if (idx >= 0 && expiration <= future_values[idx].expiration + model->params.expireslop) {
218                 /* Future value has been explored and is within the "sloppy" window */
219                 return false;
220         }
221
222         /* Limit the size of the future-values set */
223         if (model->params.maxfuturevalues > 0 &&
224                         (int)future_values.size() >= model->params.maxfuturevalues)
225                 return false;
226
227         struct future_value newfv = {value, expiration};
228         future_values.push_back(newfv);
229         return true;
230 }
231
232 /**
233  * Checks whether the future_values set for this node is empty.
234  * @return true if the future_values set is empty.
235  */
236 bool Node::future_value_empty() {
237         return ((future_index + 1) >= ((int)future_values.size()));
238 }
239
240 /**
241  * Checks if the Thread associated with this thread ID has been explored from
242  * this Node already.
243  * @param tid is the thread ID to check
244  * @return true if this thread choice has been explored already, false
245  * otherwise
246  */
247 bool Node::has_been_explored(thread_id_t tid)
248 {
249         int id = id_to_int(tid);
250         return explored_children[id];
251 }
252
253 /**
254  * Checks if the backtracking set is empty.
255  * @return true if the backtracking set is empty
256  */
257 bool Node::backtrack_empty()
258 {
259         return (numBacktracks == 0);
260 }
261
262 /**
263  * Checks whether the readsfrom set for this node is empty.
264  * @return true if the readsfrom set is empty.
265  */
266 bool Node::read_from_empty() {
267         return ((read_from_index+1) >= may_read_from.size());
268 }
269
270 /**
271  * Mark the appropriate backtracking information for exploring a thread choice.
272  * @param act The ModelAction to explore
273  */
274 void Node::explore_child(ModelAction *act, enabled_type_t * is_enabled)
275 {
276         if ( ! enabled_array )
277                 enabled_array=(enabled_type_t *)model_malloc(sizeof(enabled_type_t)*num_threads);
278         if (is_enabled != NULL)
279                 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t)*num_threads);
280         else {
281                 for(int i=0;i<num_threads;i++)
282                         enabled_array[i]=THREAD_DISABLED;
283         }
284
285         explore(act->get_tid());
286 }
287
288 /**
289  * Records a backtracking reference for a thread choice within this Node.
290  * Provides feedback as to whether this thread choice is already set for
291  * backtracking.
292  * @return false if the thread was already set to be backtracked, true
293  * otherwise
294  */
295 bool Node::set_backtrack(thread_id_t id)
296 {
297         int i = id_to_int(id);
298         ASSERT(i<((int)backtrack.size()));
299         if (backtrack[i])
300                 return false;
301         backtrack[i] = true;
302         numBacktracks++;
303         return true;
304 }
305
306 thread_id_t Node::get_next_backtrack()
307 {
308         /** @todo Find next backtrack */
309         unsigned int i;
310         for (i = 0; i < backtrack.size(); i++)
311                 if (backtrack[i] == true)
312                         break;
313         /* Backtrack set was empty? */
314         ASSERT(i != backtrack.size());
315
316         backtrack[i] = false;
317         numBacktracks--;
318         return int_to_id(i);
319 }
320
321 bool Node::is_enabled(Thread *t)
322 {
323         int thread_id=id_to_int(t->get_id());
324         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
325 }
326
327 enabled_type_t Node::enabled_status(thread_id_t tid) {
328         int thread_id=id_to_int(tid);
329         if (thread_id < num_threads)
330                 return enabled_array[thread_id];
331         else
332                 return THREAD_DISABLED;
333 }
334
335 bool Node::is_enabled(thread_id_t tid)
336 {
337         int thread_id=id_to_int(tid);
338         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
339 }
340
341 bool Node::has_priority(thread_id_t tid)
342 {
343         return fairness[id_to_int(tid)].priority;
344 }
345
346 /**
347  * Add an action to the may_read_from set.
348  * @param act is the action to add
349  */
350 void Node::add_read_from(const ModelAction *act)
351 {
352         may_read_from.push_back(act);
353 }
354
355 /**
356  * Gets the next 'future_value' value from this Node. Only valid for a node
357  * where this->action is a 'read'.
358  * @return The first element in future_values
359  */
360 uint64_t Node::get_future_value() {
361         ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
362         return future_values[future_index].value;
363 }
364
365 modelclock_t Node::get_future_value_expiration() {
366         ASSERT(future_index >= 0 && future_index<((int)future_values.size()));
367         return future_values[future_index].expiration;
368 }
369
370
371 int Node::get_read_from_size() {
372         return may_read_from.size();
373 }
374
375 const ModelAction * Node::get_read_from_at(int i) {
376         return may_read_from[i];
377 }
378
379 /**
380  * Gets the next 'may_read_from' action from this Node. Only valid for a node
381  * where this->action is a 'read'.
382  * @return The first element in may_read_from
383  */
384 const ModelAction * Node::get_read_from() {
385         if (read_from_index < may_read_from.size())
386                 return may_read_from[read_from_index];
387         else
388                 return NULL;
389 }
390
391 /**
392  * Increments the index into the readsfrom set to explore the next item.
393  * @return Returns false if we have explored all items.
394  */
395 bool Node::increment_read_from() {
396         DBG();
397         promises.clear();
398         if (read_from_index < may_read_from.size()) {
399                 read_from_index++;
400                 return read_from_index < may_read_from.size();
401         }
402         return false;
403 }
404
405 /**
406  * Increments the index into the future_values set to explore the next item.
407  * @return Returns false if we have explored all values.
408  */
409 bool Node::increment_future_value() {
410         DBG();
411         promises.clear();
412         if (future_index < ((int)future_values.size())) {
413                 future_index++;
414                 return (future_index < ((int)future_values.size()));
415         }
416         return false;
417 }
418
419 /**
420  * Add a write ModelAction to the set of writes that may break the release
421  * sequence. This is used during replay exploration of pending release
422  * sequences. This Node must correspond to a release sequence fixup action.
423  *
424  * @param write The write that may break the release sequence. NULL means we
425  * allow the release sequence to synchronize.
426  */
427 void Node::add_relseq_break(const ModelAction *write)
428 {
429         relseq_break_writes.push_back(write);
430 }
431
432 /**
433  * Get the write that may break the current pending release sequence,
434  * according to the replay / divergence pattern.
435  *
436  * @return A write that may break the release sequence. If NULL, that means
437  * the release sequence should not be broken.
438  */
439 const ModelAction * Node::get_relseq_break()
440 {
441         if (relseq_break_index < (int)relseq_break_writes.size())
442                 return relseq_break_writes[relseq_break_index];
443         else
444                 return NULL;
445 }
446
447 /**
448  * Increments the index into the relseq_break_writes set to explore the next
449  * item.
450  * @return Returns false if we have explored all values.
451  */
452 bool Node::increment_relseq_break()
453 {
454         DBG();
455         promises.clear();
456         if (relseq_break_index < ((int)relseq_break_writes.size())) {
457                 relseq_break_index++;
458                 return (relseq_break_index < ((int)relseq_break_writes.size()));
459         }
460         return false;
461 }
462
463 /**
464  * @return True if all writes that may break the release sequence have been
465  * explored
466  */
467 bool Node::relseq_break_empty() {
468         return ((relseq_break_index + 1) >= ((int)relseq_break_writes.size()));
469 }
470
471 void Node::explore(thread_id_t tid)
472 {
473         int i = id_to_int(tid);
474         ASSERT(i<((int)backtrack.size()));
475         if (backtrack[i]) {
476                 backtrack[i] = false;
477                 numBacktracks--;
478         }
479         explored_children[i] = true;
480 }
481
482 NodeStack::NodeStack() :
483         node_list(1, new Node()),
484         iter(0),
485         total_nodes(0)
486 {
487         total_nodes++;
488 }
489
490 NodeStack::~NodeStack()
491 {
492         for (unsigned int i = 0; i < node_list.size(); i++)
493                 delete node_list[i];
494 }
495
496 void NodeStack::print()
497 {
498         model_print("............................................\n");
499         model_print("NodeStack printing node_list:\n");
500         for (unsigned int it = 0; it < node_list.size(); it++) {
501                 if (it == this->iter)
502                         model_print("vvv following action is the current iterator vvv\n");
503                 node_list[it]->print();
504         }
505         model_print("............................................\n");
506 }
507
508 /** Note: The is_enabled set contains what actions were enabled when
509  *  act was chosen. */
510
511 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t * is_enabled)
512 {
513         DBG();
514
515         ASSERT(!node_list.empty());
516
517         if ((iter+1) < node_list.size()) {
518                 iter++;
519                 return node_list[iter]->get_action();
520         }
521
522         /* Record action */
523         get_head()->explore_child(act, is_enabled);
524         Node *prevfairness = NULL;
525         if ( model->params.fairwindow != 0 && iter > model->params.fairwindow ) {
526                 prevfairness = node_list[iter-model->params.fairwindow];
527         }
528         node_list.push_back(new Node(act, get_head(), model->get_num_threads(), prevfairness));
529         total_nodes++;
530         iter++;
531         return NULL;
532 }
533
534 /**
535  * Empties the stack of all trailing nodes after a given position and calls the
536  * destructor for each. This function is provided an offset which determines
537  * how many nodes (relative to the current replay state) to save before popping
538  * the stack.
539  * @param numAhead gives the number of Nodes (including this Node) to skip over
540  * before removing nodes.
541  */
542 void NodeStack::pop_restofstack(int numAhead)
543 {
544         /* Diverging from previous execution; clear out remainder of list */
545         unsigned int it=iter+numAhead;
546         for(unsigned int i=it;i<node_list.size();i++)
547                 delete node_list[i];
548         node_list.resize(it);
549 }
550
551 Node * NodeStack::get_head()
552 {
553         if (node_list.empty())
554                 return NULL;
555         return node_list[iter];
556 }
557
558 Node * NodeStack::get_next()
559 {
560         if (node_list.empty()) {
561                 DEBUG("Empty\n");
562                 return NULL;
563         }
564         unsigned int it=iter+1;
565         if (it == node_list.size()) {
566                 DEBUG("At end\n");
567                 return NULL;
568         }
569         return node_list[it];
570 }
571
572 void NodeStack::reset_execution()
573 {
574         iter = 0;
575 }