Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8845b1d9 authored by David Anderson's avatar David Anderson Committed by Gerrit Code Review
Browse files

Merge changes I262391dd,I18aad4a5,I0786e475

* changes:
  snapuserd: Move socket and io_uring status out of core and into the server.
  snapuserd: Rename UserSnapshotDmUserHandler to HandlerThread.
  snapuserd: Split out the implementation into a libsnapuserd component.
parents f8b5e48e 35badb7c
Loading
Loading
Loading
Loading
+27 −19
Original line number Diff line number Diff line
@@ -25,8 +25,6 @@ cc_defaults {
    ],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],
    export_include_dirs: ["include"],
    srcs: [
@@ -54,30 +52,48 @@ cc_library_static {
    vendor_ramdisk_available: true,
}

cc_defaults {
    name: "snapuserd_defaults",
cc_library_static {
    name: "libsnapuserd",
    defaults: [
        "fs_mgr_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",
    ],
    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,
}

    cflags: [
        "-Wall",
        "-Werror",
cc_defaults {
    name: "snapuserd_defaults",
    defaults: [
        "fs_mgr_defaults",
    ],
    srcs: [
        "dm-snapshot-merge/snapuserd_server.cpp",
        "snapuserd_daemon.cpp",
        "user-space-merge/snapuserd_server.cpp",
    ],

    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",
+8 −25
Original line number Diff line number Diff line
@@ -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<Worker> wt =
                std::make_unique<Worker>(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<bool> 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();
    }

+4 −4
Original line number Diff line number Diff line
@@ -301,7 +301,8 @@ class Worker {
class SnapshotHandler : public std::enable_shared_from_this<SnapshotHandler> {
  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<SnapshotHandler> {
    // 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<SnapshotHandler> {
    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<struct io_uring> ring_;
    std::unique_ptr<UpdateVerify> update_verify_;
+33 −16
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <android-base/scopeguard.h>
#include <android-base/strings.h>
#include <fs_mgr/file_wait.h>
#include <snapuserd/snapuserd_client.h>
#include "snapuserd_server.h"
@@ -101,7 +102,7 @@ void UserSnapshotServer::ShutdownThreads() {
    JoinAllThreads();
}

UserSnapshotDmUserHandler::UserSnapshotDmUserHandler(std::shared_ptr<SnapshotHandler> snapuserd)
HandlerThread::HandlerThread(std::shared_ptr<SnapshotHandler> 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<UserSnapshotDmUserHandler> handler) {
void UserSnapshotServer::RunThread(std::shared_ptr<HandlerThread> 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<std::shared_ptr<UserSnapshotDmUserHandler>> dm_users;
    std::vector<std::shared_ptr<HandlerThread>> dm_users;
    {
        std::lock_guard<std::mutex> guard(lock_);
        dm_users = std::move(dm_users_);
@@ -483,25 +484,42 @@ void UserSnapshotServer::Interrupt() {
    SetTerminating();
}

std::shared_ptr<UserSnapshotDmUserHandler> 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<HandlerThread> 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<SnapshotHandler>(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<UserSnapshotDmUserHandler>(snapuserd);
    auto handler = std::make_shared<HandlerThread>(snapuserd);
    {
        std::lock_guard<std::mutex> lock(lock_);
        if (FindHandler(&lock, misc_name) != dm_users_.end()) {
@@ -513,7 +531,7 @@ std::shared_ptr<UserSnapshotDmUserHandler> UserSnapshotServer::AddHandler(
    return handler;
}

bool UserSnapshotServer::StartHandler(const std::shared_ptr<UserSnapshotDmUserHandler>& handler) {
bool UserSnapshotServer::StartHandler(const std::shared_ptr<HandlerThread>& handler) {
    if (handler->snapuserd()->IsAttached()) {
        LOG(ERROR) << "Handler already attached";
        return false;
@@ -526,7 +544,7 @@ bool UserSnapshotServer::StartHandler(const std::shared_ptr<UserSnapshotDmUserHa
}

bool UserSnapshotServer::StartMerge(std::lock_guard<std::mutex>* proof_of_lock,
                                    const std::shared_ptr<UserSnapshotDmUserHandler>& handler) {
                                    const std::shared_ptr<HandlerThread>& handler) {
    CHECK(proof_of_lock);

    if (!handler->snapuserd()->IsAttached()) {
@@ -568,8 +586,7 @@ void UserSnapshotServer::TerminateMergeThreads(std::lock_guard<std::mutex>* proo
    }
}

std::string UserSnapshotServer::GetMergeStatus(
        const std::shared_ptr<UserSnapshotDmUserHandler>& handler) {
std::string UserSnapshotServer::GetMergeStatus(const std::shared_ptr<HandlerThread>& handler) {
    return handler->snapuserd()->GetMergeStatus();
}

@@ -604,7 +621,7 @@ double UserSnapshotServer::GetMergePercentage(std::lock_guard<std::mutex>* proof
}

bool UserSnapshotServer::RemoveAndJoinHandler(const std::string& misc_name) {
    std::shared_ptr<UserSnapshotDmUserHandler> handler;
    std::shared_ptr<HandlerThread> handler;
    {
        std::lock_guard<std::mutex> lock(lock_);

+13 −13
Original line number Diff line number Diff line
@@ -54,9 +54,9 @@ enum class DaemonOps {
    INVALID,
};

class UserSnapshotDmUserHandler {
class HandlerThread {
  public:
    explicit UserSnapshotDmUserHandler(std::shared_ptr<SnapshotHandler> snapuserd);
    explicit HandlerThread(std::shared_ptr<SnapshotHandler> snapuserd);

    void FreeResources() {
        // Each worker thread holds a reference to snapuserd.
@@ -99,9 +99,9 @@ class UserSnapshotServer {

    std::mutex lock_;

    using HandlerList = std::vector<std::shared_ptr<UserSnapshotDmUserHandler>>;
    using HandlerList = std::vector<std::shared_ptr<HandlerThread>>;
    HandlerList dm_users_;
    std::queue<std::shared_ptr<UserSnapshotDmUserHandler>> merge_handlers_;
    std::queue<std::shared_ptr<HandlerThread>> 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<UserSnapshotDmUserHandler> handler);
    void RunThread(std::shared_ptr<HandlerThread> 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<std::mutex>* proof_of_lock,
                                      const std::string& misc_name);

@@ -143,14 +143,14 @@ class UserSnapshotServer {
    bool RunForSocketHandoff();
    bool WaitForSocket();

    std::shared_ptr<UserSnapshotDmUserHandler> AddHandler(const std::string& misc_name,
    std::shared_ptr<HandlerThread> 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<UserSnapshotDmUserHandler>& handler);
    bool StartHandler(const std::shared_ptr<HandlerThread>& handler);
    bool StartMerge(std::lock_guard<std::mutex>* proof_of_lock,
                    const std::shared_ptr<UserSnapshotDmUserHandler>& handler);
    std::string GetMergeStatus(const std::shared_ptr<UserSnapshotDmUserHandler>& handler);
                    const std::shared_ptr<HandlerThread>& handler);
    std::string GetMergeStatus(const std::shared_ptr<HandlerThread>& handler);

    void WakeupMonitorMergeThread();
    void SetTerminating() { terminating_ = true; }