| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 16 |  | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 17 | #include "update_engine/payload_generator/cycle_breaker.h" | 
|  | 18 |  | 
| Andrew de los Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 19 | #include <inttypes.h> | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 20 |  | 
| Eric Caruso | 8a51cd7 | 2018-01-23 16:11:04 -0800 | [diff] [blame] | 21 | #include <limits> | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 22 | #include <set> | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 23 | #include <string> | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 24 | #include <utility> | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 25 |  | 
| Eric Caruso | c0cfb7d | 2018-01-23 16:04:06 -0800 | [diff] [blame] | 26 | #include <base/stl_util.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 27 | #include <base/strings/string_util.h> | 
|  | 28 | #include <base/strings/stringprintf.h> | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | #include "update_engine/payload_generator/graph_utils.h" | 
|  | 31 | #include "update_engine/payload_generator/tarjan.h" | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 32 |  | 
|  | 33 | using std::make_pair; | 
|  | 34 | using std::set; | 
|  | 35 | using std::vector; | 
|  | 36 |  | 
|  | 37 | namespace chromeos_update_engine { | 
|  | 38 |  | 
|  | 39 | // This is the outer function from the original paper. | 
|  | 40 | void CycleBreaker::BreakCycles(const Graph& graph, set<Edge>* out_cut_edges) { | 
|  | 41 | cut_edges_.clear(); | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 42 |  | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 43 | // 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 Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 47 |  | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 48 | // 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 Reyes | 182a9f5 | 2010-10-05 16:33:51 -0700 | [diff] [blame] | 56 | skipped_ops_ = 0; | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 57 |  | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 58 | for (Graph::size_type i = 0; i < subgraph_.size(); i++) { | 
| Alex Deymo | a12ee11 | 2015-08-12 22:19:32 -0700 | [diff] [blame] | 59 | InstallOperation_Type op_type = graph[i].aop.op.type(); | 
|  | 60 | if (op_type == InstallOperation::REPLACE || | 
|  | 61 | op_type == InstallOperation::REPLACE_BZ) { | 
| Andrew de los Reyes | 182a9f5 | 2010-10-05 16:33:51 -0700 | [diff] [blame] | 62 | skipped_ops_++; | 
|  | 63 | continue; | 
|  | 64 | } | 
|  | 65 |  | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 66 | 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 Caruso | c0cfb7d | 2018-01-23 16:04:06 -0800 | [diff] [blame] | 87 | if (base::ContainsKey(subgraph_[*it].out_edges, *jt)) | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 88 | subgraph_[*it].subgraph_edges.insert(*jt); | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 89 | } | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 90 | } | 
|  | 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 Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 97 | Circuit(current_vertex_, 0); | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 98 | } | 
| Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 99 |  | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 100 | out_cut_edges->swap(cut_edges_); | 
| Andrew de los Reyes | 182a9f5 | 2010-10-05 16:33:51 -0700 | [diff] [blame] | 101 | LOG(INFO) << "Cycle breaker skipped " << skipped_ops_ << " ops."; | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 102 | DCHECK(stack_.empty()); | 
|  | 103 | } | 
|  | 104 |  | 
| Andrew de los Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 105 | static const size_t kMaxEdgesToConsider = 2; | 
|  | 106 |  | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 107 | void CycleBreaker::HandleCircuit() { | 
|  | 108 | stack_.push_back(current_vertex_); | 
| Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 109 | CHECK_GE(stack_.size(), | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 110 | static_cast<vector<Vertex::Index>::size_type>(2)); | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 111 | Edge min_edge = make_pair(stack_[0], stack_[1]); | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 112 | uint64_t min_edge_weight = std::numeric_limits<uint64_t>::max(); | 
| Andrew de los Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 113 | size_t edges_considered = 0; | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 114 | 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 Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 121 | uint64_t edge_weight = graph_utils::EdgeWeight(subgraph_, edge); | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 122 | if (edge_weight < min_edge_weight) { | 
|  | 123 | min_edge_weight = edge_weight; | 
|  | 124 | min_edge = edge; | 
|  | 125 | } | 
| Andrew de los Reyes | 4510989 | 2010-07-26 13:49:31 -0700 | [diff] [blame] | 126 | edges_considered++; | 
|  | 127 | if (edges_considered == kMaxEdgesToConsider) | 
|  | 128 | break; | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 129 | } | 
|  | 130 | cut_edges_.insert(min_edge); | 
|  | 131 | stack_.pop_back(); | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | void 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 Reyes | 4510989 | 2010-07-26 13:49:31 -0700 | [diff] [blame] | 146 | bool CycleBreaker::StackContainsCutEdge() const { | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 147 | for (vector<Vertex::Index>::const_iterator it = ++stack_.begin(), | 
| Andrew de los Reyes | 182a9f5 | 2010-10-05 16:33:51 -0700 | [diff] [blame] | 148 | e = stack_.end(); it != e; ++it) { | 
| Andrew de los Reyes | 4510989 | 2010-07-26 13:49:31 -0700 | [diff] [blame] | 149 | Edge edge = make_pair(*(it - 1), *it); | 
| Eric Caruso | c0cfb7d | 2018-01-23 16:04:06 -0800 | [diff] [blame] | 150 | if (base::ContainsKey(cut_edges_, edge)) { | 
| Andrew de los Reyes | 4510989 | 2010-07-26 13:49:31 -0700 | [diff] [blame] | 151 | return true; | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 | return false; | 
|  | 155 | } | 
|  | 156 |  | 
| Andrew de los Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 157 | bool CycleBreaker::Circuit(Vertex::Index vertex, Vertex::Index depth) { | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 158 | // "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 Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 162 | { | 
|  | 163 | static int counter = 0; | 
|  | 164 | counter++; | 
|  | 165 | if (counter == 10000) { | 
|  | 166 | counter = 0; | 
|  | 167 | std::string stack_str; | 
| Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 168 | for (Vertex::Index index : stack_) { | 
|  | 169 | stack_str += std::to_string(index); | 
|  | 170 | stack_str += " -> "; | 
| Andrew de los Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 171 | } | 
|  | 172 | LOG(INFO) << "stack: " << stack_str; | 
|  | 173 | } | 
|  | 174 | } | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 175 |  | 
|  | 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 Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 186 | if (Circuit(*w, depth + 1)) { | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 187 | found = true; | 
| Andrew de los Reyes | 21ab31c | 2010-08-19 14:59:00 -0700 | [diff] [blame] | 188 | if ((depth > kMaxEdgesToConsider) || StackContainsCutEdge()) | 
| Andrew de los Reyes | 4510989 | 2010-07-26 13:49:31 -0700 | [diff] [blame] | 189 | break; | 
|  | 190 | } | 
| Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 191 | } | 
|  | 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 Reyes | 8a51e5c | 2010-07-26 13:40:03 -0700 | [diff] [blame] | 200 | 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 Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 205 | } | 
|  | 206 | } | 
|  | 207 | CHECK_EQ(vertex, stack_.back()); | 
|  | 208 | stack_.pop_back(); | 
|  | 209 | return found; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | }  // namespace chromeos_update_engine |