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 std::vector<CycleNode *> * edges=fromnode->getEdges();
111 for(unsigned int i=0;i<edges->size();i++) {
112 CycleNode * tonode=(*edges)[i];
113 if (tonode!=rmwnode) {
114 if (rmwnode->addEdge(tonode))
115 rollbackvector.push_back(rmwnode);
121 // Reflexive edges are cycles
122 hasCycles = (from == rmw);
125 // With promises we could be setting up a cycle here if we aren't
126 // careful...avoid it..
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) {
135 for (unsigned int i=0;i<nodeList.size();i++) {
136 CycleNode *cn=nodeList[i];
137 std::vector<CycleNode *> * edges=cn->getEdges();
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<edges->size();j++) {
144 CycleNode *dst=(*edges)[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) {
153 sprintf(buffer, "%s.dot",filename);
154 FILE *file=fopen(buffer, "w");
155 fprintf(file, "digraph %s {\n",filename);
163 * Checks whether one ModelAction can reach another.
164 * @param from The ModelAction from which to begin exploration
165 * @param to The ModelAction to reach
166 * @return True, @a from can reach @a to; otherwise, false
168 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) {
169 CycleNode *fromnode = actionToNode.get(from);
170 CycleNode *tonode = actionToNode.get(to);
172 if (!fromnode || !tonode)
175 return checkReachable(fromnode, tonode);
179 * Checks whether one CycleNode can reach another.
180 * @param from The CycleNode from which to begin exploration
181 * @param to The CycleNode to reach
182 * @return True, @a from can reach @a to; otherwise, false
184 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
185 std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
188 queue.push_back(from);
189 discovered->put(from, from);
190 while(!queue.empty()) {
191 CycleNode * node=queue.back();
196 for(unsigned int i=0;i<node->getEdges()->size();i++) {
197 CycleNode *next=(*node->getEdges())[i];
198 if (!discovered->contains(next)) {
199 discovered->put(next,next);
200 queue.push_back(next);
207 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) {
208 std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
210 CycleNode *from = actionToNode.get(fromact);
213 queue.push_back(from);
214 discovered->put(from, from);
215 while(!queue.empty()) {
216 CycleNode * node=queue.back();
219 if (promise->increment_threads(node->getAction()->get_tid())) {
223 for(unsigned int i=0;i<node->getEdges()->size();i++) {
224 CycleNode *next=(*node->getEdges())[i];
225 if (!discovered->contains(next)) {
226 discovered->put(next,next);
227 queue.push_back(next);
234 void CycleGraph::startChanges() {
235 ASSERT(rollbackvector.size()==0);
236 ASSERT(rmwrollbackvector.size()==0);
237 ASSERT(oldCycles==hasCycles);
238 ASSERT(oldRMWViolation==hasRMWViolation);
241 /** Commit changes to the cyclegraph. */
242 void CycleGraph::commitChanges() {
243 rollbackvector.resize(0);
244 rmwrollbackvector.resize(0);
246 oldRMWViolation=hasRMWViolation;
249 /** Rollback changes to the previous commit. */
250 void CycleGraph::rollbackChanges() {
251 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
252 rollbackvector[i]->popEdge();
255 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
256 rmwrollbackvector[i]->clearRMW();
259 hasCycles = oldCycles;
260 hasRMWViolation = oldRMWViolation;
261 rollbackvector.resize(0);
262 rmwrollbackvector.resize(0);
265 /** @returns whether a CycleGraph contains cycles. */
266 bool CycleGraph::checkForCycles() {
270 bool CycleGraph::checkForRMWViolation() {
271 return hasRMWViolation;
275 * Constructor for a CycleNode.
276 * @param modelaction The ModelAction for this node
278 CycleNode::CycleNode(const ModelAction *modelaction) :
284 /** @returns a vector of the edges from a CycleNode. */
285 std::vector<CycleNode *> * CycleNode::getEdges() {
290 * Adds an edge from this CycleNode to another CycleNode.
291 * @param node The node to which we add a directed edge
293 bool CycleNode::addEdge(CycleNode *node) {
294 for(unsigned int i=0;i<edges.size();i++)
297 edges.push_back(node);
301 /** @returns the RMW CycleNode that reads from the current CycleNode */
302 CycleNode * CycleNode::getRMW() {
307 * Set a RMW action node that reads from the current CycleNode.
308 * @param node The RMW that reads from the current node
309 * @return True, if this node already was read by another RMW; false otherwise
310 * @see CycleGraph::addRMWEdge
312 bool CycleNode::setRMW(CycleNode *node) {