Skip to content

[XRay] Reserve memory space ahead-of-time when reading native format log #76853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2024

Conversation

mshockwave
Copy link
Member

@mshockwave mshockwave commented Jan 3, 2024

XRay used to struggle reading large log files. It turned out the bottleneck was primarily caused by the reallocation happens when appending log entries into a std::vector.
This patch reserves the memory space ahead-of-time since the number of entries is known for most cases. Making llvm-xray runs 1.8 times faster and uses 1.4 times less physical memory when reading large (~2.6GB) log files.


Here are the benchmark numbers (benchmarking using hyperfine):

Benchmark 1: llvm-xray account --instr_map=../prog ./xray-log.prog.DnIfOu
  Time (mean ± σ):     20.606 s ±  1.127 s    [User: 9.467 s, System: 11.081 s]
  Range (min … max):   20.051 s … 23.296 s    10 runs

Benchmark 2: env XRAY_RESERVE_MEM=1 llvm-xray account --instr_map=../prog ./xray-log.prog.DnIfOu
  Time (mean ± σ):     11.238 s ±  0.017 s    [User: 6.544 s, System: 4.664 s]
  Range (min … max):   11.207 s … 11.261 s    10 runs

Summary
  env XRAY_RESERVE_MEM=1 llvm-xray account --instr_map=../prog ./xray-log.prog.DnIfOu ran
    1.83 ± 0.10 times faster than llvm-xray account --instr_map=../prog ./xray-log.prog.DnIfOu

Notes:

  1. The XRay mode used here is xray-basic
  2. xray-log.prog.DnIfOu is around 2.6GB
  3. XRAY_RESERVE_MEM is a temporary environment variable solely for benchmarking purposes. We are NOT using it to toggle the improvement in this patch

XRay used to struggle reading large log files. It turned out the
bottleneck was primarily caused by the reallocation happens when
appending log entries into a std::vector.
This patch reserves the memory space ahead-of-time since the number of
entries is known for most cases. Making llvm-xray runs 1.8 times faster
and uses 1.4 times less physical memory when reading large (~2.6GB)
log files.
@llvmbot
Copy link
Member

llvmbot commented Jan 3, 2024

@llvm/pr-subscribers-xray

Author: Min-Yih Hsu (mshockwave)

Changes

XRay used to struggle reading large log files. It turned out the bottleneck was primarily caused by the reallocation happens when appending log entries into a std::vector.
This patch reserves the memory space ahead-of-time since the number of entries is known for most cases. Making llvm-xray runs 1.8 times faster and uses 1.4 times less physical memory when reading large (~2.6GB) log files.


Here are the benchmark numbers (benchmarking using hyperfine):

Benchmark 1: llvm-xray account --instr_map=../prog ./xray-log.prog.DnIfOu
  Time (mean ± σ):     20.606 s ±  1.127 s    [User: 9.467 s, System: 11.081 s]
  Range (min … max):   20.051 s … 23.296 s    10 runs

Benchmark 2: env XRAY_RESERVE_MEM=1 llvm-xray account --instr_map=../prog ./xray-log. prog.DnIfOu
  Time (mean ± σ):     11.238 s ±  0.017 s    [User: 6.544 s, System: 4.664 s]
  Range (min … max):   11.207 s … 11.261 s    10 runs

Summary
  env XRAY_RESERVE_MEM=1 llvm-xray account --instr_map=../prog ./xray-log.prog.DnIfOu ran
    1.83 ± 0.10 times faster than llvm-xray account --instr_map=../prog ./xray-log.prog.DnIfOu

Notes:

  1. The XRay mode used here is xray-basic
  2. xray-log.prog.DnIfOu is around 2.6GB
  3. XRAY_RESERVE_MEM is a temporary environment variable solely for benchmarking purposes. We are NOT using it to toggle the improvement in this patch

Full diff: https://github.com/llvm/llvm-project/pull/76853.diff

1 Files Affected:

  • (modified) llvm/lib/XRay/Trace.cpp (+3)
diff --git a/llvm/lib/XRay/Trace.cpp b/llvm/lib/XRay/Trace.cpp
index b870adf565459f..74515b16417da3 100644
--- a/llvm/lib/XRay/Trace.cpp
+++ b/llvm/lib/XRay/Trace.cpp
@@ -51,6 +51,9 @@ Error loadNaiveFormatLog(StringRef Data, bool IsLittleEndian,
     return FileHeaderOrError.takeError();
   FileHeader = std::move(FileHeaderOrError.get());
 
+  size_t NumReservations = llvm::divideCeil(Reader.size() - OffsetPtr, 32U);
+  Records.reserve(NumReservations);
+
   // Each record after the header will be 32 bytes, in the following format:
   //
   //   (2)   uint16 : record type

@mshockwave
Copy link
Member Author

ping

@MaskRay MaskRay requested a review from deanberris January 11, 2024 00:09
@mshockwave mshockwave merged commit 3edf82d into llvm:main Jan 13, 2024
@mshockwave mshockwave deleted the patch/xray-read-trace branch January 13, 2024 00:34
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
…log (llvm#76853)

XRay used to struggle reading large log files. It turned out the
bottleneck was primarily caused by the reallocation happens when
appending log entries into a std::vector.
This patch reserves the memory space ahead-of-time since the number of
entries is known for most cases. Making llvm-xray runs 1.8 times faster
and uses 1.4 times less physical memory when reading large (~2.6GB) log
files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants