Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't seem to work with multiple threads #42

Open
Ploppz opened this issue Mar 8, 2019 · 5 comments
Open

Doesn't seem to work with multiple threads #42

Ploppz opened this issue Mar 8, 2019 · 5 comments

Comments

@Ploppz
Copy link

Ploppz commented Mar 8, 2019

Minimal test case:

use rayon::prelude::*;
use flame;
use std:: fs::File;

fn something(i: usize) -> f64 {
    flame::start("Something");
    std::thread::sleep(std::time::Duration::from_millis(10));
    flame::end("Something");
    0.56
}
fn main() {
    let a = (0..200).collect::<Vec<_>>()
        .par_iter()
        .map(|i| something(*i))
        .collect::<Vec<_>>();

    flame::dump_html(&mut File::create("flames.html").unwrap()).unwrap();
    println!("{:?}", flame::threads());
}

Dependencies:

rayon = "1.0.3"
flame = "0.2.2"

The written file has no graphics.
The output is: [Thread { id: 140470522639296, name: Some("main"), spans: [], _priv: () }], so no data is available.

@visceralfield
Copy link

I would also love to see this work in multi-threaded environments.

@TyOverby
Copy link
Collaborator

TyOverby commented Oct 7, 2019

I think the real problem here is that Rayon keeps those threads around in a pool, so the normal destructor-based solution doesn’t work. Try calling commit_thread at the end of your something function.

@That3Percent
Copy link

The commit_thread idea doesn't seem to work.

Here's what I tried...

let pool = rayon::ThreadPoolBuilder::new().exit_handler(|_| flame::commit_thread()).build().unwrap();
pool.install(|| {
   /* code which does rayon things */
});
drop(pool);
flame::commit_thread();

flame::dump_html(&mut File::create("flame-graph.html").unwrap()).unwrap();

This code should ensure that each thread both runs the destructor based solution because when the pool is dropped the threads are dropped as well, and it runs the commit_thread explicitly for the exit of each thread in the exit_handler. dump_html just prints a single bar with the label "undefined."

@That3Percent
Copy link

What is necessary here is in the exit_handler to also call dump_html for each thread using a unique filename based on the thread id. Unfortunately, the output is quite difficult to parse because none of the data in the several different html files will be synchronized or scaled.

@smr97
Copy link

smr97 commented May 9, 2020

Shameless plug, but @Ploppz @l-x-u you can check out https://github.com/wagnerf42/rayon-logs

This shows you how rayon is splitting tasks and how much time is spent in each task. It however, does not have the flamegraph characteristics, in that it is not useful if you want callgraph information in multithreaded code. However, you can still add some hardware counters to parallel regions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants