graph_utils.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // Copyright (C) 2009 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/payload_generator/graph_utils.h"
  17. #include <string>
  18. #include <utility>
  19. #include <vector>
  20. #include <base/logging.h>
  21. #include <base/macros.h>
  22. #include "update_engine/payload_consumer/payload_constants.h"
  23. #include "update_engine/payload_generator/annotated_operation.h"
  24. #include "update_engine/payload_generator/extent_utils.h"
  25. using std::make_pair;
  26. using std::pair;
  27. using std::string;
  28. using std::vector;
  29. namespace chromeos_update_engine {
  30. namespace graph_utils {
  31. uint64_t EdgeWeight(const Graph& graph, const Edge& edge) {
  32. uint64_t weight = 0;
  33. const vector<Extent>& extents =
  34. graph[edge.first].out_edges.find(edge.second)->second.extents;
  35. for (vector<Extent>::const_iterator it = extents.begin(); it != extents.end();
  36. ++it) {
  37. if (it->start_block() != kSparseHole)
  38. weight += it->num_blocks();
  39. }
  40. return weight;
  41. }
  42. void AddReadBeforeDep(Vertex* src, Vertex::Index dst, uint64_t block) {
  43. Vertex::EdgeMap::iterator edge_it = src->out_edges.find(dst);
  44. if (edge_it == src->out_edges.end()) {
  45. // Must create new edge
  46. pair<Vertex::EdgeMap::iterator, bool> result =
  47. src->out_edges.insert(make_pair(dst, EdgeProperties()));
  48. CHECK(result.second);
  49. edge_it = result.first;
  50. }
  51. AppendBlockToExtents(&edge_it->second.extents, block);
  52. }
  53. void AddReadBeforeDepExtents(Vertex* src,
  54. Vertex::Index dst,
  55. const vector<Extent>& extents) {
  56. // TODO(adlr): Be more efficient than adding each block individually.
  57. for (vector<Extent>::const_iterator it = extents.begin(), e = extents.end();
  58. it != e;
  59. ++it) {
  60. const Extent& extent = *it;
  61. for (uint64_t block = extent.start_block(),
  62. block_end = extent.start_block() + extent.num_blocks();
  63. block != block_end;
  64. ++block) {
  65. AddReadBeforeDep(src, dst, block);
  66. }
  67. }
  68. }
  69. void DropWriteBeforeDeps(Vertex::EdgeMap* edge_map) {
  70. // Specially crafted for-loop for the map-iterate-delete dance.
  71. for (Vertex::EdgeMap::iterator it = edge_map->begin();
  72. it != edge_map->end();) {
  73. if (!it->second.write_extents.empty())
  74. it->second.write_extents.clear();
  75. if (it->second.extents.empty()) {
  76. // Erase *it, as it contains no blocks
  77. edge_map->erase(it++);
  78. } else {
  79. ++it;
  80. }
  81. }
  82. }
  83. // For each node N in graph, drop all edges N->|index|.
  84. void DropIncomingEdgesTo(Graph* graph, Vertex::Index index) {
  85. // This would be much more efficient if we had doubly-linked
  86. // edges in the graph.
  87. for (Graph::iterator it = graph->begin(), e = graph->end(); it != e; ++it) {
  88. it->out_edges.erase(index);
  89. }
  90. }
  91. namespace {
  92. template <typename T>
  93. void DumpExtents(const T& field, int prepend_space_count) {
  94. string header(prepend_space_count, ' ');
  95. for (const auto& extent : field) {
  96. LOG(INFO) << header << "(" << extent.start_block() << ", "
  97. << extent.num_blocks() << ")";
  98. }
  99. }
  100. void DumpOutEdges(const Vertex::EdgeMap& out_edges) {
  101. for (Vertex::EdgeMap::const_iterator it = out_edges.begin(),
  102. e = out_edges.end();
  103. it != e;
  104. ++it) {
  105. LOG(INFO) << " " << it->first << " read-before:";
  106. DumpExtents(it->second.extents, 6);
  107. LOG(INFO) << " write-before:";
  108. DumpExtents(it->second.write_extents, 6);
  109. }
  110. }
  111. } // namespace
  112. void DumpGraph(const Graph& graph) {
  113. LOG(INFO) << "Graph length: " << graph.size();
  114. for (Graph::size_type i = 0, e = graph.size(); i != e; ++i) {
  115. LOG(INFO) << i << (graph[i].valid ? "" : "-INV") << ": "
  116. << graph[i].aop.name << ": "
  117. << InstallOperationTypeName(graph[i].aop.op.type());
  118. LOG(INFO) << " src_extents:";
  119. DumpExtents(graph[i].aop.op.src_extents(), 4);
  120. LOG(INFO) << " dst_extents:";
  121. DumpExtents(graph[i].aop.op.dst_extents(), 4);
  122. LOG(INFO) << " out edges:";
  123. DumpOutEdges(graph[i].out_edges);
  124. }
  125. }
  126. } // namespace graph_utils
  127. } // namespace chromeos_update_engine