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