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)),
15 /** CycleGraph destructor */
16 CycleGraph::~CycleGraph()
22 * Add a CycleNode to the graph, corresponding to a store ModelAction
23 * @param act The write action that should be added
24 * @param node The CycleNode that corresponds to the store
26 void CycleGraph::putNode(const ModelAction *act, CycleNode *node)
28 actionToNode.put(act, node);
29 #if SUPPORT_MOD_ORDER_DUMP
30 nodeList.push_back(node);
34 /** @return The corresponding CycleNode, if exists; otherwise NULL */
35 CycleNode * CycleGraph::getNode_noCreate(const ModelAction *act) const
37 return actionToNode.get(act);
40 /** @return The corresponding CycleNode, if exists; otherwise NULL */
41 CycleNode * CycleGraph::getNode_noCreate(const Promise *promise) const
43 return readerToPromiseNode.get(promise->get_action());
47 * @brief Returns the CycleNode corresponding to a given ModelAction
49 * Gets (or creates, if none exist) a CycleNode corresponding to a ModelAction
51 * @param action The ModelAction to find a node for
52 * @return The CycleNode paired with this action
54 CycleNode * CycleGraph::getNode(const ModelAction *action)
56 CycleNode *node = getNode_noCreate(action);
58 node = new CycleNode(action);
59 putNode(action, node);
65 * @brief Returns a CycleNode corresponding to a promise
67 * Gets (or creates, if none exist) a CycleNode corresponding to a promised
70 * @param promise The Promise generated by a reader
71 * @return The CycleNode corresponding to the Promise
73 CycleNode * CycleGraph::getNode(const Promise *promise)
75 const ModelAction *reader = promise->get_action();
76 CycleNode *node = getNode_noCreate(promise);
78 node = new CycleNode(promise);
79 readerToPromiseNode.put(reader, node);
85 * @return false if the resolution results in a cycle; true otherwise
87 bool CycleGraph::resolvePromise(ModelAction *reader, ModelAction *writer,
88 promise_list_t *mustResolve)
90 CycleNode *promise_node = readerToPromiseNode.get(reader);
91 CycleNode *w_node = actionToNode.get(writer);
95 return mergeNodes(w_node, promise_node, mustResolve);
96 /* No existing write-node; just convert the promise-node */
97 promise_node->resolvePromise(writer);
98 readerToPromiseNode.put(reader, NULL); /* erase promise_node */
99 putNode(writer, promise_node);
104 * @brief Merge two CycleNodes that represent the same write
106 * Note that this operation cannot be rolled back.
108 * @param w_node The write ModelAction node with which to merge
109 * @param p_node The Promise node to merge. Will be destroyed after this
111 * @param mustMerge Return (pass-by-reference) any additional Promises that
112 * must also be merged with w_node
114 * @return false if the merge results in a cycle; true otherwise
116 bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node,
117 promise_list_t *mustMerge)
119 ASSERT(!w_node->is_promise());
120 ASSERT(p_node->is_promise());
121 const Promise *promise = p_node->getPromise();
122 if (!promise->is_compatible(w_node->getAction())) {
127 /* Transfer back edges to w_node */
128 while (p_node->getNumBackEdges() > 0) {
129 CycleNode *back = p_node->removeBackEdge();
130 if (back != w_node) {
131 if (back->is_promise()) {
132 if (checkReachable(w_node, back)) {
133 /* Edge would create cycle; merge instead */
134 mustMerge->push_back(back->getPromise());
135 if (!mergeNodes(w_node, back, mustMerge))
138 back->addEdge(w_node);
140 addNodeEdge(back, w_node);
144 /* Transfer forward edges to w_node */
145 while (p_node->getNumEdges() > 0) {
146 CycleNode *forward = p_node->removeEdge();
147 if (forward != w_node) {
148 if (forward->is_promise()) {
149 if (checkReachable(forward, w_node)) {
150 mustMerge->push_back(forward->getPromise());
151 if (!mergeNodes(w_node, forward, mustMerge))
154 w_node->addEdge(forward);
156 addNodeEdge(w_node, forward);
161 readerToPromiseNode.put(promise->get_action(), NULL);
168 * Adds an edge between two CycleNodes.
169 * @param fromnode The edge comes from this CycleNode
170 * @param tonode The edge points to this CycleNode
172 void CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
175 hasCycles = checkReachable(tonode, fromnode);
177 if (fromnode->addEdge(tonode))
178 rollbackvector.push_back(fromnode);
181 * If the fromnode has a rmwnode that is not the tonode, we should add
182 * an edge between its rmwnode and the tonode
184 CycleNode *rmwnode = fromnode->getRMW();
185 if (rmwnode && rmwnode != tonode) {
187 hasCycles = checkReachable(tonode, rmwnode);
189 if (rmwnode->addEdge(tonode))
190 rollbackvector.push_back(rmwnode);
195 * @brief Add an edge between a write and the RMW which reads from it
197 * Handles special case of a RMW action, where the ModelAction rmw reads from
198 * the ModelAction from. The key differences are:
199 * (1) no write can occur in between the rmw and the from action.
200 * (2) Only one RMW action can read from a given write.
202 * @param from The edge comes from this ModelAction
203 * @param rmw The edge points to this ModelAction; this action must read from
206 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
211 CycleNode *fromnode = getNode(from);
212 CycleNode *rmwnode = getNode(rmw);
214 /* Two RMW actions cannot read from the same write. */
215 if (fromnode->setRMW(rmwnode))
218 rmwrollbackvector.push_back(fromnode);
220 /* Transfer all outgoing edges from the from node to the rmw node */
221 /* This process should not add a cycle because either:
222 * (1) The rmw should not have any incoming edges yet if it is the
224 * (2) the fromnode is the new node and therefore it should not
225 * have any outgoing edges.
227 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
228 CycleNode *tonode = fromnode->getEdge(i);
229 if (tonode != rmwnode) {
230 if (rmwnode->addEdge(tonode))
231 rollbackvector.push_back(rmwnode);
235 addNodeEdge(fromnode, rmwnode);
238 #if SUPPORT_MOD_ORDER_DUMP
239 void CycleGraph::dumpNodes(FILE *file) const
241 for (unsigned int i = 0; i < nodeList.size(); i++) {
242 CycleNode *cn = nodeList[i];
243 const ModelAction *action = cn->getAction();
244 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
245 if (cn->getRMW() != NULL) {
246 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
248 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
249 CycleNode *dst = cn->getEdge(j);
250 const ModelAction *dstaction = dst->getAction();
251 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
256 void CycleGraph::dumpGraphToFile(const char *filename) const
259 sprintf(buffer, "%s.dot", filename);
260 FILE *file = fopen(buffer, "w");
261 fprintf(file, "digraph %s {\n", filename);
263 fprintf(file, "}\n");
269 * Checks whether one ModelAction can reach another ModelAction/Promise
270 * @param from The ModelAction from which to begin exploration
271 * @param to The ModelAction or Promise to reach
272 * @return True, @a from can reach @a to; otherwise, false
274 template <typename T>
275 bool CycleGraph::checkReachable(const ModelAction *from, const T *to) const
277 CycleNode *fromnode = actionToNode.get(from);
278 CycleNode *tonode = actionToNode.get(to);
280 if (!fromnode || !tonode)
283 return checkReachable(fromnode, tonode);
287 * Checks whether one CycleNode can reach another.
288 * @param from The CycleNode from which to begin exploration
289 * @param to The CycleNode to reach
290 * @return True, @a from can reach @a to; otherwise, false
292 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
294 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
297 queue.push_back(from);
298 discovered->put(from, from);
299 while (!queue.empty()) {
300 const CycleNode *node = queue.back();
305 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
306 CycleNode *next = node->getEdge(i);
307 if (!discovered->contains(next)) {
308 discovered->put(next, next);
309 queue.push_back(next);
316 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
318 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
320 CycleNode *from = actionToNode.get(fromact);
322 queue.push_back(from);
323 discovered->put(from, from);
324 while (!queue.empty()) {
325 CycleNode *node = queue.back();
328 if (promise->eliminate_thread(node->getAction()->get_tid())) {
332 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
333 CycleNode *next = node->getEdge(i);
334 if (!discovered->contains(next)) {
335 discovered->put(next, next);
336 queue.push_back(next);
343 void CycleGraph::startChanges()
345 ASSERT(rollbackvector.empty());
346 ASSERT(rmwrollbackvector.empty());
347 ASSERT(oldCycles == hasCycles);
350 /** Commit changes to the cyclegraph. */
351 void CycleGraph::commitChanges()
353 rollbackvector.clear();
354 rmwrollbackvector.clear();
355 oldCycles = hasCycles;
358 /** Rollback changes to the previous commit. */
359 void CycleGraph::rollbackChanges()
361 for (unsigned int i = 0; i < rollbackvector.size(); i++)
362 rollbackvector[i]->popEdge();
364 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
365 rmwrollbackvector[i]->clearRMW();
367 hasCycles = oldCycles;
368 rollbackvector.clear();
369 rmwrollbackvector.clear();
372 /** @returns whether a CycleGraph contains cycles. */
373 bool CycleGraph::checkForCycles() const
379 * @brief Constructor for a CycleNode
380 * @param act The ModelAction for this node
382 CycleNode::CycleNode(const ModelAction *act) :
390 * @brief Constructor for a Promise CycleNode
391 * @param promise The Promise which was generated
393 CycleNode::CycleNode(const Promise *promise) :
401 * @param i The index of the edge to return
402 * @returns The a CycleNode edge indexed by i
404 CycleNode * CycleNode::getEdge(unsigned int i) const
409 /** @returns The number of edges leaving this CycleNode */
410 unsigned int CycleNode::getNumEdges() const
415 CycleNode * CycleNode::getBackEdge(unsigned int i) const
417 return back_edges[i];
420 unsigned int CycleNode::getNumBackEdges() const
422 return back_edges.size();
426 * @brief Remove an element from a vector
427 * @param v The vector
428 * @param n The element to remove
429 * @return True if the element was found; false otherwise
431 template <typename T>
432 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
434 for (unsigned int i = 0; i < v.size(); i++) {
436 v.erase(v.begin() + i);
444 * @brief Remove a (forward) edge from this CycleNode
445 * @return The CycleNode which was popped, if one exists; otherwise NULL
447 CycleNode * CycleNode::removeEdge()
452 CycleNode *ret = edges.back();
454 vector_remove_node(ret->back_edges, this);
459 * @brief Remove a (back) edge from this CycleNode
460 * @return The CycleNode which was popped, if one exists; otherwise NULL
462 CycleNode * CycleNode::removeBackEdge()
464 if (back_edges.empty())
467 CycleNode *ret = back_edges.back();
468 back_edges.pop_back();
469 vector_remove_node(ret->edges, this);
474 * Adds an edge from this CycleNode to another CycleNode.
475 * @param node The node to which we add a directed edge
476 * @return True if this edge is a new edge; false otherwise
478 bool CycleNode::addEdge(CycleNode *node)
480 for (unsigned int i = 0; i < edges.size(); i++)
481 if (edges[i] == node)
483 edges.push_back(node);
484 node->back_edges.push_back(this);
488 /** @returns the RMW CycleNode that reads from the current CycleNode */
489 CycleNode * CycleNode::getRMW() const
495 * Set a RMW action node that reads from the current CycleNode.
496 * @param node The RMW that reads from the current node
497 * @return True, if this node already was read by another RMW; false otherwise
498 * @see CycleGraph::addRMWEdge
500 bool CycleNode::setRMW(CycleNode *node)
509 * Convert a Promise CycleNode into a concrete-valued CycleNode. Should only be
510 * used when there's no existing ModelAction CycleNode for this write.
512 * @param writer The ModelAction which wrote the future value represented by
515 void CycleNode::resolvePromise(const ModelAction *writer)
517 ASSERT(is_promise());
518 ASSERT(promise->is_compatible(writer));
521 ASSERT(!is_promise());