blob: 10ef6fb8eb8e157d916b4d8acfabc891ce49f484 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes35a7af12010-03-10 16:33:26 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo161c4a12014-05-16 15:56:21 -07005#include "update_engine/payload_generator/cycle_breaker.h"
6
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -07007#include <inttypes.h>
Alex Deymo161c4a12014-05-16 15:56:21 -07008
Andrew de los Reyes35a7af12010-03-10 16:33:26 -08009#include <set>
10#include <utility>
Alex Deymo161c4a12014-05-16 15:56:21 -070011
Alex Vakulenko75039d72014-03-25 12:36:28 -070012#include <base/strings/string_util.h>
13#include <base/strings/stringprintf.h>
Alex Deymo161c4a12014-05-16 15:56:21 -070014
15#include "update_engine/payload_generator/graph_utils.h"
16#include "update_engine/payload_generator/tarjan.h"
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080017#include "update_engine/utils.h"
18
19using std::make_pair;
20using std::set;
21using std::vector;
22
23namespace chromeos_update_engine {
24
25// This is the outer function from the original paper.
26void CycleBreaker::BreakCycles(const Graph& graph, set<Edge>* out_cut_edges) {
27 cut_edges_.clear();
Alex Deymo161c4a12014-05-16 15:56:21 -070028
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080029 // Make a copy, which we will modify by removing edges. Thus, in each
30 // iteration subgraph_ is the current subgraph or the original with
31 // vertices we desire. This variable was "A_K" in the original paper.
32 subgraph_ = graph;
Alex Deymo161c4a12014-05-16 15:56:21 -070033
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080034 // The paper calls for the "adjacency structure (i.e., graph) of
35 // strong (-ly connected) component K with least vertex in subgraph
36 // induced by {s, s + 1, ..., n}".
37 // We arbitrarily order each vertex by its index in the graph. Thus,
38 // each iteration, we are looking at the subgraph {s, s + 1, ..., n}
39 // and looking for the strongly connected component with vertex s.
40
41 TarjanAlgorithm tarjan;
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070042 skipped_ops_ = 0;
Alex Deymo161c4a12014-05-16 15:56:21 -070043
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080044 for (Graph::size_type i = 0; i < subgraph_.size(); i++) {
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070045 DeltaArchiveManifest_InstallOperation_Type op_type = graph[i].op.type();
46 if (op_type == DeltaArchiveManifest_InstallOperation_Type_REPLACE ||
47 op_type == DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ) {
48 skipped_ops_++;
49 continue;
50 }
51
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080052 if (i > 0) {
53 // Erase node (i - 1) from subgraph_. First, erase what it points to
54 subgraph_[i - 1].out_edges.clear();
55 // Now, erase any pointers to node (i - 1)
56 for (Graph::size_type j = i; j < subgraph_.size(); j++) {
57 subgraph_[j].out_edges.erase(i - 1);
58 }
59 }
60
61 // Calculate SCC (strongly connected component) with vertex i.
62 vector<Vertex::Index> component_indexes;
63 tarjan.Execute(i, &subgraph_, &component_indexes);
64
65 // Set subgraph edges for the components in the SCC.
66 for (vector<Vertex::Index>::iterator it = component_indexes.begin();
67 it != component_indexes.end(); ++it) {
68 subgraph_[*it].subgraph_edges.clear();
69 for (vector<Vertex::Index>::iterator jt = component_indexes.begin();
70 jt != component_indexes.end(); ++jt) {
71 // If there's a link from *it -> *jt in the graph,
72 // add a subgraph_ edge
73 if (utils::MapContainsKey(subgraph_[*it].out_edges, *jt))
74 subgraph_[*it].subgraph_edges.insert(*jt);
Alex Deymo161c4a12014-05-16 15:56:21 -070075 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080076 }
77
78 current_vertex_ = i;
79 blocked_.clear();
80 blocked_.resize(subgraph_.size());
81 blocked_graph_.clear();
82 blocked_graph_.resize(subgraph_.size());
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070083 Circuit(current_vertex_, 0);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080084 }
Alex Deymo161c4a12014-05-16 15:56:21 -070085
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080086 out_cut_edges->swap(cut_edges_);
Andrew de los Reyes182a9f52010-10-05 16:33:51 -070087 LOG(INFO) << "Cycle breaker skipped " << skipped_ops_ << " ops.";
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080088 DCHECK(stack_.empty());
89}
90
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070091static const size_t kMaxEdgesToConsider = 2;
92
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080093void CycleBreaker::HandleCircuit() {
94 stack_.push_back(current_vertex_);
Mike Frysinger0f9547d2012-02-16 12:11:37 -050095 CHECK_GE(stack_.size(),
96 static_cast<std::vector<Vertex::Index>::size_type>(2));
Andrew de los Reyes35a7af12010-03-10 16:33:26 -080097 Edge min_edge = make_pair(stack_[0], stack_[1]);
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070098 uint64_t min_edge_weight = kuint64max;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -070099 size_t edges_considered = 0;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800100 for (vector<Vertex::Index>::const_iterator it = stack_.begin();
101 it != (stack_.end() - 1); ++it) {
102 Edge edge = make_pair(*it, *(it + 1));
103 if (cut_edges_.find(edge) != cut_edges_.end()) {
104 stack_.pop_back();
105 return;
106 }
Andrew de los Reyes09e56d62010-04-23 13:45:53 -0700107 uint64_t edge_weight = graph_utils::EdgeWeight(subgraph_, edge);
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800108 if (edge_weight < min_edge_weight) {
109 min_edge_weight = edge_weight;
110 min_edge = edge;
111 }
Andrew de los Reyes45109892010-07-26 13:49:31 -0700112 edges_considered++;
113 if (edges_considered == kMaxEdgesToConsider)
114 break;
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800115 }
116 cut_edges_.insert(min_edge);
117 stack_.pop_back();
118}
119
120void CycleBreaker::Unblock(Vertex::Index u) {
121 blocked_[u] = false;
122
123 for (Vertex::EdgeMap::iterator it = blocked_graph_[u].out_edges.begin();
124 it != blocked_graph_[u].out_edges.end(); ) {
125 Vertex::Index w = it->first;
126 blocked_graph_[u].out_edges.erase(it++);
127 if (blocked_[w])
128 Unblock(w);
129 }
130}
131
Andrew de los Reyes45109892010-07-26 13:49:31 -0700132bool CycleBreaker::StackContainsCutEdge() const {
133 for (std::vector<Vertex::Index>::const_iterator it = ++stack_.begin(),
Andrew de los Reyes182a9f52010-10-05 16:33:51 -0700134 e = stack_.end(); it != e; ++it) {
Andrew de los Reyes45109892010-07-26 13:49:31 -0700135 Edge edge = make_pair(*(it - 1), *it);
136 if (utils::SetContainsKey(cut_edges_, edge)) {
137 return true;
138 }
139 }
140 return false;
141}
142
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700143bool CycleBreaker::Circuit(Vertex::Index vertex, Vertex::Index depth) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800144 // "vertex" was "v" in the original paper.
145 bool found = false; // Was "f" in the original paper.
146 stack_.push_back(vertex);
147 blocked_[vertex] = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700148 {
149 static int counter = 0;
150 counter++;
151 if (counter == 10000) {
152 counter = 0;
153 std::string stack_str;
154 for (vector<Vertex::Index>::const_iterator it = stack_.begin();
155 it != stack_.end(); ++it) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700156 stack_str += base::StringPrintf("%lu -> ",
157 static_cast<long unsigned int>(*it));
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700158 }
159 LOG(INFO) << "stack: " << stack_str;
160 }
161 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800162
163 for (Vertex::SubgraphEdgeMap::iterator w =
164 subgraph_[vertex].subgraph_edges.begin();
165 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
166 if (*w == current_vertex_) {
167 // The original paper called for printing stack_ followed by
168 // current_vertex_ here, which is a cycle. Instead, we call
169 // HandleCircuit() to break it.
170 HandleCircuit();
171 found = true;
172 } else if (!blocked_[*w]) {
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700173 if (Circuit(*w, depth + 1)) {
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800174 found = true;
Andrew de los Reyes21ab31c2010-08-19 14:59:00 -0700175 if ((depth > kMaxEdgesToConsider) || StackContainsCutEdge())
Andrew de los Reyes45109892010-07-26 13:49:31 -0700176 break;
177 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800178 }
179 }
180
181 if (found) {
182 Unblock(vertex);
183 } else {
184 for (Vertex::SubgraphEdgeMap::iterator w =
185 subgraph_[vertex].subgraph_edges.begin();
186 w != subgraph_[vertex].subgraph_edges.end(); ++w) {
Andrew de los Reyes8a51e5c2010-07-26 13:40:03 -0700187 if (blocked_graph_[*w].out_edges.find(vertex) ==
188 blocked_graph_[*w].out_edges.end()) {
189 blocked_graph_[*w].out_edges.insert(make_pair(vertex,
190 EdgeProperties()));
191 }
Andrew de los Reyes35a7af12010-03-10 16:33:26 -0800192 }
193 }
194 CHECK_EQ(vertex, stack_.back());
195 stack_.pop_back();
196 return found;
197}
198
199} // namespace chromeos_update_engine