blob: 3f9b28ff388f0ac228f1ae5f0dbb50f0e3180eb7 [file] [log] [blame]
Darin Petkov8e447e02013-04-16 16:23:50 +02001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -08002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo161c4a12014-05-16 15:56:21 -07005#include "update_engine/payload_generator/graph_utils.h"
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -08006
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -07007#include <string>
8#include <utility>
9#include <vector>
10
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070011#include <base/logging.h>
Ben Chan05735a12014-09-03 07:48:22 -070012#include <base/macros.h>
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070013
Alex Deymo161c4a12014-05-16 15:56:21 -070014#include "update_engine/payload_constants.h"
Alex Deymo477aec22015-03-24 23:40:48 -070015#include "update_engine/payload_generator/annotated_operation.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070016
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070017using std::make_pair;
18using std::pair;
19using std::string;
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080020using std::vector;
21
22namespace chromeos_update_engine {
23
24namespace graph_utils {
25
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070026uint64_t EdgeWeight(const Graph& graph, const Edge& edge) {
27 uint64_t weight = 0;
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080028 const vector<Extent>& extents =
29 graph[edge.first].out_edges.find(edge.second)->second.extents;
30 for (vector<Extent>::const_iterator it = extents.begin();
31 it != extents.end(); ++it) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070032 if (it->start_block() != kSparseHole)
33 weight += it->num_blocks();
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080034 }
35 return weight;
36}
37
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070038void AppendBlockToExtents(vector<Extent>* extents, uint64_t block) {
Darin Petkov94817cb2013-05-08 14:33:24 +020039 // First try to extend the last extent in |extents|, if any.
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080040 if (!extents->empty()) {
41 Extent& extent = extents->back();
Darin Petkov94817cb2013-05-08 14:33:24 +020042 uint64_t next_block = extent.start_block() == kSparseHole ?
43 kSparseHole : extent.start_block() + extent.num_blocks();
44 if (next_block == block) {
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080045 extent.set_num_blocks(extent.num_blocks() + 1);
46 return;
47 }
48 }
Darin Petkov94817cb2013-05-08 14:33:24 +020049 // If unable to extend the last extent, append a new single-block extent.
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080050 Extent new_extent;
51 new_extent.set_start_block(block);
52 new_extent.set_num_blocks(1);
53 extents->push_back(new_extent);
54}
55
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070056void AddReadBeforeDep(Vertex* src,
57 Vertex::Index dst,
58 uint64_t block) {
59 Vertex::EdgeMap::iterator edge_it = src->out_edges.find(dst);
60 if (edge_it == src->out_edges.end()) {
61 // Must create new edge
62 pair<Vertex::EdgeMap::iterator, bool> result =
63 src->out_edges.insert(make_pair(dst, EdgeProperties()));
64 CHECK(result.second);
65 edge_it = result.first;
66 }
67 AppendBlockToExtents(&edge_it->second.extents, block);
68}
69
70void AddReadBeforeDepExtents(Vertex* src,
71 Vertex::Index dst,
72 const vector<Extent>& extents) {
73 // TODO(adlr): Be more efficient than adding each block individually.
74 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
75 it != e; ++it) {
76 const Extent& extent = *it;
77 for (uint64_t block = extent.start_block(),
78 block_end = extent.start_block() + extent.num_blocks();
79 block != block_end; ++block) {
80 AddReadBeforeDep(src, dst, block);
81 }
82 }
83}
84
85void DropWriteBeforeDeps(Vertex::EdgeMap* edge_map) {
86 // Specially crafted for-loop for the map-iterate-delete dance.
87 for (Vertex::EdgeMap::iterator it = edge_map->begin();
88 it != edge_map->end(); ) {
89 if (!it->second.write_extents.empty())
90 it->second.write_extents.clear();
91 if (it->second.extents.empty()) {
92 // Erase *it, as it contains no blocks
93 edge_map->erase(it++);
94 } else {
95 ++it;
96 }
97 }
98}
99
100// For each node N in graph, drop all edges N->|index|.
101void DropIncomingEdgesTo(Graph* graph, Vertex::Index index) {
102 // This would be much more efficient if we had doubly-linked
103 // edges in the graph.
104 for (Graph::iterator it = graph->begin(), e = graph->end(); it != e; ++it) {
105 it->out_edges.erase(index);
106 }
107}
108
Alex Deymof329b932014-10-30 01:37:48 -0700109Extent GetElement(const vector<Extent>& collection, size_t index) {
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700110 return collection[index];
111}
112Extent GetElement(
113 const google::protobuf::RepeatedPtrField<Extent>& collection,
114 size_t index) {
115 return collection.Get(index);
116}
117
118namespace {
119template<typename T>
120void DumpExtents(const T& field, int prepend_space_count) {
121 string header(prepend_space_count, ' ');
122 for (int i = 0, e = field.size(); i != e; ++i) {
123 LOG(INFO) << header << "(" << GetElement(field, i).start_block() << ", "
124 << GetElement(field, i).num_blocks() << ")";
125 }
126}
127
128void DumpOutEdges(const Vertex::EdgeMap& out_edges) {
129 for (Vertex::EdgeMap::const_iterator it = out_edges.begin(),
130 e = out_edges.end(); it != e; ++it) {
131 LOG(INFO) << " " << it->first << " read-before:";
132 DumpExtents(it->second.extents, 6);
133 LOG(INFO) << " write-before:";
134 DumpExtents(it->second.write_extents, 6);
135 }
136}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700137} // namespace
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700138
139void DumpGraph(const Graph& graph) {
140 LOG(INFO) << "Graph length: " << graph.size();
141 for (Graph::size_type i = 0, e = graph.size(); i != e; ++i) {
Darin Petkov8e447e02013-04-16 16:23:50 +0200142 LOG(INFO) << i
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700143 << (graph[i].valid ? "" : "-INV")
144 << ": " << graph[i].file_name
Darin Petkov8e447e02013-04-16 16:23:50 +0200145 << " " << graph[i].chunk_size << "@" << graph[i].chunk_offset
Alex Deymo477aec22015-03-24 23:40:48 -0700146 << ": " << InstallOperationTypeName(graph[i].op.type());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700147 LOG(INFO) << " src_extents:";
148 DumpExtents(graph[i].op.src_extents(), 4);
149 LOG(INFO) << " dst_extents:";
150 DumpExtents(graph[i].op.dst_extents(), 4);
151 LOG(INFO) << " out edges:";
152 DumpOutEdges(graph[i].out_edges);
153 }
154}
155
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800156} // namespace graph_utils
157
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700158bool operator==(const Extent& a, const Extent& b) {
159 return a.start_block() == b.start_block() && a.num_blocks() == b.num_blocks();
160}
161
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800162} // namespace chromeos_update_engine