refactor after review

This commit is contained in:
Simon Cruanes 2021-05-10 11:20:39 -04:00
parent 7e694d452a
commit d38f579ec3

View File

@ -133,28 +133,37 @@ int main( int argc, char** argv )
printf( "\33[2KParsing...\r" ); printf( "\33[2KParsing...\r" );
fflush( stdout ); fflush( stdout );
struct PidTid { // encode a pair of "real pid, real tid" from a trace into a
// pseudo thread ID living in the single namespace of Tracy threads.
struct PidTidEncoder {
uint64_t tid; uint64_t tid;
uint64_t pid; uint64_t pid;
uint64_t both; // fake thread id, unique within Tracy uint64_t pseudo_tid; // fake thread id, unique within Tracy
}; };
std::vector<PidTid> tids; std::vector<PidTidEncoder> tid_encoders;
std::vector<tracy::Worker::ImportEventTimeline> timeline; std::vector<tracy::Worker::ImportEventTimeline> timeline;
std::vector<tracy::Worker::ImportEventMessages> messages; std::vector<tracy::Worker::ImportEventMessages> messages;
std::vector<tracy::Worker::ImportEventPlots> plots; std::vector<tracy::Worker::ImportEventPlots> plots;
std::unordered_map<uint64_t, std::string> threadNames; std::unordered_map<uint64_t, std::string> threadNames;
const auto getTid = [&](uint64_t pid, uint64_t tid) -> uint64_t { const auto getPseudoTid = [&](json& val) -> uint64_t {
for ( auto &pair : tids ) { uint64_t pid = 0;
if ( pair.pid == pid && pair.tid == tid ) { if ( val.contains( "pid" ) ) {
return pair.both; pid = val["pid"].get<uint64_t>();
}
const auto real_tid = val["tid"].get<uint64_t>();
for ( auto &pair : tid_encoders) {
if ( pair.pid == pid && pair.tid == real_tid ) {
return pair.pseudo_tid;
} }
} }
const auto result = tids.size(); const auto pseudo_tid = tid_encoders.size();
tids.emplace_back(PidTid {.tid=tid, .pid=pid, .both=result}); tid_encoders.emplace_back(PidTidEncoder {real_tid, pid, pseudo_tid});
return result; return pseudo_tid;
}; };
if( j.is_object() && j.contains( "traceEvents" ) ) if( j.is_object() && j.contains( "traceEvents" ) )
@ -185,15 +194,10 @@ int main( int argc, char** argv )
} }
} }
uint64_t pid = 0;
if ( v.contains( "pid" ) ) {
pid = v["pid"].get<uint64_t>();
}
if( type == "B" ) if( type == "B" )
{ {
timeline.emplace_back( tracy::Worker::ImportEventTimeline { timeline.emplace_back( tracy::Worker::ImportEventTimeline {
getTid(pid, v["tid"].get<uint64_t>()), getPseudoTid(v),
uint64_t( v["ts"].get<double>() * 1000. ), uint64_t( v["ts"].get<double>() * 1000. ),
v["name"].get<std::string>(), v["name"].get<std::string>(),
std::move(zoneText), std::move(zoneText),
@ -203,7 +207,7 @@ int main( int argc, char** argv )
else if( type == "E" ) else if( type == "E" )
{ {
timeline.emplace_back( tracy::Worker::ImportEventTimeline { timeline.emplace_back( tracy::Worker::ImportEventTimeline {
getTid(pid, v["tid"].get<uint64_t>()), getPseudoTid(v),
uint64_t( v["ts"].get<double>() * 1000. ), uint64_t( v["ts"].get<double>() * 1000. ),
"", "",
std::move(zoneText), std::move(zoneText),
@ -212,7 +216,7 @@ int main( int argc, char** argv )
} }
else if( type == "X" ) else if( type == "X" )
{ {
const auto tid = getTid(pid, v["tid"].get<uint64_t>()); const auto tid = getPseudoTid(v);
const auto ts0 = uint64_t( v["ts"].get<double>() * 1000. ); const auto ts0 = uint64_t( v["ts"].get<double>() * 1000. );
const auto ts1 = ts0 + uint64_t( v["dur"].get<double>() * 1000. ); const auto ts1 = ts0 + uint64_t( v["dur"].get<double>() * 1000. );
const auto name = v["name"].get<std::string>(); const auto name = v["name"].get<std::string>();
@ -222,7 +226,7 @@ int main( int argc, char** argv )
else if( type == "i" || type == "I" ) else if( type == "i" || type == "I" )
{ {
messages.emplace_back( tracy::Worker::ImportEventMessages { messages.emplace_back( tracy::Worker::ImportEventMessages {
getTid(pid, v["tid"].get<uint64_t>()), getPseudoTid(v),
uint64_t( v["ts"].get<double>() * 1000. ), uint64_t( v["ts"].get<double>() * 1000. ),
v["name"].get<std::string>() v["name"].get<std::string>()
} ); } );
@ -269,7 +273,7 @@ int main( int argc, char** argv )
{ {
if (v.contains("name") && v["name"] == "thread_name" && v.contains("args") && v["args"].is_object() && v["args"].contains("name")) if (v.contains("name") && v["name"] == "thread_name" && v.contains("args") && v["args"].is_object() && v["args"].contains("name"))
{ {
const auto tid = getTid(pid, v["tid"].get<uint64_t>()); const auto tid = getPseudoTid(v);
threadNames[tid] = v["args"]["name"].get<std::string>(); threadNames[tid] = v["args"]["name"].get<std::string>();
} }
} }