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() {
22 * @brief Returns the CycleNode corresponding to a given ModelAction
23 * @param action The ModelAction to find a node for
24 * @return The CycleNode paired with this action
26 CycleNode * CycleGraph::getNode(const ModelAction *action) {
27 CycleNode *node=actionToNode.get(action);
29 node=new CycleNode(action);
30 actionToNode.put(action, node);
31 #if SUPPORT_MOD_ORDER_DUMP
32 nodeList.push_back(node);
39 * Adds an edge between two ModelActions. The ModelAction @a to is ordered
40 * after the ModelAction @a from.
41 * @param to The edge points to this ModelAction
42 * @param from The edge comes from this ModelAction
44 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
48 CycleNode *fromnode=getNode(from);
49 CycleNode *tonode=getNode(to);
52 // Reflexive edges are cycles
53 hasCycles = (from == to);
57 hasCycles=checkReachable(tonode, fromnode);
60 if (fromnode->addEdge(tonode))
61 rollbackvector.push_back(fromnode);
64 CycleNode * rmwnode=fromnode->getRMW();
66 //If the fromnode has a rmwnode that is not the tonode, we
67 //should add an edge between its rmwnode and the tonode
69 //If tonode is also a rmw, don't do this check as the execution is
70 //doomed and we'll catch the problem elsewhere, but we want to allow
71 //for the possibility of sending to's write value to rmwnode
73 if (rmwnode!=NULL&&!to->is_rmw()) {
76 hasCycles=checkReachable(tonode, rmwnode);
79 if (rmwnode->addEdge(tonode))
80 rollbackvector.push_back(rmwnode);
84 /** Handles special case of a RMW action. The ModelAction rmw reads
85 * from the ModelAction from. The key differences are: (1) no write
86 * can occur in between the rmw and the from action. Only one RMW
87 * action can read from a given write.
89 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
93 CycleNode *fromnode=getNode(from);
94 CycleNode *rmwnode=getNode(rmw);
96 /* Two RMW actions cannot read from the same write. */
97 if (fromnode->setRMW(rmwnode)) {
100 rmwrollbackvector.push_back(fromnode);
103 /* Transfer all outgoing edges from the from node to the rmw node */
104 /* This process should not add a cycle because either:
105 * (1) The rmw should not have any incoming edges yet if it is the
107 * (2) the fromnode is the new node and therefore it should not
108 * have any outgoing edges.
110 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
111 CycleNode *tonode = fromnode->getEdge(i);
112 if (tonode!=rmwnode) {
113 if (rmwnode->addEdge(tonode))
114 rollbackvector.push_back(rmwnode);
120 // Reflexive edges are cycles
121 hasCycles = (from == rmw);
124 // With promises we could be setting up a cycle here if we aren't
125 // careful...avoid it..
126 hasCycles=checkReachable(rmwnode, fromnode);
128 if (fromnode->addEdge(rmwnode))
129 rollbackvector.push_back(fromnode);
132 #if SUPPORT_MOD_ORDER_DUMP
133 void CycleGraph::dumpNodes(FILE *file) {
134 for (unsigned int i=0;i<nodeList.size();i++) {
135 CycleNode *cn=nodeList[i];
136 const ModelAction *action=cn->getAction();
137 fprintf(file, "N%u [label=\"%u, T%u\"];\n",action->get_seq_number(),action->get_seq_number(), action->get_tid());
138 if (cn->getRMW()!=NULL) {
139 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
141 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
142 CycleNode *dst = cn->getEdge(j);
143 const ModelAction *dstaction=dst->getAction();
144 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
149 void CycleGraph::dumpGraphToFile(const char *filename) {
151 sprintf(buffer, "%s.dot",filename);
152 FILE *file=fopen(buffer, "w");
153 fprintf(file, "digraph %s {\n",filename);
161 * Checks whether one ModelAction can reach another.
162 * @param from The ModelAction from which to begin exploration
163 * @param to The ModelAction to reach
164 * @return True, @a from can reach @a to; otherwise, false
166 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) {
167 CycleNode *fromnode = actionToNode.get(from);
168 CycleNode *tonode = actionToNode.get(to);
170 if (!fromnode || !tonode)
173 return checkReachable(fromnode, tonode);
177 * Checks whether one CycleNode can reach another.
178 * @param from The CycleNode from which to begin exploration
179 * @param to The CycleNode to reach
180 * @return True, @a from can reach @a to; otherwise, false
182 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
183 std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
186 queue.push_back(from);
187 discovered->put(from, from);
188 while(!queue.empty()) {
189 CycleNode * node=queue.back();
194 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
195 CycleNode *next = node->getEdge(i);
196 if (!discovered->contains(next)) {
197 discovered->put(next,next);
198 queue.push_back(next);
205 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) {
206 std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
208 CycleNode *from = actionToNode.get(fromact);
211 queue.push_back(from);
212 discovered->put(from, from);
213 while(!queue.empty()) {
214 CycleNode * node=queue.back();
217 if (promise->increment_threads(node->getAction()->get_tid())) {
221 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
222 CycleNode *next = node->getEdge(i);
223 if (!discovered->contains(next)) {
224 discovered->put(next,next);
225 queue.push_back(next);
232 void CycleGraph::startChanges() {
233 ASSERT(rollbackvector.size()==0);
234 ASSERT(rmwrollbackvector.size()==0);
235 ASSERT(oldCycles==hasCycles);
236 ASSERT(oldRMWViolation==hasRMWViolation);
239 /** Commit changes to the cyclegraph. */
240 void CycleGraph::commitChanges() {
241 rollbackvector.resize(0);
242 rmwrollbackvector.resize(0);
244 oldRMWViolation=hasRMWViolation;
247 /** Rollback changes to the previous commit. */
248 void CycleGraph::rollbackChanges() {
249 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
250 rollbackvector[i]->popEdge();
253 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
254 rmwrollbackvector[i]->clearRMW();
257 hasCycles = oldCycles;
258 hasRMWViolation = oldRMWViolation;
259 rollbackvector.resize(0);
260 rmwrollbackvector.resize(0);
263 /** @returns whether a CycleGraph contains cycles. */
264 bool CycleGraph::checkForCycles() {
268 bool CycleGraph::checkForRMWViolation() {
269 return hasRMWViolation;
273 * Constructor for a CycleNode.
274 * @param modelaction The ModelAction for this node
276 CycleNode::CycleNode(const ModelAction *modelaction) :
283 * @param i The index of the edge to return
284 * @returns The a CycleNode edge indexed by i
286 CycleNode * CycleNode::getEdge(unsigned int i) const
291 /** @returns The number of edges leaving this CycleNode */
292 unsigned int CycleNode::getNumEdges() const
298 * Adds an edge from this CycleNode to another CycleNode.
299 * @param node The node to which we add a directed edge
301 bool CycleNode::addEdge(CycleNode *node) {
302 for(unsigned int i=0;i<edges.size();i++)
305 edges.push_back(node);
309 /** @returns the RMW CycleNode that reads from the current CycleNode */
310 CycleNode * CycleNode::getRMW() {
315 * Set a RMW action node that reads from the current CycleNode.
316 * @param node The RMW that reads from the current node
317 * @return True, if this node already was read by another RMW; false otherwise
318 * @see CycleGraph::addRMWEdge
320 bool CycleNode::setRMW(CycleNode *node) {