• Stars
    star
    502
  • Rank 87,851 (Top 2 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A cargo plugin to generate Xcode Instruments trace files

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 Instruments System Trace

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:

$ 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:

$ 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

$ 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

$ cargo install --features vendored-openssl cargo-instruments

Building from Source on nix

If you're using nix, this command should provide all dependencies and build cargo-instruments from source:

$ 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)

$ cargo instruments -t Allocations

Open the trace file in Instruments.app manually

By default the trace file will immediatly be opened with Instruments.app. If you do not want this behavior use the --no-open flag.

$ open target/instruments/mybin_Allocations_2021-05-09T12_34_56.trace

If there are mutliple 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:

$ 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.

[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 <TEMPLATE> [ARGS]...

FLAGS:
    -h, --help                   Prints help information
    -l, --list-templates         List available templates
        --no-open                Do not open the generated trace file in Instruments.app
        --release                Pass --release to cargo
    -V, --version                Prints version information
        --all-features           Activate all features for the selected target
        --no-default-features    Do not activate the default features for the selected target

OPTIONS:
        --bench <NAME>                 Benchmark target to run
        --bin <NAME>                   Binary to run
        --example <NAME>               Example binary to run
        --features <CARGO-FEATURES>    Features to pass to cargo
        --manifest-path <PATH>         Path to Cargo.toml
    -p, --package <NAME>               Specify package for example/bin/bench
        --profile <NAME>               Pass --profile NAME to cargo
    -t, --template <TEMPLATE>          Specify the instruments template to run
        --time-limit <MILLIS>          Limit recording time to the specified value (in milliseconds)
    -o, --output <PATH>                Output .trace file to the given path

ARGS:
    <ARGS>...    Arguments passed to the target binary

EXAMPLE:
    cargo instruments -t time    Profile main binary with the (recommended) Time Profiler.

And cargo instruments --help provides more detail.

Templates

Instruments has the concept of 'templates', which describe sets of dtrace probes that can be enabled. You can ask cargo-instruments to list available templates, including your custom ones (see help above). If you don't provide a template name, you will be prompted to choose one.

Typically, the built-in templates are

built-in            abbrev
--------------------------
Activity Monitor
Allocations         (alloc)
Animation Hitches
App Launch
Core Data
Counters
Energy Log
File Activity       (io)
Game Performance
Leaks
Logging
Metal System Trace
Network
SceneKit
SwiftUI
System Trace        (sys)
Time Profiler       (time)
Zombies

Examples

# View all args and options
$ cargo instruments --help
# View all built-in and custom templates
$ cargo instruments --list-templates
# profile the main binary with the Allocations template
$ cargo instruments -t alloc
# profile examples/my_example.rs, with the Allocations template,
# for 10 seconds, and open the trace when finished
$ cargo instruments -t Allocations --example my_example --time-limit 10000 --open

Resources

Instruments Help

WWDC videos

The best source of information about Instruments is likely the various WWDC sessions over the years:

More Repositories

1

RustPlayground

Quickly test Rust code on macOS
Rust
238
star
2

anagramatron

twitter anagram hunter
Python
143
star
3

keypath

Swift-style keypaths in Rust
Rust
58
star
4

rust-dash-docset-gen

Quickly generate Dash docsets for third-party Rust crates
Python
26
star
5

fea-rs

Parsing and compiling adobe feature files
Rust
13
star
6

poetryutils2

better poetry utils
Python
9
star
7

INTERESTING_JPG

simple bot that posts CV descriptions of images to twitter
Python
7
star
8

lsp-client

playing with the language server protocol in Rust.
Rust
5
star
9

anagram-review-ios

small iOS app for reviewing twitter anagrams
Objective-C
5
star
10

rust-python-drawing

Drawing to a rust graphics context from a python script
Rust
3
star
11

twitter-zmq-stream

utility for connecting to the twitter stream and publishing it locally to consumers via ØMQ
Python
3
star
12

bloat-cmp

😵
JavaScript
3
star
13

druid-template

hello dolly
Rust
2
star
14

xi-rustfmt

A rustfmt plugin for xi-editor
Rust
2
star
15

haikubot

a little bot that assembles haiku from tweets
Python
2
star
16

gnudbm

Ergonomic Rust bindings for gdbm
Rust
2
star
17

advent-2017

Jupyter Notebook
2
star
18

xi-future

experiments towards a future's based runloop for xi
Rust
2
star
19

pars

Deriving FromStr
Rust
2
star
20

twitter-text-tools

python tools for working with twitter data
Python
2
star
21

cargo-clean-all

A small utility script to recursively clean cargo projects
Python
2
star
22

ttx-rs

ttx, rs
Rust
1
star
23

ios-keyboard-type-selector

An iOS project for debugging Keyboard Types
Swift
1
star
24

rust-from-python-example

demo rust->python bindings
Rust
1
star
25

poetryutils

python tools for working with metre and rhyme
Python
1
star
26

aoc2018

Advent of Code 2018
Rust
1
star
27

poetastr

programatic poetry generation
Python
1
star
28

norad

Rust
1
star
29

bitflipper

Flipping bits in strings?
Rust
1
star
30

rust-buffer-bench

Benchmarking Rust text buffer implementations
Rust
1
star