xref: /aosp_15_r20/system/core/fs_mgr/libsnapshot/snapuserd/user-space-merge/merge_worker.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include "worker.h"
17 
18 #include <liburing.h>
19 
20 namespace android {
21 namespace snapshot {
22 
23 class MergeWorker : public Worker {
24   public:
25     MergeWorker(const std::string& cow_device, const std::string& misc_name,
26                 const std::string& base_path_merge, std::shared_ptr<SnapshotHandler> snapuserd,
27                 uint32_t cow_op_merge_size);
28     bool Run();
29 
30   private:
31     int PrepareMerge(uint64_t* source_offset, int* pending_ops,
32                      std::vector<const CowOperation*>* replace_zero_vec = nullptr);
33     bool MergeReplaceZeroOps();
34     bool MergeOrderedOps();
35     bool MergeOrderedOpsAsync();
36     bool Merge();
37     bool AsyncMerge();
38     bool SyncMerge();
39     bool InitializeIouring();
40     void FinalizeIouring();
41 
42   private:
43     BufferSink bufsink_;
44     std::unique_ptr<ICowOpIter> cowop_iter_;
45     std::unique_ptr<struct io_uring> ring_;
46     size_t ra_block_index_ = 0;
47     uint64_t blocks_merged_in_group_ = 0;
48     bool merge_async_ = false;
49     // Queue depth of 8 seems optimal. We don't want
50     // to have a huge depth as it may put more memory pressure
51     // on the kernel worker threads given that we use
52     // IOSQE_ASYNC flag - ASYNC flags can potentially
53     // result in EINTR; Since we don't restart
54     // syscalls and fallback to synchronous I/O, we
55     // don't want huge queue depth
56     int queue_depth_ = 8;
57     uint32_t cow_op_merge_size_ = 0;
58 };
59 
60 }  // namespace snapshot
61 }  // namespace android
62