diff --git a/fs_mgr/libsnapshot/snapuserd/Android.bp b/fs_mgr/libsnapshot/snapuserd/Android.bp index a67e37c1b..926148201 100644 --- a/fs_mgr/libsnapshot/snapuserd/Android.bp +++ b/fs_mgr/libsnapshot/snapuserd/Android.bp @@ -25,8 +25,6 @@ cc_defaults { ], cflags: [ "-D_FILE_OFFSET_BITS=64", - "-Wall", - "-Werror", ], export_include_dirs: ["include"], srcs: [ @@ -54,6 +52,39 @@ cc_library_static { vendor_ramdisk_available: true, } +cc_library_static { + name: "libsnapuserd", + defaults: [ + "fs_mgr_defaults", + ], + srcs: [ + "dm-snapshot-merge/snapuserd.cpp", + "dm-snapshot-merge/snapuserd_worker.cpp", + "dm-snapshot-merge/snapuserd_readahead.cpp", + "snapuserd_buffer.cpp", + "user-space-merge/snapuserd_core.cpp", + "user-space-merge/snapuserd_dm_user.cpp", + "user-space-merge/snapuserd_merge.cpp", + "user-space-merge/snapuserd_readahead.cpp", + "user-space-merge/snapuserd_transitions.cpp", + "user-space-merge/snapuserd_verify.cpp", + ], + static_libs: [ + "libbase", + "libdm", + "libext4_utils", + "libsnapshot_cow", + "liburing", + ], + include_dirs: ["bionic/libc/kernel"], + header_libs: [ + "libstorage_literals_headers", + ], + ramdisk_available: true, + vendor_ramdisk_available: true, + recovery_available: true, +} + cc_defaults { name: "snapuserd_defaults", defaults: [ @@ -61,23 +92,8 @@ cc_defaults { ], srcs: [ "dm-snapshot-merge/snapuserd_server.cpp", - "dm-snapshot-merge/snapuserd.cpp", - "dm-snapshot-merge/snapuserd_worker.cpp", - "dm-snapshot-merge/snapuserd_readahead.cpp", "snapuserd_daemon.cpp", - "snapuserd_buffer.cpp", - "user-space-merge/snapuserd_core.cpp", - "user-space-merge/snapuserd_dm_user.cpp", - "user-space-merge/snapuserd_merge.cpp", - "user-space-merge/snapuserd_readahead.cpp", - "user-space-merge/snapuserd_transitions.cpp", "user-space-merge/snapuserd_server.cpp", - "user-space-merge/snapuserd_verify.cpp", - ], - - cflags: [ - "-Wall", - "-Werror", ], static_libs: [ @@ -90,6 +106,7 @@ cc_defaults { "liblog", "libsnapshot_cow", "libsnapshot_snapuserd", + "libsnapuserd", "libz", "liblz4", "libext4_utils", @@ -125,7 +142,6 @@ cc_binary { ], ramdisk_available: false, vendor_ramdisk_available: true, - recovery_available: true, } // This target will install to /system/bin/snapuserd_ramdisk @@ -158,10 +174,6 @@ cc_test { "dm-snapshot-merge/snapuserd_worker.cpp", "snapuserd_buffer.cpp", ], - cflags: [ - "-Wall", - "-Werror", - ], shared_libs: [ "libbase", "liblog", @@ -197,10 +209,6 @@ cc_test { srcs: [ "user-space-merge/snapuserd_test.cpp", ], - cflags: [ - "-Wall", - "-Werror", - ], shared_libs: [ "libbase", "liblog", diff --git a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_core.cpp b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_core.cpp index 492c43f42..2c201ffdb 100644 --- a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_core.cpp +++ b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_core.cpp @@ -31,30 +31,21 @@ using namespace android::dm; using android::base::unique_fd; SnapshotHandler::SnapshotHandler(std::string misc_name, std::string cow_device, - std::string backing_device, std::string base_path_merge) { + std::string backing_device, std::string base_path_merge, + int num_worker_threads, bool use_iouring, + bool perform_verification) { misc_name_ = std::move(misc_name); cow_device_ = std::move(cow_device); backing_store_device_ = std::move(backing_device); control_device_ = "/dev/dm-user/" + misc_name_; base_path_merge_ = std::move(base_path_merge); + num_worker_threads_ = num_worker_threads; + is_io_uring_enabled_ = use_iouring; + perform_verification_ = perform_verification; } bool SnapshotHandler::InitializeWorkers() { - int num_worker_threads = kNumWorkerThreads; - - // We will need multiple worker threads only during - // device boot after OTA. For all other purposes, - // one thread is sufficient. We don't want to consume - // unnecessary memory especially during OTA install phase - // when daemon will be up during entire post install phase. - // - // During boot up, we need multiple threads primarily for - // update-verification. - if (is_socket_present_) { - num_worker_threads = 1; - } - - for (int i = 0; i < num_worker_threads; i++) { + for (int i = 0; i < num_worker_threads_; i++) { std::unique_ptr wt = std::make_unique(cow_device_, backing_store_device_, control_device_, misc_name_, base_path_merge_, GetSharedPtr()); @@ -331,19 +322,11 @@ bool SnapshotHandler::Start() { std::async(std::launch::async, &Worker::RunThread, worker_threads_[i].get())); } - bool partition_verification = true; - - // We don't want to read the blocks during first stage init or - // during post-install phase. - if (android::base::EndsWith(misc_name_, "-init") || is_socket_present_) { - partition_verification = false; - } - std::future merge_thread = std::async(std::launch::async, &Worker::RunMergeThread, merge_thread_.get()); // Now that the worker threads are up, scan the partitions. - if (partition_verification) { + if (perform_verification_) { update_verify_->VerifyUpdatePartition(); } diff --git a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_core.h b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_core.h index 42237ef60..777aa0705 100644 --- a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_core.h +++ b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_core.h @@ -301,7 +301,8 @@ class Worker { class SnapshotHandler : public std::enable_shared_from_this { public: SnapshotHandler(std::string misc_name, std::string cow_device, std::string backing_device, - std::string base_path_merge); + std::string base_path_merge, int num_workers, bool use_iouring, + bool perform_verification); bool InitCowDevice(); bool Start(); @@ -369,8 +370,6 @@ class SnapshotHandler : public std::enable_shared_from_this { // Total number of blocks to be merged in a given read-ahead buffer region void SetMergedBlockCountForNextCommit(int x) { total_ra_blocks_merged_ = x; } int GetTotalBlocksToMerge() { return total_ra_blocks_merged_; } - void SetSocketPresent(bool socket) { is_socket_present_ = socket; } - void SetIouringEnabled(bool io_uring_enabled) { is_io_uring_enabled_ = io_uring_enabled; } bool MergeInitiated() { return merge_initiated_; } bool MergeMonitored() { return merge_monitored_; } double GetMergePercentage() { return merge_completion_percentage_; } @@ -441,9 +440,10 @@ class SnapshotHandler : public std::enable_shared_from_this { bool merge_initiated_ = false; bool merge_monitored_ = false; bool attached_ = false; - bool is_socket_present_; bool is_io_uring_enabled_ = false; bool scratch_space_ = false; + int num_worker_threads_ = kNumWorkerThreads; + bool perform_verification_ = true; std::unique_ptr ring_; std::unique_ptr update_verify_; diff --git a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_server.cpp b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_server.cpp index d437d3286..b7ce2109a 100644 --- a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_server.cpp +++ b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_server.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include "snapuserd_server.h" @@ -101,7 +102,7 @@ void UserSnapshotServer::ShutdownThreads() { JoinAllThreads(); } -UserSnapshotDmUserHandler::UserSnapshotDmUserHandler(std::shared_ptr snapuserd) +HandlerThread::HandlerThread(std::shared_ptr snapuserd) : snapuserd_(snapuserd), misc_name_(snapuserd_->GetMiscName()) {} bool UserSnapshotServer::Sendmsg(android::base::borrowed_fd fd, const std::string& msg) { @@ -307,7 +308,7 @@ bool UserSnapshotServer::Receivemsg(android::base::borrowed_fd fd, const std::st } } -void UserSnapshotServer::RunThread(std::shared_ptr handler) { +void UserSnapshotServer::RunThread(std::shared_ptr handler) { LOG(INFO) << "Entering thread for handler: " << handler->misc_name(); if (!handler->snapuserd()->Start()) { @@ -428,7 +429,7 @@ bool UserSnapshotServer::Run() { void UserSnapshotServer::JoinAllThreads() { // Acquire the thread list within the lock. - std::vector> dm_users; + std::vector> dm_users; { std::lock_guard guard(lock_); dm_users = std::move(dm_users_); @@ -483,25 +484,42 @@ void UserSnapshotServer::Interrupt() { SetTerminating(); } -std::shared_ptr UserSnapshotServer::AddHandler( - const std::string& misc_name, const std::string& cow_device_path, - const std::string& backing_device, const std::string& base_path_merge) { +std::shared_ptr UserSnapshotServer::AddHandler(const std::string& misc_name, + const std::string& cow_device_path, + const std::string& backing_device, + const std::string& base_path_merge) { + // We will need multiple worker threads only during + // device boot after OTA. For all other purposes, + // one thread is sufficient. We don't want to consume + // unnecessary memory especially during OTA install phase + // when daemon will be up during entire post install phase. + // + // During boot up, we need multiple threads primarily for + // update-verification. + int num_worker_threads = kNumWorkerThreads; + if (is_socket_present_) { + num_worker_threads = 1; + } + + bool perform_verification = true; + if (android::base::EndsWith(misc_name, "-init") || is_socket_present_) { + perform_verification = false; + } + auto snapuserd = std::make_shared(misc_name, cow_device_path, backing_device, - base_path_merge); + base_path_merge, num_worker_threads, + io_uring_enabled_, perform_verification); if (!snapuserd->InitCowDevice()) { LOG(ERROR) << "Failed to initialize Snapuserd"; return nullptr; } - snapuserd->SetSocketPresent(is_socket_present_); - snapuserd->SetIouringEnabled(io_uring_enabled_); - if (!snapuserd->InitializeWorkers()) { LOG(ERROR) << "Failed to initialize workers"; return nullptr; } - auto handler = std::make_shared(snapuserd); + auto handler = std::make_shared(snapuserd); { std::lock_guard lock(lock_); if (FindHandler(&lock, misc_name) != dm_users_.end()) { @@ -513,7 +531,7 @@ std::shared_ptr UserSnapshotServer::AddHandler( return handler; } -bool UserSnapshotServer::StartHandler(const std::shared_ptr& handler) { +bool UserSnapshotServer::StartHandler(const std::shared_ptr& handler) { if (handler->snapuserd()->IsAttached()) { LOG(ERROR) << "Handler already attached"; return false; @@ -526,7 +544,7 @@ bool UserSnapshotServer::StartHandler(const std::shared_ptr* proof_of_lock, - const std::shared_ptr& handler) { + const std::shared_ptr& handler) { CHECK(proof_of_lock); if (!handler->snapuserd()->IsAttached()) { @@ -568,8 +586,7 @@ void UserSnapshotServer::TerminateMergeThreads(std::lock_guard* proo } } -std::string UserSnapshotServer::GetMergeStatus( - const std::shared_ptr& handler) { +std::string UserSnapshotServer::GetMergeStatus(const std::shared_ptr& handler) { return handler->snapuserd()->GetMergeStatus(); } @@ -604,7 +621,7 @@ double UserSnapshotServer::GetMergePercentage(std::lock_guard* proof } bool UserSnapshotServer::RemoveAndJoinHandler(const std::string& misc_name) { - std::shared_ptr handler; + std::shared_ptr handler; { std::lock_guard lock(lock_); diff --git a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_server.h b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_server.h index c2af61fbf..12c3903c4 100644 --- a/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_server.h +++ b/fs_mgr/libsnapshot/snapuserd/user-space-merge/snapuserd_server.h @@ -54,9 +54,9 @@ enum class DaemonOps { INVALID, }; -class UserSnapshotDmUserHandler { +class HandlerThread { public: - explicit UserSnapshotDmUserHandler(std::shared_ptr snapuserd); + explicit HandlerThread(std::shared_ptr snapuserd); void FreeResources() { // Each worker thread holds a reference to snapuserd. @@ -99,9 +99,9 @@ class UserSnapshotServer { std::mutex lock_; - using HandlerList = std::vector>; + using HandlerList = std::vector>; HandlerList dm_users_; - std::queue> merge_handlers_; + std::queue> merge_handlers_; void AddWatchedFd(android::base::borrowed_fd fd, int events); void AcceptClient(); @@ -118,13 +118,13 @@ class UserSnapshotServer { bool IsTerminating() { return terminating_; } - void RunThread(std::shared_ptr handler); + void RunThread(std::shared_ptr handler); void MonitorMerge(); void JoinAllThreads(); bool StartWithSocket(bool start_listening); - // Find a UserSnapshotDmUserHandler within a lock. + // Find a HandlerThread within a lock. HandlerList::iterator FindHandler(std::lock_guard* proof_of_lock, const std::string& misc_name); @@ -143,14 +143,14 @@ class UserSnapshotServer { bool RunForSocketHandoff(); bool WaitForSocket(); - std::shared_ptr AddHandler(const std::string& misc_name, - const std::string& cow_device_path, - const std::string& backing_device, - const std::string& base_path_merge); - bool StartHandler(const std::shared_ptr& handler); + std::shared_ptr AddHandler(const std::string& misc_name, + const std::string& cow_device_path, + const std::string& backing_device, + const std::string& base_path_merge); + bool StartHandler(const std::shared_ptr& handler); bool StartMerge(std::lock_guard* proof_of_lock, - const std::shared_ptr& handler); - std::string GetMergeStatus(const std::shared_ptr& handler); + const std::shared_ptr& handler); + std::string GetMergeStatus(const std::shared_ptr& handler); void WakeupMonitorMergeThread(); void SetTerminating() { terminating_ = true; }