1 #include "cyclegraph.h"
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)),
15 /** CycleGraph destructor */
16 CycleGraph::~CycleGraph()
22 * Add a CycleNode to the graph, corresponding to a store ModelAction
23 * @param act The write action that should be added
24 * @param node The CycleNode that corresponds to the store
26 void CycleGraph::putNode(const ModelAction *act, CycleNode *node)
28 actionToNode.put(act, node);
29 #if SUPPORT_MOD_ORDER_DUMP
30 nodeList.push_back(node);
35 * @brief Returns the CycleNode corresponding to a given ModelAction
36 * @param action The ModelAction to find a node for
37 * @return The CycleNode paired with this action
39 CycleNode * CycleGraph::getNode(const ModelAction *action)
41 CycleNode *node = actionToNode.get(action);
43 node = new CycleNode(action);
44 putNode(action, node);
50 * @brief Adds an edge between objects
52 * This function will add an edge between any two objects which can be
53 * associated with a CycleNode. That is, if they have a CycleGraph::getNode
56 * The object to is ordered after the object from.
58 * @param to The edge points to this object, of type T
59 * @param from The edge comes from this object, of type U
61 template <typename T, typename U>
62 void CycleGraph::addEdge(const T from, const U to)
67 CycleNode *fromnode = getNode(from);
68 CycleNode *tonode = getNode(to);
70 addNodeEdge(fromnode, tonode);
74 * Adds an edge between two CycleNodes.
75 * @param fromnode The edge comes from this CycleNode
76 * @param tonode The edge points to this CycleNode
78 void CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
81 hasCycles = checkReachable(tonode, fromnode);
83 if (fromnode->addEdge(tonode))
84 rollbackvector.push_back(fromnode);
87 * If the fromnode has a rmwnode that is not the tonode, we should add
88 * an edge between its rmwnode and the tonode
90 CycleNode *rmwnode = fromnode->getRMW();
91 if (rmwnode && rmwnode != tonode) {
93 hasCycles = checkReachable(tonode, rmwnode);
95 if (rmwnode->addEdge(tonode))
96 rollbackvector.push_back(rmwnode);
101 * @brief Add an edge between a write and the RMW which reads from it
103 * Handles special case of a RMW action, where the ModelAction rmw reads from
104 * the ModelAction from. The key differences are:
105 * (1) no write can occur in between the rmw and the from action.
106 * (2) Only one RMW action can read from a given write.
108 * @param from The edge comes from this ModelAction
109 * @param rmw The edge points to this ModelAction; this action must read from
112 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
117 CycleNode *fromnode = getNode(from);
118 CycleNode *rmwnode = getNode(rmw);
120 /* Two RMW actions cannot read from the same write. */
121 if (fromnode->setRMW(rmwnode))
124 rmwrollbackvector.push_back(fromnode);
126 /* Transfer all outgoing edges from the from node to the rmw node */
127 /* This process should not add a cycle because either:
128 * (1) The rmw should not have any incoming edges yet if it is the
130 * (2) the fromnode is the new node and therefore it should not
131 * have any outgoing edges.
133 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
134 CycleNode *tonode = fromnode->getEdge(i);
135 if (tonode != rmwnode) {
136 if (rmwnode->addEdge(tonode))
137 rollbackvector.push_back(rmwnode);
141 addNodeEdge(fromnode, rmwnode);
144 #if SUPPORT_MOD_ORDER_DUMP
145 void CycleGraph::dumpNodes(FILE *file) const
147 for (unsigned int i = 0; i < nodeList.size(); i++) {
148 CycleNode *cn = nodeList[i];
149 const ModelAction *action = cn->getAction();
150 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
151 if (cn->getRMW() != NULL) {
152 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
154 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
155 CycleNode *dst = cn->getEdge(j);
156 const ModelAction *dstaction = dst->getAction();
157 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
162 void CycleGraph::dumpGraphToFile(const char *filename) const
165 sprintf(buffer, "%s.dot", filename);
166 FILE *file = fopen(buffer, "w");
167 fprintf(file, "digraph %s {\n", filename);
169 fprintf(file, "}\n");
175 * Checks whether one ModelAction can reach another.
176 * @param from The ModelAction from which to begin exploration
177 * @param to The ModelAction to reach
178 * @return True, @a from can reach @a to; otherwise, false
180 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) const
182 CycleNode *fromnode = actionToNode.get(from);
183 CycleNode *tonode = actionToNode.get(to);
185 if (!fromnode || !tonode)
188 return checkReachable(fromnode, tonode);
192 * Checks whether one CycleNode can reach another.
193 * @param from The CycleNode from which to begin exploration
194 * @param to The CycleNode to reach
195 * @return True, @a from can reach @a to; otherwise, false
197 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
199 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
202 queue.push_back(from);
203 discovered->put(from, from);
204 while (!queue.empty()) {
205 const CycleNode *node = queue.back();
210 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
211 CycleNode *next = node->getEdge(i);
212 if (!discovered->contains(next)) {
213 discovered->put(next, next);
214 queue.push_back(next);
221 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
223 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
225 CycleNode *from = actionToNode.get(fromact);
227 queue.push_back(from);
228 discovered->put(from, from);
229 while (!queue.empty()) {
230 CycleNode *node = queue.back();
233 if (promise->eliminate_thread(node->getAction()->get_tid())) {
237 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
238 CycleNode *next = node->getEdge(i);
239 if (!discovered->contains(next)) {
240 discovered->put(next, next);
241 queue.push_back(next);
248 void CycleGraph::startChanges()
250 ASSERT(rollbackvector.empty());
251 ASSERT(rmwrollbackvector.empty());
252 ASSERT(oldCycles == hasCycles);
255 /** Commit changes to the cyclegraph. */
256 void CycleGraph::commitChanges()
258 rollbackvector.clear();
259 rmwrollbackvector.clear();
260 oldCycles = hasCycles;
263 /** Rollback changes to the previous commit. */
264 void CycleGraph::rollbackChanges()
266 for (unsigned int i = 0; i < rollbackvector.size(); i++)
267 rollbackvector[i]->popEdge();
269 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
270 rmwrollbackvector[i]->clearRMW();
272 hasCycles = oldCycles;
273 rollbackvector.clear();
274 rmwrollbackvector.clear();
277 /** @returns whether a CycleGraph contains cycles. */
278 bool CycleGraph::checkForCycles() const
284 * @brief Constructor for a CycleNode
285 * @param act The ModelAction for this node
287 CycleNode::CycleNode(const ModelAction *act) :
295 * @brief Constructor for a Promise CycleNode
296 * @param promise The Promise which was generated
298 CycleNode::CycleNode(const Promise *promise) :
306 * @param i The index of the edge to return
307 * @returns The a CycleNode edge indexed by i
309 CycleNode * CycleNode::getEdge(unsigned int i) const
314 /** @returns The number of edges leaving this CycleNode */
315 unsigned int CycleNode::getNumEdges() const
320 CycleNode * CycleNode::getBackEdge(unsigned int i) const
322 return back_edges[i];
325 unsigned int CycleNode::getNumBackEdges() const
327 return back_edges.size();
331 * @brief Remove an element from a vector
332 * @param v The vector
333 * @param n The element to remove
334 * @return True if the element was found; false otherwise
336 template <typename T>
337 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
339 for (unsigned int i = 0; i < v.size(); i++) {
341 v.erase(v.begin() + i);
349 * @brief Remove a (forward) edge from this CycleNode
350 * @return The CycleNode which was popped, if one exists; otherwise NULL
352 CycleNode * CycleNode::removeEdge()
357 CycleNode *ret = edges.back();
359 vector_remove_node(ret->back_edges, this);
364 * @brief Remove a (back) edge from this CycleNode
365 * @return The CycleNode which was popped, if one exists; otherwise NULL
367 CycleNode * CycleNode::removeBackEdge()
369 if (back_edges.empty())
372 CycleNode *ret = back_edges.back();
373 back_edges.pop_back();
374 vector_remove_node(ret->edges, this);
379 * Adds an edge from this CycleNode to another CycleNode.
380 * @param node The node to which we add a directed edge
381 * @return True if this edge is a new edge; false otherwise
383 bool CycleNode::addEdge(CycleNode *node)
385 for (unsigned int i = 0; i < edges.size(); i++)
386 if (edges[i] == node)
388 edges.push_back(node);
389 node->back_edges.push_back(this);
393 /** @returns the RMW CycleNode that reads from the current CycleNode */
394 CycleNode * CycleNode::getRMW() const
400 * Set a RMW action node that reads from the current CycleNode.
401 * @param node The RMW that reads from the current node
402 * @return True, if this node already was read by another RMW; false otherwise
403 * @see CycleGraph::addRMWEdge
405 bool CycleNode::setRMW(CycleNode *node)