1 #include "cyclegraph.h"
7 /** Initializes a CycleGraph object. */
8 CycleGraph::CycleGraph() :
11 hasRMWViolation(false),
12 oldRMWViolation(false)
16 /** CycleGraph destructor */
17 CycleGraph::~CycleGraph() {
21 * @brief Returns the CycleNode corresponding to a given ModelAction
22 * @param action The ModelAction to find a node for
23 * @return The CycleNode paired with this action
25 CycleNode * CycleGraph::getNode(const ModelAction *action) {
26 CycleNode *node=actionToNode.get(action);
28 node=new CycleNode(action);
29 actionToNode.put(action, node);
30 #if SUPPORT_MOD_ORDER_DUMP
31 nodeList.push_back(node);
38 * Adds an edge between two ModelActions. The ModelAction @a to is ordered
39 * after the ModelAction @a from.
40 * @param to The edge points to this ModelAction
41 * @param from The edge comes from this ModelAction
43 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
48 CycleNode *fromnode=getNode(from);
49 CycleNode *tonode=getNode(to);
53 hasCycles=checkReachable(tonode, fromnode);
56 if (fromnode->addEdge(tonode))
57 rollbackvector.push_back(fromnode);
60 CycleNode * rmwnode=fromnode->getRMW();
62 //If the fromnode has a rmwnode that is not the tonode, we
63 //should add an edge between its rmwnode and the tonode
65 //If tonode is also a rmw, don't do this check as the execution is
66 //doomed and we'll catch the problem elsewhere, but we want to allow
67 //for the possibility of sending to's write value to rmwnode
69 if (rmwnode!=NULL&&!to->is_rmw()) {
72 hasCycles=checkReachable(tonode, rmwnode);
75 if (rmwnode->addEdge(tonode))
76 rollbackvector.push_back(rmwnode);
80 /** Handles special case of a RMW action. The ModelAction rmw reads
81 * from the ModelAction from. The key differences are: (1) no write
82 * can occur in between the rmw and the from action. Only one RMW
83 * action can read from a given write.
85 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw) {
90 CycleNode *fromnode=getNode(from);
91 CycleNode *rmwnode=getNode(rmw);
93 /* Two RMW actions cannot read from the same write. */
94 if (fromnode->setRMW(rmwnode)) {
97 rmwrollbackvector.push_back(fromnode);
100 /* Transfer all outgoing edges from the from node to the rmw node */
101 /* This process should not add a cycle because either:
102 * (1) The rmw should not have any incoming edges yet if it is the
104 * (2) the fromnode is the new node and therefore it should not
105 * have any outgoing edges.
107 std::vector<CycleNode *> * edges=fromnode->getEdges();
108 for(unsigned int i=0;i<edges->size();i++) {
109 CycleNode * tonode=(*edges)[i];
110 if (tonode!=rmwnode) {
111 if (rmwnode->addEdge(tonode))
112 rollbackvector.push_back(rmwnode);
118 // With promises we could be setting up a cycle here if we aren't
119 // careful...avoid it..
120 hasCycles=checkReachable(rmwnode, fromnode);
122 if(fromnode->addEdge(rmwnode))
123 rollbackvector.push_back(fromnode);
126 #if SUPPORT_MOD_ORDER_DUMP
127 void CycleGraph::dumpNodes(FILE *file) {
128 for(unsigned int i=0;i<nodeList.size();i++) {
129 CycleNode *cn=nodeList[i];
130 std::vector<CycleNode *> * edges=cn->getEdges();
131 const ModelAction *action=cn->getAction();
132 fprintf(file, "N%u [label=\"%u, T%u\"];\n",action->get_seq_number(),action->get_seq_number(), action->get_tid());
133 if (cn->getRMW()!=NULL) {
134 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
136 for(unsigned int j=0;j<edges->size();j++) {
137 CycleNode *dst=(*edges)[j];
138 const ModelAction *dstaction=dst->getAction();
139 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
144 void CycleGraph::dumpGraphToFile(const char *filename) {
146 sprintf(buffer, "%s.dot",filename);
147 FILE *file=fopen(buffer, "w");
148 fprintf(file, "digraph %s {\n",filename);
156 * Checks whether one ModelAction can reach another.
157 * @param from The ModelAction from which to begin exploration
158 * @param to The ModelAction to reach
159 * @return True, @a from can reach @a to; otherwise, false
161 bool CycleGraph::checkReachable(const ModelAction *from, const ModelAction *to) {
162 CycleNode *fromnode = actionToNode.get(from);
163 CycleNode *tonode = actionToNode.get(to);
165 if (!fromnode || !tonode)
168 return checkReachable(fromnode, tonode);
172 * Checks whether one CycleNode can reach another.
173 * @param from The CycleNode from which to begin exploration
174 * @param to The CycleNode to reach
175 * @return True, @a from can reach @a to; otherwise, false
177 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
178 std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
179 HashTable<CycleNode *, CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> discovered(64);
181 queue.push_back(from);
182 discovered.put(from, from);
183 while(!queue.empty()) {
184 CycleNode * node=queue.back();
189 for(unsigned int i=0;i<node->getEdges()->size();i++) {
190 CycleNode *next=(*node->getEdges())[i];
191 if (!discovered.contains(next)) {
192 discovered.put(next,next);
193 queue.push_back(next);
200 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) {
201 std::vector<CycleNode *, ModelAlloc<CycleNode *> > queue;
202 HashTable<CycleNode *, CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free> discovered(64);
203 CycleNode *from = actionToNode.get(fromact);
206 queue.push_back(from);
207 discovered.put(from, from);
208 while(!queue.empty()) {
209 CycleNode * node=queue.back();
212 if (promise->increment_threads(node->getAction()->get_tid())) {
216 for(unsigned int i=0;i<node->getEdges()->size();i++) {
217 CycleNode *next=(*node->getEdges())[i];
218 if (!discovered.contains(next)) {
219 discovered.put(next,next);
220 queue.push_back(next);
227 void CycleGraph::startChanges() {
228 ASSERT(rollbackvector.size()==0);
229 ASSERT(rmwrollbackvector.size()==0);
230 ASSERT(oldCycles==hasCycles);
231 ASSERT(oldRMWViolation==hasRMWViolation);
234 /** Commit changes to the cyclegraph. */
235 void CycleGraph::commitChanges() {
236 rollbackvector.resize(0);
237 rmwrollbackvector.resize(0);
239 oldRMWViolation=hasRMWViolation;
242 /** Rollback changes to the previous commit. */
243 void CycleGraph::rollbackChanges() {
244 for (unsigned int i = 0; i < rollbackvector.size(); i++) {
245 rollbackvector[i]->popEdge();
248 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++) {
249 rmwrollbackvector[i]->clearRMW();
252 hasCycles = oldCycles;
253 hasRMWViolation = oldRMWViolation;
254 rollbackvector.resize(0);
255 rmwrollbackvector.resize(0);
258 /** @returns whether a CycleGraph contains cycles. */
259 bool CycleGraph::checkForCycles() {
263 bool CycleGraph::checkForRMWViolation() {
264 return hasRMWViolation;
268 * Constructor for a CycleNode.
269 * @param modelaction The ModelAction for this node
271 CycleNode::CycleNode(const ModelAction *modelaction) :
277 /** @returns a vector of the edges from a CycleNode. */
278 std::vector<CycleNode *> * CycleNode::getEdges() {
283 * Adds an edge from this CycleNode to another CycleNode.
284 * @param node The node to which we add a directed edge
286 bool CycleNode::addEdge(CycleNode *node) {
287 for(unsigned int i=0;i<edges.size();i++)
290 edges.push_back(node);
294 /** @returns the RMW CycleNode that reads from the current CycleNode */
295 CycleNode * CycleNode::getRMW() {
300 * Set a RMW action node that reads from the current CycleNode.
301 * @param node The RMW that reads from the current node
302 * @return True, if this node already was read by another RMW; false otherwise
303 * @see CycleGraph::addRMWEdge
305 bool CycleNode::setRMW(CycleNode *node) {