From d5f3da8dc568edad9ae161f00a51e260e37bf00f Mon Sep 17 00:00:00 2001 From: Konstantin Vyshetsky Date: Thu, 4 Nov 2021 10:27:06 -0700 Subject: [PATCH] fastbootd: use O_DIRECT for write partition Direct writes for partition flashing significantly increase performance. Use O_DIRECT flag when opening partition for flashing. Additionally use a 4096b aligned buffer which is required for O_DIRECT. Switch from using 8MB write buffer to 1MB write buffer, as the extra allocation has no performance impact. Test: flash locally and reach home screen Bug: 205151372 Signed-off-by: Konstantin Vyshetsky Change-Id: I060f438cf698d0fda1e59e35338bb5dc1cd05b51 --- fastboot/device/flashing.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/fastboot/device/flashing.cpp b/fastboot/device/flashing.cpp index 9b5d2cd25..3f9bcdc14 100644 --- a/fastboot/device/flashing.cpp +++ b/fastboot/device/flashing.cpp @@ -16,6 +16,7 @@ #include "flashing.h" #include +#include #include #include @@ -77,9 +78,20 @@ void WipeOverlayfsForPartition(FastbootDevice* device, const std::string& partit int FlashRawDataChunk(int fd, const char* data, size_t len) { size_t ret = 0; + const size_t max_write_size = 1048576; + void* aligned_buffer; + + if (posix_memalign(&aligned_buffer, 4096, max_write_size)) { + PLOG(ERROR) << "Failed to allocate write buffer"; + return -ENOMEM; + } + + auto aligned_buffer_unique_ptr = std::unique_ptr{aligned_buffer, free}; + while (ret < len) { - int this_len = std::min(static_cast(1048576UL * 8), len - ret); - int this_ret = write(fd, data, this_len); + int this_len = std::min(max_write_size, len - ret); + memcpy(aligned_buffer_unique_ptr.get(), data, this_len); + int this_ret = write(fd, aligned_buffer_unique_ptr.get(), this_len); if (this_ret < 0) { PLOG(ERROR) << "Failed to flash data of len " << len; return -1; @@ -147,7 +159,7 @@ static void CopyAVBFooter(std::vector* data, const uint64_t block_device_s int Flash(FastbootDevice* device, const std::string& partition_name) { PartitionHandle handle; - if (!OpenPartition(device, partition_name, &handle)) { + if (!OpenPartition(device, partition_name, &handle, O_WRONLY | O_DIRECT)) { return -ENOENT; }