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 Returns a CycleNode corresponding to a promise
52 * Gets (or creates, if none exist) a CycleNode corresponding to a promised
55 * @param promise The Promise generated by a reader
56 * @return The CycleNode corresponding to the Promise
58 CycleNode * CycleGraph::getNode(const Promise *promise)
60 const ModelAction *reader = promise->get_action();
61 CycleNode *node = readerToPromiseNode.get(reader);
63 node = new CycleNode(promise);
64 readerToPromiseNode.put(reader, node);
70 * @brief Adds an edge between objects
72 * This function will add an edge between any two objects which can be
73 * associated with a CycleNode. That is, if they have a CycleGraph::getNode
76 * The object to is ordered after the object from.
78 * @param to The edge points to this object, of type T
79 * @param from The edge comes from this object, of type U
81 template <typename T, typename U>
82 void CycleGraph::addEdge(const T from, const U to)
87 CycleNode *fromnode = getNode(from);
88 CycleNode *tonode = getNode(to);
90 addNodeEdge(fromnode, tonode);
94 * Adds an edge between two CycleNodes.
95 * @param fromnode The edge comes from this CycleNode
96 * @param tonode The edge points to this CycleNode
98 void CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
101 hasCycles = checkReachable(tonode, fromnode);
103 if (fromnode->addEdge(tonode))
104 rollbackvector.push_back(fromnode);
107 * If the fromnode has a rmwnode that is not the tonode, we should add
108 * an edge between its rmwnode and the tonode
110 CycleNode *rmwnode = fromnode->getRMW();
111 if (rmwnode && rmwnode != tonode) {
113 hasCycles = checkReachable(tonode, rmwnode);
115 if (rmwnode->addEdge(tonode))
116 rollbackvector.push_back(rmwnode);
121 * @brief Add an edge between a write and the RMW which reads from it
123 * Handles special case of a RMW action, where the ModelAction rmw reads from
124 * the ModelAction from. The key differences are:
125 * (1) no write can occur in between the rmw and the from action.
126 * (2) Only one RMW action can read from a given write.
128 * @param from The edge comes from this ModelAction
129 * @param rmw The edge points to this ModelAction; this action must read from
132 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
137 CycleNode *fromnode = getNode(from);
138 CycleNode *rmwnode = getNode(rmw);
140 /* Two RMW actions cannot read from the same write. */
141 if (fromnode->setRMW(rmwnode))
144 rmwrollbackvector.push_back(fromnode);
146 /* Transfer all outgoing edges from the from node to the rmw node */
147 /* This process should not add a cycle because either:
148 * (1) The rmw should not have any incoming edges yet if it is the
150 * (2) the fromnode is the new node and therefore it should not
151 * have any outgoing edges.
153 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
154 CycleNode *tonode = fromnode->getEdge(i);
155 if (tonode != rmwnode) {
156 if (rmwnode->addEdge(tonode))
157 rollbackvector.push_back(rmwnode);
161 addNodeEdge(fromnode, rmwnode);
164 #if SUPPORT_MOD_ORDER_DUMP
165 void CycleGraph::dumpNodes(FILE *file) const
167 for (unsigned int i = 0; i < nodeList.size(); i++) {
168 CycleNode *cn = nodeList[i];
169 const ModelAction *action = cn->getAction();
170 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
171 if (cn->getRMW() != NULL) {
172 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
174 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
175 CycleNode *dst = cn->getEdge(j);
176 const ModelAction *dstaction = dst->getAction();
177 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
182 void CycleGraph::dumpGraphToFile(const char *filename) const
185 sprintf(buffer, "%s.dot", filename);
186 FILE *file = fopen(buffer, "w");
187 fprintf(file, "digraph %s {\n", filename);
189 fprintf(file, "}\n");
195 * Checks whether one ModelAction can reach another.
196 * @param from The ModelAction from which to begin exploration
197 * @param to The ModelAction to reach
198 * @return True, @a from can reach @a to; otherwise, false
200 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) const
202 CycleNode *fromnode = actionToNode.get(from);
203 CycleNode *tonode = actionToNode.get(to);
205 if (!fromnode || !tonode)
208 return checkReachable(fromnode, tonode);
212 * Checks whether one CycleNode can reach another.
213 * @param from The CycleNode from which to begin exploration
214 * @param to The CycleNode to reach
215 * @return True, @a from can reach @a to; otherwise, false
217 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
219 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
222 queue.push_back(from);
223 discovered->put(from, from);
224 while (!queue.empty()) {
225 const CycleNode *node = queue.back();
230 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
231 CycleNode *next = node->getEdge(i);
232 if (!discovered->contains(next)) {
233 discovered->put(next, next);
234 queue.push_back(next);
241 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
243 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
245 CycleNode *from = actionToNode.get(fromact);
247 queue.push_back(from);
248 discovered->put(from, from);
249 while (!queue.empty()) {
250 CycleNode *node = queue.back();
253 if (promise->eliminate_thread(node->getAction()->get_tid())) {
257 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
258 CycleNode *next = node->getEdge(i);
259 if (!discovered->contains(next)) {
260 discovered->put(next, next);
261 queue.push_back(next);
268 void CycleGraph::startChanges()
270 ASSERT(rollbackvector.empty());
271 ASSERT(rmwrollbackvector.empty());
272 ASSERT(oldCycles == hasCycles);
275 /** Commit changes to the cyclegraph. */
276 void CycleGraph::commitChanges()
278 rollbackvector.clear();
279 rmwrollbackvector.clear();
280 oldCycles = hasCycles;
283 /** Rollback changes to the previous commit. */
284 void CycleGraph::rollbackChanges()
286 for (unsigned int i = 0; i < rollbackvector.size(); i++)
287 rollbackvector[i]->popEdge();
289 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
290 rmwrollbackvector[i]->clearRMW();
292 hasCycles = oldCycles;
293 rollbackvector.clear();
294 rmwrollbackvector.clear();
297 /** @returns whether a CycleGraph contains cycles. */
298 bool CycleGraph::checkForCycles() const
304 * @brief Constructor for a CycleNode
305 * @param act The ModelAction for this node
307 CycleNode::CycleNode(const ModelAction *act) :
315 * @brief Constructor for a Promise CycleNode
316 * @param promise The Promise which was generated
318 CycleNode::CycleNode(const Promise *promise) :
326 * @param i The index of the edge to return
327 * @returns The a CycleNode edge indexed by i
329 CycleNode * CycleNode::getEdge(unsigned int i) const
334 /** @returns The number of edges leaving this CycleNode */
335 unsigned int CycleNode::getNumEdges() const
340 CycleNode * CycleNode::getBackEdge(unsigned int i) const
342 return back_edges[i];
345 unsigned int CycleNode::getNumBackEdges() const
347 return back_edges.size();
351 * @brief Remove an element from a vector
352 * @param v The vector
353 * @param n The element to remove
354 * @return True if the element was found; false otherwise
356 template <typename T>
357 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
359 for (unsigned int i = 0; i < v.size(); i++) {
361 v.erase(v.begin() + i);
369 * @brief Remove a (forward) edge from this CycleNode
370 * @return The CycleNode which was popped, if one exists; otherwise NULL
372 CycleNode * CycleNode::removeEdge()
377 CycleNode *ret = edges.back();
379 vector_remove_node(ret->back_edges, this);
384 * @brief Remove a (back) edge from this CycleNode
385 * @return The CycleNode which was popped, if one exists; otherwise NULL
387 CycleNode * CycleNode::removeBackEdge()
389 if (back_edges.empty())
392 CycleNode *ret = back_edges.back();
393 back_edges.pop_back();
394 vector_remove_node(ret->edges, this);
399 * Adds an edge from this CycleNode to another CycleNode.
400 * @param node The node to which we add a directed edge
401 * @return True if this edge is a new edge; false otherwise
403 bool CycleNode::addEdge(CycleNode *node)
405 for (unsigned int i = 0; i < edges.size(); i++)
406 if (edges[i] == node)
408 edges.push_back(node);
409 node->back_edges.push_back(this);
413 /** @returns the RMW CycleNode that reads from the current CycleNode */
414 CycleNode * CycleNode::getRMW() const
420 * Set a RMW action node that reads from the current CycleNode.
421 * @param node The RMW that reads from the current node
422 * @return True, if this node already was read by another RMW; false otherwise
423 * @see CycleGraph::addRMWEdge
425 bool CycleNode::setRMW(CycleNode *node)