cycle_breaker.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. //
  16. #include "update_engine/payload_generator/cycle_breaker.h"
  17. #include <inttypes.h>
  18. #include <limits>
  19. #include <set>
  20. #include <string>
  21. #include <utility>
  22. #include <base/stl_util.h>
  23. #include <base/strings/string_util.h>
  24. #include <base/strings/stringprintf.h>
  25. #include "update_engine/payload_generator/graph_utils.h"
  26. #include "update_engine/payload_generator/tarjan.h"
  27. using std::make_pair;
  28. using std::set;
  29. using std::vector;
  30. namespace chromeos_update_engine {
  31. // This is the outer function from the original paper.
  32. void CycleBreaker::BreakCycles(const Graph& graph, set<Edge>* out_cut_edges) {
  33. cut_edges_.clear();
  34. // Make a copy, which we will modify by removing edges. Thus, in each
  35. // iteration subgraph_ is the current subgraph or the original with
  36. // vertices we desire. This variable was "A_K" in the original paper.
  37. subgraph_ = graph;
  38. // The paper calls for the "adjacency structure (i.e., graph) of
  39. // strong (-ly connected) component K with least vertex in subgraph
  40. // induced by {s, s + 1, ..., n}".
  41. // We arbitrarily order each vertex by its index in the graph. Thus,
  42. // each iteration, we are looking at the subgraph {s, s + 1, ..., n}
  43. // and looking for the strongly connected component with vertex s.
  44. TarjanAlgorithm tarjan;
  45. skipped_ops_ = 0;
  46. for (Graph::size_type i = 0; i < subgraph_.size(); i++) {
  47. InstallOperation::Type op_type = graph[i].aop.op.type();
  48. if (op_type == InstallOperation::REPLACE ||
  49. op_type == InstallOperation::REPLACE_BZ) {
  50. skipped_ops_++;
  51. continue;
  52. }
  53. if (i > 0) {
  54. // Erase node (i - 1) from subgraph_. First, erase what it points to
  55. subgraph_[i - 1].out_edges.clear();
  56. // Now, erase any pointers to node (i - 1)
  57. for (Graph::size_type j = i; j < subgraph_.size(); j++) {
  58. subgraph_[j].out_edges.erase(i - 1);
  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. // Set subgraph edges for the components in the SCC.
  65. for (vector<Vertex::Index>::iterator it = component_indexes.begin();
  66. it != component_indexes.end();
  67. ++it) {
  68. subgraph_[*it].subgraph_edges.clear();
  69. for (vector<Vertex::Index>::iterator jt = component_indexes.begin();
  70. jt != component_indexes.end();
  71. ++jt) {
  72. // If there's a link from *it -> *jt in the graph,
  73. // add a subgraph_ edge
  74. if (base::ContainsKey(subgraph_[*it].out_edges, *jt))
  75. subgraph_[*it].subgraph_edges.insert(*jt);
  76. }
  77. }
  78. current_vertex_ = i;
  79. blocked_.clear();
  80. blocked_.resize(subgraph_.size());
  81. blocked_graph_.clear();
  82. blocked_graph_.resize(subgraph_.size());
  83. Circuit(current_vertex_, 0);
  84. }
  85. out_cut_edges->swap(cut_edges_);
  86. LOG(INFO) << "Cycle breaker skipped " << skipped_ops_ << " ops.";
  87. DCHECK(stack_.empty());
  88. }
  89. static const size_t kMaxEdgesToConsider = 2;
  90. void CycleBreaker::HandleCircuit() {
  91. stack_.push_back(current_vertex_);
  92. CHECK_GE(stack_.size(), static_cast<vector<Vertex::Index>::size_type>(2));
  93. Edge min_edge = make_pair(stack_[0], stack_[1]);
  94. uint64_t min_edge_weight = std::numeric_limits<uint64_t>::max();
  95. size_t edges_considered = 0;
  96. for (vector<Vertex::Index>::const_iterator it = stack_.begin();
  97. it != (stack_.end() - 1);
  98. ++it) {
  99. Edge edge = make_pair(*it, *(it + 1));
  100. if (cut_edges_.find(edge) != cut_edges_.end()) {
  101. stack_.pop_back();
  102. return;
  103. }
  104. uint64_t edge_weight = graph_utils::EdgeWeight(subgraph_, edge);
  105. if (edge_weight < min_edge_weight) {
  106. min_edge_weight = edge_weight;
  107. min_edge = edge;
  108. }
  109. edges_considered++;
  110. if (edges_considered == kMaxEdgesToConsider)
  111. break;
  112. }
  113. cut_edges_.insert(min_edge);
  114. stack_.pop_back();
  115. }
  116. void CycleBreaker::Unblock(Vertex::Index u) {
  117. blocked_[u] = false;
  118. for (Vertex::EdgeMap::iterator it = blocked_graph_[u].out_edges.begin();
  119. it != blocked_graph_[u].out_edges.end();) {
  120. Vertex::Index w = it->first;
  121. blocked_graph_[u].out_edges.erase(it++);
  122. if (blocked_[w])
  123. Unblock(w);
  124. }
  125. }
  126. bool CycleBreaker::StackContainsCutEdge() const {
  127. for (vector<Vertex::Index>::const_iterator it = ++stack_.begin(),
  128. e = stack_.end();
  129. it != e;
  130. ++it) {
  131. Edge edge = make_pair(*(it - 1), *it);
  132. if (base::ContainsKey(cut_edges_, edge)) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. bool CycleBreaker::Circuit(Vertex::Index vertex, Vertex::Index depth) {
  139. // "vertex" was "v" in the original paper.
  140. bool found = false; // Was "f" in the original paper.
  141. stack_.push_back(vertex);
  142. blocked_[vertex] = true;
  143. {
  144. static int counter = 0;
  145. counter++;
  146. if (counter == 10000) {
  147. counter = 0;
  148. std::string stack_str;
  149. for (Vertex::Index index : stack_) {
  150. stack_str += std::to_string(index);
  151. stack_str += " -> ";
  152. }
  153. LOG(INFO) << "stack: " << stack_str;
  154. }
  155. }
  156. for (Vertex::SubgraphEdgeMap::iterator w =
  157. subgraph_[vertex].subgraph_edges.begin();
  158. w != subgraph_[vertex].subgraph_edges.end();
  159. ++w) {
  160. if (*w == current_vertex_) {
  161. // The original paper called for printing stack_ followed by
  162. // current_vertex_ here, which is a cycle. Instead, we call
  163. // HandleCircuit() to break it.
  164. HandleCircuit();
  165. found = true;
  166. } else if (!blocked_[*w]) {
  167. if (Circuit(*w, depth + 1)) {
  168. found = true;
  169. if ((depth > kMaxEdgesToConsider) || StackContainsCutEdge())
  170. break;
  171. }
  172. }
  173. }
  174. if (found) {
  175. Unblock(vertex);
  176. } else {
  177. for (Vertex::SubgraphEdgeMap::iterator w =
  178. subgraph_[vertex].subgraph_edges.begin();
  179. w != subgraph_[vertex].subgraph_edges.end();
  180. ++w) {
  181. if (blocked_graph_[*w].out_edges.find(vertex) ==
  182. blocked_graph_[*w].out_edges.end()) {
  183. blocked_graph_[*w].out_edges.insert(
  184. make_pair(vertex, EdgeProperties()));
  185. }
  186. }
  187. }
  188. CHECK_EQ(vertex, stack_.back());
  189. stack_.pop_back();
  190. return found;
  191. }
  192. } // namespace chromeos_update_engine