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);
43 #if SUPPORT_MOD_ORDER_DUMP
44 nodeList.push_back(node);
49 * @brief Remove the Promise node from the graph
50 * @param promise The promise to remove from the graph
52 void CycleGraph::erasePromiseNode(const Promise *promise)
54 const ModelAction *reader = promise->get_action();
55 readerToPromiseNode.put(reader, NULL);
56 #if SUPPORT_MOD_ORDER_DUMP
57 /* Remove the promise node from nodeList */
58 CycleNode *node = getNode_noCreate(promise);
59 for (unsigned int i = 0; i < nodeList.size(); )
60 if (nodeList[i] == node)
61 nodeList.erase(nodeList.begin() + i);
67 /** @return The corresponding CycleNode, if exists; otherwise NULL */
68 CycleNode * CycleGraph::getNode_noCreate(const ModelAction *act) const
70 return actionToNode.get(act);
73 /** @return The corresponding CycleNode, if exists; otherwise NULL */
74 CycleNode * CycleGraph::getNode_noCreate(const Promise *promise) const
76 return readerToPromiseNode.get(promise->get_action());
80 * @brief Returns the CycleNode corresponding to a given ModelAction
82 * Gets (or creates, if none exist) a CycleNode corresponding to a ModelAction
84 * @param action The ModelAction to find a node for
85 * @return The CycleNode paired with this action
87 CycleNode * CycleGraph::getNode(const ModelAction *action)
89 CycleNode *node = getNode_noCreate(action);
91 node = new CycleNode(action);
92 putNode(action, node);
98 * @brief Returns a CycleNode corresponding to a promise
100 * Gets (or creates, if none exist) a CycleNode corresponding to a promised
103 * @param promise The Promise generated by a reader
104 * @return The CycleNode corresponding to the Promise
106 CycleNode * CycleGraph::getNode(const Promise *promise)
108 CycleNode *node = getNode_noCreate(promise);
110 node = new CycleNode(promise);
111 putNode(promise, node);
117 * @return false if the resolution results in a cycle; true otherwise
119 bool CycleGraph::resolvePromise(ModelAction *reader, ModelAction *writer,
120 promise_list_t *mustResolve)
122 CycleNode *promise_node = readerToPromiseNode.get(reader);
123 CycleNode *w_node = actionToNode.get(writer);
124 ASSERT(promise_node);
127 return mergeNodes(w_node, promise_node, mustResolve);
128 /* No existing write-node; just convert the promise-node */
129 promise_node->resolvePromise(writer);
130 erasePromiseNode(promise_node->getPromise());
131 putNode(writer, promise_node);
136 * @brief Merge two CycleNodes that represent the same write
138 * Note that this operation cannot be rolled back.
140 * @param w_node The write ModelAction node with which to merge
141 * @param p_node The Promise node to merge. Will be destroyed after this
143 * @param mustMerge Return (pass-by-reference) any additional Promises that
144 * must also be merged with w_node
146 * @return false if the merge results in a cycle; true otherwise
148 bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node,
149 promise_list_t *mustMerge)
151 ASSERT(!w_node->is_promise());
152 ASSERT(p_node->is_promise());
154 const Promise *promise = p_node->getPromise();
155 if (!promise->is_compatible(w_node->getAction())) {
160 /* Transfer the RMW */
161 CycleNode *promise_rmw = p_node->getRMW();
162 if (promise_rmw && promise_rmw != w_node->getRMW() && w_node->setRMW(promise_rmw)) {
167 /* Transfer back edges to w_node */
168 while (p_node->getNumBackEdges() > 0) {
169 CycleNode *back = p_node->removeBackEdge();
172 if (back->is_promise()) {
173 if (checkReachable(w_node, back)) {
174 /* Edge would create cycle; merge instead */
175 mustMerge->push_back(back->getPromise());
176 if (!mergeNodes(w_node, back, mustMerge))
179 back->addEdge(w_node);
181 addNodeEdge(back, w_node);
184 /* Transfer forward edges to w_node */
185 while (p_node->getNumEdges() > 0) {
186 CycleNode *forward = p_node->removeEdge();
187 if (forward == w_node)
189 if (forward->is_promise()) {
190 if (checkReachable(forward, w_node)) {
191 mustMerge->push_back(forward->getPromise());
192 if (!mergeNodes(w_node, forward, mustMerge))
195 w_node->addEdge(forward);
197 addNodeEdge(w_node, forward);
200 erasePromiseNode(promise);
201 /* Not deleting p_node, to maintain consistency if mergeNodes() fails */
207 * Adds an edge between two CycleNodes.
208 * @param fromnode The edge comes from this CycleNode
209 * @param tonode The edge points to this CycleNode
210 * @return True, if new edge(s) are added; otherwise false
212 bool CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
217 hasCycles = checkReachable(tonode, fromnode);
219 if ((added = fromnode->addEdge(tonode)))
220 rollbackvector.push_back(fromnode);
223 * If the fromnode has a rmwnode that is not the tonode, we should add
224 * an edge between its rmwnode and the tonode
226 CycleNode *rmwnode = fromnode->getRMW();
227 if (rmwnode && rmwnode != tonode) {
229 hasCycles = checkReachable(tonode, rmwnode);
231 if (rmwnode->addEdge(tonode)) {
232 rollbackvector.push_back(rmwnode);
240 * @brief Add an edge between a write and the RMW which reads from it
242 * Handles special case of a RMW action, where the ModelAction rmw reads from
243 * the ModelAction/Promise from. The key differences are:
244 * (1) no write can occur in between the rmw and the from action.
245 * (2) Only one RMW action can read from a given write.
247 * @param from The edge comes from this ModelAction/Promise
248 * @param rmw The edge points to this ModelAction; this action must read from
249 * the ModelAction/Promise from
251 template <typename T>
252 void CycleGraph::addRMWEdge(const T *from, const ModelAction *rmw)
257 CycleNode *fromnode = getNode(from);
258 CycleNode *rmwnode = getNode(rmw);
260 /* Two RMW actions cannot read from the same write. */
261 if (fromnode->setRMW(rmwnode))
264 rmwrollbackvector.push_back(fromnode);
266 /* Transfer all outgoing edges from the from node to the rmw node */
267 /* This process should not add a cycle because either:
268 * (1) The rmw should not have any incoming edges yet if it is the
270 * (2) the fromnode is the new node and therefore it should not
271 * have any outgoing edges.
273 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
274 CycleNode *tonode = fromnode->getEdge(i);
275 if (tonode != rmwnode) {
276 if (rmwnode->addEdge(tonode))
277 rollbackvector.push_back(rmwnode);
281 addNodeEdge(fromnode, rmwnode);
283 /* Instantiate two forms of CycleGraph::addRMWEdge */
284 template void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw);
285 template void CycleGraph::addRMWEdge(const Promise *from, const ModelAction *rmw);
288 * @brief Adds an edge between objects
290 * This function will add an edge between any two objects which can be
291 * associated with a CycleNode. That is, if they have a CycleGraph::getNode
294 * The object to is ordered after the object from.
296 * @param to The edge points to this object, of type T
297 * @param from The edge comes from this object, of type U
298 * @return True, if new edge(s) are added; otherwise false
300 template <typename T, typename U>
301 bool CycleGraph::addEdge(const T *from, const U *to)
306 CycleNode *fromnode = getNode(from);
307 CycleNode *tonode = getNode(to);
309 return addNodeEdge(fromnode, tonode);
311 /* Instantiate four forms of CycleGraph::addEdge */
312 template bool CycleGraph::addEdge(const ModelAction *from, const ModelAction *to);
313 template bool CycleGraph::addEdge(const ModelAction *from, const Promise *to);
314 template bool CycleGraph::addEdge(const Promise *from, const ModelAction *to);
315 template bool CycleGraph::addEdge(const Promise *from, const Promise *to);
317 #if SUPPORT_MOD_ORDER_DUMP
319 static void print_node(const CycleNode *node, FILE *file, int label)
322 if (node->is_promise()) {
323 const Promise *promise = node->getPromise();
324 idx = promise->get_action()->get_seq_number();
325 fprintf(file, "P%u", idx);
328 fprintf(file, " [label=\"P%u, T", idx);
329 for (unsigned int i = 0 ; i < model->get_num_threads(); i++)
330 if (promise->thread_is_available(int_to_id(i))) {
331 fprintf(file, "%s%u", first ? "": ",", i);
334 fprintf(file, "\"]");
337 const ModelAction *act = node->getAction();
338 idx = act->get_seq_number();
339 fprintf(file, "N%u", idx);
341 fprintf(file, " [label=\"N%u, T%u\"]", idx, act->get_tid());
345 void CycleGraph::dumpNodes(FILE *file) const
347 for (unsigned int i = 0; i < nodeList.size(); i++) {
348 CycleNode *n = nodeList[i];
349 print_node(n, file, 1);
350 fprintf(file, ";\n");
351 if (n->getRMW() != NULL) {
352 print_node(n, file, 0);
353 fprintf(file, " -> ");
354 print_node(n->getRMW(), file, 0);
355 fprintf(file, "[style=dotted];\n");
357 for (unsigned int j = 0; j < n->getNumEdges(); j++) {
358 print_node(n, file, 0);
359 fprintf(file, " -> ");
360 print_node(n->getEdge(j), file, 0);
361 fprintf(file, ";\n");
366 void CycleGraph::dumpGraphToFile(const char *filename) const
369 sprintf(buffer, "%s.dot", filename);
370 FILE *file = fopen(buffer, "w");
371 fprintf(file, "digraph %s {\n", filename);
373 fprintf(file, "}\n");
379 * Checks whether one CycleNode can reach another.
380 * @param from The CycleNode from which to begin exploration
381 * @param to The CycleNode to reach
382 * @return True, @a from can reach @a to; otherwise, false
384 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
386 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
389 queue.push_back(from);
390 discovered->put(from, from);
391 while (!queue.empty()) {
392 const CycleNode *node = queue.back();
397 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
398 CycleNode *next = node->getEdge(i);
399 if (!discovered->contains(next)) {
400 discovered->put(next, next);
401 queue.push_back(next);
409 * Checks whether one ModelAction/Promise can reach another ModelAction/Promise
410 * @param from The ModelAction or Promise from which to begin exploration
411 * @param to The ModelAction or Promise to reach
412 * @return True, @a from can reach @a to; otherwise, false
414 template <typename T, typename U>
415 bool CycleGraph::checkReachable(const T *from, const U *to) const
417 CycleNode *fromnode = getNode_noCreate(from);
418 CycleNode *tonode = getNode_noCreate(to);
420 if (!fromnode || !tonode)
423 return checkReachable(fromnode, tonode);
425 /* Instantiate three forms of CycleGraph::checkReachable */
426 template bool CycleGraph::checkReachable(const ModelAction *from,
427 const ModelAction *to) const;
428 template bool CycleGraph::checkReachable(const ModelAction *from,
429 const Promise *to) const;
430 template bool CycleGraph::checkReachable(const Promise *from,
431 const ModelAction *to) const;
433 /** @return True, if the promise has failed; false otherwise */
434 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
436 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
438 CycleNode *from = actionToNode.get(fromact);
440 queue.push_back(from);
441 discovered->put(from, from);
442 while (!queue.empty()) {
443 CycleNode *node = queue.back();
446 if (node->getPromise() == promise)
448 if (!node->is_promise() &&
449 promise->eliminate_thread(node->getAction()->get_tid()))
452 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
453 CycleNode *next = node->getEdge(i);
454 if (!discovered->contains(next)) {
455 discovered->put(next, next);
456 queue.push_back(next);
463 void CycleGraph::startChanges()
465 ASSERT(rollbackvector.empty());
466 ASSERT(rmwrollbackvector.empty());
467 ASSERT(oldCycles == hasCycles);
470 /** Commit changes to the cyclegraph. */
471 void CycleGraph::commitChanges()
473 rollbackvector.clear();
474 rmwrollbackvector.clear();
475 oldCycles = hasCycles;
478 /** Rollback changes to the previous commit. */
479 void CycleGraph::rollbackChanges()
481 for (unsigned int i = 0; i < rollbackvector.size(); i++)
482 rollbackvector[i]->removeEdge();
484 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
485 rmwrollbackvector[i]->clearRMW();
487 hasCycles = oldCycles;
488 rollbackvector.clear();
489 rmwrollbackvector.clear();
492 /** @returns whether a CycleGraph contains cycles. */
493 bool CycleGraph::checkForCycles() const
499 * @brief Constructor for a CycleNode
500 * @param act The ModelAction for this node
502 CycleNode::CycleNode(const ModelAction *act) :
510 * @brief Constructor for a Promise CycleNode
511 * @param promise The Promise which was generated
513 CycleNode::CycleNode(const Promise *promise) :
521 * @param i The index of the edge to return
522 * @returns The a CycleNode edge indexed by i
524 CycleNode * CycleNode::getEdge(unsigned int i) const
529 /** @returns The number of edges leaving this CycleNode */
530 unsigned int CycleNode::getNumEdges() const
535 CycleNode * CycleNode::getBackEdge(unsigned int i) const
537 return back_edges[i];
540 unsigned int CycleNode::getNumBackEdges() const
542 return back_edges.size();
546 * @brief Remove an element from a vector
547 * @param v The vector
548 * @param n The element to remove
549 * @return True if the element was found; false otherwise
551 template <typename T>
552 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
554 for (unsigned int i = 0; i < v.size(); i++) {
556 v.erase(v.begin() + i);
564 * @brief Remove a (forward) edge from this CycleNode
565 * @return The CycleNode which was popped, if one exists; otherwise NULL
567 CycleNode * CycleNode::removeEdge()
572 CycleNode *ret = edges.back();
574 vector_remove_node(ret->back_edges, this);
579 * @brief Remove a (back) edge from this CycleNode
580 * @return The CycleNode which was popped, if one exists; otherwise NULL
582 CycleNode * CycleNode::removeBackEdge()
584 if (back_edges.empty())
587 CycleNode *ret = back_edges.back();
588 back_edges.pop_back();
589 vector_remove_node(ret->edges, this);
594 * Adds an edge from this CycleNode to another CycleNode.
595 * @param node The node to which we add a directed edge
596 * @return True if this edge is a new edge; false otherwise
598 bool CycleNode::addEdge(CycleNode *node)
600 for (unsigned int i = 0; i < edges.size(); i++)
601 if (edges[i] == node)
603 edges.push_back(node);
604 node->back_edges.push_back(this);
608 /** @returns the RMW CycleNode that reads from the current CycleNode */
609 CycleNode * CycleNode::getRMW() const
615 * Set a RMW action node that reads from the current CycleNode.
616 * @param node The RMW that reads from the current node
617 * @return True, if this node already was read by another RMW; false otherwise
618 * @see CycleGraph::addRMWEdge
620 bool CycleNode::setRMW(CycleNode *node)
629 * Convert a Promise CycleNode into a concrete-valued CycleNode. Should only be
630 * used when there's no existing ModelAction CycleNode for this write.
632 * @param writer The ModelAction which wrote the future value represented by
635 void CycleNode::resolvePromise(const ModelAction *writer)
637 ASSERT(is_promise());
638 ASSERT(promise->is_compatible(writer));
641 ASSERT(!is_promise());