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