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 * Resolve/satisfy a Promise with a particular store ModelAction, taking care
117 * of the CycleGraph cleanups, including merging any necessary CycleNodes.
119 * @param promise The Promise to resolve
120 * @param writer The store that will resolve this Promise
121 * @return false if the resolution results in a cycle (or fails in some other
122 * way); true otherwise
124 bool CycleGraph::resolvePromise(const Promise *promise, ModelAction *writer)
126 CycleNode *promise_node = promiseToNode.get(promise);
127 CycleNode *w_node = actionToNode.get(writer);
128 ASSERT(promise_node);
131 return mergeNodes(w_node, promise_node);
132 /* No existing write-node; just convert the promise-node */
133 promise_node->resolvePromise(writer);
134 erasePromiseNode(promise_node->getPromise());
135 putNode(writer, promise_node);
140 * @brief Merge two CycleNodes that represent the same write
142 * Note that this operation cannot be rolled back.
144 * @param w_node The write ModelAction node with which to merge
145 * @param p_node The Promise node to merge. Will be destroyed after this
148 * @return false if the merge cannot succeed; true otherwise
150 bool CycleGraph::mergeNodes(CycleNode *w_node, CycleNode *p_node)
152 ASSERT(!w_node->is_promise());
153 ASSERT(p_node->is_promise());
155 const Promise *promise = p_node->getPromise();
156 if (!promise->is_compatible(w_node->getAction()) ||
157 !promise->same_value(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))
165 /* Transfer back edges to w_node */
166 while (p_node->getNumBackEdges() > 0) {
167 CycleNode *back = p_node->removeBackEdge();
170 addNodeEdge(back, w_node);
175 /* Transfer forward edges to w_node */
176 while (p_node->getNumEdges() > 0) {
177 CycleNode *forward = p_node->removeEdge();
178 if (forward == w_node)
180 addNodeEdge(w_node, forward);
185 erasePromiseNode(promise);
186 /* Not deleting p_node, to maintain consistency if mergeNodes() fails */
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
195 * @return True, if new edge(s) are added; otherwise false
197 bool CycleGraph::addNodeEdge(CycleNode *fromnode, CycleNode *tonode)
202 hasCycles = checkReachable(tonode, fromnode);
204 if ((added = fromnode->addEdge(tonode)))
205 rollbackvector.push_back(fromnode);
208 * If the fromnode has a rmwnode that is not the tonode, we should add
209 * an edge between its rmwnode and the tonode
211 CycleNode *rmwnode = fromnode->getRMW();
212 if (rmwnode && rmwnode != tonode) {
214 hasCycles = checkReachable(tonode, rmwnode);
216 if (rmwnode->addEdge(tonode)) {
217 rollbackvector.push_back(rmwnode);
225 * @brief Add an edge between a write and the RMW which reads from it
227 * Handles special case of a RMW action, where the ModelAction rmw reads from
228 * the ModelAction/Promise from. The key differences are:
229 * (1) no write can occur in between the rmw and the from action.
230 * (2) Only one RMW action can read from a given write.
232 * @param from The edge comes from this ModelAction/Promise
233 * @param rmw The edge points to this ModelAction; this action must read from
234 * the ModelAction/Promise from
236 template <typename T>
237 void CycleGraph::addRMWEdge(const T *from, const ModelAction *rmw)
242 CycleNode *fromnode = getNode(from);
243 CycleNode *rmwnode = getNode(rmw);
245 /* Two RMW actions cannot read from the same write. */
246 if (fromnode->setRMW(rmwnode))
249 rmwrollbackvector.push_back(fromnode);
251 /* Transfer all outgoing edges from the from node to the rmw node */
252 /* This process should not add a cycle because either:
253 * (1) The rmw should not have any incoming edges yet if it is the
255 * (2) the fromnode is the new node and therefore it should not
256 * have any outgoing edges.
258 for (unsigned int i = 0; i < fromnode->getNumEdges(); i++) {
259 CycleNode *tonode = fromnode->getEdge(i);
260 if (tonode != rmwnode) {
261 if (rmwnode->addEdge(tonode))
262 rollbackvector.push_back(rmwnode);
266 addNodeEdge(fromnode, rmwnode);
268 /* Instantiate two forms of CycleGraph::addRMWEdge */
269 template void CycleGraph::addRMWEdge(const ModelAction *from, const ModelAction *rmw);
270 template void CycleGraph::addRMWEdge(const Promise *from, const ModelAction *rmw);
273 * @brief Adds an edge between objects
275 * This function will add an edge between any two objects which can be
276 * associated with a CycleNode. That is, if they have a CycleGraph::getNode
279 * The object to is ordered after the object from.
281 * @param to The edge points to this object, of type T
282 * @param from The edge comes from this object, of type U
283 * @return True, if new edge(s) are added; otherwise false
285 template <typename T, typename U>
286 bool CycleGraph::addEdge(const T *from, const U *to)
291 CycleNode *fromnode = getNode(from);
292 CycleNode *tonode = getNode(to);
294 return addNodeEdge(fromnode, tonode);
296 /* Instantiate four forms of CycleGraph::addEdge */
297 template bool CycleGraph::addEdge(const ModelAction *from, const ModelAction *to);
298 template bool CycleGraph::addEdge(const ModelAction *from, const Promise *to);
299 template bool CycleGraph::addEdge(const Promise *from, const ModelAction *to);
300 template bool CycleGraph::addEdge(const Promise *from, const Promise *to);
302 #if SUPPORT_MOD_ORDER_DUMP
304 static void print_node(FILE *file, const CycleNode *node, int label)
306 if (node->is_promise()) {
307 const Promise *promise = node->getPromise();
308 int idx = model->get_promise_number(promise);
309 fprintf(file, "P%u", idx);
312 fprintf(file, " [label=\"P%d, T", idx);
313 for (unsigned int i = 0 ; i < model->get_num_threads(); i++)
314 if (promise->thread_is_available(int_to_id(i))) {
315 fprintf(file, "%s%u", first ? "": ",", i);
318 fprintf(file, "\"]");
321 const ModelAction *act = node->getAction();
322 modelclock_t idx = act->get_seq_number();
323 fprintf(file, "N%u", idx);
325 fprintf(file, " [label=\"N%u, T%u\"]", idx, act->get_tid());
329 static void print_edge(FILE *file, const CycleNode *from, const CycleNode *to, const char *prop)
331 print_node(file, from, 0);
332 fprintf(file, " -> ");
333 print_node(file, to, 0);
334 if (prop && strlen(prop))
335 fprintf(file, " [%s]", prop);
336 fprintf(file, ";\n");
339 void CycleGraph::dot_print_node(FILE *file, const ModelAction *act)
341 print_node(file, getNode(act), 1);
344 template <typename T, typename U>
345 void CycleGraph::dot_print_edge(FILE *file, const T *from, const U *to, const char *prop)
347 CycleNode *fromnode = getNode(from);
348 CycleNode *tonode = getNode(to);
350 print_edge(file, fromnode, tonode, prop);
352 /* Instantiate two forms of CycleGraph::dot_print_edge */
353 template void CycleGraph::dot_print_edge(FILE *file, const Promise *from, const ModelAction *to, const char *prop);
354 template void CycleGraph::dot_print_edge(FILE *file, const ModelAction *from, const ModelAction *to, const char *prop);
356 void CycleGraph::dumpNodes(FILE *file) const
358 for (unsigned int i = 0; i < nodeList.size(); i++) {
359 CycleNode *n = nodeList[i];
360 print_node(file, n, 1);
361 fprintf(file, ";\n");
363 print_edge(file, n, n->getRMW(), "style=dotted");
364 for (unsigned int j = 0; j < n->getNumEdges(); j++)
365 print_edge(file, n, n->getEdge(j), NULL);
369 void CycleGraph::dumpGraphToFile(const char *filename) const
372 sprintf(buffer, "%s.dot", filename);
373 FILE *file = fopen(buffer, "w");
374 fprintf(file, "digraph %s {\n", filename);
376 fprintf(file, "}\n");
382 * Checks whether one CycleNode can reach another.
383 * @param from The CycleNode from which to begin exploration
384 * @param to The CycleNode to reach
385 * @return True, @a from can reach @a to; otherwise, false
387 bool CycleGraph::checkReachable(const CycleNode *from, const CycleNode *to) const
389 std::vector< const CycleNode *, ModelAlloc<const CycleNode *> > queue;
392 queue.push_back(from);
393 discovered->put(from, from);
394 while (!queue.empty()) {
395 const CycleNode *node = queue.back();
400 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
401 CycleNode *next = node->getEdge(i);
402 if (!discovered->contains(next)) {
403 discovered->put(next, next);
404 queue.push_back(next);
412 * Checks whether one ModelAction/Promise can reach another ModelAction/Promise
413 * @param from The ModelAction or Promise from which to begin exploration
414 * @param to The ModelAction or Promise to reach
415 * @return True, @a from can reach @a to; otherwise, false
417 template <typename T, typename U>
418 bool CycleGraph::checkReachable(const T *from, const U *to) const
420 CycleNode *fromnode = getNode_noCreate(from);
421 CycleNode *tonode = getNode_noCreate(to);
423 if (!fromnode || !tonode)
426 return checkReachable(fromnode, tonode);
428 /* Instantiate three forms of CycleGraph::checkReachable */
429 template bool CycleGraph::checkReachable(const ModelAction *from,
430 const ModelAction *to) const;
431 template bool CycleGraph::checkReachable(const ModelAction *from,
432 const Promise *to) const;
433 template bool CycleGraph::checkReachable(const Promise *from,
434 const ModelAction *to) const;
436 /** @return True, if the promise has failed; false otherwise */
437 bool CycleGraph::checkPromise(const ModelAction *fromact, Promise *promise) const
439 std::vector< CycleNode *, ModelAlloc<CycleNode *> > queue;
441 CycleNode *from = actionToNode.get(fromact);
443 queue.push_back(from);
444 discovered->put(from, from);
445 while (!queue.empty()) {
446 CycleNode *node = queue.back();
449 if (node->getPromise() == promise)
451 if (!node->is_promise() &&
452 promise->eliminate_thread(node->getAction()->get_tid()))
455 for (unsigned int i = 0; i < node->getNumEdges(); i++) {
456 CycleNode *next = node->getEdge(i);
457 if (!discovered->contains(next)) {
458 discovered->put(next, next);
459 queue.push_back(next);
466 void CycleGraph::startChanges()
468 ASSERT(rollbackvector.empty());
469 ASSERT(rmwrollbackvector.empty());
470 ASSERT(oldCycles == hasCycles);
473 /** Commit changes to the cyclegraph. */
474 void CycleGraph::commitChanges()
476 rollbackvector.clear();
477 rmwrollbackvector.clear();
478 oldCycles = hasCycles;
481 /** Rollback changes to the previous commit. */
482 void CycleGraph::rollbackChanges()
484 for (unsigned int i = 0; i < rollbackvector.size(); i++)
485 rollbackvector[i]->removeEdge();
487 for (unsigned int i = 0; i < rmwrollbackvector.size(); i++)
488 rmwrollbackvector[i]->clearRMW();
490 hasCycles = oldCycles;
491 rollbackvector.clear();
492 rmwrollbackvector.clear();
495 /** @returns whether a CycleGraph contains cycles. */
496 bool CycleGraph::checkForCycles() const
502 * @brief Constructor for a CycleNode
503 * @param act The ModelAction for this node
505 CycleNode::CycleNode(const ModelAction *act) :
513 * @brief Constructor for a Promise CycleNode
514 * @param promise The Promise which was generated
516 CycleNode::CycleNode(const Promise *promise) :
524 * @param i The index of the edge to return
525 * @returns The a CycleNode edge indexed by i
527 CycleNode * CycleNode::getEdge(unsigned int i) const
532 /** @returns The number of edges leaving this CycleNode */
533 unsigned int CycleNode::getNumEdges() const
538 CycleNode * CycleNode::getBackEdge(unsigned int i) const
540 return back_edges[i];
543 unsigned int CycleNode::getNumBackEdges() const
545 return back_edges.size();
549 * @brief Remove an element from a vector
550 * @param v The vector
551 * @param n The element to remove
552 * @return True if the element was found; false otherwise
554 template <typename T>
555 static bool vector_remove_node(std::vector<T, SnapshotAlloc<T> >& v, const T n)
557 for (unsigned int i = 0; i < v.size(); i++) {
559 v.erase(v.begin() + i);
567 * @brief Remove a (forward) edge from this CycleNode
568 * @return The CycleNode which was popped, if one exists; otherwise NULL
570 CycleNode * CycleNode::removeEdge()
575 CycleNode *ret = edges.back();
577 vector_remove_node(ret->back_edges, this);
582 * @brief Remove a (back) edge from this CycleNode
583 * @return The CycleNode which was popped, if one exists; otherwise NULL
585 CycleNode * CycleNode::removeBackEdge()
587 if (back_edges.empty())
590 CycleNode *ret = back_edges.back();
591 back_edges.pop_back();
592 vector_remove_node(ret->edges, this);
597 * Adds an edge from this CycleNode to another CycleNode.
598 * @param node The node to which we add a directed edge
599 * @return True if this edge is a new edge; false otherwise
601 bool CycleNode::addEdge(CycleNode *node)
603 for (unsigned int i = 0; i < edges.size(); i++)
604 if (edges[i] == node)
606 edges.push_back(node);
607 node->back_edges.push_back(this);
611 /** @returns the RMW CycleNode that reads from the current CycleNode */
612 CycleNode * CycleNode::getRMW() const
618 * Set a RMW action node that reads from the current CycleNode.
619 * @param node The RMW that reads from the current node
620 * @return True, if this node already was read by another RMW; false otherwise
621 * @see CycleGraph::addRMWEdge
623 bool CycleNode::setRMW(CycleNode *node)
632 * Convert a Promise CycleNode into a concrete-valued CycleNode. Should only be
633 * used when there's no existing ModelAction CycleNode for this write.
635 * @param writer The ModelAction which wrote the future value represented by
638 void CycleNode::resolvePromise(const ModelAction *writer)
640 ASSERT(is_promise());
641 ASSERT(promise->is_compatible(writer));
644 ASSERT(!is_promise());