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