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)),
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 hasCycles = checkReachable(tonode, fromnode);
68 if (fromnode->addEdge(tonode))
69 rollbackvector.push_back(fromnode);
72 CycleNode *rmwnode = fromnode->getRMW();
75 * If the fromnode has a rmwnode that is not the tonode, we should add
76 * an edge between its rmwnode and the tonode
78 * If tonode is also a rmw, don't do this check as the execution is
79 * doomed and we'll catch the problem elsewhere, but we want to allow
80 * for the possibility of sending to's write value to rmwnode
82 if (rmwnode != NULL && !to->is_rmw()) {
84 hasCycles = checkReachable(tonode, rmwnode);
86 if (rmwnode->addEdge(tonode))
87 rollbackvector.push_back(rmwnode);
91 /** Handles special case of a RMW action. The ModelAction rmw reads
92 * from the ModelAction from. The key differences are: (1) no write
93 * can occur in between the rmw and the from action. Only one RMW
94 * action can read from a given write.
96 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
101 CycleNode *fromnode = getNode(from);
102 CycleNode *rmwnode = getNode(rmw);
104 /* Two RMW actions cannot read from the same write. */
105 if (fromnode->setRMW(rmwnode)) {
106 hasRMWViolation = true;
108 rmwrollbackvector.push_back(fromnode);
111 /* Transfer all outgoing edges from the from node to the rmw node */
112 /* This process should not add a cycle because either:
113 * (1) The rmw should not have any incoming edges yet if it is the
115 * (2) the fromnode is the new node and therefore it should not
116 * have any outgoing edges.
118 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
119 CycleNode *tonode = fromnode->getEdge(i);
120 if (tonode != rmwnode) {
121 if (rmwnode->addEdge(tonode))
122 rollbackvector.push_back(rmwnode);
127 hasCycles = checkReachable(rmwnode, fromnode);
129 if (fromnode->addEdge(rmwnode))
130 rollbackvector.push_back(fromnode);
133 #if SUPPORT_MOD_ORDER_DUMP
134 void CycleGraph::dumpNodes(FILE *file) const
136 for (unsigned int i = 0; i < nodeList.size(); i++) {
137 CycleNode *cn = nodeList[i];
138 const ModelAction *action = cn->getAction();
139 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
140 if (cn->getRMW() != NULL) {
141 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
143 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
144 CycleNode *dst = cn->getEdge(j);
145 const ModelAction *dstaction = dst->getAction();
146 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
151 void CycleGraph::dumpGraphToFile(const char *filename) const
154 sprintf(buffer, "%s.dot", filename);
155 FILE *file = fopen(buffer, "w");
156 fprintf(file, "digraph %s {\n", filename);
158 fprintf(file, "}\n");
164 * Checks whether one ModelAction can reach another.
165 * @param from The ModelAction from which to begin exploration
166 * @param to The ModelAction to reach
167 * @return True, @a from can reach @a to; otherwise, false
169 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) const
171 CycleNode *fromnode = actionToNode.get(from);
172 CycleNode *tonode = actionToNode.get(to);
174 if (!fromnode || !tonode)
177 return checkReachable(fromnode, tonode);
181 * Checks whether one CycleNode can reach another.
182 * @param from The CycleNode from which to begin exploration
183 * @param to The CycleNode to reach
184 * @return True, @a from can reach @a to; otherwise, false
186 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
188 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
191 queue.push_back(from);
192 discovered->put(from, from);
193 while (!queue.empty()) {
194 const CycleNode *node = queue.back();
199 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
200 CycleNode *next = node->getEdge(i);
201 if (!discovered->contains(next)) {
202 discovered->put(next, next);
203 queue.push_back(next);
210 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
212 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
214 CycleNode *from = actionToNode.get(fromact);
216 queue.push_back(from);
217 discovered->put(from, from);
218 while (!queue.empty()) {
219 CycleNode *node = queue.back();
222 if (promise->increment_threads(node->getAction()->get_tid())) {
226 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
227 CycleNode *next = node->getEdge(i);
228 if (!discovered->contains(next)) {
229 discovered->put(next, next);
230 queue.push_back(next);
237 void CycleGraph::startChanges()
239 ASSERT(rollbackvector.size() == 0);
240 ASSERT(rmwrollbackvector.size() == 0);
241 ASSERT(oldCycles == hasCycles);
242 ASSERT(oldRMWViolation == hasRMWViolation);
245 /** Commit changes to the cyclegraph. */
246 void CycleGraph::commitChanges()
248 rollbackvector.resize(0);
249 rmwrollbackvector.resize(0);
250 oldCycles = hasCycles;
251 oldRMWViolation = hasRMWViolation;
254 /** Rollback changes to the previous commit. */
255 void CycleGraph::rollbackChanges()
257 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
258 rollbackvector[i]->popEdge();
261 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
262 rmwrollbackvector[i]->clearRMW();
265 hasCycles = oldCycles;
266 hasRMWViolation = oldRMWViolation;
267 rollbackvector.resize(0);
268 rmwrollbackvector.resize(0);
271 /** @returns whether a CycleGraph contains cycles. */
272 bool CycleGraph::checkForCycles() const
277 bool CycleGraph::checkForRMWViolation() const
279 return hasRMWViolation;
283 * @brief Constructor for a CycleNode
284 * @param act The ModelAction for this node
286 CycleNode::CycleNode(const ModelAction *act) :
293 * @param i The index of the edge to return
294 * @returns The a CycleNode edge indexed by i
296 CycleNode * CycleNode::getEdge(unsigned int i) const
301 /** @returns The number of edges leaving this CycleNode */
302 unsigned int CycleNode::getNumEdges() const
307 CycleNode * CycleNode::getBackEdge(unsigned int i) const
309 return back_edges[i];
312 unsigned int CycleNode::getNumBackEdges() const
314 return back_edges.size();
318 * Adds an edge from this CycleNode to another CycleNode.
319 * @param node The node to which we add a directed edge
320 * @return True if this edge is a new edge; false otherwise
322 bool CycleNode::addEdge(CycleNode *node)
324 for (unsigned int i = 0; i < edges.size(); i++)
325 if (edges[i] == node)
327 edges.push_back(node);
328 node->back_edges.push_back(this);
332 /** @returns the RMW CycleNode that reads from the current CycleNode */
333 CycleNode * CycleNode::getRMW() const
339 * Set a RMW action node that reads from the current CycleNode.
340 * @param node The RMW that reads from the current node
341 * @return True, if this node already was read by another RMW; false otherwise
342 * @see CycleGraph::addRMWEdge
344 bool CycleNode::setRMW(CycleNode *node)