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

Commit a8f6ce33 authored by Akilesh Kailash's avatar Akilesh Kailash
Browse files

libsnapshot: Tune readahead during OTA for source and COW block device



Scanning of partitions post OTA leads to memory pressure. Tune
the read-ahead of source and COW block device. This is currently
set to 32KB.

This reduces Inactive(file) and Active(file) usage during entire
duration of boot post OTA.

On Pixel 6: For incremental OTA ~400M. During boot:

                            Without-patch         With-patch    Delta
			    --------------------------------------------
1: Peak Inactive(file):     4469MB                3118MB        ~30%

2: Peak Active(file):       985MB                 712MB         ~27%

No regression observed on boot time.

Additionally, cut down the number of threads to verify the partitions.

Bug: 311233916
Test: Incremental OTA on Pixel 6
Change-Id: I0b842776c36fa089c39c170fa7bf0f246e16636d
Signed-off-by: default avatarAkilesh Kailash <akailash@google.com>
parent 240f7084
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -826,6 +826,9 @@ class SnapshotManager final : public ISnapshotManager {
    bool DeleteDeviceIfExists(const std::string& name,
                              const std::chrono::milliseconds& timeout_ms = {});

    // Set read-ahead size during OTA
    void SetReadAheadSize(const std::string& entry_block_device, off64_t size_kb);

    android::dm::IDeviceMapper& dm_;
    std::unique_ptr<IDeviceInfo> device_;
    std::string metadata_dir_;
+46 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <android-base/unique_fd.h>
#include <cutils/sockets.h>
@@ -82,12 +83,28 @@ using RepeatedPtrField = google::protobuf::RepeatedPtrField<T>;
using std::chrono::duration_cast;
using namespace std::chrono_literals;
using namespace std::string_literals;
using android::base::Realpath;
using android::base::StringPrintf;

static constexpr char kBootSnapshotsWithoutSlotSwitch[] =
        "/metadata/ota/snapshot-boot-without-slot-switch";
static constexpr char kBootIndicatorPath[] = "/metadata/ota/snapshot-boot";
static constexpr char kRollbackIndicatorPath[] = "/metadata/ota/rollback-indicator";
static constexpr auto kUpdateStateCheckInterval = 2s;
/*
 * The readahead size is set to 32kb so that
 * there is no significant memory pressure (/proc/pressure/memory) during boot.
 * After OTA, during boot, partitions are scanned before marking slot as successful.
 * This scan will trigger readahead both on source and COW block device thereby
 * leading to Inactive(file) pages to be very high.
 *
 * A lower value may help reduce memory pressure further, however, that will
 * increase the boot time. Thus, for device which don't care about OTA boot
 * time, they could use O_DIRECT functionality wherein the I/O to the source
 * block device will be O_DIRECT.
 */
static constexpr auto kCowReadAheadSizeKb = 32;
static constexpr auto kSourceReadAheadSizeKb = 32;

// Note: IImageManager is an incomplete type in the header, so the default
// destructor doesn't work.
@@ -1748,6 +1765,9 @@ bool SnapshotManager::PerformInitTransition(InitTransition transition,
                snapuserd_argv->emplace_back(std::move(message));
            }

            SetReadAheadSize(cow_image_device, kCowReadAheadSizeKb);
            SetReadAheadSize(source_device, kSourceReadAheadSizeKb);

            // Do not attempt to connect to the new snapuserd yet, it hasn't
            // been started. We do however want to wait for the misc device
            // to have been created.
@@ -4404,5 +4424,31 @@ bool SnapshotManager::PrepareDeviceToBootWithoutSnapshot() {
    return true;
}

void SnapshotManager::SetReadAheadSize(const std::string& entry_block_device, off64_t size_kb) {
    std::string block_device;
    if (!Realpath(entry_block_device, &block_device)) {
        PLOG(ERROR) << "Failed to realpath " << entry_block_device;
        return;
    }

    static constexpr std::string_view kDevBlockPrefix("/dev/block/");
    if (!android::base::StartsWith(block_device, kDevBlockPrefix)) {
        LOG(ERROR) << block_device << " is not a block device";
        return;
    }

    std::string block_name = block_device.substr(kDevBlockPrefix.length());
    std::string sys_partition =
            android::base::StringPrintf("/sys/class/block/%s/partition", block_name.c_str());
    struct stat info;
    if (lstat(sys_partition.c_str(), &info) == 0) {
        block_name += "/..";
    }
    std::string sys_ra = android::base::StringPrintf("/sys/class/block/%s/queue/read_ahead_kb",
                                                     block_name.c_str());
    std::string size = std::to_string(size_kb);
    android::base::WriteStringToFile(size, sys_ra.c_str());
}

}  // namespace snapshot
}  // namespace android
+20 −3
Original line number Diff line number Diff line
@@ -48,10 +48,27 @@ class UpdateVerify {
    std::mutex m_lock_;
    std::condition_variable m_cv_;

    /*
     * Scanning of partitions is an expensive operation both in terms of memory
     * and CPU usage. The goal here is to scan the partitions fast enough without
     * significant increase in the boot time.
     *
     * Partitions such as system, product which may be huge and may need multiple
     * threads to speed up the verification process. Using multiple threads for
     * all partitions may increase CPU usage significantly. Hence, limit that to
     * 1 thread per partition.
     *
     * These numbers were derived by monitoring the memory and CPU pressure
     * (/proc/pressure/{cpu,memory}; and monitoring the Inactive(file) and
     * Active(file) pages from /proc/meminfo.
     *
     * Additionally, for low memory devices, it is advisible to use O_DIRECT
     * fucntionality for source block device.
     */
    int kMinThreadsToVerify = 1;
    int kMaxThreadsToVerify = 4;
    uint64_t kThresholdSize = 512_MiB;
    uint64_t kBlockSizeVerify = 1_MiB;
    int kMaxThreadsToVerify = 3;
    uint64_t kThresholdSize = 750_MiB;
    uint64_t kBlockSizeVerify = 2_MiB;

    bool IsBlockAligned(uint64_t read_size) { return ((read_size & (BLOCK_SZ - 1)) == 0); }
    void UpdatePartitionVerificationState(UpdateVerifyState state);