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

Record thread relationships #174

Open
emilk opened this issue Dec 11, 2023 · 0 comments
Open

Record thread relationships #174

emilk opened this issue Dec 11, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@emilk
Copy link
Collaborator

emilk commented Dec 11, 2023

I'd like to be able to record the relationship between a "parent" thread and the tasks it spawns. Consider this:

use rayon::prelude::*;

fn do_many_jobs(jobs: &[Jobs]) -> Vec<Output> {
    puffin::profile_scope!("do_many_jobs")!;
    jobs.par_iter().map(|job| {
        puffin::profile_scope!("do_job");
        do_job(job);
    }).collect()
}

This will show up as one thread with do_many_jobs, and then maybe four worker threads doing do_job. In the flamegrpah however, there is no relationship behind them. It would be great if each do_job scope had an arrow pointing to it from the do_many_jobs scope, showing their connection.

We could maybe accomplish this with something like

use rayon::prelude::*;

fn do_many_jobs(jobs: &[Jobs]) -> Vec<Output> {
    puffin::profile_scope!("do_many_jobs")!;
    let parent_thread_id = puffin::thread_id();
    jobs.par_iter().map(move |job| {
        puffin::profile_scope!("do_job", parent=parent_thread_id);
        do_job(job);
    }).collect()
}

In the recording stream, these thread relationships would be rare, but would require some additional dynamic data. For instance, one extra control-byte which, if some bit is set in it, would be followed by a thread id.
We could use another bit in the same control-byte to indicate if there is any dynamic string, saving us a byte again in common cases.

The thread-id could be the u64 returned by std::thread::current().id().as_u64.

@emilk emilk added the enhancement New feature or request label Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant