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 * @brief Adds an edge between objects
87 * This function will add an edge between any two objects which can be
88 * associated with a CycleNode. That is, if they have a CycleGraph::getNode
91 * The object to is ordered after the object from.
93 * @param to The edge points to this object, of type T
94 * @param from The edge comes from this object, of type U
96 template <typename T, typename U>
97 void CycleGraph::addEdge(const T from, const U to)
102 CycleNode *fromnode = getNode(from);
103 CycleNode *tonode = getNode(to);
105 addNodeEdge(fromnode, tonode);
109 * @return false if the resolution results in a cycle; true otherwise
111 bool CycleGraph::resolvePromise(ModelAction *reader, ModelAction *writer,
112 promise_list_t *mustResolve)
114 CycleNode *promise_node = readerToPromiseNode.get(reader);
115 CycleNode *w_node = actionToNode.get(writer);
116 ASSERT(promise_node);
119 return mergeNodes(w_node, promise_node, mustResolve);
120 /* No existing write-node; just convert the promise-node */
121 promise_node->resolvePromise(writer);
122 readerToPromiseNode.put(reader, NULL); /* erase promise_node */
123 putNode(writer, promise_node);
128 * @brief Merge two CycleNodes that represent the same write
130 * Note that this operation cannot be rolled back.
132 * @param w_node The write ModelAction node with which to merge
133 * @param p_node The Promise node to merge. Will be destroyed after this
135 * @param mustMerge Return (pass-by-reference) any additional Promises that
136 * must also be merged with w_node
138 * @return false if the merge results in a cycle; true otherwise
140 bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node,
141 promise_list_t *mustMerge)
143 ASSERT(!w_node->is_promise());
144 ASSERT(p_node->is_promise());
145 const Promise *promise = p_node->getPromise();
146 if (!promise->is_compatible(w_node->getAction())) {
151 /* Transfer back edges to w_node */
152 while (p_node->getNumBackEdges() > 0) {
153 CycleNode *back = p_node->removeBackEdge();
154 if (back != w_node) {
155 if (back->is_promise()) {
156 if (checkReachable(w_node, back)) {
157 /* Edge would create cycle; merge instead */
158 mustMerge->push_back(back->getPromise());
159 if (!mergeNodes(w_node, back, mustMerge))
162 back->addEdge(w_node);
164 addNodeEdge(back, w_node);
168 /* Transfer forward edges to w_node */
169 while (p_node->getNumEdges() > 0) {
170 CycleNode *forward = p_node->removeEdge();
171 if (forward != w_node) {
172 if (forward->is_promise()) {
173 if (checkReachable(forward, w_node)) {
174 mustMerge->push_back(forward->getPromise());
175 if (!mergeNodes(w_node, forward, mustMerge))
178 w_node->addEdge(forward);
180 addNodeEdge(w_node, forward);
185 readerToPromiseNode.put(promise->get_action(), NULL);
192 * Adds an edge between two CycleNodes.
193 * @param fromnode The edge comes from this CycleNode
194 * @param tonode The edge points to this CycleNode
196 void CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
199 hasCycles = checkReachable(tonode, fromnode);
201 if (fromnode->addEdge(tonode))
202 rollbackvector.push_back(fromnode);
205 * If the fromnode has a rmwnode that is not the tonode, we should add
206 * an edge between its rmwnode and the tonode
208 CycleNode *rmwnode = fromnode->getRMW();
209 if (rmwnode && rmwnode != tonode) {
211 hasCycles = checkReachable(tonode, rmwnode);
213 if (rmwnode->addEdge(tonode))
214 rollbackvector.push_back(rmwnode);
219 * @brief Add an edge between a write and the RMW which reads from it
221 * Handles special case of a RMW action, where the ModelAction rmw reads from
222 * the ModelAction from. The key differences are:
223 * (1) no write can occur in between the rmw and the from action.
224 * (2) Only one RMW action can read from a given write.
226 * @param from The edge comes from this ModelAction
227 * @param rmw The edge points to this ModelAction; this action must read from
230 void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw)
235 CycleNode *fromnode = getNode(from);
236 CycleNode *rmwnode = getNode(rmw);
238 /* Two RMW actions cannot read from the same write. */
239 if (fromnode->setRMW(rmwnode))
242 rmwrollbackvector.push_back(fromnode);
244 /* Transfer all outgoing edges from the from node to the rmw node */
245 /* This process should not add a cycle because either:
246 * (1) The rmw should not have any incoming edges yet if it is the
248 * (2) the fromnode is the new node and therefore it should not
249 * have any outgoing edges.
251 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
252 CycleNode *tonode = fromnode->getEdge(i);
253 if (tonode != rmwnode) {
254 if (rmwnode->addEdge(tonode))
255 rollbackvector.push_back(rmwnode);
259 addNodeEdge(fromnode, rmwnode);
262 #if SUPPORT_MOD_ORDER_DUMP
263 void CycleGraph::dumpNodes(FILE *file) const
265 for (unsigned int i = 0; i < nodeList.size(); i++) {
266 CycleNode *cn = nodeList[i];
267 const ModelAction *action = cn->getAction();
268 fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
269 if (cn->getRMW() != NULL) {
270 fprintf(file, "N%u -> N%u[style=dotted];\n", action->get_seq_number(), cn->getRMW()->getAction()->get_seq_number());
272 for (unsigned int j = 0; j < cn->getNumEdges(); j++) {
273 CycleNode *dst = cn->getEdge(j);
274 const ModelAction *dstaction = dst->getAction();
275 fprintf(file, "N%u -> N%u;\n", action->get_seq_number(), dstaction->get_seq_number());
280 void CycleGraph::dumpGraphToFile(const char *filename) const
283 sprintf(buffer, "%s.dot", filename);
284 FILE *file = fopen(buffer, "w");
285 fprintf(file, "digraph %s {\n", filename);
287 fprintf(file, "}\n");
293 * Checks whether one ModelAction can reach another ModelAction/Promise
294 * @param from The ModelAction from which to begin exploration
295 * @param to The ModelAction or Promise to reach
296 * @return True, @a from can reach @a to; otherwise, false
298 template <typename T>
299 bool CycleGraph::checkReachable(const ModelAction *from, const T *to) const
301 CycleNode *fromnode = actionToNode.get(from);
302 CycleNode *tonode = actionToNode.get(to);
304 if (!fromnode || !tonode)
307 return checkReachable(fromnode, tonode);
311 * Checks whether one CycleNode can reach another.
312 * @param from The CycleNode from which to begin exploration
313 * @param to The CycleNode to reach
314 * @return True, @a from can reach @a to; otherwise, false
316 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
318 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
321 queue.push_back(from);
322 discovered->put(from, from);
323 while (!queue.empty()) {
324 const CycleNode *node = queue.back();
329 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
330 CycleNode *next = node->getEdge(i);
331 if (!discovered->contains(next)) {
332 discovered->put(next, next);
333 queue.push_back(next);
340 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
342 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
344 CycleNode *from = actionToNode.get(fromact);
346 queue.push_back(from);
347 discovered->put(from, from);
348 while (!queue.empty()) {
349 CycleNode *node = queue.back();
352 if (promise->eliminate_thread(node->getAction()->get_tid())) {
356 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
357 CycleNode *next = node->getEdge(i);
358 if (!discovered->contains(next)) {
359 discovered->put(next, next);
360 queue.push_back(next);
367 void CycleGraph::startChanges()
369 ASSERT(rollbackvector.empty());
370 ASSERT(rmwrollbackvector.empty());
371 ASSERT(oldCycles == hasCycles);
374 /** Commit changes to the cyclegraph. */
375 void CycleGraph::commitChanges()
377 rollbackvector.clear();
378 rmwrollbackvector.clear();
379 oldCycles = hasCycles;
382 /** Rollback changes to the previous commit. */
383 void CycleGraph::rollbackChanges()
385 for (unsigned int i = 0; i < rollbackvector.size(); i++)
386 rollbackvector[i]->popEdge();
388 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
389 rmwrollbackvector[i]->clearRMW();
391 hasCycles = oldCycles;
392 rollbackvector.clear();
393 rmwrollbackvector.clear();
396 /** @returns whether a CycleGraph contains cycles. */
397 bool CycleGraph::checkForCycles() const
403 * @brief Constructor for a CycleNode
404 * @param act The ModelAction for this node
406 CycleNode::CycleNode(const ModelAction *act) :
414 * @brief Constructor for a Promise CycleNode
415 * @param promise The Promise which was generated
417 CycleNode::CycleNode(const Promise *promise) :
425 * @param i The index of the edge to return
426 * @returns The a CycleNode edge indexed by i
428 CycleNode * CycleNode::getEdge(unsigned int i) const
433 /** @returns The number of edges leaving this CycleNode */
434 unsigned int CycleNode::getNumEdges() const
439 CycleNode * CycleNode::getBackEdge(unsigned int i) const
441 return back_edges[i];
444 unsigned int CycleNode::getNumBackEdges() const
446 return back_edges.size();
450 * @brief Remove an element from a vector
451 * @param v The vector
452 * @param n The element to remove
453 * @return True if the element was found; false otherwise
455 template <typename T>
456 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
458 for (unsigned int i = 0; i < v.size(); i++) {
460 v.erase(v.begin() + i);
468 * @brief Remove a (forward) edge from this CycleNode
469 * @return The CycleNode which was popped, if one exists; otherwise NULL
471 CycleNode * CycleNode::removeEdge()
476 CycleNode *ret = edges.back();
478 vector_remove_node(ret->back_edges, this);
483 * @brief Remove a (back) edge from this CycleNode
484 * @return The CycleNode which was popped, if one exists; otherwise NULL
486 CycleNode * CycleNode::removeBackEdge()
488 if (back_edges.empty())
491 CycleNode *ret = back_edges.back();
492 back_edges.pop_back();
493 vector_remove_node(ret->edges, this);
498 * Adds an edge from this CycleNode to another CycleNode.
499 * @param node The node to which we add a directed edge
500 * @return True if this edge is a new edge; false otherwise
502 bool CycleNode::addEdge(CycleNode *node)
504 for (unsigned int i = 0; i < edges.size(); i++)
505 if (edges[i] == node)
507 edges.push_back(node);
508 node->back_edges.push_back(this);
512 /** @returns the RMW CycleNode that reads from the current CycleNode */
513 CycleNode * CycleNode::getRMW() const
519 * Set a RMW action node that reads from the current CycleNode.
520 * @param node The RMW that reads from the current node
521 * @return True, if this node already was read by another RMW; false otherwise
522 * @see CycleGraph::addRMWEdge
524 bool CycleNode::setRMW(CycleNode *node)
533 * Convert a Promise CycleNode into a concrete-valued CycleNode. Should only be
534 * used when there's no existing ModelAction CycleNode for this write.
536 * @param writer The ModelAction which wrote the future value represented by
539 void CycleNode::resolvePromise(const ModelAction *writer)
541 ASSERT(is_promise());
542 ASSERT(promise->is_compatible(writer));
545 ASSERT(!is_promise());