• Stars
    star
    672
  • Rank 67,180 (Top 2 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

An intrusive flamegraph profiling tool for rust.

FLAME

A cool flamegraph library for rust

Flamegraphs are a great way to view profiling information. At a glance, they give you information about how much time your program spends in critical sections of your code giving you some much-needed insight into where optimizations may be needed.

Unlike tools like perf which have the OS interrupt your running program repeatedly and reports on every function in your callstack, FLAME lets you choose what you want to see in the graph by adding performance instrumentation to your own code.

Simply use any of FLAMEs APIs to annotate the start and end of a block code that you want timing information from, and FLAME will organize these timings hierarchically.

Docs

Here's an example of how to use some of FLAMEs APIs:

extern crate flame;

use std::fs::File;

fn main() {
    // Manual `start` and `end`
    flame::start("read file");
    let x = read_a_file();
    flame::end("read file");

    // Time the execution of a closure.  (the result of the closure is returned)
    let y = flame::span_of("database query", || query_database());

    // Time the execution of a block by creating a guard.
    let z = {
        let _guard = flame::start_guard("cpu-heavy calculation");
        cpu_heavy_operations_1();
        // Notes can be used to annotate a particular instant in time.
        flame::note("something interesting happened", None);
        cpu_heavy_operations_2()
    };

    // Dump the report to disk
    flame::dump_html(&mut File::create("flame-graph.html").unwrap()).unwrap();
    
    // Or read and process the data yourself!
    let spans = flame::spans();
    
    println!("{} {} {}", x, y, z);
}

And here's a screenshot of a flamegraph produced by dump_html (from a different project):

flamegraph

Full Example

use std::fs::File;

use flame;

fn make_vec(size: usize) -> Vec<u32> {
    // start_guard needs to drop to calculate duration.
    let _fg = ::flame::start_guard("make_vec");

    let mut res = flame::span_of("vec init", || vec![0_u32; size]);
    for x in 0..size {
        res[x] = ((x + 10)/3) as u32;
    }
    let mut waste_time = 0;
    for i in 0..size*10 {
        waste_time += i
    }
    res
}

fn more_computing(i: usize) {
    let _fg = ::flame::start_guard("more_computation");

    for x in 0..(i * 100) {
        let mut v = make_vec(x);
        let x = Vec::from(&v[..]);
        for i in 0..v.len() {
            let flip = (v.len() - 1) - i as usize;
            v[i] = x[flip];
        }
    }
}

fn some_computation() {
    let _fg = ::flame::start_guard("some_computation");

    for i in 0..15 {
        more_computing(i);
    }
}


fn main() {
    let _fg = ::flame::start_guard("main");
    
    some_computation();
    // in order to create the flamegraph you must call one of the
    // flame::dump_* functions.
    flame::dump_html(File::create("flamegraph.html").unwrap()).unwrap();
}

Below is the resulting flamegraph.

flamegraph

llogiq has created flamer, a compiler plugin that automatically inserts FLAME instrumentation into annotated functions allowing you to write code like

#[flame]
fn this_function_is_profiled() {
    ...
}

More Repositories

1

mutagen

Breaking your Rust code for fun and profit
Rust
621
star
2

flamer

A compiler plugin to insert flame calls
Rust
364
star
3

momo

A Rust proc_macro_attribute to outline conversions from generic functions
Rust
239
star
4

bytecount

Counting occurrences of a given byte or UTF-8 characters in a slice of memory – fast
Rust
214
star
5

stdx-dev

Rust's missing development batteries
120
star
6

metacollect

A lint to collect some crate metadata
Rust
115
star
7

overflower

A Rust compiler plugin and support library to annotate overflow behavior
Rust
103
star
8

compact_arena

A crate with indexed arenas with small memory footprint
Rust
76
star
9

optional

A small crate to provide space-efficient Option<_> replacements
Rust
35
star
10

serdebench

Rust
30
star
11

newlinebench

Rust
21
star
12

partition

partition slices in-place by a predicate
Rust
14
star
13

compressbench

A benchmark of Rust compression libraries
Rust
9
star
14

smallvectune

Rust
9
star
15

llogiq.github.io

My github page
HTML
8
star
16

pathsep

A os agnostic way to get a path separator in macros
Rust
7
star
17

extra_lints

more lints for rust (now subsumed in rust-clippy)
Rust
7
star
18

arraymap

Adds a trait to map functions over arrays
Rust
6
star
19

bsdiff-rs

A Rust BSDiff port
Rust
4
star
20

arraymapbench

A benchmark of various map methods
Rust
2
star
21

twirer

A short program I use to collect and filter the core changes for This Week In Rust
Rust
2
star
22

picnic

Your Picnic Is On Fire!
Rust
1
star
23

openpgp

Rust
1
star
24

rangeset

(WIP) a RangeSet implementation
Rust
1
star
25

typetree

a data structure within Rust's type system
Rust
1
star
26

rust-stockfighter

Simple Rust Wrapper for stockfigher
Rust
1
star
27

ternary

Kleene logic in Rust's type system
Rust
1
star
28

lib_json

Allocationless Json Parsing
Rust
1
star
29

wom

Write-Only Memory for Rust
Rust
1
star