• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2020 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 
17 #ifndef UPDATE_ENGINE_PAYLOAD_GENERATOR_MERGE_SEQUENCE_GENERATOR_H_
18 #define UPDATE_ENGINE_PAYLOAD_GENERATOR_MERGE_SEQUENCE_GENERATOR_H_
19 
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <utility>
24 #include <vector>
25 
26 #include "update_engine/payload_generator/annotated_operation.h"
27 #include "update_engine/payload_generator/extent_ranges.h"
28 #include "update_engine/payload_generator/extent_utils.h"
29 #include "update_engine/update_metadata.pb.h"
30 
31 namespace chromeos_update_engine {
32 // Constructs CowMergeOperation from src & dst extents
33 CowMergeOperation CreateCowMergeOperation(const Extent& src_extent,
34                                           const Extent& dst_extent);
35 
36 // Comparator for CowMergeOperation.
37 bool operator<(const CowMergeOperation& op1, const CowMergeOperation& op2);
38 bool operator==(const CowMergeOperation& op1, const CowMergeOperation& op2);
39 
40 std::ostream& operator<<(std::ostream& os,
41                          const CowMergeOperation& merge_operation);
42 
43 // This class takes a list of CowMergeOperations; and sorts them so that no
44 // read after write will happen by following the sequence. When there is a
45 // cycle, we will omit some operations in the list. Therefore, the result
46 // sequence may not contain all blocks in the input list.
47 class MergeSequenceGenerator {
48  public:
49   // Creates an object from a list of OTA InstallOperations. Returns nullptr on
50   // failure.
51   static std::unique_ptr<MergeSequenceGenerator> Create(
52       const std::vector<AnnotatedOperation>& aops);
53   // Checks that no read after write happens in the given sequence.
54   static bool ValidateSequence(const std::vector<CowMergeOperation>& sequence);
55 
56   // Generates a merge sequence from |operations_|, puts the result in
57   // |sequence|. Returns false on failure.
58   bool Generate(std::vector<CowMergeOperation>* sequence) const;
59 
60  private:
61   friend class MergeSequenceGeneratorTest;
MergeSequenceGenerator(std::vector<CowMergeOperation> transfers)62   explicit MergeSequenceGenerator(std::vector<CowMergeOperation> transfers)
63       : operations_(std::move(transfers)) {}
64 
65   // For a given merge operation, finds all the operations that should merge
66   // after myself. Put the result in |merge_after|.
67   bool FindDependency(std::map<CowMergeOperation, std::set<CowMergeOperation>>*
68                           merge_after) const;
69   // The list of CowMergeOperations to sort.
70   std::vector<CowMergeOperation> operations_;
71 };
72 
73 void SplitSelfOverlapping(const Extent& src_extent,
74                           const Extent& dst_extent,
75                           std::vector<CowMergeOperation>* sequence);
76 
77 }  // namespace chromeos_update_engine
78 #endif
79