blob: 840036667a01f3a64372e93381370c9ed9676e2b [file] [log] [blame]
Andrew de los Reyes35a7af12010-03-10 16:33:26 -08001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/cycle_breaker.h"
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -07006#include <inttypes.h>
Andrew de los Reyes35a7af12010-03-10 16:33:26 -08007#include <set>
8#include <utility>
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -07009#include "base/string_util.h"
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080010#include "update_engine/graph_utils.h"
11#include "update_engine/tarjan.h"
12#include "update_engine/utils.h"
13
14using std::make_pair;
15using std::set;
16using std::vector;
17
18namespace chromeos_update_engine {
19
20// This is the outer function from the original paper.
21void CycleBreaker::BreakCycles(const Graph& graph, set<Edge>* out_cut_edges) {
22 cut_edges_.clear();
23
24 // Make a copy, which we will modify by removing edges. Thus, in each
25 // iteration subgraph_ is the current subgraph or the original with
26 // vertices we desire. This variable was "A_K" in the original paper.
27 subgraph_ = graph;
28
29 // The paper calls for the "adjacency structure (i.e., graph) of
30 // strong (-ly connected) component K with least vertex in subgraph
31 // induced by {s, s + 1, ..., n}".
32 // We arbitrarily order each vertex by its index in the graph. Thus,
33 // each iteration, we are looking at the subgraph {s, s + 1, ..., n}
34 // and looking for the strongly connected component with vertex s.
35
36 TarjanAlgorithm tarjan;
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070037 skipped_ops_ = 0;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080038
39 for (Graph::size_type i = 0; i < subgraph_.size(); i++) {
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070040 DeltaArchiveManifest_InstallOperation_Type op_type = graph[i].op.type();
41 if (op_type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
42 op_type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
43 skipped_ops_++;
44 continue;
45 }
46
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080047 if (i > 0) {
48 // Erase node (i - 1) from subgraph_. First, erase what it points to
49 subgraph_[i - 1].out_edges.clear();
50 // Now, erase any pointers to node (i - 1)
51 for (Graph::size_type j = i; j < subgraph_.size(); j++) {
52 subgraph_[j].out_edges.erase(i - 1);
53 }
54 }
55
56 // Calculate SCC (strongly connected component) with vertex i.
57 vector<Vertex::Index> component_indexes;
58 tarjan.Execute(i, &subgraph_, &component_indexes);
59
60 // Set subgraph edges for the components in the SCC.
61 for (vector<Vertex::Index>::iterator it = component_indexes.begin();
62 it != component_indexes.end(); ++it) {
63 subgraph_[*it].subgraph_edges.clear();
64 for (vector<Vertex::Index>::iterator jt = component_indexes.begin();
65 jt != component_indexes.end(); ++jt) {
66 // If there's a link from *it -> *jt in the graph,
67 // add a subgraph_ edge
68 if (utils::MapContainsKey(subgraph_[*it].out_edges, *jt))
69 subgraph_[*it].subgraph_edges.insert(*jt);
70 }
71 }
72
73 current_vertex_ = i;
74 blocked_.clear();
75 blocked_.resize(subgraph_.size());
76 blocked_graph_.clear();
77 blocked_graph_.resize(subgraph_.size());
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070078 Circuit(current_vertex_, 0);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080079 }
80
81 out_cut_edges->swap(cut_edges_);
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070082 LOG(INFO) << "Cycle breaker skipped " << skipped_ops_ << " ops.";
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080083 DCHECK(stack_.empty());
84}
85
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070086static const size_t kMaxEdgesToConsider = 2;
87
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080088void CycleBreaker::HandleCircuit() {
89 stack_.push_back(current_vertex_);
Mike Frysinger0f9547d2012-02-16 12:11:37 -050090 CHECK_GE(stack_.size(),
91 static_cast<std::vector<Vertex::Index>::size_type>(2));
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080092 Edge min_edge = make_pair(stack_[0], stack_[1]);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070093 uint64_t min_edge_weight = kuint64max;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070094 size_t edges_considered = 0;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080095 for (vector<Vertex::Index>::const_iterator it = stack_.begin();
96 it != (stack_.end() - 1); ++it) {
97 Edge edge = make_pair(*it, *(it + 1));
98 if (cut_edges_.find(edge) != cut_edges_.end()) {
99 stack_.pop_back();
100 return;
101 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700102 uint64_t edge_weight = graph_utils::EdgeWeight(subgraph_, edge);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800103 if (edge_weight < min_edge_weight) {
104 min_edge_weight = edge_weight;
105 min_edge = edge;
106 }
Andrew de los Reyes45109892010-07-26 13:49:31 -0700107 edges_considered++;
108 if (edges_considered == kMaxEdgesToConsider)
109 break;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800110 }
111 cut_edges_.insert(min_edge);
112 stack_.pop_back();
113}
114
115void CycleBreaker::Unblock(Vertex::Index u) {
116 blocked_[u] = false;
117
118 for (Vertex::EdgeMap::iterator it = blocked_graph_[u].out_edges.begin();
119 it != blocked_graph_[u].out_edges.end(); ) {
120 Vertex::Index w = it->first;
121 blocked_graph_[u].out_edges.erase(it++);
122 if (blocked_[w])
123 Unblock(w);
124 }
125}
126
Andrew de los Reyes45109892010-07-26 13:49:31 -0700127bool CycleBreaker::StackContainsCutEdge() const {
128 for (std::vector<Vertex::Index>::const_iterator it = ++stack_.begin(),
Andrew de los Reyes182a9f52010-10-05 16:33:51 -0700129 e = stack_.end(); it != e; ++it) {
Andrew de los Reyes45109892010-07-26 13:49:31 -0700130 Edge edge = make_pair(*(it - 1), *it);
131 if (utils::SetContainsKey(cut_edges_, edge)) {
132 return true;
133 }
134 }
135 return false;
136}
137
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700138bool CycleBreaker::Circuit(Vertex::Index vertex, Vertex::Index depth) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800139 // "vertex" was "v" in the original paper.
140 bool found = false; // Was "f" in the original paper.
141 stack_.push_back(vertex);
142 blocked_[vertex] = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700143 {
144 static int counter = 0;
145 counter++;
146 if (counter == 10000) {
147 counter = 0;
148 std::string stack_str;
149 for (vector<Vertex::Index>::const_iterator it = stack_.begin();
150 it != stack_.end(); ++it) {
151 stack_str += StringPrintf("%lu -> ",
152 static_cast<long unsigned int>(*it));
153 }
154 LOG(INFO) << "stack: " << stack_str;
155 }
156 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800157
158 for (Vertex::SubgraphEdgeMap::iterator w =
159 subgraph_[vertex].subgraph_edges.begin();
160 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
161 if (*w == current_vertex_) {
162 // The original paper called for printing stack_ followed by
163 // current_vertex_ here, which is a cycle. Instead, we call
164 // HandleCircuit() to break it.
165 HandleCircuit();
166 found = true;
167 } else if (!blocked_[*w]) {
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700168 if (Circuit(*w, depth + 1)) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800169 found = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700170 if ((depth > kMaxEdgesToConsider) || StackContainsCutEdge())
Andrew de los Reyes45109892010-07-26 13:49:31 -0700171 break;
172 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800173 }
174 }
175
176 if (found) {
177 Unblock(vertex);
178 } else {
179 for (Vertex::SubgraphEdgeMap::iterator w =
180 subgraph_[vertex].subgraph_edges.begin();
181 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
Andrew de los Reyes8a51e5c2010-07-26 13:40:03 -0700182 if (blocked_graph_[*w].out_edges.find(vertex) ==
183 blocked_graph_[*w].out_edges.end()) {
184 blocked_graph_[*w].out_edges.insert(make_pair(vertex,
185 EdgeProperties()));
186 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800187 }
188 }
189 CHECK_EQ(vertex, stack_.back());
190 stack_.pop_back();
191 return found;
192}
193
194} // namespace chromeos_update_engine