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 * @brief Returns the CycleNode corresponding to a given ModelAction
25 * @param action The ModelAction to find a node for
26 * @return The CycleNode paired with this action
28 CycleNode * CycleGraph::getNode(const ModelAction *action)
30 CycleNode *node = actionToNode.get(action);
32 node = new CycleNode(action);
33 actionToNode.put(action, node);
34 #if SUPPORT_MOD_ORDER_DUMP
35 nodeList.push_back(node);
42 * Adds an edge between two ModelActions. The ModelAction @a to is ordered
43 * after the ModelAction @a from.
44 * @param to The edge points to this ModelAction
45 * @param from The edge comes from this ModelAction
47 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to)
52 CycleNode *fromnode = getNode(from);
53 CycleNode *tonode = getNode(to);
56 // Reflexive edges are cycles
57 hasCycles = (from == to);
61 hasCycles = checkReachable(tonode, fromnode);
64 if (fromnode->addEdge(tonode))
65 rollbackvector.push_back(fromnode);
68 CycleNode *rmwnode = fromnode->getRMW();
71 * If the fromnode has a rmwnode that is not the tonode, we should add
72 * an edge between its rmwnode and the tonode
74 * If tonode is also a rmw, don't do this check as the execution is
75 * doomed and we'll catch the problem elsewhere, but we want to allow
76 * for the possibility of sending to's write value to rmwnode
78 if (rmwnode != NULL && !to->is_rmw()) {
81 hasCycles = checkReachable(tonode, rmwnode);
84 if (rmwnode->addEdge(tonode))
85 rollbackvector.push_back(rmwnode);
89 /** Handles special case of a RMW action. The ModelAction rmw reads
90 * from the ModelAction from. The key differences are: (1) no write
91 * can occur in between the rmw and the from action. Only one RMW
92 * action can read from a given write.
94 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
99 CycleNode *fromnode = getNode(from);
100 CycleNode *rmwnode = getNode(rmw);
102 /* Two RMW actions cannot read from the same write. */
103 if (fromnode->setRMW(rmwnode)) {
104 hasRMWViolation = true;
106 rmwrollbackvector.push_back(fromnode);
109 /* Transfer all outgoing edges from the from node to the rmw node */
110 /* This process should not add a cycle because either:
111 * (1) The rmw should not have any incoming edges yet if it is the
113 * (2) the fromnode is the new node and therefore it should not
114 * have any outgoing edges.
116 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
117 CycleNode *tonode = fromnode->getEdge(i);
118 if (tonode != rmwnode) {
119 if (rmwnode->addEdge(tonode))
120 rollbackvector.push_back(rmwnode);
126 // Reflexive edges are cycles
127 hasCycles = (from == rmw);
130 // With promises we could be setting up a cycle here if we aren't
131 // careful...avoid it..
132 hasCycles = checkReachable(rmwnode, fromnode);
134 if (fromnode->addEdge(rmwnode))
135 rollbackvector.push_back(fromnode);
138 #if SUPPORT_MOD_ORDER_DUMP
139 void CycleGraph::dumpNodes(FILE *file) const
141 for (unsigned int i = 0; i < nodeList.size(); i++) {
142 CycleNode *cn = nodeList[i];
143 const ModelAction *action = cn->getAction();
144 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
145 if (cn->getRMW() != NULL) {
146 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
148 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
149 CycleNode *dst = cn->getEdge(j);
150 const ModelAction *dstaction = dst->getAction();
151 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
156 void CycleGraph::dumpGraphToFile(const char *filename) const
159 sprintf(buffer, "%s.dot", filename);
160 FILE *file = fopen(buffer, "w");
161 fprintf(file, "digraph %s {\n", filename);
163 fprintf(file, "}\n");
169 * Checks whether one ModelAction can reach another.
170 * @param from The ModelAction from which to begin exploration
171 * @param to The ModelAction to reach
172 * @return True, @a from can reach @a to; otherwise, false
174 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) const
176 CycleNode *fromnode = actionToNode.get(from);
177 CycleNode *tonode = actionToNode.get(to);
179 if (!fromnode || !tonode)
182 return checkReachable(fromnode, tonode);
186 * Checks whether one CycleNode can reach another.
187 * @param from The CycleNode from which to begin exploration
188 * @param to The CycleNode to reach
189 * @return True, @a from can reach @a to; otherwise, false
191 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) const
193 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
196 queue.push_back(from);
197 discovered->put(from, from);
198 while (!queue.empty()) {
199 CycleNode *node = queue.back();
204 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
205 CycleNode *next = node->getEdge(i);
206 if (!discovered->contains(next)) {
207 discovered->put(next, next);
208 queue.push_back(next);
215 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
217 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
219 CycleNode *from = actionToNode.get(fromact);
221 queue.push_back(from);
222 discovered->put(from, from);
223 while (!queue.empty()) {
224 CycleNode *node = queue.back();
227 if (promise->increment_threads(node->getAction()->get_tid())) {
231 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
232 CycleNode *next = node->getEdge(i);
233 if (!discovered->contains(next)) {
234 discovered->put(next, next);
235 queue.push_back(next);
242 void CycleGraph::startChanges()
244 ASSERT(rollbackvector.size() == 0);
245 ASSERT(rmwrollbackvector.size() == 0);
246 ASSERT(oldCycles == hasCycles);
247 ASSERT(oldRMWViolation == hasRMWViolation);
250 /** Commit changes to the cyclegraph. */
251 void CycleGraph::commitChanges()
253 rollbackvector.resize(0);
254 rmwrollbackvector.resize(0);
255 oldCycles = hasCycles;
256 oldRMWViolation = hasRMWViolation;
259 /** Rollback changes to the previous commit. */
260 void CycleGraph::rollbackChanges()
262 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
263 rollbackvector[i]->popEdge();
266 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
267 rmwrollbackvector[i]->clearRMW();
270 hasCycles = oldCycles;
271 hasRMWViolation = oldRMWViolation;
272 rollbackvector.resize(0);
273 rmwrollbackvector.resize(0);
276 /** @returns whether a CycleGraph contains cycles. */
277 bool CycleGraph::checkForCycles() const
282 bool CycleGraph::checkForRMWViolation() const
284 return hasRMWViolation;
288 * @brief Constructor for a CycleNode
289 * @param act The ModelAction for this node
291 CycleNode::CycleNode(const ModelAction *act) :
298 * @param i The index of the edge to return
299 * @returns The a CycleNode edge indexed by i
301 CycleNode * CycleNode::getEdge(unsigned int i) const
306 /** @returns The number of edges leaving this CycleNode */
307 unsigned int CycleNode::getNumEdges() const
313 * Adds an edge from this CycleNode to another CycleNode.
314 * @param node The node to which we add a directed edge
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);
325 /** @returns the RMW CycleNode that reads from the current CycleNode */
326 CycleNode * CycleNode::getRMW() const
332 * Set a RMW action node that reads from the current CycleNode.
333 * @param node The RMW that reads from the current node
334 * @return True, if this node already was read by another RMW; false otherwise
335 * @see CycleGraph::addRMWEdge
337 bool CycleNode::setRMW(CycleNode *node)