blob: 7f5cf8fb84a8ea5594f3706b985136c8f6dee7bb [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;
Amin Hassani232f8f92019-01-14 16:15:31 -080042 for (vector<Extent>::const_iterator it = extents.begin(); it != extents.end();
43 ++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
Amin Hassani232f8f92019-01-14 16:15:31 -080050void AddReadBeforeDep(Vertex* src, Vertex::Index dst, uint64_t block) {
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070051 Vertex::EdgeMap::iterator edge_it = src->out_edges.find(dst);
52 if (edge_it == src->out_edges.end()) {
53 // Must create new edge
54 pair<Vertex::EdgeMap::iterator, bool> result =
55 src->out_edges.insert(make_pair(dst, EdgeProperties()));
56 CHECK(result.second);
57 edge_it = result.first;
58 }
59 AppendBlockToExtents(&edge_it->second.extents, block);
60}
61
62void AddReadBeforeDepExtents(Vertex* src,
63 Vertex::Index dst,
64 const vector<Extent>& extents) {
65 // TODO(adlr): Be more efficient than adding each block individually.
66 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
Amin Hassani232f8f92019-01-14 16:15:31 -080067 it != e;
68 ++it) {
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070069 const Extent& extent = *it;
70 for (uint64_t block = extent.start_block(),
Amin Hassani232f8f92019-01-14 16:15:31 -080071 block_end = extent.start_block() + extent.num_blocks();
72 block != block_end;
73 ++block) {
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070074 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();
Amin Hassani232f8f92019-01-14 16:15:31 -080082 it != edge_map->end();) {
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070083 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 {
Amin Hassani232f8f92019-01-14 16:15:31 -0800104template <typename T>
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700105void DumpExtents(const T& field, int prepend_space_count) {
106 string header(prepend_space_count, ' ');
Amin Hassanid8b67f42017-12-06 13:47:52 -0800107 for (const auto& extent : field) {
108 LOG(INFO) << header << "(" << extent.start_block() << ", "
109 << extent.num_blocks() << ")";
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700110 }
111}
112
113void DumpOutEdges(const Vertex::EdgeMap& out_edges) {
114 for (Vertex::EdgeMap::const_iterator it = out_edges.begin(),
Amin Hassani232f8f92019-01-14 16:15:31 -0800115 e = out_edges.end();
116 it != e;
117 ++it) {
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700118 LOG(INFO) << " " << it->first << " read-before:";
119 DumpExtents(it->second.extents, 6);
120 LOG(INFO) << " write-before:";
121 DumpExtents(it->second.write_extents, 6);
122 }
123}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700124} // namespace
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700125
126void DumpGraph(const Graph& graph) {
127 LOG(INFO) << "Graph length: " << graph.size();
128 for (Graph::size_type i = 0, e = graph.size(); i != e; ++i) {
Amin Hassani232f8f92019-01-14 16:15:31 -0800129 LOG(INFO) << i << (graph[i].valid ? "" : "-INV") << ": "
130 << graph[i].aop.name << ": "
131 << InstallOperationTypeName(graph[i].aop.op.type());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700132 LOG(INFO) << " src_extents:";
Alex Deymo3b2c7d02015-07-16 16:08:16 -0700133 DumpExtents(graph[i].aop.op.src_extents(), 4);
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700134 LOG(INFO) << " dst_extents:";
Alex Deymo3b2c7d02015-07-16 16:08:16 -0700135 DumpExtents(graph[i].aop.op.dst_extents(), 4);
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700136 LOG(INFO) << " out edges:";
137 DumpOutEdges(graph[i].out_edges);
138 }
139}
140
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800141} // namespace graph_utils
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800142} // namespace chromeos_update_engine