blob: a8a04abfe599c57fcde5cef07c7391920ff4c018 [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++) {
Alex Deymoa12ee112015-08-12 22:19:32 -070059 InstallOperation_Type op_type = graph[i].aop.op.type();
60 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();
81 it != component_indexes.end(); ++it) {
82 subgraph_[*it].subgraph_edges.clear();
83 for (vector<Vertex::Index>::iterator jt = component_indexes.begin();
84 jt != component_indexes.end(); ++jt) {
85 // If there's a link from *it -> *jt in the graph,
86 // add a subgraph_ edge
Eric Carusoc0cfb7d2018-01-23 16:04:06 -080087 if (base::ContainsKey(subgraph_[*it].out_edges, *jt))
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080088 subgraph_[*it].subgraph_edges.insert(*jt);
Alex Deymo161c4a12014-05-16 15:56:21 -070089 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080090 }
91
92 current_vertex_ = i;
93 blocked_.clear();
94 blocked_.resize(subgraph_.size());
95 blocked_graph_.clear();
96 blocked_graph_.resize(subgraph_.size());
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070097 Circuit(current_vertex_, 0);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080098 }
Alex Deymo161c4a12014-05-16 15:56:21 -070099
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800100 out_cut_edges->swap(cut_edges_);
Andrew de los Reyes182a9f52010-10-05 16:33:51 -0700101 LOG(INFO) << "Cycle breaker skipped " << skipped_ops_ << " ops.";
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800102 DCHECK(stack_.empty());
103}
104
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700105static const size_t kMaxEdgesToConsider = 2;
106
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800107void CycleBreaker::HandleCircuit() {
108 stack_.push_back(current_vertex_);
Mike Frysinger0f9547d2012-02-16 12:11:37 -0500109 CHECK_GE(stack_.size(),
Alex Deymof329b932014-10-30 01:37:48 -0700110 static_cast<vector<Vertex::Index>::size_type>(2));
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800111 Edge min_edge = make_pair(stack_[0], stack_[1]);
Alex Vakulenko0103c362016-01-20 07:56:15 -0800112 uint64_t min_edge_weight = std::numeric_limits<uint64_t>::max();
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700113 size_t edges_considered = 0;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800114 for (vector<Vertex::Index>::const_iterator it = stack_.begin();
115 it != (stack_.end() - 1); ++it) {
116 Edge edge = make_pair(*it, *(it + 1));
117 if (cut_edges_.find(edge) != cut_edges_.end()) {
118 stack_.pop_back();
119 return;
120 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700121 uint64_t edge_weight = graph_utils::EdgeWeight(subgraph_, edge);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800122 if (edge_weight < min_edge_weight) {
123 min_edge_weight = edge_weight;
124 min_edge = edge;
125 }
Andrew de los Reyes45109892010-07-26 13:49:31 -0700126 edges_considered++;
127 if (edges_considered == kMaxEdgesToConsider)
128 break;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800129 }
130 cut_edges_.insert(min_edge);
131 stack_.pop_back();
132}
133
134void CycleBreaker::Unblock(Vertex::Index u) {
135 blocked_[u] = false;
136
137 for (Vertex::EdgeMap::iterator it = blocked_graph_[u].out_edges.begin();
138 it != blocked_graph_[u].out_edges.end(); ) {
139 Vertex::Index w = it->first;
140 blocked_graph_[u].out_edges.erase(it++);
141 if (blocked_[w])
142 Unblock(w);
143 }
144}
145
Andrew de los Reyes45109892010-07-26 13:49:31 -0700146bool CycleBreaker::StackContainsCutEdge() const {
Alex Deymof329b932014-10-30 01:37:48 -0700147 for (vector<Vertex::Index>::const_iterator it = ++stack_.begin(),
Andrew de los Reyes182a9f52010-10-05 16:33:51 -0700148 e = stack_.end(); it != e; ++it) {
Andrew de los Reyes45109892010-07-26 13:49:31 -0700149 Edge edge = make_pair(*(it - 1), *it);
Eric Carusoc0cfb7d2018-01-23 16:04:06 -0800150 if (base::ContainsKey(cut_edges_, edge)) {
Andrew de los Reyes45109892010-07-26 13:49:31 -0700151 return true;
152 }
153 }
154 return false;
155}
156
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700157bool CycleBreaker::Circuit(Vertex::Index vertex, Vertex::Index depth) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800158 // "vertex" was "v" in the original paper.
159 bool found = false; // Was "f" in the original paper.
160 stack_.push_back(vertex);
161 blocked_[vertex] = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700162 {
163 static int counter = 0;
164 counter++;
165 if (counter == 10000) {
166 counter = 0;
167 std::string stack_str;
Alex Vakulenko072359c2014-07-18 11:41:07 -0700168 for (Vertex::Index index : stack_) {
169 stack_str += std::to_string(index);
170 stack_str += " -> ";
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700171 }
172 LOG(INFO) << "stack: " << stack_str;
173 }
174 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800175
176 for (Vertex::SubgraphEdgeMap::iterator w =
177 subgraph_[vertex].subgraph_edges.begin();
178 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
179 if (*w == current_vertex_) {
180 // The original paper called for printing stack_ followed by
181 // current_vertex_ here, which is a cycle. Instead, we call
182 // HandleCircuit() to break it.
183 HandleCircuit();
184 found = true;
185 } else if (!blocked_[*w]) {
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700186 if (Circuit(*w, depth + 1)) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800187 found = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700188 if ((depth > kMaxEdgesToConsider) || StackContainsCutEdge())
Andrew de los Reyes45109892010-07-26 13:49:31 -0700189 break;
190 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800191 }
192 }
193
194 if (found) {
195 Unblock(vertex);
196 } else {
197 for (Vertex::SubgraphEdgeMap::iterator w =
198 subgraph_[vertex].subgraph_edges.begin();
199 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
Andrew de los Reyes8a51e5c2010-07-26 13:40:03 -0700200 if (blocked_graph_[*w].out_edges.find(vertex) ==
201 blocked_graph_[*w].out_edges.end()) {
202 blocked_graph_[*w].out_edges.insert(make_pair(vertex,
203 EdgeProperties()));
204 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800205 }
206 }
207 CHECK_EQ(vertex, stack_.back());
208 stack_.pop_back();
209 return found;
210}
211
212} // namespace chromeos_update_engine