1 #include "cyclegraph.h"
7 /** Initializes a CycleGraph object. */
8 CycleGraph::CycleGraph() :
9 discovered(new HashTable<CycleNode *, CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free>(16)),
12 hasRMWViolation(false),
13 oldRMWViolation(false)
17 /** CycleGraph destructor */
18 CycleGraph::~CycleGraph()
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
28 void CycleGraph::putNode(const ModelAction *act, CycleNode *node)
30 actionToNode.put(act, node);
31 #if SUPPORT_MOD_ORDER_DUMP
32 nodeList.push_back(node);
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
41 CycleNode * CycleGraph::getNode(const ModelAction *action)
43 CycleNode *node = actionToNode.get(action);
45 node = new CycleNode(action);
46 putNode(action, node);
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
57 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to)
62 CycleNode *fromnode = getNode(from);
63 CycleNode *tonode = getNode(to);
66 // Reflexive edges are cycles
67 hasCycles = (from == to);
71 hasCycles = checkReachable(tonode, fromnode);
74 if (fromnode->addEdge(tonode))
75 rollbackvector.push_back(fromnode);
78 CycleNode *rmwnode = fromnode->getRMW();
81 * If the fromnode has a rmwnode that is not the tonode, we should add
82 * an edge between its rmwnode and the tonode
84 * If tonode is also a rmw, don't do this check as the execution is
85 * doomed and we'll catch the problem elsewhere, but we want to allow
86 * for the possibility of sending to's write value to rmwnode
88 if (rmwnode != NULL && !to->is_rmw()) {
91 hasCycles = checkReachable(tonode, rmwnode);
94 if (rmwnode->addEdge(tonode))
95 rollbackvector.push_back(rmwnode);
99 /** Handles special case of a RMW action. The ModelAction rmw reads
100 * from the ModelAction from. The key differences are: (1) no write
101 * can occur in between the rmw and the from action. Only one RMW
102 * action can read from a given write.
104 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
109 CycleNode *fromnode = getNode(from);
110 CycleNode *rmwnode = getNode(rmw);
112 /* Two RMW actions cannot read from the same write. */
113 if (fromnode->setRMW(rmwnode)) {
114 hasRMWViolation = true;
116 rmwrollbackvector.push_back(fromnode);
119 /* Transfer all outgoing edges from the from node to the rmw node */
120 /* This process should not add a cycle because either:
121 * (1) The rmw should not have any incoming edges yet if it is the
123 * (2) the fromnode is the new node and therefore it should not
124 * have any outgoing edges.
126 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
127 CycleNode *tonode = fromnode->getEdge(i);
128 if (tonode != rmwnode) {
129 if (rmwnode->addEdge(tonode))
130 rollbackvector.push_back(rmwnode);
136 // Reflexive edges are cycles
137 hasCycles = (from == rmw);
140 // With promises we could be setting up a cycle here if we aren't
141 // careful...avoid it..
142 hasCycles = checkReachable(rmwnode, fromnode);
144 if (fromnode->addEdge(rmwnode))
145 rollbackvector.push_back(fromnode);
148 #if SUPPORT_MOD_ORDER_DUMP
149 void CycleGraph::dumpNodes(FILE *file) const
151 for (unsigned int i = 0; i < nodeList.size(); i++) {
152 CycleNode *cn = nodeList[i];
153 const ModelAction *action = cn->getAction();
154 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
155 if (cn->getRMW() != NULL) {
156 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
158 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
159 CycleNode *dst = cn->getEdge(j);
160 const ModelAction *dstaction = dst->getAction();
161 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
166 void CycleGraph::dumpGraphToFile(const char *filename) const
169 sprintf(buffer, "%s.dot", filename);
170 FILE *file = fopen(buffer, "w");
171 fprintf(file, "digraph %s {\n", filename);
173 fprintf(file, "}\n");
179 * Checks whether one ModelAction can reach another.
180 * @param from The ModelAction from which to begin exploration
181 * @param to The ModelAction to reach
182 * @return True, @a from can reach @a to; otherwise, false
184 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) const
186 CycleNode *fromnode = actionToNode.get(from);
187 CycleNode *tonode = actionToNode.get(to);
189 if (!fromnode || !tonode)
192 return checkReachable(fromnode, tonode);
196 * Checks whether one CycleNode can reach another.
197 * @param from The CycleNode from which to begin exploration
198 * @param to The CycleNode to reach
199 * @return True, @a from can reach @a to; otherwise, false
201 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) const
203 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
206 queue.push_back(from);
207 discovered->put(from, from);
208 while (!queue.empty()) {
209 CycleNode *node = queue.back();
214 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
215 CycleNode *next = node->getEdge(i);
216 if (!discovered->contains(next)) {
217 discovered->put(next, next);
218 queue.push_back(next);
225 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
227 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
229 CycleNode *from = actionToNode.get(fromact);
231 queue.push_back(from);
232 discovered->put(from, from);
233 while (!queue.empty()) {
234 CycleNode *node = queue.back();
237 if (promise->increment_threads(node->getAction()->get_tid())) {
241 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
242 CycleNode *next = node->getEdge(i);
243 if (!discovered->contains(next)) {
244 discovered->put(next, next);
245 queue.push_back(next);
252 void CycleGraph::startChanges()
254 ASSERT(rollbackvector.size() == 0);
255 ASSERT(rmwrollbackvector.size() == 0);
256 ASSERT(oldCycles == hasCycles);
257 ASSERT(oldRMWViolation == hasRMWViolation);
260 /** Commit changes to the cyclegraph. */
261 void CycleGraph::commitChanges()
263 rollbackvector.resize(0);
264 rmwrollbackvector.resize(0);
265 oldCycles = hasCycles;
266 oldRMWViolation = hasRMWViolation;
269 /** Rollback changes to the previous commit. */
270 void CycleGraph::rollbackChanges()
272 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
273 rollbackvector[i]->popEdge();
276 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
277 rmwrollbackvector[i]->clearRMW();
280 hasCycles = oldCycles;
281 hasRMWViolation = oldRMWViolation;
282 rollbackvector.resize(0);
283 rmwrollbackvector.resize(0);
286 /** @returns whether a CycleGraph contains cycles. */
287 bool CycleGraph::checkForCycles() const
292 bool CycleGraph::checkForRMWViolation() const
294 return hasRMWViolation;
298 * @brief Constructor for a CycleNode
299 * @param act The ModelAction for this node
301 CycleNode::CycleNode(const ModelAction *act) :
308 * @param i The index of the edge to return
309 * @returns The a CycleNode edge indexed by i
311 CycleNode * CycleNode::getEdge(unsigned int i) const
316 /** @returns The number of edges leaving this CycleNode */
317 unsigned int CycleNode::getNumEdges() const
323 * Adds an edge from this CycleNode to another CycleNode.
324 * @param node The node to which we add a directed edge
326 bool CycleNode::addEdge(CycleNode *node)
328 for (unsigned int i = 0; i < edges.size(); i++)
329 if (edges[i] == node)
331 edges.push_back(node);
335 /** @returns the RMW CycleNode that reads from the current CycleNode */
336 CycleNode * CycleNode::getRMW() const
342 * Set a RMW action node that reads from the current CycleNode.
343 * @param node The RMW that reads from the current node
344 * @return True, if this node already was read by another RMW; false otherwise
345 * @see CycleGraph::addRMWEdge
347 bool CycleNode::setRMW(CycleNode *node)