blob: 7b03236d2032c93a77939294e119ed2276895874 [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 Deymo5c6c6552015-06-03 19:06:50 +020016#include "update_engine/payload_generator/extent_utils.h"
Alex Deymo161c4a12014-05-16 15:56:21 -070017
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070018using std::make_pair;
19using std::pair;
20using std::string;
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080021using std::vector;
22
23namespace chromeos_update_engine {
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080024namespace 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 Reyes1bc16ab2010-10-06 15:07:21 -070038void AddReadBeforeDep(Vertex* src,
39 Vertex::Index dst,
40 uint64_t block) {
41 Vertex::EdgeMap::iterator edge_it = src->out_edges.find(dst);
42 if (edge_it == src->out_edges.end()) {
43 // Must create new edge
44 pair<Vertex::EdgeMap::iterator, bool> result =
45 src->out_edges.insert(make_pair(dst, EdgeProperties()));
46 CHECK(result.second);
47 edge_it = result.first;
48 }
49 AppendBlockToExtents(&edge_it->second.extents, block);
50}
51
52void AddReadBeforeDepExtents(Vertex* src,
53 Vertex::Index dst,
54 const vector<Extent>& extents) {
55 // TODO(adlr): Be more efficient than adding each block individually.
56 for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
57 it != e; ++it) {
58 const Extent& extent = *it;
59 for (uint64_t block = extent.start_block(),
60 block_end = extent.start_block() + extent.num_blocks();
61 block != block_end; ++block) {
62 AddReadBeforeDep(src, dst, block);
63 }
64 }
65}
66
67void DropWriteBeforeDeps(Vertex::EdgeMap* edge_map) {
68 // Specially crafted for-loop for the map-iterate-delete dance.
69 for (Vertex::EdgeMap::iterator it = edge_map->begin();
70 it != edge_map->end(); ) {
71 if (!it->second.write_extents.empty())
72 it->second.write_extents.clear();
73 if (it->second.extents.empty()) {
74 // Erase *it, as it contains no blocks
75 edge_map->erase(it++);
76 } else {
77 ++it;
78 }
79 }
80}
81
82// For each node N in graph, drop all edges N->|index|.
83void DropIncomingEdgesTo(Graph* graph, Vertex::Index index) {
84 // This would be much more efficient if we had doubly-linked
85 // edges in the graph.
86 for (Graph::iterator it = graph->begin(), e = graph->end(); it != e; ++it) {
87 it->out_edges.erase(index);
88 }
89}
90
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070091namespace {
92template<typename T>
93void DumpExtents(const T& field, int prepend_space_count) {
94 string header(prepend_space_count, ' ');
95 for (int i = 0, e = field.size(); i != e; ++i) {
96 LOG(INFO) << header << "(" << GetElement(field, i).start_block() << ", "
97 << GetElement(field, i).num_blocks() << ")";
98 }
99}
100
101void DumpOutEdges(const Vertex::EdgeMap& out_edges) {
102 for (Vertex::EdgeMap::const_iterator it = out_edges.begin(),
103 e = out_edges.end(); it != e; ++it) {
104 LOG(INFO) << " " << it->first << " read-before:";
105 DumpExtents(it->second.extents, 6);
106 LOG(INFO) << " write-before:";
107 DumpExtents(it->second.write_extents, 6);
108 }
109}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700110} // namespace
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700111
112void DumpGraph(const Graph& graph) {
113 LOG(INFO) << "Graph length: " << graph.size();
114 for (Graph::size_type i = 0, e = graph.size(); i != e; ++i) {
Darin Petkov8e447e02013-04-16 16:23:50 +0200115 LOG(INFO) << i
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700116 << (graph[i].valid ? "" : "-INV")
117 << ": " << graph[i].file_name
Darin Petkov8e447e02013-04-16 16:23:50 +0200118 << " " << graph[i].chunk_size << "@" << graph[i].chunk_offset
Alex Deymo477aec22015-03-24 23:40:48 -0700119 << ": " << InstallOperationTypeName(graph[i].op.type());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700120 LOG(INFO) << " src_extents:";
121 DumpExtents(graph[i].op.src_extents(), 4);
122 LOG(INFO) << " dst_extents:";
123 DumpExtents(graph[i].op.dst_extents(), 4);
124 LOG(INFO) << " out edges:";
125 DumpOutEdges(graph[i].out_edges);
126 }
127}
128
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800129} // namespace graph_utils
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -0800130} // namespace chromeos_update_engine