1 #include "cyclegraph.h"
6 #include "threads-model.h"
8 /** Initializes a CycleGraph object. */
9 CycleGraph::CycleGraph() :
10 discovered(new HashTable<const CycleNode *, const CycleNode *, uintptr_t, 4, model_malloc, model_calloc, model_free>(16)),
16 /** CycleGraph destructor */
17 CycleGraph::~CycleGraph()
23 * Add a CycleNode to the graph, corresponding to a store ModelAction
24 * @param act The write action that should be added
25 * @param node The CycleNode that corresponds to the store
27 void CycleGraph::putNode(const ModelAction *act, CycleNode *node)
29 actionToNode.put(act, node);
30 #if SUPPORT_MOD_ORDER_DUMP
31 nodeList.push_back(node);
36 * Add a CycleNode to the graph, corresponding to a Promise
37 * @param promise The Promise that should be added
38 * @param node The CycleNode that corresponds to the Promise
40 void CycleGraph::putNode(const Promise *promise, CycleNode *node)
42 promiseToNode.put(promise, 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 promiseToNode.put(promise, NULL);
55 #if SUPPORT_MOD_ORDER_DUMP
56 /* Remove the promise node from nodeList */
57 CycleNode *node = getNode_noCreate(promise);
58 for (unsigned int i = 0; i < nodeList.size(); )
59 if (nodeList[i] == node)
60 nodeList.erase(nodeList.begin() + i);
66 /** @return The corresponding CycleNode, if exists; otherwise NULL */
67 CycleNode * CycleGraph::getNode_noCreate(const ModelAction *act) const
69 return actionToNode.get(act);
72 /** @return The corresponding CycleNode, if exists; otherwise NULL */
73 CycleNode * CycleGraph::getNode_noCreate(const Promise *promise) const
75 return promiseToNode.get(promise);
79 * @brief Returns the CycleNode corresponding to a given ModelAction
81 * Gets (or creates, if none exist) a CycleNode corresponding to a ModelAction
83 * @param action The ModelAction to find a node for
84 * @return The CycleNode paired with this action
86 CycleNode * CycleGraph::getNode(const ModelAction *action)
88 CycleNode *node = getNode_noCreate(action);
90 node = new CycleNode(action);
91 putNode(action, node);
97 * @brief Returns a CycleNode corresponding to a promise
99 * Gets (or creates, if none exist) a CycleNode corresponding to a promised
102 * @param promise The Promise generated by a reader
103 * @return The CycleNode corresponding to the Promise
105 CycleNode * CycleGraph::getNode(const Promise *promise)
107 CycleNode *node = getNode_noCreate(promise);
109 node = new CycleNode(promise);
110 putNode(promise, node);
116 * @return false if the resolution results in a cycle; true otherwise
118 bool CycleGraph::resolvePromise(const Promise *promise, ModelAction *writer,
119 promise_list_t *mustResolve)
121 CycleNode *promise_node = promiseToNode.get(promise);
122 CycleNode *w_node = actionToNode.get(writer);
123 ASSERT(promise_node);
126 return mergeNodes(w_node, promise_node, mustResolve);
127 /* No existing write-node; just convert the promise-node */
128 promise_node->resolvePromise(writer);
129 erasePromiseNode(promise_node->getPromise());
130 putNode(writer, promise_node);
135 * @brief Merge two CycleNodes that represent the same write
137 * Note that this operation cannot be rolled back.
139 * @param w_node The write ModelAction node with which to merge
140 * @param p_node The Promise node to merge. Will be destroyed after this
142 * @param mustMerge Return (pass-by-reference) any additional Promises that
143 * must also be merged with w_node
145 * @return false if the merge results in a cycle; true otherwise
147 bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node,
148 promise_list_t *mustMerge)
150 ASSERT(!w_node->is_promise());
151 ASSERT(p_node->is_promise());
153 const Promise *promise = p_node->getPromise();
154 if (!promise->is_compatible(w_node->getAction())) {
159 /* Transfer the RMW */
160 CycleNode *promise_rmw = p_node->getRMW();
161 if (promise_rmw && promise_rmw != w_node->getRMW() && w_node->setRMW(promise_rmw)) {
166 /* Transfer back edges to w_node */
167 while (p_node->getNumBackEdges() > 0) {
168 CycleNode *back = p_node->removeBackEdge();
171 if (back->is_promise()) {
172 if (checkReachable(w_node, back)) {
173 /* Edge would create cycle; merge instead */
174 mustMerge->push_back(back->getPromise());
175 if (!mergeNodes(w_node, back, mustMerge))
178 back->addEdge(w_node);
180 addNodeEdge(back, w_node);
183 /* Transfer forward edges to w_node */
184 while (p_node->getNumEdges() > 0) {
185 CycleNode *forward = p_node->removeEdge();
186 if (forward == w_node)
188 if (forward->is_promise()) {
189 if (checkReachable(forward, w_node)) {
190 mustMerge->push_back(forward->getPromise());
191 if (!mergeNodes(w_node, forward, mustMerge))
194 w_node->addEdge(forward);
196 addNodeEdge(w_node, forward);
199 erasePromiseNode(promise);
200 /* Not deleting p_node, to maintain consistency if mergeNodes() fails */
206 * Adds an edge between two CycleNodes.
207 * @param fromnode The edge comes from this CycleNode
208 * @param tonode The edge points to this CycleNode
209 * @return True, if new edge(s) are added; otherwise false
211 bool CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
216 hasCycles = checkReachable(tonode, fromnode);
218 if ((added = fromnode->addEdge(tonode)))
219 rollbackvector.push_back(fromnode);
222 * If the fromnode has a rmwnode that is not the tonode, we should add
223 * an edge between its rmwnode and the tonode
225 CycleNode *rmwnode = fromnode->getRMW();
226 if (rmwnode && rmwnode != tonode) {
228 hasCycles = checkReachable(tonode, rmwnode);
230 if (rmwnode->addEdge(tonode)) {
231 rollbackvector.push_back(rmwnode);
239 * @brief Add an edge between a write and the RMW which reads from it
241 * Handles special case of a RMW action, where the ModelAction rmw reads from
242 * the ModelAction/Promise from. The key differences are:
243 * (1) no write can occur in between the rmw and the from action.
244 * (2) Only one RMW action can read from a given write.
246 * @param from The edge comes from this ModelAction/Promise
247 * @param rmw The edge points to this ModelAction; this action must read from
248 * the ModelAction/Promise from
250 template <typename T>
251 void CycleGraph::addRMWEdge(const T *from, const ModelAction *rmw)
256 CycleNode *fromnode = getNode(from);
257 CycleNode *rmwnode = getNode(rmw);
259 /* Two RMW actions cannot read from the same write. */
260 if (fromnode->setRMW(rmwnode))
263 rmwrollbackvector.push_back(fromnode);
265 /* Transfer all outgoing edges from the from node to the rmw node */
266 /* This process should not add a cycle because either:
267 * (1) The rmw should not have any incoming edges yet if it is the
269 * (2) the fromnode is the new node and therefore it should not
270 * have any outgoing edges.
272 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
273 CycleNode *tonode = fromnode->getEdge(i);
274 if (tonode != rmwnode) {
275 if (rmwnode->addEdge(tonode))
276 rollbackvector.push_back(rmwnode);
280 addNodeEdge(fromnode, rmwnode);
282 /* Instantiate two forms of CycleGraph::addRMWEdge */
283 template void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw);
284 template void CycleGraph::addRMWEdge(const Promise *from, const ModelAction *rmw);
287 * @brief Adds an edge between objects
289 * This function will add an edge between any two objects which can be
290 * associated with a CycleNode. That is, if they have a CycleGraph::getNode
293 * The object to is ordered after the object from.
295 * @param to The edge points to this object, of type T
296 * @param from The edge comes from this object, of type U
297 * @return True, if new edge(s) are added; otherwise false
299 template <typename T, typename U>
300 bool CycleGraph::addEdge(const T *from, const U *to)
305 CycleNode *fromnode = getNode(from);
306 CycleNode *tonode = getNode(to);
308 return addNodeEdge(fromnode, tonode);
310 /* Instantiate four forms of CycleGraph::addEdge */
311 template bool CycleGraph::addEdge(const ModelAction *from, const ModelAction *to);
312 template bool CycleGraph::addEdge(const ModelAction *from, const Promise *to);
313 template bool CycleGraph::addEdge(const Promise *from, const ModelAction *to);
314 template bool CycleGraph::addEdge(const Promise *from, const Promise *to);
316 #if SUPPORT_MOD_ORDER_DUMP
318 static void print_node(const CycleNode *node, FILE *file, int label)
321 if (node->is_promise()) {
322 const Promise *promise = node->getPromise();
323 idx = promise->get_action()->get_seq_number();
324 fprintf(file, "P%u", idx);
327 fprintf(file, " [label=\"P%u, T", idx);
328 for (unsigned int i = 0 ; i < model->get_num_threads(); i++)
329 if (promise->thread_is_available(int_to_id(i))) {
330 fprintf(file, "%s%u", first ? "": ",", i);
333 fprintf(file, "\"]");
336 const ModelAction *act = node->getAction();
337 idx = act->get_seq_number();
338 fprintf(file, "N%u", idx);
340 fprintf(file, " [label=\"N%u, T%u\"]", idx, act->get_tid());
344 void CycleGraph::dumpNodes(FILE *file) const
346 for (unsigned int i = 0; i < nodeList.size(); i++) {
347 CycleNode *n = nodeList[i];
348 print_node(n, file, 1);
349 fprintf(file, ";\n");
350 if (n->getRMW() != NULL) {
351 print_node(n, file, 0);
352 fprintf(file, " -> ");
353 print_node(n->getRMW(), file, 0);
354 fprintf(file, "[style=dotted];\n");
356 for (unsigned int j = 0; j < n->getNumEdges(); j++) {
357 print_node(n, file, 0);
358 fprintf(file, " -> ");
359 print_node(n->getEdge(j), file, 0);
360 fprintf(file, ";\n");
365 void CycleGraph::dumpGraphToFile(const char *filename) const
368 sprintf(buffer, "%s.dot", filename);
369 FILE *file = fopen(buffer, "w");
370 fprintf(file, "digraph %s {\n", filename);
372 fprintf(file, "}\n");
378 * Checks whether one CycleNode can reach another.
379 * @param from The CycleNode from which to begin exploration
380 * @param to The CycleNode to reach
381 * @return True, @a from can reach @a to; otherwise, false
383 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
385 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
388 queue.push_back(from);
389 discovered->put(from, from);
390 while (!queue.empty()) {
391 const CycleNode *node = queue.back();
396 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
397 CycleNode *next = node->getEdge(i);
398 if (!discovered->contains(next)) {
399 discovered->put(next, next);
400 queue.push_back(next);
408 * Checks whether one ModelAction/Promise can reach another ModelAction/Promise
409 * @param from The ModelAction or Promise from which to begin exploration
410 * @param to The ModelAction or Promise to reach
411 * @return True, @a from can reach @a to; otherwise, false
413 template <typename T, typename U>
414 bool CycleGraph::checkReachable(const T *from, const U *to) const
416 CycleNode *fromnode = getNode_noCreate(from);
417 CycleNode *tonode = getNode_noCreate(to);
419 if (!fromnode || !tonode)
422 return checkReachable(fromnode, tonode);
424 /* Instantiate three forms of CycleGraph::checkReachable */
425 template bool CycleGraph::checkReachable(const ModelAction *from,
426 const ModelAction *to) const;
427 template bool CycleGraph::checkReachable(const ModelAction *from,
428 const Promise *to) const;
429 template bool CycleGraph::checkReachable(const Promise *from,
430 const ModelAction *to) const;
432 /** @return True, if the promise has failed; false otherwise */
433 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
435 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
437 CycleNode *from = actionToNode.get(fromact);
439 queue.push_back(from);
440 discovered->put(from, from);
441 while (!queue.empty()) {
442 CycleNode *node = queue.back();
445 if (node->getPromise() == promise)
447 if (!node->is_promise() &&
448 promise->eliminate_thread(node->getAction()->get_tid()))
451 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
452 CycleNode *next = node->getEdge(i);
453 if (!discovered->contains(next)) {
454 discovered->put(next, next);
455 queue.push_back(next);
462 void CycleGraph::startChanges()
464 ASSERT(rollbackvector.empty());
465 ASSERT(rmwrollbackvector.empty());
466 ASSERT(oldCycles == hasCycles);
469 /** Commit changes to the cyclegraph. */
470 void CycleGraph::commitChanges()
472 rollbackvector.clear();
473 rmwrollbackvector.clear();
474 oldCycles = hasCycles;
477 /** Rollback changes to the previous commit. */
478 void CycleGraph::rollbackChanges()
480 for (unsigned int i = 0; i < rollbackvector.size(); i++)
481 rollbackvector[i]->removeEdge();
483 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
484 rmwrollbackvector[i]->clearRMW();
486 hasCycles = oldCycles;
487 rollbackvector.clear();
488 rmwrollbackvector.clear();
491 /** @returns whether a CycleGraph contains cycles. */
492 bool CycleGraph::checkForCycles() const
498 * @brief Constructor for a CycleNode
499 * @param act The ModelAction for this node
501 CycleNode::CycleNode(const ModelAction *act) :
509 * @brief Constructor for a Promise CycleNode
510 * @param promise The Promise which was generated
512 CycleNode::CycleNode(const Promise *promise) :
520 * @param i The index of the edge to return
521 * @returns The a CycleNode edge indexed by i
523 CycleNode * CycleNode::getEdge(unsigned int i) const
528 /** @returns The number of edges leaving this CycleNode */
529 unsigned int CycleNode::getNumEdges() const
534 CycleNode * CycleNode::getBackEdge(unsigned int i) const
536 return back_edges[i];
539 unsigned int CycleNode::getNumBackEdges() const
541 return back_edges.size();
545 * @brief Remove an element from a vector
546 * @param v The vector
547 * @param n The element to remove
548 * @return True if the element was found; false otherwise
550 template <typename T>
551 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
553 for (unsigned int i = 0; i < v.size(); i++) {
555 v.erase(v.begin() + i);
563 * @brief Remove a (forward) edge from this CycleNode
564 * @return The CycleNode which was popped, if one exists; otherwise NULL
566 CycleNode * CycleNode::removeEdge()
571 CycleNode *ret = edges.back();
573 vector_remove_node(ret->back_edges, this);
578 * @brief Remove a (back) edge from this CycleNode
579 * @return The CycleNode which was popped, if one exists; otherwise NULL
581 CycleNode * CycleNode::removeBackEdge()
583 if (back_edges.empty())
586 CycleNode *ret = back_edges.back();
587 back_edges.pop_back();
588 vector_remove_node(ret->edges, this);
593 * Adds an edge from this CycleNode to another CycleNode.
594 * @param node The node to which we add a directed edge
595 * @return True if this edge is a new edge; false otherwise
597 bool CycleNode::addEdge(CycleNode *node)
599 for (unsigned int i = 0; i < edges.size(); i++)
600 if (edges[i] == node)
602 edges.push_back(node);
603 node->back_edges.push_back(this);
607 /** @returns the RMW CycleNode that reads from the current CycleNode */
608 CycleNode * CycleNode::getRMW() const
614 * Set a RMW action node that reads from the current CycleNode.
615 * @param node The RMW that reads from the current node
616 * @return True, if this node already was read by another RMW; false otherwise
617 * @see CycleGraph::addRMWEdge
619 bool CycleNode::setRMW(CycleNode *node)
628 * Convert a Promise CycleNode into a concrete-valued CycleNode. Should only be
629 * used when there's no existing ModelAction CycleNode for this write.
631 * @param writer The ModelAction which wrote the future value represented by
634 void CycleNode::resolvePromise(const ModelAction *writer)
636 ASSERT(is_promise());
637 ASSERT(promise->is_compatible(writer));
640 ASSERT(!is_promise());