blob: 2d5fb631e8a1a0ce41043743bbdd3c07810be916 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2009 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 Reyes0ce161b2010-02-22 15:27:01 -080016
Alex Deymo161c4a12014-05-16 15:56:21 -070017#include "update_engine/payload_generator/graph_utils.h"
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080018
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070019#include <string>
20#include <utility>
21#include <vector>
22
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070023#include <base/logging.h>
Ben Chan05735a12014-09-03 07:48:22 -070024#include <base/macros.h>
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo477aec22015-03-24 23:40:48 -070027#include "update_engine/payload_generator/annotated_operation.h"
Alex Deymo5c6c6552015-06-03 19:06:50 +020028#include "update_engine/payload_generator/extent_utils.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070029
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070030using std::make_pair;
31using std::pair;
32using std::string;
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080033using std::vector;
34
35namespace chromeos_update_engine {
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080036namespace graph_utils {
37
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070038uint64_t EdgeWeight(const Graph& graph, const Edge& edge) {
39 uint64_t weight = 0;
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080040 const vector<Extent>& extents =
41 graph[edge.first].out_edges.find(edge.second)->second.extents;
42 for (vector<Extent>::const_iterator it = extents.begin();
43 it != extents.end(); ++it) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070044 if (it->start_block() != kSparseHole)
45 weight += it->num_blocks();
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080046 }
47 return weight;
48}
49
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070050void AddReadBeforeDep(Vertex* src,
51 Vertex::Index dst,
52 uint64_t block) {
53 Vertex::EdgeMap::iterator edge_it = src->out_edges.find(dst);
54 if (edge_it == src->out_edges.end()) {
55 // Must create new edge
56 pair<Vertex::EdgeMap::iterator, bool> result =
57 src->out_edges.insert(make_pair(dst, EdgeProperties()));
58 CHECK(result.second);
59 edge_it = result.first;
60 }
61 AppendBlockToExtents(&edge_it->second.extents, block);
62}
63
64void AddReadBeforeDepExtents(Vertex* src,
65 Vertex::Index dst,
66 const vector<Extent>& extents) {
67 // TODO(adlr): Be more efficient than adding each block individually.
68 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
69 it != e; ++it) {
70 const Extent& extent = *it;
71 for (uint64_t block = extent.start_block(),
72 block_end = extent.start_block() + extent.num_blocks();
73 block != block_end; ++block) {
74 AddReadBeforeDep(src, dst, block);
75 }
76 }
77}
78
79void DropWriteBeforeDeps(Vertex::EdgeMap* edge_map) {
80 // Specially crafted for-loop for the map-iterate-delete dance.
81 for (Vertex::EdgeMap::iterator it = edge_map->begin();
82 it != edge_map->end(); ) {
83 if (!it->second.write_extents.empty())
84 it->second.write_extents.clear();
85 if (it->second.extents.empty()) {
86 // Erase *it, as it contains no blocks
87 edge_map->erase(it++);
88 } else {
89 ++it;
90 }
91 }
92}
93
94// For each node N in graph, drop all edges N->|index|.
95void DropIncomingEdgesTo(Graph* graph, Vertex::Index index) {
96 // This would be much more efficient if we had doubly-linked
97 // edges in the graph.
98 for (Graph::iterator it = graph->begin(), e = graph->end(); it != e; ++it) {
99 it->out_edges.erase(index);
100 }
101}
102
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700103namespace {
104template<typename T>
105void DumpExtents(const T& field, int prepend_space_count) {
106 string header(prepend_space_count, ' ');
107 for (int i = 0, e = field.size(); i != e; ++i) {
108 LOG(INFO) << header << "(" << GetElement(field, i).start_block() << ", "
109 << GetElement(field, i).num_blocks() << ")";
110 }
111}
112
113void DumpOutEdges(const Vertex::EdgeMap& out_edges) {
114 for (Vertex::EdgeMap::const_iterator it = out_edges.begin(),
115 e = out_edges.end(); it != e; ++it) {
116 LOG(INFO) << " " << it->first << " read-before:";
117 DumpExtents(it->second.extents, 6);
118 LOG(INFO) << " write-before:";
119 DumpExtents(it->second.write_extents, 6);
120 }
121}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700122} // namespace
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700123
124void DumpGraph(const Graph& graph) {
125 LOG(INFO) << "Graph length: " << graph.size();
126 for (Graph::size_type i = 0, e = graph.size(); i != e; ++i) {
Darin Petkov8e447e02013-04-16 16:23:50 +0200127 LOG(INFO) << i
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700128 << (graph[i].valid ? "" : "-INV")
Alex Deymo3b2c7d02015-07-16 16:08:16 -0700129 << ": " << graph[i].aop.name
130 << ": " << InstallOperationTypeName(graph[i].aop.op.type());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700131 LOG(INFO) << " src_extents:";
Alex Deymo3b2c7d02015-07-16 16:08:16 -0700132 DumpExtents(graph[i].aop.op.src_extents(), 4);
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700133 LOG(INFO) << " dst_extents:";
Alex Deymo3b2c7d02015-07-16 16:08:16 -0700134 DumpExtents(graph[i].aop.op.dst_extents(), 4);
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700135 LOG(INFO) << " out edges:";
136 DumpOutEdges(graph[i].out_edges);
137 }
138}
139
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800140} // namespace graph_utils
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800141} // namespace chromeos_update_engine