Alex Deymo | 759c275 | 2014-03-17 21:09:36 -0700 | [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 Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_GRAPH_UTILS_H_ |
| 6 | #define UPDATE_ENGINE_PAYLOAD_GENERATOR_GRAPH_UTILS_H_ |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 7 | |
| 8 | #include <vector> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 9 | |
| 10 | #include <base/basictypes.h> |
| 11 | |
| 12 | #include "update_engine/payload_generator/graph_types.h" |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 13 | #include "update_engine/update_metadata.pb.h" |
| 14 | |
| 15 | // A few utility functions for graphs |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | namespace graph_utils { |
| 20 | |
| 21 | // Returns the number of blocks represented by all extents in the edge. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 22 | uint64_t EdgeWeight(const Graph& graph, const Edge& edge); |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 23 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 24 | // These add a read-before dependency from graph[src] -> graph[dst]. If the dep |
| 25 | // already exists, the block/s is/are added to the existing edge. |
| 26 | void AddReadBeforeDep(Vertex* src, |
| 27 | Vertex::Index dst, |
| 28 | uint64_t block); |
| 29 | void AddReadBeforeDepExtents(Vertex* src, |
| 30 | Vertex::Index dst, |
| 31 | const std::vector<Extent>& extents); |
| 32 | |
| 33 | void DropWriteBeforeDeps(Vertex::EdgeMap* edge_map); |
| 34 | |
| 35 | // For each node N in graph, drop all edges N->|index|. |
| 36 | void DropIncomingEdgesTo(Graph* graph, Vertex::Index index); |
| 37 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 38 | // block must either be the next block in the last extent or a block |
| 39 | // in the next extent. This function will not handle inserting block |
| 40 | // into an arbitrary place in the extents. |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 41 | void AppendBlockToExtents(std::vector<Extent>* extents, uint64_t block); |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 42 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 43 | // Get/SetElement are intentionally overloaded so that templated functions |
| 44 | // can accept either type of collection of Extents. |
| 45 | Extent GetElement(const std::vector<Extent>& collection, size_t index); |
| 46 | Extent GetElement( |
| 47 | const google::protobuf::RepeatedPtrField<Extent>& collection, |
| 48 | size_t index); |
| 49 | |
| 50 | template<typename T> |
| 51 | uint64_t BlocksInExtents(const T& collection) { |
| 52 | uint64_t ret = 0; |
| 53 | for (size_t i = 0; i < static_cast<size_t>(collection.size()); ++i) { |
| 54 | ret += GetElement(collection, i).num_blocks(); |
| 55 | } |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | void DumpGraph(const Graph& graph); |
| 60 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 61 | } // namespace graph_utils |
| 62 | |
| 63 | } // namespace chromeos_update_engine |
| 64 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 65 | #endif // UPDATE_ENGINE_PAYLOAD_GENERATOR_GRAPH_UTILS_H_ |