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