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 * Adds an edge between two ModelActions. The ModelAction @a to is ordered
51 * after the ModelAction @a from.
52 * @param to The edge points to this ModelAction
53 * @param from The edge comes from this ModelAction
55 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to)
60 CycleNode *fromnode = getNode(from);
61 CycleNode *tonode = getNode(to);
63 addEdge(fromnode, tonode);
67 * Adds an edge between two CycleNodes.
68 * @param fromnode The edge comes from this CycleNode
69 * @param tonode The edge points to this CycleNode
71 void CycleGraph::addEdge(CycleNode *fromnode, CycleNode *tonode)
74 hasCycles = checkReachable(tonode, fromnode);
76 if (fromnode->addEdge(tonode))
77 rollbackvector.push_back(fromnode);
80 * If the fromnode has a rmwnode that is not the tonode, we should add
81 * an edge between its rmwnode and the tonode
83 CycleNode *rmwnode = fromnode->getRMW();
84 if (rmwnode && rmwnode != tonode) {
86 hasCycles = checkReachable(tonode, rmwnode);
88 if (rmwnode->addEdge(tonode))
89 rollbackvector.push_back(rmwnode);
94 * @brief Add an edge between a write and the RMW which reads from it
96 * Handles special case of a RMW action, where the ModelAction rmw reads from
97 * the ModelAction from. The key differences are:
98 * (1) no write can occur in between the rmw and the from action.
99 * (2) Only one RMW action can read from a given write.
101 * @param from The edge comes from this ModelAction
102 * @param rmw The edge points to this ModelAction; this action must read from
105 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
110 CycleNode *fromnode = getNode(from);
111 CycleNode *rmwnode = getNode(rmw);
113 /* Two RMW actions cannot read from the same write. */
114 if (fromnode->setRMW(rmwnode))
117 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);
134 addEdge(fromnode, rmwnode);
137 #if SUPPORT_MOD_ORDER_DUMP
138 void CycleGraph::dumpNodes(FILE *file) const
140 for (unsigned int i = 0; i < nodeList.size(); i++) {
141 CycleNode *cn = nodeList[i];
142 const ModelAction *action = cn->getAction();
143 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
144 if (cn->getRMW() != NULL) {
145 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
147 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
148 CycleNode *dst = cn->getEdge(j);
149 const ModelAction *dstaction = dst->getAction();
150 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
155 void CycleGraph::dumpGraphToFile(const char *filename) const
158 sprintf(buffer, "%s.dot", filename);
159 FILE *file = fopen(buffer, "w");
160 fprintf(file, "digraph %s {\n", filename);
162 fprintf(file, "}\n");
168 * Checks whether one ModelAction can reach another.
169 * @param from The ModelAction from which to begin exploration
170 * @param to The ModelAction to reach
171 * @return True, @a from can reach @a to; otherwise, false
173 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) const
175 CycleNode *fromnode = actionToNode.get(from);
176 CycleNode *tonode = actionToNode.get(to);
178 if (!fromnode || !tonode)
181 return checkReachable(fromnode, tonode);
185 * Checks whether one CycleNode can reach another.
186 * @param from The CycleNode from which to begin exploration
187 * @param to The CycleNode to reach
188 * @return True, @a from can reach @a to; otherwise, false
190 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
192 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
195 queue.push_back(from);
196 discovered->put(from, from);
197 while (!queue.empty()) {
198 const CycleNode *node = queue.back();
203 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
204 CycleNode *next = node->getEdge(i);
205 if (!discovered->contains(next)) {
206 discovered->put(next, next);
207 queue.push_back(next);
214 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
216 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
218 CycleNode *from = actionToNode.get(fromact);
220 queue.push_back(from);
221 discovered->put(from, from);
222 while (!queue.empty()) {
223 CycleNode *node = queue.back();
226 if (promise->eliminate_thread(node->getAction()->get_tid())) {
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 void CycleGraph::startChanges()
243 ASSERT(rollbackvector.empty());
244 ASSERT(rmwrollbackvector.empty());
245 ASSERT(oldCycles == hasCycles);
248 /** Commit changes to the cyclegraph. */
249 void CycleGraph::commitChanges()
251 rollbackvector.clear();
252 rmwrollbackvector.clear();
253 oldCycles = hasCycles;
256 /** Rollback changes to the previous commit. */
257 void CycleGraph::rollbackChanges()
259 for (unsigned int i = 0; i < rollbackvector.size(); i++)
260 rollbackvector[i]->popEdge();
262 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
263 rmwrollbackvector[i]->clearRMW();
265 hasCycles = oldCycles;
266 rollbackvector.clear();
267 rmwrollbackvector.clear();
270 /** @returns whether a CycleGraph contains cycles. */
271 bool CycleGraph::checkForCycles() const
277 * @brief Constructor for a CycleNode
278 * @param act The ModelAction for this node
280 CycleNode::CycleNode(const ModelAction *act) :
287 * @param i The index of the edge to return
288 * @returns The a CycleNode edge indexed by i
290 CycleNode * CycleNode::getEdge(unsigned int i) const
295 /** @returns The number of edges leaving this CycleNode */
296 unsigned int CycleNode::getNumEdges() const
301 CycleNode * CycleNode::getBackEdge(unsigned int i) const
303 return back_edges[i];
306 unsigned int CycleNode::getNumBackEdges() const
308 return back_edges.size();
312 * Adds an edge from this CycleNode to another CycleNode.
313 * @param node The node to which we add a directed edge
314 * @return True if this edge is a new edge; false otherwise
316 bool CycleNode::addEdge(CycleNode *node)
318 for (unsigned int i = 0; i < edges.size(); i++)
319 if (edges[i] == node)
321 edges.push_back(node);
322 node->back_edges.push_back(this);
326 /** @returns the RMW CycleNode that reads from the current CycleNode */
327 CycleNode * CycleNode::getRMW() const
333 * Set a RMW action node that reads from the current CycleNode.
334 * @param node The RMW that reads from the current node
335 * @return True, if this node already was read by another RMW; false otherwise
336 * @see CycleGraph::addRMWEdge
338 bool CycleNode::setRMW(CycleNode *node)