cyclegraph: template for addEdge()
[c11tester.git] / cyclegraph.cc
1 #include "cyclegraph.h"
2 #include "action.h"
3 #include "common.h"
4 #include "promise.h"
5 #include "model.h"
6
7 /** Initializes a CycleGraph object. */
8 CycleGraph::CycleGraph() :
9         discovered(new HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free>(16)),
10         hasCycles(false),
11         oldCycles(false)
12 {
13 }
14
15 /** CycleGraph destructor */
16 CycleGraph::~CycleGraph()
17 {
18         delete discovered;
19 }
20
21 /**
22  * Add a CycleNode to the graph, corresponding to a store ModelAction
23  * @param act The write action that should be added
24  * @param node The CycleNode that corresponds to the store
25  */
26 void CycleGraph::putNode(const ModelAction *act, CycleNode *node)
27 {
28         actionToNode.put(act, node);
29 #if SUPPORT_MOD_ORDER_DUMP
30         nodeList.push_back(node);
31 #endif
32 }
33
34 /**
35  * @brief Returns the CycleNode corresponding to a given ModelAction
36  * @param action The ModelAction to find a node for
37  * @return The CycleNode paired with this action
38  */
39 CycleNode * CycleGraph::getNode(const ModelAction *action)
40 {
41         CycleNode *node = actionToNode.get(action);
42         if (node == NULL) {
43                 node = new CycleNode(action);
44                 putNode(action, node);
45         }
46         return node;
47 }
48
49 /**
50  * @brief Adds an edge between objects
51  *
52  * This function will add an edge between any two objects which can be
53  * associated with a CycleNode. That is, if they have a CycleGraph::getNode
54  * implementation.
55  *
56  * The object to is ordered after the object from.
57  *
58  * @param to The edge points to this object, of type T
59  * @param from The edge comes from this object, of type U
60  */
61 template <typename T, typename U>
62 void CycleGraph::addEdge(const T from, const U to)
63 {
64         ASSERT(from);
65         ASSERT(to);
66
67         CycleNode *fromnode = getNode(from);
68         CycleNode *tonode = getNode(to);
69
70         addNodeEdge(fromnode, tonode);
71 }
72
73 /**
74  * Adds an edge between two CycleNodes.
75  * @param fromnode The edge comes from this CycleNode
76  * @param tonode The edge points to this CycleNode
77  */
78 void CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
79 {
80         if (!hasCycles)
81                 hasCycles = checkReachable(tonode, fromnode);
82
83         if (fromnode->addEdge(tonode))
84                 rollbackvector.push_back(fromnode);
85
86         /*
87          * If the fromnode has a rmwnode that is not the tonode, we should add
88          * an edge between its rmwnode and the tonode
89          */
90         CycleNode *rmwnode = fromnode->getRMW();
91         if (rmwnode && rmwnode != tonode) {
92                 if (!hasCycles)
93                         hasCycles = checkReachable(tonode, rmwnode);
94
95                 if (rmwnode->addEdge(tonode))
96                         rollbackvector.push_back(rmwnode);
97         }
98 }
99
100 /**
101  * @brief Add an edge between a write and the RMW which reads from it
102  *
103  * Handles special case of a RMW action, where the ModelAction rmw reads from
104  * the ModelAction from. The key differences are:
105  * (1) no write can occur in between the rmw and the from action.
106  * (2) Only one RMW action can read from a given write.
107  *
108  * @param from The edge comes from this ModelAction
109  * @param rmw The edge points to this ModelAction; this action must read from
110  * ModelAction from
111  */
112 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
113 {
114         ASSERT(from);
115         ASSERT(rmw);
116
117         CycleNode *fromnode = getNode(from);
118         CycleNode *rmwnode = getNode(rmw);
119
120         /* Two RMW actions cannot read from the same write. */
121         if (fromnode->setRMW(rmwnode))
122                 hasCycles = true;
123         else
124                 rmwrollbackvector.push_back(fromnode);
125
126         /* Transfer all outgoing edges from the from node to the rmw node */
127         /* This process should not add a cycle because either:
128          * (1) The rmw should not have any incoming edges yet if it is the
129          * new node or
130          * (2) the fromnode is the new node and therefore it should not
131          * have any outgoing edges.
132          */
133         for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
134                 CycleNode *tonode = fromnode->getEdge(i);
135                 if (tonode != rmwnode) {
136                         if (rmwnode->addEdge(tonode))
137                                 rollbackvector.push_back(rmwnode);
138                 }
139         }
140
141         addNodeEdge(fromnode, rmwnode);
142 }
143
144 #if SUPPORT_MOD_ORDER_DUMP
145 void CycleGraph::dumpNodes(FILE *file) const
146 {
147         for (unsigned int i = 0; i < nodeList.size(); i++) {
148                 CycleNode *cn = nodeList[i];
149                 const ModelAction *action = cn->getAction();
150                 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
151                 if (cn->getRMW() != NULL) {
152                         fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
153                 }
154                 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
155                         CycleNode *dst = cn->getEdge(j);
156                         const ModelAction *dstaction = dst->getAction();
157                         fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
158                 }
159         }
160 }
161
162 void CycleGraph::dumpGraphToFile(const char *filename) const
163 {
164         char buffer[200];
165         sprintf(buffer, "%s.dot", filename);
166         FILE *file = fopen(buffer, "w");
167         fprintf(file, "digraph %s {\n", filename);
168         dumpNodes(file);
169         fprintf(file, "}\n");
170         fclose(file);
171 }
172 #endif
173
174 /**
175  * Checks whether one ModelAction can reach another.
176  * @param from The ModelAction from which to begin exploration
177  * @param to The ModelAction to reach
178  * @return True, @a from can reach @a to; otherwise, false
179  */
180 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) const
181 {
182         CycleNode *fromnode = actionToNode.get(from);
183         CycleNode *tonode = actionToNode.get(to);
184
185         if (!fromnode || !tonode)
186                 return false;
187
188         return checkReachable(fromnode, tonode);
189 }
190
191 /**
192  * Checks whether one CycleNode can reach another.
193  * @param from The CycleNode from which to begin exploration
194  * @param to The CycleNode to reach
195  * @return True, @a from can reach @a to; otherwise, false
196  */
197 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
198 {
199         std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
200         discovered->reset();
201
202         queue.push_back(from);
203         discovered->put(from, from);
204         while (!queue.empty()) {
205                 const CycleNode *node = queue.back();
206                 queue.pop_back();
207                 if (node == to)
208                         return true;
209
210                 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
211                         CycleNode *next = node->getEdge(i);
212                         if (!discovered->contains(next)) {
213                                 discovered->put(next, next);
214                                 queue.push_back(next);
215                         }
216                 }
217         }
218         return false;
219 }
220
221 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
222 {
223         std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
224         discovered->reset();
225         CycleNode *from = actionToNode.get(fromact);
226
227         queue.push_back(from);
228         discovered->put(from, from);
229         while (!queue.empty()) {
230                 CycleNode *node = queue.back();
231                 queue.pop_back();
232
233                 if (promise->eliminate_thread(node->getAction()->get_tid())) {
234                         return true;
235                 }
236
237                 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
238                         CycleNode *next = node->getEdge(i);
239                         if (!discovered->contains(next)) {
240                                 discovered->put(next, next);
241                                 queue.push_back(next);
242                         }
243                 }
244         }
245         return false;
246 }
247
248 void CycleGraph::startChanges()
249 {
250         ASSERT(rollbackvector.empty());
251         ASSERT(rmwrollbackvector.empty());
252         ASSERT(oldCycles == hasCycles);
253 }
254
255 /** Commit changes to the cyclegraph. */
256 void CycleGraph::commitChanges()
257 {
258         rollbackvector.clear();
259         rmwrollbackvector.clear();
260         oldCycles = hasCycles;
261 }
262
263 /** Rollback changes to the previous commit. */
264 void CycleGraph::rollbackChanges()
265 {
266         for (unsigned int i = 0; i < rollbackvector.size(); i++)
267                 rollbackvector[i]->popEdge();
268
269         for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
270                 rmwrollbackvector[i]->clearRMW();
271
272         hasCycles = oldCycles;
273         rollbackvector.clear();
274         rmwrollbackvector.clear();
275 }
276
277 /** @returns whether a CycleGraph contains cycles. */
278 bool CycleGraph::checkForCycles() const
279 {
280         return hasCycles;
281 }
282
283 /**
284  * @brief Constructor for a CycleNode
285  * @param act The ModelAction for this node
286  */
287 CycleNode::CycleNode(const ModelAction *act) :
288         action(act),
289         promise(NULL),
290         hasRMW(NULL)
291 {
292 }
293
294 /**
295  * @brief Constructor for a Promise CycleNode
296  * @param promise The Promise which was generated
297  */
298 CycleNode::CycleNode(const Promise *promise) :
299         action(NULL),
300         promise(promise),
301         hasRMW(NULL)
302 {
303 }
304
305 /**
306  * @param i The index of the edge to return
307  * @returns The a CycleNode edge indexed by i
308  */
309 CycleNode * CycleNode::getEdge(unsigned int i) const
310 {
311         return edges[i];
312 }
313
314 /** @returns The number of edges leaving this CycleNode */
315 unsigned int CycleNode::getNumEdges() const
316 {
317         return edges.size();
318 }
319
320 CycleNode * CycleNode::getBackEdge(unsigned int i) const
321 {
322         return back_edges[i];
323 }
324
325 unsigned int CycleNode::getNumBackEdges() const
326 {
327         return back_edges.size();
328 }
329
330 /**
331  * @brief Remove an element from a vector
332  * @param v The vector
333  * @param n The element to remove
334  * @return True if the element was found; false otherwise
335  */
336 template <typename T>
337 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
338 {
339         for (unsigned int i = 0; i < v.size(); i++) {
340                 if (v[i] == n) {
341                         v.erase(v.begin() + i);
342                         return true;
343                 }
344         }
345         return false;
346 }
347
348 /**
349  * @brief Remove a (forward) edge from this CycleNode
350  * @return The CycleNode which was popped, if one exists; otherwise NULL
351  */
352 CycleNode * CycleNode::removeEdge()
353 {
354         if (edges.empty())
355                 return NULL;
356
357         CycleNode *ret = edges.back();
358         edges.pop_back();
359         vector_remove_node(ret->back_edges, this);
360         return ret;
361 }
362
363 /**
364  * @brief Remove a (back) edge from this CycleNode
365  * @return The CycleNode which was popped, if one exists; otherwise NULL
366  */
367 CycleNode * CycleNode::removeBackEdge()
368 {
369         if (back_edges.empty())
370                 return NULL;
371
372         CycleNode *ret = back_edges.back();
373         back_edges.pop_back();
374         vector_remove_node(ret->edges, this);
375         return ret;
376 }
377
378 /**
379  * Adds an edge from this CycleNode to another CycleNode.
380  * @param node The node to which we add a directed edge
381  * @return True if this edge is a new edge; false otherwise
382  */
383 bool CycleNode::addEdge(CycleNode *node)
384 {
385         for (unsigned int i = 0; i < edges.size(); i++)
386                 if (edges[i] == node)
387                         return false;
388         edges.push_back(node);
389         node->back_edges.push_back(this);
390         return true;
391 }
392
393 /** @returns the RMW CycleNode that reads from the current CycleNode */
394 CycleNode * CycleNode::getRMW() const
395 {
396         return hasRMW;
397 }
398
399 /**
400  * Set a RMW action node that reads from the current CycleNode.
401  * @param node The RMW that reads from the current node
402  * @return True, if this node already was read by another RMW; false otherwise
403  * @see CycleGraph::addRMWEdge
404  */
405 bool CycleNode::setRMW(CycleNode *node)
406 {
407         if (hasRMW != NULL)
408                 return true;
409         hasRMW = node;
410         return false;
411 }