# cargo-instruments Easily profile your rust crate with Xcode [Instruments]. `cargo-instruments` is the glue between Cargo and Xcode's bundled profiling suite. It allows you to easily profile any binary in your crate, generating files that can be viewed in the Instruments app. ![Instruments Time Profiler](https://raw.githubusercontent.com/cmyr/cargo-instruments/screenshots/instruments_time1.png) ![Instruments System Trace](https://raw.githubusercontent.com/cmyr/cargo-instruments/screenshots/instruments_sys1.png) ## Pre-requisites ### Xcode Instruments This crate only works on macOS because it uses [Instruments] for profiling and creating the trace file. The benefit is that Instruments provides great templates and UI to explore the Profiling Trace. To install Xcode Instruments, simply install the Command Line Tools: ```sh $ xcode-select --install ``` ### Compatibility This crate works on macOS 10.13+. In practice, it transparently detects and uses the appropriate Xcode Instruments version based on your macOS version: either `/usr/bin/instruments` on older macOS, or starting with macOS 10.15, the new `xcrun xctrace`. ## Installation ### brew The simplest way to install is via Homebrew: ```sh $ brew install cargo-instruments ``` Alternatively, you can install from source. ### Building from Source First, ensure that you are running macOS, with Cargo, Xcode, and the Xcode Command Line Tools installed. If OpenSSL is installed (e.g., via `brew`), then install with ```sh $ cargo install cargo-instruments ``` If OpenSSL is not installed or if `cargo install` fails with an error message starting with "Could not find directory of OpenSSL installation, and this `-sys` crate cannot proceed without this knowledge," then install with ```sh $ cargo install --features vendored-openssl cargo-instruments ``` #### Building from Source on nix If you're using [nix](https://nixos.org/guides/install-nix.html), this command should provide all dependencies and build `cargo-instruments` from source: ```sh $ nix-shell --command 'cargo install cargo-instruments' --pure -p \ darwin.apple_sdk.frameworks.SystemConfiguration \ darwin.apple_sdk.frameworks.CoreServices \ rustc cargo sccache libgit2 pkg-config libiconv \ llvmPackages_13.libclang openssl ``` ## Usage ### Basic `cargo-instruments` requires a binary target to run. By default, it will try to build the current crate's `main.rs`. You can specify an alternative binary by using the `--bin` or `--example` flags, or a benchmark target with the `--bench` flag. Assuming your crate has one binary target named `mybin`, and you want to profile using the `Allocations` Instruments template: _Generate a new trace file_ (by default saved in `target/instruments`) ```sh $ cargo instruments -t Allocations ``` _Open the trace file in Instruments.app manually_ By default the trace file will immediately be opened with `Instruments.app`. If you do not want this behavior use the `--no-open` flag. ```sh $ open target/instruments/mybin_Allocations_2021-05-09T12_34_56.trace ``` If there are multiple packages, you can specify the package to profile with the `--package` flag. For example, you use Cargo's workspace to manage multiple packages. To profile the bin `bar` of the package `foo`: ```sh $ cargo instruments --package foo --template alloc --bin bar ``` In many cases, a package only has one binary. In this case `--package` behaves the same as `--bin`. ### Profiling application in release mode When profiling the application in release mode the compiler doesn't provide debugging symbols in the default configuration. To let the compiler generate the debugging symbols even in release mode you can append the following section in your `Cargo.toml`. ```toml [profile.release] debug = true ``` ### All options As usual, thanks to Clap, running `cargo instruments -h` prints the compact help. ``` cargo-instruments 0.4.8 Profile a binary with Xcode Instruments. By default, cargo-instruments will build your main binary. USAGE: cargo instruments [FLAGS] [OPTIONS] --template