• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2010 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 
17 #include "update_engine/payload_generator/cycle_breaker.h"
18 
19 #include <set>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include <base/logging.h>
25 #include <base/stl_util.h>
26 #include <gtest/gtest.h>
27 
28 #include "update_engine/payload_generator/graph_types.h"
29 
30 using std::make_pair;
31 using std::pair;
32 using std::set;
33 using std::string;
34 using std::vector;
35 
36 namespace chromeos_update_engine {
37 
38 namespace {
SetOpForNodes(Graph * graph)39 void SetOpForNodes(Graph* graph) {
40   for (Vertex& vertex : *graph) {
41     vertex.aop.op.set_type(InstallOperation::MOVE);
42   }
43 }
44 }  // namespace
45 
46 class CycleBreakerTest : public ::testing::Test {};
47 
TEST(CycleBreakerTest,SimpleTest)48 TEST(CycleBreakerTest, SimpleTest) {
49   int counter = 0;
50   const Vertex::Index n_a = counter++;
51   const Vertex::Index n_b = counter++;
52   const Vertex::Index n_c = counter++;
53   const Vertex::Index n_d = counter++;
54   const Vertex::Index n_e = counter++;
55   const Vertex::Index n_f = counter++;
56   const Vertex::Index n_g = counter++;
57   const Vertex::Index n_h = counter++;
58   const Graph::size_type kNodeCount = counter++;
59 
60   Graph graph(kNodeCount);
61   SetOpForNodes(&graph);
62 
63   graph[n_a].out_edges.insert(make_pair(n_e, EdgeProperties()));
64   graph[n_a].out_edges.insert(make_pair(n_f, EdgeProperties()));
65   graph[n_b].out_edges.insert(make_pair(n_a, EdgeProperties()));
66   graph[n_c].out_edges.insert(make_pair(n_d, EdgeProperties()));
67   graph[n_d].out_edges.insert(make_pair(n_e, EdgeProperties()));
68   graph[n_d].out_edges.insert(make_pair(n_f, EdgeProperties()));
69   graph[n_e].out_edges.insert(make_pair(n_b, EdgeProperties()));
70   graph[n_e].out_edges.insert(make_pair(n_c, EdgeProperties()));
71   graph[n_e].out_edges.insert(make_pair(n_f, EdgeProperties()));
72   graph[n_f].out_edges.insert(make_pair(n_g, EdgeProperties()));
73   graph[n_g].out_edges.insert(make_pair(n_h, EdgeProperties()));
74   graph[n_h].out_edges.insert(make_pair(n_g, EdgeProperties()));
75 
76   CycleBreaker breaker;
77 
78   set<Edge> broken_edges;
79   breaker.BreakCycles(graph, &broken_edges);
80 
81   // The following cycles must be cut:
82   // A->E->B
83   // C->D->E
84   // G->H
85 
86   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_a, n_e)) ||
87               base::ContainsKey(broken_edges, make_pair(n_e, n_b)) ||
88               base::ContainsKey(broken_edges, make_pair(n_b, n_a)));
89   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_c, n_d)) ||
90               base::ContainsKey(broken_edges, make_pair(n_d, n_e)) ||
91               base::ContainsKey(broken_edges, make_pair(n_e, n_c)));
92   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_g, n_h)) ||
93               base::ContainsKey(broken_edges, make_pair(n_h, n_g)));
94   EXPECT_EQ(3U, broken_edges.size());
95 }
96 
97 namespace {
EdgeWithWeight(Vertex::Index dest,uint64_t weight)98 pair<Vertex::Index, EdgeProperties> EdgeWithWeight(Vertex::Index dest,
99                                                    uint64_t weight) {
100   EdgeProperties props;
101   props.extents.resize(1);
102   props.extents[0].set_num_blocks(weight);
103   return make_pair(dest, props);
104 }
105 }  // namespace
106 
107 // This creates a bunch of cycles like this:
108 //
109 //               root <------.
110 //    (t)->     / | \        |
111 //             V  V  V       |
112 //             N  N  N       |
113 //              \ | /        |
114 //               VVV         |
115 //                N          |
116 //              / | \        |
117 //             V  V  V       |
118 //             N  N  N       |
119 //               ...         |
120 //     (s)->    \ | /        |
121 //               VVV         |
122 //                N          |
123 //                 \_________/
124 //
125 // such that the original cutting algo would cut edges (s). We changed
126 // the algorithm to cut cycles (t) instead, since they are closer to the
127 // root, and that can massively speed up cycle cutting.
TEST(CycleBreakerTest,AggressiveCutTest)128 TEST(CycleBreakerTest, AggressiveCutTest) {
129   size_t counter = 0;
130 
131   const int kNodesPerGroup = 4;
132   const int kGroups = 33;
133 
134   Graph graph(kGroups * kNodesPerGroup + 1);  // + 1 for the root node
135   SetOpForNodes(&graph);
136 
137   const Vertex::Index n_root = counter++;
138 
139   Vertex::Index last_hub = n_root;
140   for (int i = 0; i < kGroups; i++) {
141     uint64_t weight = 5;
142     if (i == 0)
143       weight = 2;
144     else if (i == (kGroups - 1))
145       weight = 1;
146 
147     const Vertex::Index next_hub = counter++;
148 
149     for (int j = 0; j < (kNodesPerGroup - 1); j++) {
150       const Vertex::Index node = counter++;
151       graph[last_hub].out_edges.insert(EdgeWithWeight(node, weight));
152       graph[node].out_edges.insert(EdgeWithWeight(next_hub, weight));
153     }
154     last_hub = next_hub;
155   }
156 
157   graph[last_hub].out_edges.insert(EdgeWithWeight(n_root, 5));
158 
159   EXPECT_EQ(counter, graph.size());
160 
161   CycleBreaker breaker;
162 
163   set<Edge> broken_edges;
164   LOG(INFO) << "If this hangs for more than 1 second, the test has failed.";
165   breaker.BreakCycles(graph, &broken_edges);
166 
167   set<Edge> expected_cuts;
168 
169   for (Vertex::EdgeMap::const_iterator it = graph[n_root].out_edges.begin(),
170                                        e = graph[n_root].out_edges.end();
171        it != e;
172        ++it) {
173     expected_cuts.insert(make_pair(n_root, it->first));
174   }
175 
176   EXPECT_TRUE(broken_edges == expected_cuts);
177 }
178 
TEST(CycleBreakerTest,WeightTest)179 TEST(CycleBreakerTest, WeightTest) {
180   size_t counter = 0;
181   const Vertex::Index n_a = counter++;
182   const Vertex::Index n_b = counter++;
183   const Vertex::Index n_c = counter++;
184   const Vertex::Index n_d = counter++;
185   const Vertex::Index n_e = counter++;
186   const Vertex::Index n_f = counter++;
187   const Vertex::Index n_g = counter++;
188   const Vertex::Index n_h = counter++;
189   const Vertex::Index n_i = counter++;
190   const Vertex::Index n_j = counter++;
191   const Graph::size_type kNodeCount = counter++;
192 
193   Graph graph(kNodeCount);
194   SetOpForNodes(&graph);
195 
196   graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 4));
197   graph[n_a].out_edges.insert(EdgeWithWeight(n_f, 3));
198   graph[n_a].out_edges.insert(EdgeWithWeight(n_h, 2));
199   graph[n_b].out_edges.insert(EdgeWithWeight(n_a, 3));
200   graph[n_b].out_edges.insert(EdgeWithWeight(n_c, 4));
201   graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 5));
202   graph[n_c].out_edges.insert(EdgeWithWeight(n_d, 3));
203   graph[n_d].out_edges.insert(EdgeWithWeight(n_a, 6));
204   graph[n_d].out_edges.insert(EdgeWithWeight(n_e, 3));
205   graph[n_e].out_edges.insert(EdgeWithWeight(n_d, 4));
206   graph[n_e].out_edges.insert(EdgeWithWeight(n_g, 5));
207   graph[n_f].out_edges.insert(EdgeWithWeight(n_g, 2));
208   graph[n_g].out_edges.insert(EdgeWithWeight(n_f, 3));
209   graph[n_g].out_edges.insert(EdgeWithWeight(n_d, 5));
210   graph[n_h].out_edges.insert(EdgeWithWeight(n_i, 8));
211   graph[n_i].out_edges.insert(EdgeWithWeight(n_e, 4));
212   graph[n_i].out_edges.insert(EdgeWithWeight(n_h, 9));
213   graph[n_i].out_edges.insert(EdgeWithWeight(n_j, 6));
214 
215   CycleBreaker breaker;
216 
217   set<Edge> broken_edges;
218   breaker.BreakCycles(graph, &broken_edges);
219 
220   // These are required to be broken:
221   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_b, n_a)));
222   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_b, n_c)));
223   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_d, n_e)));
224   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_f, n_g)));
225   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_h, n_i)));
226 }
227 
TEST(CycleBreakerTest,UnblockGraphTest)228 TEST(CycleBreakerTest, UnblockGraphTest) {
229   size_t counter = 0;
230   const Vertex::Index n_a = counter++;
231   const Vertex::Index n_b = counter++;
232   const Vertex::Index n_c = counter++;
233   const Vertex::Index n_d = counter++;
234   const Graph::size_type kNodeCount = counter++;
235 
236   Graph graph(kNodeCount);
237   SetOpForNodes(&graph);
238 
239   graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 1));
240   graph[n_a].out_edges.insert(EdgeWithWeight(n_c, 1));
241   graph[n_b].out_edges.insert(EdgeWithWeight(n_c, 2));
242   graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 2));
243   graph[n_b].out_edges.insert(EdgeWithWeight(n_d, 2));
244   graph[n_d].out_edges.insert(EdgeWithWeight(n_a, 2));
245 
246   CycleBreaker breaker;
247 
248   set<Edge> broken_edges;
249   breaker.BreakCycles(graph, &broken_edges);
250 
251   // These are required to be broken:
252   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_a, n_b)));
253   EXPECT_TRUE(base::ContainsKey(broken_edges, make_pair(n_a, n_c)));
254 }
255 
TEST(CycleBreakerTest,SkipOpsTest)256 TEST(CycleBreakerTest, SkipOpsTest) {
257   size_t counter = 0;
258   const Vertex::Index n_a = counter++;
259   const Vertex::Index n_b = counter++;
260   const Vertex::Index n_c = counter++;
261   const Graph::size_type kNodeCount = counter++;
262 
263   Graph graph(kNodeCount);
264   SetOpForNodes(&graph);
265   graph[n_a].aop.op.set_type(InstallOperation::REPLACE_BZ);
266   graph[n_c].aop.op.set_type(InstallOperation::REPLACE);
267 
268   graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 1));
269   graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 1));
270 
271   CycleBreaker breaker;
272 
273   set<Edge> broken_edges;
274   breaker.BreakCycles(graph, &broken_edges);
275 
276   EXPECT_EQ(2U, breaker.skipped_ops());
277 }
278 
279 }  // namespace chromeos_update_engine
280