blob: 52a6f60502dd325ca32b94b96f219b2e0654ac46 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080016
Alex Deymo161c4a12014-05-16 15:56:21 -070017#include "update_engine/payload_generator/cycle_breaker.h"
18
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070019#include <inttypes.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070020
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080021#include <set>
Alex Vakulenko072359c2014-07-18 11:41:07 -070022#include <string>
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080023#include <utility>
Alex Deymo161c4a12014-05-16 15:56:21 -070024
Alex Vakulenko75039d72014-03-25 12:36:28 -070025#include <base/strings/string_util.h>
26#include <base/strings/stringprintf.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/utils.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070029#include "update_engine/payload_generator/graph_utils.h"
30#include "update_engine/payload_generator/tarjan.h"
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080031
32using std::make_pair;
33using std::set;
34using std::vector;
35
36namespace chromeos_update_engine {
37
38// This is the outer function from the original paper.
39void CycleBreaker::BreakCycles(const Graph& graph, set<Edge>* out_cut_edges) {
40 cut_edges_.clear();
Alex Deymo161c4a12014-05-16 15:56:21 -070041
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080042 // Make a copy, which we will modify by removing edges. Thus, in each
43 // iteration subgraph_ is the current subgraph or the original with
44 // vertices we desire. This variable was "A_K" in the original paper.
45 subgraph_ = graph;
Alex Deymo161c4a12014-05-16 15:56:21 -070046
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080047 // The paper calls for the "adjacency structure (i.e., graph) of
48 // strong (-ly connected) component K with least vertex in subgraph
49 // induced by {s, s + 1, ..., n}".
50 // We arbitrarily order each vertex by its index in the graph. Thus,
51 // each iteration, we are looking at the subgraph {s, s + 1, ..., n}
52 // and looking for the strongly connected component with vertex s.
53
54 TarjanAlgorithm tarjan;
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070055 skipped_ops_ = 0;
Alex Deymo161c4a12014-05-16 15:56:21 -070056
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080057 for (Graph::size_type i = 0; i < subgraph_.size(); i++) {
Alex Deymoa12ee112015-08-12 22:19:32 -070058 InstallOperation_Type op_type = graph[i].aop.op.type();
59 if (op_type == InstallOperation::REPLACE ||
60 op_type == InstallOperation::REPLACE_BZ) {
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070061 skipped_ops_++;
62 continue;
63 }
64
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080065 if (i > 0) {
66 // Erase node (i - 1) from subgraph_. First, erase what it points to
67 subgraph_[i - 1].out_edges.clear();
68 // Now, erase any pointers to node (i - 1)
69 for (Graph::size_type j = i; j < subgraph_.size(); j++) {
70 subgraph_[j].out_edges.erase(i - 1);
71 }
72 }
73
74 // Calculate SCC (strongly connected component) with vertex i.
75 vector<Vertex::Index> component_indexes;
76 tarjan.Execute(i, &subgraph_, &component_indexes);
77
78 // Set subgraph edges for the components in the SCC.
79 for (vector<Vertex::Index>::iterator it = component_indexes.begin();
80 it != component_indexes.end(); ++it) {
81 subgraph_[*it].subgraph_edges.clear();
82 for (vector<Vertex::Index>::iterator jt = component_indexes.begin();
83 jt != component_indexes.end(); ++jt) {
84 // If there's a link from *it -> *jt in the graph,
85 // add a subgraph_ edge
86 if (utils::MapContainsKey(subgraph_[*it].out_edges, *jt))
87 subgraph_[*it].subgraph_edges.insert(*jt);
Alex Deymo161c4a12014-05-16 15:56:21 -070088 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080089 }
90
91 current_vertex_ = i;
92 blocked_.clear();
93 blocked_.resize(subgraph_.size());
94 blocked_graph_.clear();
95 blocked_graph_.resize(subgraph_.size());
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070096 Circuit(current_vertex_, 0);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080097 }
Alex Deymo161c4a12014-05-16 15:56:21 -070098
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080099 out_cut_edges->swap(cut_edges_);
Andrew de los Reyes182a9f52010-10-05 16:33:51 -0700100 LOG(INFO) << "Cycle breaker skipped " << skipped_ops_ << " ops.";
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800101 DCHECK(stack_.empty());
102}
103
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700104static const size_t kMaxEdgesToConsider = 2;
105
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800106void CycleBreaker::HandleCircuit() {
107 stack_.push_back(current_vertex_);
Mike Frysinger0f9547d2012-02-16 12:11:37 -0500108 CHECK_GE(stack_.size(),
Alex Deymof329b932014-10-30 01:37:48 -0700109 static_cast<vector<Vertex::Index>::size_type>(2));
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800110 Edge min_edge = make_pair(stack_[0], stack_[1]);
Alex Vakulenko0103c362016-01-20 07:56:15 -0800111 uint64_t min_edge_weight = std::numeric_limits<uint64_t>::max();
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700112 size_t edges_considered = 0;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800113 for (vector<Vertex::Index>::const_iterator it = stack_.begin();
114 it != (stack_.end() - 1); ++it) {
115 Edge edge = make_pair(*it, *(it + 1));
116 if (cut_edges_.find(edge) != cut_edges_.end()) {
117 stack_.pop_back();
118 return;
119 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700120 uint64_t edge_weight = graph_utils::EdgeWeight(subgraph_, edge);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800121 if (edge_weight < min_edge_weight) {
122 min_edge_weight = edge_weight;
123 min_edge = edge;
124 }
Andrew de los Reyes45109892010-07-26 13:49:31 -0700125 edges_considered++;
126 if (edges_considered == kMaxEdgesToConsider)
127 break;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800128 }
129 cut_edges_.insert(min_edge);
130 stack_.pop_back();
131}
132
133void CycleBreaker::Unblock(Vertex::Index u) {
134 blocked_[u] = false;
135
136 for (Vertex::EdgeMap::iterator it = blocked_graph_[u].out_edges.begin();
137 it != blocked_graph_[u].out_edges.end(); ) {
138 Vertex::Index w = it->first;
139 blocked_graph_[u].out_edges.erase(it++);
140 if (blocked_[w])
141 Unblock(w);
142 }
143}
144
Andrew de los Reyes45109892010-07-26 13:49:31 -0700145bool CycleBreaker::StackContainsCutEdge() const {
Alex Deymof329b932014-10-30 01:37:48 -0700146 for (vector<Vertex::Index>::const_iterator it = ++stack_.begin(),
Andrew de los Reyes182a9f52010-10-05 16:33:51 -0700147 e = stack_.end(); it != e; ++it) {
Andrew de los Reyes45109892010-07-26 13:49:31 -0700148 Edge edge = make_pair(*(it - 1), *it);
149 if (utils::SetContainsKey(cut_edges_, edge)) {
150 return true;
151 }
152 }
153 return false;
154}
155
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700156bool CycleBreaker::Circuit(Vertex::Index vertex, Vertex::Index depth) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800157 // "vertex" was "v" in the original paper.
158 bool found = false; // Was "f" in the original paper.
159 stack_.push_back(vertex);
160 blocked_[vertex] = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700161 {
162 static int counter = 0;
163 counter++;
164 if (counter == 10000) {
165 counter = 0;
166 std::string stack_str;
Alex Vakulenko072359c2014-07-18 11:41:07 -0700167 for (Vertex::Index index : stack_) {
168 stack_str += std::to_string(index);
169 stack_str += " -> ";
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700170 }
171 LOG(INFO) << "stack: " << stack_str;
172 }
173 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800174
175 for (Vertex::SubgraphEdgeMap::iterator w =
176 subgraph_[vertex].subgraph_edges.begin();
177 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
178 if (*w == current_vertex_) {
179 // The original paper called for printing stack_ followed by
180 // current_vertex_ here, which is a cycle. Instead, we call
181 // HandleCircuit() to break it.
182 HandleCircuit();
183 found = true;
184 } else if (!blocked_[*w]) {
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700185 if (Circuit(*w, depth + 1)) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800186 found = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700187 if ((depth > kMaxEdgesToConsider) || StackContainsCutEdge())
Andrew de los Reyes45109892010-07-26 13:49:31 -0700188 break;
189 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800190 }
191 }
192
193 if (found) {
194 Unblock(vertex);
195 } else {
196 for (Vertex::SubgraphEdgeMap::iterator w =
197 subgraph_[vertex].subgraph_edges.begin();
198 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
Andrew de los Reyes8a51e5c2010-07-26 13:40:03 -0700199 if (blocked_graph_[*w].out_edges.find(vertex) ==
200 blocked_graph_[*w].out_edges.end()) {
201 blocked_graph_[*w].out_edges.insert(make_pair(vertex,
202 EdgeProperties()));
203 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800204 }
205 }
206 CHECK_EQ(vertex, stack_.back());
207 stack_.pop_back();
208 return found;
209}
210
211} // namespace chromeos_update_engine