Skip to content
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

Create PGO optimized binaries #1469

Open
YamatoSecurity opened this issue Nov 3, 2024 · 7 comments
Open

Create PGO optimized binaries #1469

YamatoSecurity opened this issue Nov 3, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@YamatoSecurity
Copy link
Collaborator

YamatoSecurity commented Nov 3, 2024

I was able to get a 11.5% speed increase with PGO optimization. (Memory usage did not change)

cargo install cargo-pgo
rustup component add llvm-tools-preview
cargo pgo build
cargo pgo instrument --keep-profiles run csv-timeline -d ../hayabusa-sample-evtx -w -D -n -x -X -s -o delete.csv -C -p super-verbose
cargo pgo instrument --keep-profiles run json-timeline -d ../hayabusa-sample-evtx -w -D -n -x -X -s -o delete.csv -C -p super-verbose
cargo pgo instrument --keep-profiles run logon-summary -d ../hayabusa-sample-evtx  -o delete.csv -C
cargo pgo instrument --keep-profiles run eid-metrics -d ../hayabusa-sample-evtx -o delete.csv -C
cargo pgo optimize

Notes

  1. The optimized binary gets outputted at target/x86_64-apple-darwin/release/, etc... directories instead of ./target/release
  2. The profiles will differ based on workload, so we should probably be running the hayabusa commands against the hayabusa-sample-evtx files as well as the evtx-baseline files
  3. I still haven't tried optimization with BOLT although apparently rustc already does some optimizations so might not be necessary.
  4. The profiles are not getting created on Windows for some reason...

Info:

@fukusuket Whenever you have time, could you test to see if you get faster benchmarks as well? If so, I'd like to add to clone the release binary automation action and add PGO optimizations to it.

@YamatoSecurity YamatoSecurity added the enhancement New feature or request label Nov 3, 2024
@fukusuket
Copy link
Collaborator

@YamatoSecurity
I ran csv-timeline on a binary that was optimized for hayabusa-sample-evtx using the following procedure!
(Because I wanted to check with --release build)

But I could not see any speed improvement when I performed the above steps🤔
Could you confirm the speed improvement with the --release build? (Is it possible to do a --release build with cargo-pgo?)

@YamatoSecurity
Copy link
Collaborator Author

I don't think it is possible to combine --release build and cargo-pgo. I compared binaries of ones built with --release build and one built with cargo-pgo. I don't think the hayabusa-sample-evtx logs are good to benchmark with because the data is so small we are going to get randomly a few seconds different anyways. You probably need to test with evtx-baseline and other logs as well if possible. I tested on 14GB.

@fukusuket
Copy link
Collaborator

@YamatoSecurity
I see... Indeed, hayabusa-sample-evtx is not suitable. I'll try it with baseline-evtx(and cargo-pgo)!

@YamatoSecurity
Copy link
Collaborator Author

Humm.. on Windows, it does not create any profiles.. I tried to use the profiles I created on my mac but I get a profile uses zlib compression but the profile reader was built without zlib support error.

I did find out that you can stop the clearing of PGO profiles with the --keep-profiles flag so that will make things simplier without having to copy out the profiles and move them back.

Reference: https://kobzol.github.io/rust/cargo/2023/07/28/rust-cargo-pgo.html

Other references: https://doc.rust-lang.org/rustc/profile-guided-optimization.html

@YamatoSecurity
Copy link
Collaborator Author

I updated the commands to run at the top of the issue. With the same commands it creates the PGO profiles on linux but not on Windows... I can't find information googling it and all the examples seem to be for *nix systems so maybe it is not working now on Windows.

@YamatoSecurity
Copy link
Collaborator Author

I was able to use PGO optimization on Windows by specifying the .profdata file I created on my Mac by copying it over and setting the environment variable $env:RUSTFLAGS="-C profile-use = d:¥yamatosecurity¥hayabusa¥pgo-profiles.profdata" and then run cargo build --release as normal.
So when doing this as an action, we can create the PGO binary on *nix, run various hayabusa commands, create the .profdata file and then use that .profdata file for each time we build the binary for the various OSes. We could save the .profdata file to the repository if need be.

@YamatoSecurity
Copy link
Collaborator Author

When testing really big data (130GB) I only got a 3% speed up on Windows. 5 hours 31 minutes to 5 hours 20 minutes but still an improvement. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants