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

Prevent Negative TIDs in Trace #1002

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 14 additions & 5 deletions libkineto/src/output_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ void ChromeTraceLogger::sanitizeStrForJSON(std::string& value) {
value.erase(std::remove(value.begin(), value.end(), '\n'), value.end());
}

static inline int32_t sanitizeTid(int32_t tid) {
// Convert all negative tids to its positive value. Create a specific case
// for INT_MIN so it is obvious how it is being handled.
if (tid == INT_MIN) {
return 0;
}
return std::abs(tid);
}

void ChromeTraceLogger::metadataToJSON(
const std::unordered_map<std::string, std::string>& metadata) {
for (auto [k, v] : metadata) {
Expand Down Expand Up @@ -197,9 +206,9 @@ void ChromeTraceLogger::handleResourceInfo(
"sort_index": {}
}}
}},)JSON",
time/1000, time%1000, info.deviceId, info.id,
time/1000, time%1000, info.deviceId, sanitizeTid(info.id),
info.name,
time/1000, time%1000, info.deviceId, info.id,
time/1000, time%1000, info.deviceId, sanitizeTid(info.id),
info.sortIndex);
// clang-format on
}
Expand Down Expand Up @@ -312,7 +321,7 @@ void ChromeTraceLogger::handleGenericInstantEvent(
toString(op.type()),
op.name(),
op.deviceId(),
op.resourceId(),
sanitizeTid(op.resourceId()),
ts / 1000,
ts % 1000,
op.metadataJson());
Expand Down Expand Up @@ -483,7 +492,7 @@ void ChromeTraceLogger::handleActivity(const libkineto::ITraceActivity& op) {
"ph": "X", "cat": "{}", "name": "{}", "pid": {}, "tid": {},
"ts": {}.{:03}, "dur": {}.{:03}{}
}},)JSON",
toString(op.type()), op_name, device, resource,
toString(op.type()), op_name, device, sanitizeTid(resource),
ts/1000, ts %1000, duration/1000, duration %1000, args);
// clang-format on
if (op.flowId() > 0) {
Expand Down Expand Up @@ -537,7 +546,7 @@ void ChromeTraceLogger::handleLink(
"ph": "{}", "id": {}, "pid": {}, "tid": {}, "ts": {}.{:03},
"cat": "{}", "name": "{}"{}
}},)JSON",
type, id, e.deviceId(), e.resourceId(), ts/1000, ts%1000, name, name, binding);
type, id, e.deviceId(), sanitizeTid(e.resourceId()), ts/1000, ts%1000, name, name, binding);
// clang-format on
}

Expand Down
Loading