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