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);
35 * Add a CycleNode to the graph, corresponding to a Promise
36 * @param promise The Promise that should be added
37 * @param node The CycleNode that corresponds to the Promise
39 void CycleGraph::putNode(const Promise *promise, CycleNode *node)
41 const ModelAction *reader = promise->get_action();
42 readerToPromiseNode.put(reader, node);
46 * @brief Remove the Promise node from the graph
47 * @param promise The promise to remove from the graph
49 void CycleGraph::erasePromiseNode(const Promise *promise)
51 const ModelAction *reader = promise->get_action();
52 readerToPromiseNode.put(reader, NULL);
55 /** @return The corresponding CycleNode, if exists; otherwise NULL */
56 CycleNode * CycleGraph::getNode_noCreate(const ModelAction *act) const
58 return actionToNode.get(act);
61 /** @return The corresponding CycleNode, if exists; otherwise NULL */
62 CycleNode * CycleGraph::getNode_noCreate(const Promise *promise) const
64 return readerToPromiseNode.get(promise->get_action());
68 * @brief Returns the CycleNode corresponding to a given ModelAction
70 * Gets (or creates, if none exist) a CycleNode corresponding to a ModelAction
72 * @param action The ModelAction to find a node for
73 * @return The CycleNode paired with this action
75 CycleNode * CycleGraph::getNode(const ModelAction *action)
77 CycleNode *node = getNode_noCreate(action);
79 node = new CycleNode(action);
80 putNode(action, node);
86 * @brief Returns a CycleNode corresponding to a promise
88 * Gets (or creates, if none exist) a CycleNode corresponding to a promised
91 * @param promise The Promise generated by a reader
92 * @return The CycleNode corresponding to the Promise
94 CycleNode * CycleGraph::getNode(const Promise *promise)
96 CycleNode *node = getNode_noCreate(promise);
98 node = new CycleNode(promise);
99 putNode(promise, node);
105 * @return false if the resolution results in a cycle; true otherwise
107 bool CycleGraph::resolvePromise(ModelAction *reader, ModelAction *writer,
108 promise_list_t *mustResolve)
110 CycleNode *promise_node = readerToPromiseNode.get(reader);
111 CycleNode *w_node = actionToNode.get(writer);
112 ASSERT(promise_node);
115 return mergeNodes(w_node, promise_node, mustResolve);
116 /* No existing write-node; just convert the promise-node */
117 promise_node->resolvePromise(writer);
118 erasePromiseNode(promise_node->getPromise());
119 putNode(writer, promise_node);
124 * @brief Merge two CycleNodes that represent the same write
126 * Note that this operation cannot be rolled back.
128 * @param w_node The write ModelAction node with which to merge
129 * @param p_node The Promise node to merge. Will be destroyed after this
131 * @param mustMerge Return (pass-by-reference) any additional Promises that
132 * must also be merged with w_node
134 * @return false if the merge results in a cycle; true otherwise
136 bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node,
137 promise_list_t *mustMerge)
139 ASSERT(!w_node->is_promise());
140 ASSERT(p_node->is_promise());
142 const Promise *promise = p_node->getPromise();
143 if (!promise->is_compatible(w_node->getAction())) {
148 /* Transfer the RMW */
149 CycleNode *promise_rmw = p_node->getRMW();
150 if (promise_rmw && promise_rmw != w_node->getRMW() && w_node->setRMW(promise_rmw))
153 /* Transfer back edges to w_node */
154 while (p_node->getNumBackEdges() > 0) {
155 CycleNode *back = p_node->removeBackEdge();
156 if (back != w_node) {
157 if (back->is_promise()) {
158 if (checkReachable(w_node, back)) {
159 /* Edge would create cycle; merge instead */
160 mustMerge->push_back(back->getPromise());
161 if (!mergeNodes(w_node, back, mustMerge))
164 back->addEdge(w_node);
166 addNodeEdge(back, w_node);
170 /* Transfer forward edges to w_node */
171 while (p_node->getNumEdges() > 0) {
172 CycleNode *forward = p_node->removeEdge();
173 if (forward != w_node) {
174 if (forward->is_promise()) {
175 if (checkReachable(forward, w_node)) {
176 mustMerge->push_back(forward->getPromise());
177 if (!mergeNodes(w_node, forward, mustMerge))
180 w_node->addEdge(forward);
182 addNodeEdge(w_node, forward);
186 erasePromiseNode(promise);
193 * Adds an edge between two CycleNodes.
194 * @param fromnode The edge comes from this CycleNode
195 * @param tonode The edge points to this CycleNode
196 * @return True, if new edge(s) are added; otherwise false
198 bool CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
203 hasCycles = checkReachable(tonode, fromnode);
205 if ((added = fromnode->addEdge(tonode)))
206 rollbackvector.push_back(fromnode);
209 * If the fromnode has a rmwnode that is not the tonode, we should add
210 * an edge between its rmwnode and the tonode
212 CycleNode *rmwnode = fromnode->getRMW();
213 if (rmwnode && rmwnode != tonode) {
215 hasCycles = checkReachable(tonode, rmwnode);
217 if (rmwnode->addEdge(tonode)) {
218 rollbackvector.push_back(rmwnode);
226 * @brief Add an edge between a write and the RMW which reads from it
228 * Handles special case of a RMW action, where the ModelAction rmw reads from
229 * the ModelAction/Promise from. The key differences are:
230 * (1) no write can occur in between the rmw and the from action.
231 * (2) Only one RMW action can read from a given write.
233 * @param from The edge comes from this ModelAction/Promise
234 * @param rmw The edge points to this ModelAction; this action must read from
235 * the ModelAction/Promise from
237 template <typename T>
238 void CycleGraph::addRMWEdge(const T *from, const ModelAction *rmw)
243 CycleNode *fromnode = getNode(from);
244 CycleNode *rmwnode = getNode(rmw);
246 /* Two RMW actions cannot read from the same write. */
247 if (fromnode->setRMW(rmwnode))
250 rmwrollbackvector.push_back(fromnode);
252 /* Transfer all outgoing edges from the from node to the rmw node */
253 /* This process should not add a cycle because either:
254 * (1) The rmw should not have any incoming edges yet if it is the
256 * (2) the fromnode is the new node and therefore it should not
257 * have any outgoing edges.
259 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
260 CycleNode *tonode = fromnode->getEdge(i);
261 if (tonode != rmwnode) {
262 if (rmwnode->addEdge(tonode))
263 rollbackvector.push_back(rmwnode);
267 addNodeEdge(fromnode, rmwnode);
269 /* Instantiate two forms of CycleGraph::addRMWEdge */
270 template void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw);
271 template void CycleGraph::addRMWEdge(const Promise *from, const ModelAction *rmw);
274 * @brief Adds an edge between objects
276 * This function will add an edge between any two objects which can be
277 * associated with a CycleNode. That is, if they have a CycleGraph::getNode
280 * The object to is ordered after the object from.
282 * @param to The edge points to this object, of type T
283 * @param from The edge comes from this object, of type U
284 * @return True, if new edge(s) are added; otherwise false
286 template <typename T, typename U>
287 bool CycleGraph::addEdge(const T *from, const U *to)
292 CycleNode *fromnode = getNode(from);
293 CycleNode *tonode = getNode(to);
295 return addNodeEdge(fromnode, tonode);
297 /* Instantiate three forms of CycleGraph::addEdge */
298 template bool CycleGraph::addEdge(const ModelAction *from, const ModelAction *to);
299 template bool CycleGraph::addEdge(const ModelAction *from, const Promise *to);
300 template bool CycleGraph::addEdge(const Promise *from, const ModelAction *to);
302 #if SUPPORT_MOD_ORDER_DUMP
303 void CycleGraph::dumpNodes(FILE *file) const
305 for (unsigned int i = 0; i < nodeList.size(); i++) {
306 CycleNode *cn = nodeList[i];
307 const ModelAction *action = cn->getAction();
308 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
309 if (cn->getRMW() != NULL) {
310 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
312 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
313 CycleNode *dst = cn->getEdge(j);
314 const ModelAction *dstaction = dst->getAction();
315 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
320 void CycleGraph::dumpGraphToFile(const char *filename) const
323 sprintf(buffer, "%s.dot", filename);
324 FILE *file = fopen(buffer, "w");
325 fprintf(file, "digraph %s {\n", filename);
327 fprintf(file, "}\n");
333 * Checks whether one CycleNode can reach another.
334 * @param from The CycleNode from which to begin exploration
335 * @param to The CycleNode to reach
336 * @return True, @a from can reach @a to; otherwise, false
338 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
340 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
343 queue.push_back(from);
344 discovered->put(from, from);
345 while (!queue.empty()) {
346 const CycleNode *node = queue.back();
351 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
352 CycleNode *next = node->getEdge(i);
353 if (!discovered->contains(next)) {
354 discovered->put(next, next);
355 queue.push_back(next);
363 * Checks whether one ModelAction can reach another ModelAction/Promise
364 * @param from The ModelAction from which to begin exploration
365 * @param to The ModelAction or Promise to reach
366 * @return True, @a from can reach @a to; otherwise, false
368 template <typename T>
369 bool CycleGraph::checkReachable(const ModelAction *from, const T *to) const
371 CycleNode *fromnode = getNode_noCreate(from);
372 CycleNode *tonode = getNode_noCreate(to);
374 if (!fromnode || !tonode)
377 return checkReachable(fromnode, tonode);
379 /* Instantiate two forms of CycleGraph::checkReachable */
380 template bool CycleGraph::checkReachable(const ModelAction *from,
381 const ModelAction *to) const;
382 template bool CycleGraph::checkReachable(const ModelAction *from,
383 const Promise *to) const;
385 /** @return True, if the promise has failed; false otherwise */
386 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
388 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
390 CycleNode *from = actionToNode.get(fromact);
392 queue.push_back(from);
393 discovered->put(from, from);
394 while (!queue.empty()) {
395 CycleNode *node = queue.back();
398 if (!node->is_promise() &&
399 promise->eliminate_thread(node->getAction()->get_tid()))
402 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
403 CycleNode *next = node->getEdge(i);
404 if (!discovered->contains(next)) {
405 discovered->put(next, next);
406 queue.push_back(next);
413 void CycleGraph::startChanges()
415 ASSERT(rollbackvector.empty());
416 ASSERT(rmwrollbackvector.empty());
417 ASSERT(oldCycles == hasCycles);
420 /** Commit changes to the cyclegraph. */
421 void CycleGraph::commitChanges()
423 rollbackvector.clear();
424 rmwrollbackvector.clear();
425 oldCycles = hasCycles;
428 /** Rollback changes to the previous commit. */
429 void CycleGraph::rollbackChanges()
431 for (unsigned int i = 0; i < rollbackvector.size(); i++)
432 rollbackvector[i]->removeEdge();
434 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
435 rmwrollbackvector[i]->clearRMW();
437 hasCycles = oldCycles;
438 rollbackvector.clear();
439 rmwrollbackvector.clear();
442 /** @returns whether a CycleGraph contains cycles. */
443 bool CycleGraph::checkForCycles() const
449 * @brief Constructor for a CycleNode
450 * @param act The ModelAction for this node
452 CycleNode::CycleNode(const ModelAction *act) :
460 * @brief Constructor for a Promise CycleNode
461 * @param promise The Promise which was generated
463 CycleNode::CycleNode(const Promise *promise) :
471 * @param i The index of the edge to return
472 * @returns The a CycleNode edge indexed by i
474 CycleNode * CycleNode::getEdge(unsigned int i) const
479 /** @returns The number of edges leaving this CycleNode */
480 unsigned int CycleNode::getNumEdges() const
485 CycleNode * CycleNode::getBackEdge(unsigned int i) const
487 return back_edges[i];
490 unsigned int CycleNode::getNumBackEdges() const
492 return back_edges.size();
496 * @brief Remove an element from a vector
497 * @param v The vector
498 * @param n The element to remove
499 * @return True if the element was found; false otherwise
501 template <typename T>
502 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
504 for (unsigned int i = 0; i < v.size(); i++) {
506 v.erase(v.begin() + i);
514 * @brief Remove a (forward) edge from this CycleNode
515 * @return The CycleNode which was popped, if one exists; otherwise NULL
517 CycleNode * CycleNode::removeEdge()
522 CycleNode *ret = edges.back();
524 vector_remove_node(ret->back_edges, this);
529 * @brief Remove a (back) edge from this CycleNode
530 * @return The CycleNode which was popped, if one exists; otherwise NULL
532 CycleNode * CycleNode::removeBackEdge()
534 if (back_edges.empty())
537 CycleNode *ret = back_edges.back();
538 back_edges.pop_back();
539 vector_remove_node(ret->edges, this);
544 * Adds an edge from this CycleNode to another CycleNode.
545 * @param node The node to which we add a directed edge
546 * @return True if this edge is a new edge; false otherwise
548 bool CycleNode::addEdge(CycleNode *node)
550 for (unsigned int i = 0; i < edges.size(); i++)
551 if (edges[i] == node)
553 edges.push_back(node);
554 node->back_edges.push_back(this);
558 /** @returns the RMW CycleNode that reads from the current CycleNode */
559 CycleNode * CycleNode::getRMW() const
565 * Set a RMW action node that reads from the current CycleNode.
566 * @param node The RMW that reads from the current node
567 * @return True, if this node already was read by another RMW; false otherwise
568 * @see CycleGraph::addRMWEdge
570 bool CycleNode::setRMW(CycleNode *node)
579 * Convert a Promise CycleNode into a concrete-valued CycleNode. Should only be
580 * used when there's no existing ModelAction CycleNode for this write.
582 * @param writer The ModelAction which wrote the future value represented by
585 void CycleNode::resolvePromise(const ModelAction *writer)
587 ASSERT(is_promise());
588 ASSERT(promise->is_compatible(writer));
591 ASSERT(!is_promise());