47f724d856652a501805241df58e939121429e43
[model-checker.git] / nodestack.cc
1 #include "nodestack.h"
2 #include "action.h"
3 #include "common.h"
4 #include "model.h"
5
6 /**
7  * @brief Node constructor
8  *
9  * Constructs a single Node for use in a NodeStack. Each Node is associated
10  * with exactly one ModelAction (exception: the first Node should be created
11  * as an empty stub, to represent the first thread "choice") and up to one
12  * parent.
13  *
14  * @param act The ModelAction to associate with this Node. May be NULL.
15  * @param par The parent Node in the NodeStack. May be NULL if there is no
16  * parent.
17  * @param nthreads The number of threads which exist at this point in the
18  * execution trace.
19  */
20 Node::Node(ModelAction *act, Node *par, int nthreads)
21         : action(act),
22         parent(par),
23         num_threads(nthreads),
24         explored_children(num_threads),
25         backtrack(num_threads),
26         numBacktracks(0),
27         may_read_from(),
28         read_from_index(0),
29         future_values(),
30         future_index(-1)
31 {
32         if (act)
33                 act->set_node(this);
34 }
35
36 /** @brief Node desctructor */
37 Node::~Node()
38 {
39         if (action)
40                 delete action;
41 }
42
43 /** Prints debugging info for the ModelAction associated with this Node */
44 void Node::print()
45 {
46         if (action)
47                 action->print();
48         else
49                 printf("******** empty action ********\n");
50 }
51
52 /** @brief Prints info about may_read_from set */
53 void Node::print_may_read_from()
54 {
55         for (unsigned int i = 0; i < may_read_from.size(); i++)
56                 may_read_from[i]->print();
57 }
58
59 /**
60  * Sets a promise to explore meeting with the given node.
61  * @param i is the promise index.
62  */
63 void Node::set_promise(unsigned int i) {
64         if (i>=promises.size())
65                 promises.resize(i+1,0);
66         promises[i]=1;
67 }
68
69 /**
70  * Looks up whether a given promise should be satisfied by this node.
71  * @param i The promise index.
72  * @return true if the promise should be satisfied by the given model action.
73  */
74 bool Node::get_promise(unsigned int i) {
75         return (i<promises.size())&&(promises[i]==2);
76 }
77
78 /**
79  * Increments to the next combination of promises.
80  * @return true if we have a valid combination.
81  */
82 bool Node::increment_promise() {
83         for (unsigned int i=0;i<promises.size();i++) {
84                 if (promises[i]==1) {
85                         promises[i]=2;
86                         while (i>0) {
87                                 i--;
88                                 if (promises[i]==2)
89                                         promises[i]=1;
90                         }
91                         return true;
92                 }
93         }
94         return false;
95 }
96
97 /**
98  * Returns whether the promise set is empty.
99  * @return true if we have explored all promise combinations.
100  */
101 bool Node::promise_empty() {
102         for (unsigned int i=0;i<promises.size();i++)
103                 if (promises[i]==1)
104                         return false;
105         return true;
106 }
107
108 /**
109  * Adds a value from a weakly ordered future write to backtrack to.
110  * @param value is the value to backtrack to.
111  */
112 bool Node::add_future_value(uint64_t value) {
113         for(unsigned int i=0;i<future_values.size();i++)
114                 if (future_values[i]==value)
115                         return false;
116
117         future_values.push_back(value);
118         return true;
119 }
120
121 /**
122  * Checks whether the future_values set for this node is empty.
123  * @return true if the future_values set is empty.
124  */
125 bool Node::future_value_empty() {
126         return ((future_index+1)>=future_values.size());
127 }
128
129 /**
130  * Checks if the Thread associated with this thread ID has been explored from
131  * this Node already.
132  * @param tid is the thread ID to check
133  * @return true if this thread choice has been explored already, false
134  * otherwise
135  */
136 bool Node::has_been_explored(thread_id_t tid)
137 {
138         int id = id_to_int(tid);
139         return explored_children[id];
140 }
141
142 /**
143  * Checks if the backtracking set is empty.
144  * @return true if the backtracking set is empty
145  */
146 bool Node::backtrack_empty()
147 {
148         return (numBacktracks == 0);
149 }
150
151 /**
152  * Checks whether the readsfrom set for this node is empty.
153  * @return true if the readsfrom set is empty.
154  */
155 bool Node::read_from_empty() {
156         return ((read_from_index+1)>=may_read_from.size());
157 }
158
159 /**
160  * Mark the appropriate backtracking information for exploring a thread choice.
161  * @param act The ModelAction to explore
162  */
163 void Node::explore_child(ModelAction *act)
164 {
165         explore(act->get_tid());
166 }
167
168 /**
169  * Records a backtracking reference for a thread choice within this Node.
170  * Provides feedback as to whether this thread choice is already set for
171  * backtracking.
172  * @return false if the thread was already set to be backtracked, true
173  * otherwise
174  */
175 bool Node::set_backtrack(thread_id_t id)
176 {
177         int i = id_to_int(id);
178         if (backtrack[i])
179                 return false;
180         backtrack[i] = true;
181         numBacktracks++;
182         return true;
183 }
184
185 thread_id_t Node::get_next_backtrack()
186 {
187         /** @todo Find next backtrack */
188         unsigned int i;
189         for (i = 0; i < backtrack.size(); i++)
190                 if (backtrack[i] == true)
191                         break;
192         /* Backtrack set was empty? */
193         ASSERT(i != backtrack.size());
194
195         backtrack[i] = false;
196         numBacktracks--;
197         return int_to_id(i);
198 }
199
200 bool Node::is_enabled(Thread *t)
201 {
202         return id_to_int(t->get_id()) < num_threads;
203 }
204
205 /**
206  * Add an action to the may_read_from set.
207  * @param act is the action to add
208  */
209 void Node::add_read_from(const ModelAction *act)
210 {
211         may_read_from.push_back(act);
212 }
213
214 /**
215  * Gets the next 'future_value' value from this Node. Only valid for a node
216  * where this->action is a 'read'.
217  * @return The first element in future_values
218  */
219 uint64_t Node::get_future_value() {
220         ASSERT(future_index<future_values.size());
221         return future_values[future_index];
222 }
223
224 /**
225  * Gets the next 'may_read_from' action from this Node. Only valid for a node
226  * where this->action is a 'read'.
227  * @return The first element in may_read_from
228  */
229 const ModelAction * Node::get_read_from() {
230         if (read_from_index<may_read_from.size())
231                 return may_read_from[read_from_index];
232         else
233                 return NULL;
234 }
235
236 /**
237  * Increments the index into the readsfrom set to explore the next item.
238  * @return Returns false if we have explored all items.
239  */
240 bool Node::increment_read_from() {
241         read_from_index++;
242         return (read_from_index<may_read_from.size());
243 }
244
245 /**
246  * Increments the index into the future_values set to explore the next item.
247  * @return Returns false if we have explored all values.
248  */
249 bool Node::increment_future_value() {
250         future_index++;
251         return (future_index<future_values.size());
252 }
253
254 void Node::explore(thread_id_t tid)
255 {
256         int i = id_to_int(tid);
257         if (backtrack[i]) {
258                 backtrack[i] = false;
259                 numBacktracks--;
260         }
261         explored_children[i] = true;
262 }
263
264 static void clear_node_list(node_list_t *list, node_list_t::iterator start,
265                                                node_list_t::iterator end)
266 {
267         node_list_t::iterator it;
268
269         for (it = start; it != end; it++)
270                 delete (*it);
271         list->erase(start, end);
272 }
273
274 NodeStack::NodeStack()
275         : total_nodes(0)
276 {
277         node_list.push_back(new Node());
278         total_nodes++;
279         iter = node_list.begin();
280 }
281
282 NodeStack::~NodeStack()
283 {
284         clear_node_list(&node_list, node_list.begin(), node_list.end());
285 }
286
287 void NodeStack::print()
288 {
289         node_list_t::iterator it;
290         printf("............................................\n");
291         printf("NodeStack printing node_list:\n");
292         for (it = node_list.begin(); it != node_list.end(); it++) {
293                 if (it == this->iter)
294                         printf("vvv following action is the current iterator vvv\n");
295                 (*it)->print();
296         }
297         printf("............................................\n");
298 }
299
300 ModelAction * NodeStack::explore_action(ModelAction *act)
301 {
302         DBG();
303
304         ASSERT(!node_list.empty());
305         node_list_t::iterator it=iter;
306         it++;
307
308         if (it != node_list.end()) {
309                 iter++;
310                 return (*iter)->get_action();
311         }
312
313         /* Record action */
314         get_head()->explore_child(act);
315         node_list.push_back(new Node(act, get_head(), model->get_num_threads()));
316         total_nodes++;
317         iter++;
318         return NULL;
319 }
320
321 /**
322  * Empties the stack of all trailing nodes after a given position and calls the
323  * destructor for each. This function is provided an offset which determines
324  * how many nodes (relative to the current replay state) to save before popping
325  * the stack.
326  * @param numAhead gives the number of Nodes (including this Node) to skip over
327  * before removing nodes.
328  */
329 void NodeStack::pop_restofstack(int numAhead)
330 {
331         /* Diverging from previous execution; clear out remainder of list */
332         node_list_t::iterator it = iter;
333         while (numAhead--)
334                 it++;
335         clear_node_list(&node_list, it, node_list.end());
336 }
337
338 Node * NodeStack::get_head()
339 {
340         if (node_list.empty())
341                 return NULL;
342         return *iter;
343 }
344
345 Node * NodeStack::get_next()
346 {
347         node_list_t::iterator it = iter;
348         if (node_list.empty()) {
349                 DEBUG("Empty\n");
350                 return NULL;
351         }
352         it++;
353         if (it == node_list.end()) {
354                 DEBUG("At end\n");
355                 return NULL;
356         }
357         return *it;
358 }
359
360 void NodeStack::reset_execution()
361 {
362         iter = node_list.begin();
363 }