remove old release sequences
[c11tester.git] / nodestack.cc
1 #define __STDC_FORMAT_MACROS
2 #include <inttypes.h>
3 #include <cstdlib>
4
5 #include <string.h>
6
7 #include "nodestack.h"
8 #include "action.h"
9 #include "common.h"
10 #include "threads-model.h"
11 #include "modeltypes.h"
12 #include "execution.h"
13 #include "params.h"
14
15 /**
16  * @brief Node constructor
17  *
18  * Constructs a single Node for use in a NodeStack. Each Node is associated
19  * with exactly one ModelAction (exception: the first Node should be created
20  * as an empty stub, to represent the first thread "choice") and up to one
21  * parent.
22  *
23  * @param params The model-checker parameters
24  * @param act The ModelAction to associate with this Node. May be NULL.
25  * @param par The parent Node in the NodeStack. May be NULL if there is no
26  * parent.
27  * @param nthreads The number of threads which exist at this point in the
28  * execution trace.
29  */
30 Node::Node(const struct model_params *params, ModelAction *act, Node *par,
31                 int nthreads, Node *prevfairness) :
32         read_from_status(READ_FROM_PAST),
33         action(act),
34         params(params),
35         uninit_action(NULL),
36         parent(par),
37         num_threads(nthreads),
38         explored_children(num_threads),
39         backtrack(num_threads),
40         fairness(num_threads),
41         numBacktracks(0),
42         enabled_array(NULL),
43         read_from_past(),
44         read_from_past_idx(0),
45         misc_index(0),
46         misc_max(0),
47         yield_data(NULL)
48 {
49         ASSERT(act);
50         act->set_node(this);
51         int currtid = id_to_int(act->get_tid());
52         int prevtid = prevfairness ? id_to_int(prevfairness->action->get_tid()) : 0;
53
54         if (get_params()->fairwindow != 0) {
55                 for (int i = 0; i < num_threads; i++) {
56                         ASSERT(i < ((int)fairness.size()));
57                         struct fairness_info *fi = &fairness[i];
58                         struct fairness_info *prevfi = (parent && i < parent->get_num_threads()) ? &parent->fairness[i] : NULL;
59                         if (prevfi) {
60                                 *fi = *prevfi;
61                         }
62                         if (parent && parent->is_enabled(int_to_id(i))) {
63                                 fi->enabled_count++;
64                         }
65                         if (i == currtid) {
66                                 fi->turns++;
67                                 fi->priority = false;
68                         }
69                         /* Do window processing */
70                         if (prevfairness != NULL) {
71                                 if (prevfairness->parent->is_enabled(int_to_id(i)))
72                                         fi->enabled_count--;
73                                 if (i == prevtid) {
74                                         fi->turns--;
75                                 }
76                                 /* Need full window to start evaluating
77                                  * conditions
78                                  * If we meet the enabled count and have no
79                                  * turns, give us priority */
80                                 if ((fi->enabled_count >= get_params()->enabledcount) &&
81                                                 (fi->turns == 0))
82                                         fi->priority = true;
83                         }
84                 }
85         }
86 }
87
88 int Node::get_yield_data(int tid1, int tid2) const {
89         if (tid1<num_threads && tid2 < num_threads)
90                 return yield_data[YIELD_INDEX(tid1,tid2,num_threads)];
91         else
92                 return YIELD_S | YIELD_D;
93 }
94
95 void Node::update_yield(Scheduler * scheduler) {
96         if (yield_data==NULL)
97                 yield_data=(int *) model_calloc(1, sizeof(int)*num_threads*num_threads);
98         //handle base case
99         if (parent == NULL) {
100                 for(int i = 0; i < num_threads*num_threads; i++) {
101                         yield_data[i] = YIELD_S | YIELD_D;
102                 }
103                 return;
104         }
105         int curr_tid=id_to_int(action->get_tid());
106
107         for(int u = 0; u < num_threads; u++) {
108                 for(int v = 0; v < num_threads; v++) {
109                         int yield_state=parent->get_yield_data(u, v);
110                         bool next_enabled=scheduler->is_enabled(int_to_id(v));
111                         bool curr_enabled=parent->is_enabled(int_to_id(v));
112                         if (!next_enabled) {
113                                 //Compute intersection of ES and E
114                                 yield_state&=~YIELD_E;
115                                 //Check to see if we disabled the thread
116                                 if (u==curr_tid && curr_enabled)
117                                         yield_state|=YIELD_D;
118                         }
119                         yield_data[YIELD_INDEX(u, v, num_threads)]=yield_state;
120                 }
121                 yield_data[YIELD_INDEX(u, curr_tid, num_threads)]=(yield_data[YIELD_INDEX(u, curr_tid, num_threads)]&~YIELD_P)|YIELD_S;
122         }
123         //handle curr.yield(t) part of computation
124         if (action->is_yield()) {
125                 for(int v = 0; v < num_threads; v++) {
126                         int yield_state=yield_data[YIELD_INDEX(curr_tid, v, num_threads)];
127                         if ((yield_state & (YIELD_E | YIELD_D)) && (!(yield_state & YIELD_S)))
128                                 yield_state |= YIELD_P;
129                         yield_state &= YIELD_P;
130                         if (scheduler->is_enabled(int_to_id(v))) {
131                                 yield_state|=YIELD_E;
132                         }
133                         yield_data[YIELD_INDEX(curr_tid, v, num_threads)]=yield_state;
134                 }
135         }
136 }
137
138 /** @brief Node desctructor */
139 Node::~Node()
140 {
141         delete action;
142         if (uninit_action)
143                 delete uninit_action;
144         if (enabled_array)
145                 model_free(enabled_array);
146         if (yield_data)
147                 model_free(yield_data);
148 }
149
150 /** Prints debugging info for the ModelAction associated with this Node */
151 void Node::print() const
152 {
153         action->print();
154         model_print("          thread status: ");
155         if (enabled_array) {
156                 for (int i = 0; i < num_threads; i++) {
157                         char str[20];
158                         enabled_type_to_string(enabled_array[i], str);
159                         model_print("[%d: %s]", i, str);
160                 }
161                 model_print("\n");
162         } else
163                 model_print("(info not available)\n");
164         model_print("          backtrack: %s", backtrack_empty() ? "empty" : "non-empty ");
165         for (int i = 0; i < (int)backtrack.size(); i++)
166                 if (backtrack[i] == true)
167                         model_print("[%d]", i);
168         model_print("\n");
169
170         model_print("          read from past: %s", read_from_past_empty() ? "empty" : "non-empty ");
171         for (int i = read_from_past_idx + 1; i < (int)read_from_past.size(); i++)
172                 model_print("[%d]", read_from_past[i]->get_seq_number());
173         model_print("\n");
174
175         model_print("          misc: %s\n", misc_empty() ? "empty" : "non-empty");
176 }
177
178 /****************************** threads backtracking **************************/
179
180 /**
181  * Checks if the Thread associated with this thread ID has been explored from
182  * this Node already.
183  * @param tid is the thread ID to check
184  * @return true if this thread choice has been explored already, false
185  * otherwise
186  */
187 bool Node::has_been_explored(thread_id_t tid) const
188 {
189         int id = id_to_int(tid);
190         return explored_children[id];
191 }
192
193 /**
194  * Checks if the backtracking set is empty.
195  * @return true if the backtracking set is empty
196  */
197 bool Node::backtrack_empty() const
198 {
199         return (numBacktracks == 0);
200 }
201
202 void Node::explore(thread_id_t tid)
203 {
204         int i = id_to_int(tid);
205         ASSERT(i < ((int)backtrack.size()));
206         if (backtrack[i]) {
207                 backtrack[i] = false;
208                 numBacktracks--;
209         }
210         explored_children[i] = true;
211 }
212
213 /**
214  * Mark the appropriate backtracking information for exploring a thread choice.
215  * @param act The ModelAction to explore
216  */
217 void Node::explore_child(ModelAction *act, enabled_type_t *is_enabled)
218 {
219         if (!enabled_array)
220                 enabled_array = (enabled_type_t *)model_malloc(sizeof(enabled_type_t) * num_threads);
221         if (is_enabled != NULL)
222                 memcpy(enabled_array, is_enabled, sizeof(enabled_type_t) * num_threads);
223         else {
224                 for (int i = 0; i < num_threads; i++)
225                         enabled_array[i] = THREAD_DISABLED;
226         }
227
228         explore(act->get_tid());
229 }
230
231 /**
232  * Records a backtracking reference for a thread choice within this Node.
233  * Provides feedback as to whether this thread choice is already set for
234  * backtracking.
235  * @return false if the thread was already set to be backtracked, true
236  * otherwise
237  */
238 bool Node::set_backtrack(thread_id_t id)
239 {
240         int i = id_to_int(id);
241         ASSERT(i < ((int)backtrack.size()));
242         if (backtrack[i])
243                 return false;
244         backtrack[i] = true;
245         numBacktracks++;
246         return true;
247 }
248
249 thread_id_t Node::get_next_backtrack()
250 {
251         /** @todo Find next backtrack */
252         unsigned int i;
253         for (i = 0; i < backtrack.size(); i++)
254                 if (backtrack[i] == true)
255                         break;
256         /* Backtrack set was empty? */
257         ASSERT(i != backtrack.size());
258
259         backtrack[i] = false;
260         numBacktracks--;
261         return int_to_id(i);
262 }
263
264 void Node::clear_backtracking()
265 {
266         for (unsigned int i = 0; i < backtrack.size(); i++)
267                 backtrack[i] = false;
268         for (unsigned int i = 0; i < explored_children.size(); i++)
269                 explored_children[i] = false;
270         numBacktracks = 0;
271 }
272
273 /************************** end threads backtracking **************************/
274
275 void Node::set_misc_max(int i)
276 {
277         misc_max = i;
278 }
279
280 int Node::get_misc() const
281 {
282         return misc_index;
283 }
284
285 bool Node::increment_misc()
286 {
287         return (misc_index < misc_max) && ((++misc_index) < misc_max);
288 }
289
290 bool Node::misc_empty() const
291 {
292         return (misc_index + 1) >= misc_max;
293 }
294
295 bool Node::is_enabled(Thread *t) const
296 {
297         int thread_id = id_to_int(t->get_id());
298         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
299 }
300
301 enabled_type_t Node::enabled_status(thread_id_t tid) const
302 {
303         int thread_id = id_to_int(tid);
304         if (thread_id < num_threads)
305                 return enabled_array[thread_id];
306         else
307                 return THREAD_DISABLED;
308 }
309
310 bool Node::is_enabled(thread_id_t tid) const
311 {
312         int thread_id = id_to_int(tid);
313         return thread_id < num_threads && (enabled_array[thread_id] != THREAD_DISABLED);
314 }
315
316 bool Node::has_priority(thread_id_t tid) const
317 {
318         return fairness[id_to_int(tid)].priority;
319 }
320
321 bool Node::has_priority_over(thread_id_t tid1, thread_id_t tid2) const
322 {
323         return get_yield_data(id_to_int(tid1), id_to_int(tid2)) & YIELD_P;
324 }
325
326 /*********************************** read from ********************************/
327
328 /**
329  * Get the current state of the may-read-from set iteration
330  * @return The read-from type we should currently be checking (past)
331  */
332 read_from_type_t Node::get_read_from_status()
333 {
334         if (read_from_status == READ_FROM_PAST && read_from_past.empty())
335                 increment_read_from();
336         return read_from_status;
337 }
338
339 /**
340  * Iterate one step in the may-read-from iteration. This includes a step in
341  * reading from the either the past or the future.
342  * @return True if there is a new read-from to explore; false otherwise
343  */
344 bool Node::increment_read_from()
345 {
346         if (increment_read_from_past()) {
347                read_from_status = READ_FROM_PAST;
348                return true;
349         }
350         read_from_status = READ_FROM_NONE;
351         return false;
352 }
353
354 /**
355  * @return True if there are any new read-froms to explore
356  */
357 bool Node::read_from_empty() const
358 {
359   return read_from_past_empty();
360 }
361
362 /**
363  * Get the total size of the may-read-from set, including both past
364  * @return The size of may-read-from
365  */
366 unsigned int Node::read_from_size() const
367 {
368   return read_from_past.size();
369 }
370
371 /******************************* end read from ********************************/
372
373 /****************************** read from past ********************************/
374
375 /** @brief Prints info about read_from_past set */
376 void Node::print_read_from_past()
377 {
378         for (unsigned int i = 0; i < read_from_past.size(); i++)
379                 read_from_past[i]->print();
380 }
381
382 /**
383  * Add an action to the read_from_past set.
384  * @param act is the action to add
385  */
386 void Node::add_read_from_past(const ModelAction *act)
387 {
388         read_from_past.push_back(act);
389 }
390
391 /**
392  * Gets the next 'read_from_past' action from this Node. Only valid for a node
393  * where this->action is a 'read'.
394  * @return The first element in read_from_past
395  */
396 const ModelAction * Node::get_read_from_past() const
397 {
398         if (read_from_past_idx < read_from_past.size()) {
399                 int random_index = rand() % read_from_past.size(); 
400                 return read_from_past[random_index];
401         }
402 //              return read_from_past[read_from_past_idx];
403         else
404                 return NULL;
405 }
406
407 const ModelAction * Node::get_read_from_past(int i) const
408 {
409         return read_from_past[i];
410 }
411
412 int Node::get_read_from_past_size() const
413 {
414         return read_from_past.size();
415 }
416
417 /**
418  * Checks whether the readsfrom set for this node is empty.
419  * @return true if the readsfrom set is empty.
420  */
421 bool Node::read_from_past_empty() const
422 {
423         return ((read_from_past_idx + 1) >= read_from_past.size());
424 }
425
426 /**
427  * Increments the index into the readsfrom set to explore the next item.
428  * @return Returns false if we have explored all items.
429  */
430 bool Node::increment_read_from_past()
431 {
432         DBG();
433         if (read_from_past_idx < read_from_past.size()) {
434                 read_from_past_idx++;
435                 return read_from_past_idx < read_from_past.size();
436         }
437         return false;
438 }
439
440 /************************** end read from past ********************************/
441
442
443 /**
444  * Increments some behavior's index, if a new behavior is available
445  * @return True if there is a new behavior available; otherwise false
446  */
447 bool Node::increment_behaviors()
448 {
449         /* satisfy a different misc_index values */
450         if (increment_misc())
451                 return true;
452         /* read from a different value */
453         if (increment_read_from())
454                 return true;
455         return false;
456 }
457
458 NodeStack::NodeStack() :
459         node_list(),
460         head_idx(-1),
461         total_nodes(0)
462 {
463         total_nodes++;
464 }
465
466 NodeStack::~NodeStack()
467 {
468         for (unsigned int i = 0; i < node_list.size(); i++)
469                 delete node_list[i];
470 }
471
472 /**
473  * @brief Register the model-checker object with this NodeStack
474  * @param exec The execution structure for the ModelChecker
475  */
476 void NodeStack::register_engine(const ModelExecution *exec)
477 {
478         this->execution = exec;
479 }
480
481 const struct model_params * NodeStack::get_params() const
482 {
483         return execution->get_params();
484 }
485
486 void NodeStack::print() const
487 {
488         model_print("............................................\n");
489         model_print("NodeStack printing node_list:\n");
490         for (unsigned int it = 0; it < node_list.size(); it++) {
491                 if ((int)it == this->head_idx)
492                         model_print("vvv following action is the current iterator vvv\n");
493                 node_list[it]->print();
494         }
495         model_print("............................................\n");
496 }
497
498 /** Note: The is_enabled set contains what actions were enabled when
499  *  act was chosen. */
500 ModelAction * NodeStack::explore_action(ModelAction *act, enabled_type_t *is_enabled)
501 {
502         DBG();
503
504         if ((head_idx + 1) < (int)node_list.size()) {
505                 head_idx++;
506                 return node_list[head_idx]->get_action();
507         }
508
509         /* Record action */
510         Node *head = get_head();
511         Node *prevfairness = NULL;
512         if (head) {
513                 head->explore_child(act, is_enabled);
514                 if (get_params()->fairwindow != 0 && head_idx > (int)get_params()->fairwindow)
515                         prevfairness = node_list[head_idx - get_params()->fairwindow];
516         }
517
518         int next_threads = execution->get_num_threads();
519         if (act->get_type() == THREAD_CREATE || act->get_type() == PTHREAD_CREATE ) // may need to be changed
520                 next_threads++;
521         node_list.push_back(new Node(get_params(), act, head, next_threads, prevfairness));
522         total_nodes++;
523         head_idx++;
524         return NULL;
525 }
526
527 /**
528  * Empties the stack of all trailing nodes after a given position and calls the
529  * destructor for each. This function is provided an offset which determines
530  * how many nodes (relative to the current replay state) to save before popping
531  * the stack.
532  * @param numAhead gives the number of Nodes (including this Node) to skip over
533  * before removing nodes.
534  */
535 void NodeStack::pop_restofstack(int numAhead)
536 {
537         /* Diverging from previous execution; clear out remainder of list */
538         unsigned int it = head_idx + numAhead;
539         for (unsigned int i = it; i < node_list.size(); i++)
540                 delete node_list[i];
541         node_list.resize(it);
542         node_list.back()->clear_backtracking();
543 }
544
545 /** Reset the node stack. */
546 void NodeStack::full_reset() 
547 {
548         for (unsigned int i = 0; i < node_list.size(); i++)
549                 delete node_list[i];
550         node_list.clear();
551         reset_execution();
552         total_nodes = 1;
553 }
554
555 Node * NodeStack::get_head() const
556 {
557         if (node_list.empty() || head_idx < 0)
558                 return NULL;
559         return node_list[head_idx];
560 }
561
562 Node * NodeStack::get_next() const
563 {
564         if (node_list.empty()) {
565                 DEBUG("Empty\n");
566                 return NULL;
567         }
568         unsigned int it = head_idx + 1;
569         if (it == node_list.size()) {
570                 DEBUG("At end\n");
571                 return NULL;
572         }
573         return node_list[it];
574 }
575
576 void NodeStack::reset_execution()
577 {
578         head_idx = -1;
579 }