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