測量除鏽

上次重新整理時間:2019 年 9 月 3 日

用於分析 Fuchsia 中 Rust 程式二進位檔大小和組合的操作說明。範例使用 //src/diagnostics/archivist 套件。

必要條件

貨物

以下操作說明涵蓋安裝及使用 cargo-bloat 工具 (以 Bloaty McBloatface 為靈感的 Rust 專案工具)。

如何安裝:

cargo install cargo-bloat --features regex-filter [--force]

如果您已安裝舊版,或想為現有的安裝作業新增規則運算式篩選支援功能,則可能需要新增 --force

法戈

這個範例使用 crates.io 的 Cargo 子指令,因此您必須安裝 Fargo

執行 fx build

如要瞭解如何在建構引數中加入正確的二進位目標,請參閱 fx 常見工具說明文件

接著,在 Fuchsia 來源目錄中執行 fx build

建立 Cargo.toml

按照操作說明為專案產生 Cargo.toml

使用 Fargo 建構

cargo-bloat 目前不支援傳遞任意資訊清單路徑,您需要使用 cd 至具有產生的 Cargo.toml 的目錄:

cd $FUCHSIA_DIR/src/diagnostics/archivist

從該目錄確保您可以使用 Fargo 產生發布子版本:

fargo build --release

先從純文字執行發布子版本,因為 Cargo-bloat 會消除任何建構錯誤。

利用碳水化合物測量尺寸貢獻

確定遠處能夠為目標產生發布二進位檔後,請執行 Cargo-bloat:

fargo cargo bloat -- --release -n 5
Compiling ...
Analyzing .../src/../out/cargo_target/x86_64-fuchsia/release/archivist

 File  .text     Size              Crate Name
 1.7%   4.5%  38.0KiB              regex <regex::exec::ExecNoSync as regex::re_trait::Regular...
 1.0%   2.5%  21.4KiB       regex_syntax regex_syntax::ast::parse::ParserI<P>::parse_with_comments
 0.8%   2.1%  17.7KiB fuchsia_component? <fuchsia_component::server::ServiceFs<ServiceObjTy> as...
 0.4%   0.9%   8.0KiB              regex regex::re_unicode::Regex::shortest_match_at
 0.4%   0.9%   7.8KiB                std _ZN9libunwind10CFI_ParserINS_17LocalAddressSpaceEE17paI...
33.8%  89.0% 751.9KiB                    And 6152 smaller methods. Use -n N to show more.
38.0% 100.0% 844.8KiB                    .text section size, the file size is 2.2MiB

請務必測量發布時的二進位檔大小,因為要預測變更對偵錯版本的影響並不容易。

-n 5 引數會將輸出上限為 5 行。針對所有選項執行 cargo bloat --help,請參閱下方的常見選項。

依 Crate 分組函數

使用 --crates 旗標,按照來源 Crate 的預估值將粗暴分析分組:

fargo cargo bloat -- --release -n 5 --crates
Compiling ...
Analyzing .../src/../out/cargo_target/x86_64-fuchsia/release/archivist

 File  .text     Size Crate
13.3%  34.9% 294.8KiB std
 6.3%  16.7% 141.0KiB regex
 5.0%  13.2% 111.3KiB regex_syntax
 2.1%   5.5%  46.1KiB fidl
 1.8%   4.7%  39.5KiB json5
 9.6%  25.1% 212.2KiB And 64 more crates. Use -n N to show more.
38.0% 100.0% 844.8KiB .text section size, the file size is 2.2MiB

Note: numbers above are a result of guesswork. They are not 100% correct and never will be.

將一般函式歸因至原始 Crate 是一種容易出錯的經驗法則分析,且應使用篩選器和精細的輸出內容來細查資料,以確認 Crate 分組輸出內容產生的任何發現。

篩選函式

如要深入瞭解特定 Crate 中的大量來源,您可以使用 --filter 標記,按照來源 Crate 名稱,或針對函式名稱 (已啟用功能旗標) 的規則運算式進行篩選:

fargo cargo bloat -- --release -n 5 --filter regex_syntax
Compiling ...
Analyzing .../src/../out/cargo_target/x86_64-fuchsia/release/archivist

File .text     Size        Crate Name
1.0%  2.5%  21.4KiB regex_syntax regex_syntax::ast::parse::ParserI<P>::parse_with_comments
0.3%  0.8%   6.4KiB regex_syntax regex_syntax::ast::parse::ParserI<P>::parse_escape
0.2%  0.5%   3.8KiB regex_syntax <regex_syntax::hir::translate::TranslatorI as regex_syntax::ast...
0.1%  0.2%   1.8KiB regex_syntax <regex_syntax::hir::translate::TranslatorI as regex_syntax::ast...
0.1%  0.2%   1.7KiB regex_syntax regex_syntax::unicode::class
3.4%  9.0%  76.2KiB              And 698 smaller methods. Use -n N to show more.
5.0% 13.2% 111.2KiB              filtered data size, the file size is 2.2MiB