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

Add PID and TID to the JSON output #30

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/chrometrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,25 @@ pub fn write_chrometrace(target: &Path, profile_data: &[GpuTimerScopeResult]) ->
}

fn write_results_recursive(file: &mut File, result: &GpuTimerScopeResult, last: bool) -> std::io::Result<()> {
// note: ThreadIds are under the control of Rust’s standard library
// and there may not be any relationship between ThreadId and the underlying platform’s notion of a thread identifier
//
// There's a proposal for stabilization of ThreadId::as_u64, which
// would eliminate the need for this hack: https://github.com/rust-lang/rust/pull/110738
//
// for now, we use this hack to convert to integer
let tid_to_int = |tid| {
format!("{:?}", tid)
.replace("ThreadId(", "")
.replace(')', "")
.parse::<u64>()
.unwrap_or(std::u64::MAX)
};
write!(
file,
r#"{{ "pid":1, "tid":1, "ts":{}, "dur":{}, "ph":"X", "name":"{}" }}{}"#,
r#"{{ "pid":{}, "tid":{}, "ts":{}, "dur":{}, "ph":"X", "name":"{}" }}{}"#,
result.pid,
tid_to_int(result.tid),
result.time.start * 1000.0 * 1000.0,
(result.time.end - result.time.start) * 1000.0 * 1000.0,
result.label,
Expand Down
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ On [`GpuProfiler::end_frame`], we memorize the total size of all `QueryPool`s in
`QueryPool` from finished frames are re-used, unless they are deemed too small.
*/

use std::{convert::TryInto, ops::Range};
use std::{convert::TryInto, ops::Range, thread::ThreadId};

pub mod chrometrace;
pub mod macros;
Expand All @@ -88,6 +88,8 @@ pub struct GpuTimerScopeResult {
pub time: Range<f64>,

pub nested_scopes: Vec<GpuTimerScopeResult>,
pub pid: u32,
pub tid: ThreadId,
}

pub struct GpuProfiler {
Expand Down Expand Up @@ -171,10 +173,15 @@ impl GpuProfiler {
start_query.query_idx,
);

let pid = std::process::id();
let tid = std::thread::current().id();

self.open_scopes.push(UnprocessedTimerScope {
label: String::from(label),
start_query,
..Default::default()
nested_scopes: Vec::new(),
pid,
tid,
});
}
if self.enable_debug_marker {
Expand Down Expand Up @@ -406,6 +413,8 @@ impl GpuProfiler {
label: scope.label,
time: (start_raw as f64 * timestamp_to_sec)..(end_raw as f64 * timestamp_to_sec),
nested_scopes,
pid: scope.pid,
tid: scope.tid,
}
})
.collect()
Expand All @@ -418,11 +427,12 @@ struct QueryPoolQueryAddress {
query_idx: u32,
}

#[derive(Default)]
struct UnprocessedTimerScope {
label: String,
start_query: QueryPoolQueryAddress,
nested_scopes: Vec<UnprocessedTimerScope>,
pub pid: u32,
pub tid: ThreadId,
}

/// A pool of queries, consisting of a single queryset & buffer for query results.
Expand Down