Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 5 | #include "update_engine/payload_generator/graph_utils.h" |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 6 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame] | 12 | #include <base/macros.h> |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 13 | |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 14 | #include "update_engine/payload_constants.h" |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame^] | 15 | #include "update_engine/payload_generator/annotated_operation.h" |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 16 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 17 | using std::make_pair; |
| 18 | using std::pair; |
| 19 | using std::string; |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 20 | using std::vector; |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
| 24 | namespace graph_utils { |
| 25 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 26 | uint64_t EdgeWeight(const Graph& graph, const Edge& edge) { |
| 27 | uint64_t weight = 0; |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 28 | 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 Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 32 | if (it->start_block() != kSparseHole) |
| 33 | weight += it->num_blocks(); |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 34 | } |
| 35 | return weight; |
| 36 | } |
| 37 | |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 38 | void AppendBlockToExtents(vector<Extent>* extents, uint64_t block) { |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 39 | // First try to extend the last extent in |extents|, if any. |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 40 | if (!extents->empty()) { |
| 41 | Extent& extent = extents->back(); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 42 | uint64_t next_block = extent.start_block() == kSparseHole ? |
| 43 | kSparseHole : extent.start_block() + extent.num_blocks(); |
| 44 | if (next_block == block) { |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 45 | extent.set_num_blocks(extent.num_blocks() + 1); |
| 46 | return; |
| 47 | } |
| 48 | } |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 49 | // If unable to extend the last extent, append a new single-block extent. |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 50 | 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 Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 56 | void 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 | |
| 70 | void 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 | |
| 85 | void 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|. |
| 101 | void 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 Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 109 | Extent GetElement(const vector<Extent>& collection, size_t index) { |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 110 | return collection[index]; |
| 111 | } |
| 112 | Extent GetElement( |
| 113 | const google::protobuf::RepeatedPtrField<Extent>& collection, |
| 114 | size_t index) { |
| 115 | return collection.Get(index); |
| 116 | } |
| 117 | |
| 118 | namespace { |
| 119 | template<typename T> |
| 120 | void 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 | |
| 128 | void 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 Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 137 | } // namespace |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 138 | |
| 139 | void 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 Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 142 | LOG(INFO) << i |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 143 | << (graph[i].valid ? "" : "-INV") |
| 144 | << ": " << graph[i].file_name |
Darin Petkov | 8e447e0 | 2013-04-16 16:23:50 +0200 | [diff] [blame] | 145 | << " " << graph[i].chunk_size << "@" << graph[i].chunk_offset |
Alex Deymo | 477aec2 | 2015-03-24 23:40:48 -0700 | [diff] [blame^] | 146 | << ": " << InstallOperationTypeName(graph[i].op.type()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 147 | 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 Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 156 | } // namespace graph_utils |
| 157 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 158 | bool 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 Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 162 | } // namespace chromeos_update_engine |