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

Add a collector command to examine and compare binary size statistics #1772

Merged
merged 3 commits into from
Dec 18, 2023

Conversation

Kobzol
Copy link
Contributor

@Kobzol Kobzol commented Dec 17, 2023

This PR adds a new collector command called binary_stats, which can be used to analyze the sizes of ELF binaries and libraries produced by compile benchmarks. The command could be easily modified in the future to allow analyzing arbitrary binary artifacts specified by a filesystem path.

The command uses the elf crate to print the sizes of sections and symbols in the ELF artifact. A few heuristics are used to normalize and group section/symbol names for less output clutter.

The command can also be used to compare the binary size of a benchmark between two different versions of rustc (this will probably be the most common use-case) and to compare the binary size between two different codegen backends (currently Llvm/Cranelift).

Examples:

  • Examine size
    image
  • Diff between two compilers/codegen backends:
    image

@Mark-Simulacrum
Copy link
Member

Hm, can you say more about how you chose the elf crate? I personally usually use object (https://docs.rs/object/latest/object/) -- it's also used in std, so to some extent is more maintained. It would probably make it easier to run this tool on non-ELF platforms (e.g., macOS or Windows) as well.

@Kobzol
Copy link
Contributor Author

Kobzol commented Dec 17, 2023

Googled "Rust ELF" and selected the first thing that looked maintained, documented and allowed me to solve what I needed :)

Happy to switch to object, if it can handle binaries and also .rlibs and .sos.

@Mark-Simulacrum
Copy link
Member

.sos should definitely work (they're also ELF). .rlib may require https://docs.rs/ar/latest/ar/ or so - they're unix archives under the hood containing (usually) a pile of .o files (ELF or Mach-O or etc depending on platform) + the metadata.

@Kobzol
Copy link
Contributor Author

Kobzol commented Dec 17, 2023

Yeah, this PR already uses ar to split rlibs into objects and then parse them as ELF :) I'll take a look into using object. It shouldn't change the other parts of the PR.

Copy link
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broadly though this looks great to me.

Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks! I don't have any opinions about which ELF crate to use.

@Kobzol
Copy link
Contributor Author

Kobzol commented Dec 18, 2023

I pushed a change that just switches from elf + ar to object, which should hopefully also support Windows/macOS binaries.

@Kobzol Kobzol merged commit cbdf7fb into rust-lang:master Dec 18, 2023
10 checks passed
@Kobzol Kobzol deleted the binary-stats branch December 18, 2023 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants