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