blob: d6eeed2bd11c22140834bb5f5b2279bc457ce0dd [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
Eric Caruso8a51cd72018-01-23 16:11:04 -080021#include <limits>
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080022#include <set>
Alex Vakulenko072359c2014-07-18 11:41:07 -070023#include <string>
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080024#include <utility>
Alex Deymo161c4a12014-05-16 15:56:21 -070025
Eric Carusoc0cfb7d2018-01-23 16:04:06 -080026#include <base/stl_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070027#include <base/strings/string_util.h>
28#include <base/strings/stringprintf.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070029
30#include "update_engine/payload_generator/graph_utils.h"
31#include "update_engine/payload_generator/tarjan.h"
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080032
33using std::make_pair;
34using std::set;
35using std::vector;
36
37namespace chromeos_update_engine {
38
39// This is the outer function from the original paper.
40void CycleBreaker::BreakCycles(const Graph& graph, set<Edge>* out_cut_edges) {
41 cut_edges_.clear();
Alex Deymo161c4a12014-05-16 15:56:21 -070042
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080043 // Make a copy, which we will modify by removing edges. Thus, in each
44 // iteration subgraph_ is the current subgraph or the original with
45 // vertices we desire. This variable was "A_K" in the original paper.
46 subgraph_ = graph;
Alex Deymo161c4a12014-05-16 15:56:21 -070047
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080048 // The paper calls for the "adjacency structure (i.e., graph) of
49 // strong (-ly connected) component K with least vertex in subgraph
50 // induced by {s, s + 1, ..., n}".
51 // We arbitrarily order each vertex by its index in the graph. Thus,
52 // each iteration, we are looking at the subgraph {s, s + 1, ..., n}
53 // and looking for the strongly connected component with vertex s.
54
55 TarjanAlgorithm tarjan;
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070056 skipped_ops_ = 0;
Alex Deymo161c4a12014-05-16 15:56:21 -070057
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080058 for (Graph::size_type i = 0; i < subgraph_.size(); i++) {
Sen Jiangcebb6882019-02-26 16:23:28 +080059 InstallOperation::Type op_type = graph[i].aop.op.type();
Alex Deymoa12ee112015-08-12 22:19:32 -070060 if (op_type == InstallOperation::REPLACE ||
61 op_type == InstallOperation::REPLACE_BZ) {
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070062 skipped_ops_++;
63 continue;
64 }
65
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080066 if (i > 0) {
67 // Erase node (i - 1) from subgraph_. First, erase what it points to
68 subgraph_[i - 1].out_edges.clear();
69 // Now, erase any pointers to node (i - 1)
70 for (Graph::size_type j = i; j < subgraph_.size(); j++) {
71 subgraph_[j].out_edges.erase(i - 1);
72 }
73 }
74
75 // Calculate SCC (strongly connected component) with vertex i.
76 vector<Vertex::Index> component_indexes;
77 tarjan.Execute(i, &subgraph_, &component_indexes);
78
79 // Set subgraph edges for the components in the SCC.
80 for (vector<Vertex::Index>::iterator it = component_indexes.begin();
Amin Hassani232f8f92019-01-14 16:15:31 -080081 it != component_indexes.end();
82 ++it) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080083 subgraph_[*it].subgraph_edges.clear();
84 for (vector<Vertex::Index>::iterator jt = component_indexes.begin();
Amin Hassani232f8f92019-01-14 16:15:31 -080085 jt != component_indexes.end();
86 ++jt) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080087 // If there's a link from *it -> *jt in the graph,
88 // add a subgraph_ edge
Eric Carusoc0cfb7d2018-01-23 16:04:06 -080089 if (base::ContainsKey(subgraph_[*it].out_edges, *jt))
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080090 subgraph_[*it].subgraph_edges.insert(*jt);
Alex Deymo161c4a12014-05-16 15:56:21 -070091 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080092 }
93
94 current_vertex_ = i;
95 blocked_.clear();
96 blocked_.resize(subgraph_.size());
97 blocked_graph_.clear();
98 blocked_graph_.resize(subgraph_.size());
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070099 Circuit(current_vertex_, 0);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800100 }
Alex Deymo161c4a12014-05-16 15:56:21 -0700101
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800102 out_cut_edges->swap(cut_edges_);
Andrew de los Reyes182a9f52010-10-05 16:33:51 -0700103 LOG(INFO) << "Cycle breaker skipped " << skipped_ops_ << " ops.";
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800104 DCHECK(stack_.empty());
105}
106
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700107static const size_t kMaxEdgesToConsider = 2;
108
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800109void CycleBreaker::HandleCircuit() {
110 stack_.push_back(current_vertex_);
Amin Hassani232f8f92019-01-14 16:15:31 -0800111 CHECK_GE(stack_.size(), static_cast<vector<Vertex::Index>::size_type>(2));
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800112 Edge min_edge = make_pair(stack_[0], stack_[1]);
Alex Vakulenko0103c362016-01-20 07:56:15 -0800113 uint64_t min_edge_weight = std::numeric_limits<uint64_t>::max();
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700114 size_t edges_considered = 0;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800115 for (vector<Vertex::Index>::const_iterator it = stack_.begin();
Amin Hassani232f8f92019-01-14 16:15:31 -0800116 it != (stack_.end() - 1);
117 ++it) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800118 Edge edge = make_pair(*it, *(it + 1));
119 if (cut_edges_.find(edge) != cut_edges_.end()) {
120 stack_.pop_back();
121 return;
122 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700123 uint64_t edge_weight = graph_utils::EdgeWeight(subgraph_, edge);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800124 if (edge_weight < min_edge_weight) {
125 min_edge_weight = edge_weight;
126 min_edge = edge;
127 }
Andrew de los Reyes45109892010-07-26 13:49:31 -0700128 edges_considered++;
129 if (edges_considered == kMaxEdgesToConsider)
130 break;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800131 }
132 cut_edges_.insert(min_edge);
133 stack_.pop_back();
134}
135
136void CycleBreaker::Unblock(Vertex::Index u) {
137 blocked_[u] = false;
138
139 for (Vertex::EdgeMap::iterator it = blocked_graph_[u].out_edges.begin();
Amin Hassani232f8f92019-01-14 16:15:31 -0800140 it != blocked_graph_[u].out_edges.end();) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800141 Vertex::Index w = it->first;
142 blocked_graph_[u].out_edges.erase(it++);
143 if (blocked_[w])
144 Unblock(w);
145 }
146}
147
Andrew de los Reyes45109892010-07-26 13:49:31 -0700148bool CycleBreaker::StackContainsCutEdge() const {
Alex Deymof329b932014-10-30 01:37:48 -0700149 for (vector<Vertex::Index>::const_iterator it = ++stack_.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800150 e = stack_.end();
151 it != e;
152 ++it) {
Andrew de los Reyes45109892010-07-26 13:49:31 -0700153 Edge edge = make_pair(*(it - 1), *it);
Eric Carusoc0cfb7d2018-01-23 16:04:06 -0800154 if (base::ContainsKey(cut_edges_, edge)) {
Andrew de los Reyes45109892010-07-26 13:49:31 -0700155 return true;
156 }
157 }
158 return false;
159}
160
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700161bool CycleBreaker::Circuit(Vertex::Index vertex, Vertex::Index depth) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800162 // "vertex" was "v" in the original paper.
163 bool found = false; // Was "f" in the original paper.
164 stack_.push_back(vertex);
165 blocked_[vertex] = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700166 {
167 static int counter = 0;
168 counter++;
169 if (counter == 10000) {
170 counter = 0;
171 std::string stack_str;
Alex Vakulenko072359c2014-07-18 11:41:07 -0700172 for (Vertex::Index index : stack_) {
173 stack_str += std::to_string(index);
174 stack_str += " -> ";
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700175 }
176 LOG(INFO) << "stack: " << stack_str;
177 }
178 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800179
180 for (Vertex::SubgraphEdgeMap::iterator w =
181 subgraph_[vertex].subgraph_edges.begin();
Amin Hassani232f8f92019-01-14 16:15:31 -0800182 w != subgraph_[vertex].subgraph_edges.end();
183 ++w) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800184 if (*w == current_vertex_) {
185 // The original paper called for printing stack_ followed by
186 // current_vertex_ here, which is a cycle. Instead, we call
187 // HandleCircuit() to break it.
188 HandleCircuit();
189 found = true;
190 } else if (!blocked_[*w]) {
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700191 if (Circuit(*w, depth + 1)) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800192 found = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700193 if ((depth > kMaxEdgesToConsider) || StackContainsCutEdge())
Andrew de los Reyes45109892010-07-26 13:49:31 -0700194 break;
195 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800196 }
197 }
198
199 if (found) {
200 Unblock(vertex);
201 } else {
202 for (Vertex::SubgraphEdgeMap::iterator w =
203 subgraph_[vertex].subgraph_edges.begin();
Amin Hassani232f8f92019-01-14 16:15:31 -0800204 w != subgraph_[vertex].subgraph_edges.end();
205 ++w) {
Andrew de los Reyes8a51e5c2010-07-26 13:40:03 -0700206 if (blocked_graph_[*w].out_edges.find(vertex) ==
207 blocked_graph_[*w].out_edges.end()) {
Amin Hassani232f8f92019-01-14 16:15:31 -0800208 blocked_graph_[*w].out_edges.insert(
209 make_pair(vertex, EdgeProperties()));
Andrew de los Reyes8a51e5c2010-07-26 13:40:03 -0700210 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800211 }
212 }
213 CHECK_EQ(vertex, stack_.back());
214 stack_.pop_back();
215 return found;
216}
217
218} // namespace chromeos_update_engine