• Stars
    star
    2,135
  • Rank 21,619 (Top 0.5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 2 months ago

Reviews

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

Repository Details

Command-line sampling profiler for macOS and Linux

samply

samply is a command line CPU profiler which uses the Firefox profiler as its UI.

At the moment it runs on macOS and Linux. Windows support is planned. samply is still under development and far from finished, but works quite well already.

Give it a try:

% cargo install samply
% samply record ./your-command your-arguments

This spawns ./your-command your-arguments in a subprocess and records a profile of its execution. When the command finishes, samply opens profiler.firefox.com in your default browser, loads the recorded profile in it, and starts a local webserver which serves symbol information and source code.

Then you can inspect the profile. And you can upload it.

Here's an example: https://share.firefox.dev/3j3PJoK

This is a profile of dump_syms, running on macOS, recorded as follows:

samply record ./dump_syms ~/mold-opt-libxul.so > /dev/null

You can see which functions were running for how long. You can see flame graphs and timelines. You can double-click functions in the call tree to open the source view, and see which lines of code were sampled how many times.

All data is kept locally (on disk and in RAM) until you choose to upload your profile.

samply is a sampling profiler and collects stack traces, per thread, at some sampling interval (the default 1000Hz, i.e. 1ms). On macOS, both on- and off-cpu samples are collected (so you can see under which stack you were blocking on a lock, for example). On Linux, only on-cpu samples are collected at the moment.

On Linux, samply needs access to performance events system for unprivileged users. For this, you can either:

  • if using Linux 5.8 or later, set the CAP_PERFMON capability as effective and permitted for samply (recommended):

    sudo setcap 'cap_perfmon+ep' `which samply`
    
  • allow use of (almost) all events by changing the perf_event_paranoid kernel variable (not recommended):

    sudo sysctl kernel.perf_event_paranoid=-1 
    

If you still get a mmap failed error (an EPERM), you might also need to increase the mlock limit, e.g.:

sudo sysctl kernel.perf_event_mlock_kb=2048

Examples

Here's a profile from samply record rustup check: https://share.firefox.dev/3hteKZZ

I'll add some Linux examples when I get a chance.

Turn on debug info for full stacks

If you profile Rust code, make sure to profile a binary which was compiled in release mode and with debug info. This will give you inline stacks and a working source code view.

The best way is the following:

  1. Create a global cargo profile called profiling, see below how.
  2. Compile with cargo build --profile profiling.
  3. Record with samply record ./target/profiling/yourrustprogram.

To create the profiling cargo profile, create a text file at ~/.cargo/config.toml with the following content:

[profile.profiling]
inherits = "release"
debug = true

Similar advice applies to other compiled languages. For C++, you'll want to make sure the -g flag is included in the compiler invocation.

Known issues

On macOS, samply cannot profile system commands, such as the sleep command or system python. This is because system executables are signed in such a way that they block the DYLD_INSERT_LIBRARIES environment variable, which breaks samply's ability to siphon out the mach_port of the process.

But you can profile any binaries that you've compiled yourself, or which are unsigned or locally-signed (such as anything installed by cargo install or by Homebrew).

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

framehop

Stack unwinding library in Rust
Rust
81
star
2

msvc-demangler-rust

A rust library that demangles / undecorates C++ symbols mangled by MSVC
Rust
42
star
3

pdb-addr2line

A rust crate to symbolicate addresses from PDBs, like addr2line. Uses the `pdb` crate.
Rust
31
star
4

fxprof-perf-convert

Convert perf.data files to the Firefox Profiler format
Rust
26
star
5

symsrv

Download pdbs from symbol servers and cache locally, parse symbol paths from env vars
Rust
20
star
6

macho-unwind-info

A parser for Apple's Compact Unwinding Format, which is used in the __unwind_info section of mach-O binaries.
Rust
20
star
7

linux-perf-data

A parser for the perf.data format
Rust
19
star
8

mach-shared-memory

Objective-C++
8
star
9

profiler-get-symbols

Extracts symbol information from binaries, used in the Gecko Profiler. Demo: http://tests.themasta.com/profiler-get-symbols/
HTML
7
star
10

mediakeysappleremotesimfy

Media Keys and Apple Remote Support for simfy.de
JavaScript
6
star
11

linux-perf-stuff

Experimenting with a fast `perf script` replacement
Rust
5
star
12

linux-perf-event-reader

Parse Linux perf_event information from raw bytes
Rust
5
star
13

profiler-symbol-server

A local webserver that lets the Firefox profiler obtain symbol information.
Rust
3
star
14

analyze-tryserver-profiles

Python
3
star
15

aya-test-bpf

An experiment for getting profiles with on- and off-cpu samples with the help of BPF and aya.
Rust
3
star
16

reliost

Prototype of an Eliot symbol server rewrite in Rust, "my first server-side Rust project"
Rust
2
star
17

svgspritesheet

Converts a bodymovin animation to an SVG sprite sheet
JavaScript
2
star
18

macos-compositing

In this repo I want to try out different ways of getting OpenGL content onto the screen in order to compare power usage.
Objective-C
2
star
19

mini-profiler-fosdem2024

A small example project for a presentation
TypeScript
2
star
20

srcsrv

Parses and interprets the `srcsrv` stream of a Windows PDB file.
Rust
1
star
21

dwarf-location-consistency

A tool to find inconsistencies in DWARF debug data
Rust
1
star
22

core-animation-opengl-two-windows

A test app that helps me measure GPU bandwidth
Objective-C
1
star
23

macho-stubs

An attempt to symbolicate __stubs functions
Rust
1
star
24

gecko-profiler-demangle

Contains wrapper function for C++ and rust name demangling, which is intended to be used through WASM in perf.html.
Rust
1
star
25

chromeless-window

Basic add-on SDK add-on that opens a chromeless top-level window.
JavaScript
1
star