2019-12-15 22:28:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
#include <fstream>
|
2019-12-15 22:28:19 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
#include "json.hpp"
|
|
|
|
|
2019-12-15 22:28:19 +00:00
|
|
|
#include "../../server/TracyFileWrite.hpp"
|
|
|
|
#include "../../server/TracyWorker.hpp"
|
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
using json = nlohmann::json;
|
2019-12-15 22:28:19 +00:00
|
|
|
|
|
|
|
void Usage()
|
|
|
|
{
|
2019-12-16 17:55:21 +00:00
|
|
|
printf( "Usage: import-chrome input.json output.tracy\n\n" );
|
2019-12-15 22:28:19 +00:00
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if( !AttachConsole( ATTACH_PARENT_PROCESS ) )
|
|
|
|
{
|
|
|
|
AllocConsole();
|
|
|
|
SetConsoleMode( GetStdHandle( STD_OUTPUT_HANDLE ), 0x07 );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tracy::FileWrite::Compression clev = tracy::FileWrite::Compression::Fast;
|
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
if( argc != 3 ) Usage();
|
2019-12-15 22:28:19 +00:00
|
|
|
|
|
|
|
const char* input = argv[1];
|
|
|
|
const char* output = argv[2];
|
|
|
|
|
|
|
|
printf( "Loading...\r" );
|
|
|
|
fflush( stdout );
|
2019-12-16 17:55:21 +00:00
|
|
|
|
|
|
|
std::ifstream is( input );
|
|
|
|
if( !is.is_open() )
|
2019-12-15 22:28:19 +00:00
|
|
|
{
|
|
|
|
fprintf( stderr, "Cannot open input file!\n" );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
2019-12-16 17:55:21 +00:00
|
|
|
json j;
|
|
|
|
is >> j;
|
|
|
|
is.close();
|
2019-12-15 22:28:19 +00:00
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
printf( "\33[2KParsing...\r" );
|
|
|
|
fflush( stdout );
|
2019-12-15 22:28:19 +00:00
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
std::vector<tracy::Worker::ImportEventTimeline> timeline;
|
|
|
|
std::vector<tracy::Worker::ImportEventMessages> messages;
|
2020-06-20 13:30:06 +00:00
|
|
|
std::vector<tracy::Worker::ImportEventPlots> plots;
|
2019-12-15 22:28:19 +00:00
|
|
|
|
2019-12-16 20:41:57 +00:00
|
|
|
if( j.is_object() && j.contains( "traceEvents" ) )
|
|
|
|
{
|
|
|
|
j = j["traceEvents"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !j.is_array() )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Input must be either an array of events or an object containing an array of events under \"traceEvents\" key.\n" );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
for( auto& v : j )
|
|
|
|
{
|
|
|
|
const auto type = v["ph"].get<std::string>();
|
2020-05-12 18:36:11 +00:00
|
|
|
|
|
|
|
std::string zoneText = "";
|
|
|
|
if ( v.contains( "args" ) )
|
|
|
|
{
|
|
|
|
for ( auto& kv : v["args"].items() )
|
|
|
|
{
|
|
|
|
zoneText += kv.key() + ": " + kv.value().dump() + "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
if( type == "B" )
|
|
|
|
{
|
|
|
|
timeline.emplace_back( tracy::Worker::ImportEventTimeline {
|
|
|
|
v["tid"].get<uint64_t>(),
|
|
|
|
uint64_t( v["ts"].get<double>() * 1000. ),
|
|
|
|
v["name"].get<std::string>(),
|
2020-05-12 18:36:11 +00:00
|
|
|
std::move(zoneText),
|
2019-12-16 17:55:21 +00:00
|
|
|
false
|
|
|
|
} );
|
2019-12-15 22:28:19 +00:00
|
|
|
}
|
2019-12-16 17:55:21 +00:00
|
|
|
else if( type == "E" )
|
|
|
|
{
|
|
|
|
timeline.emplace_back( tracy::Worker::ImportEventTimeline {
|
|
|
|
v["tid"].get<uint64_t>(),
|
|
|
|
uint64_t( v["ts"].get<double>() * 1000. ),
|
|
|
|
"",
|
2020-05-12 18:36:11 +00:00
|
|
|
std::move(zoneText),
|
2019-12-16 17:55:21 +00:00
|
|
|
true
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
else if( type == "X" )
|
|
|
|
{
|
|
|
|
const auto tid = v["tid"].get<uint64_t>();
|
|
|
|
const auto ts0 = uint64_t( v["ts"].get<double>() * 1000. );
|
|
|
|
const auto ts1 = v["dur"].is_object() ? ts0 + uint64_t( v["dur"].get<double>() * 1000. ) : ts0;
|
|
|
|
const auto name = v["name"].get<std::string>();
|
2020-05-12 18:36:11 +00:00
|
|
|
timeline.emplace_back( tracy::Worker::ImportEventTimeline { tid, ts0, name, std::move(zoneText), false } );
|
2020-05-12 09:44:36 +00:00
|
|
|
timeline.emplace_back( tracy::Worker::ImportEventTimeline { tid, ts1, "", "", true } );
|
2019-12-16 17:55:21 +00:00
|
|
|
}
|
|
|
|
else if( type == "i" || type == "I" )
|
|
|
|
{
|
|
|
|
messages.emplace_back( tracy::Worker::ImportEventMessages {
|
|
|
|
v["tid"].get<uint64_t>(),
|
|
|
|
uint64_t( v["ts"].get<double>() * 1000. ),
|
|
|
|
v["name"].get<std::string>()
|
|
|
|
} );
|
|
|
|
}
|
2020-06-20 21:57:30 +00:00
|
|
|
else if( type == "C" )
|
|
|
|
{
|
|
|
|
auto timestamp = int64_t( v["ts"].get<double>() * 1000 );
|
|
|
|
for( auto& kv : v["args"].items() )
|
|
|
|
{
|
|
|
|
bool plotFound = false;
|
|
|
|
auto& metricName = kv.key();
|
|
|
|
auto dataPoint = std::make_pair( timestamp, kv.value().get<double>() );
|
|
|
|
|
|
|
|
// The input file is assumed to have only very few metrics,
|
|
|
|
// so iterating through plots is not a problem.
|
|
|
|
for( auto& plot : plots )
|
|
|
|
{
|
|
|
|
if( plot.name == metricName )
|
|
|
|
{
|
|
|
|
plot.data.emplace_back( dataPoint );
|
|
|
|
plotFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !plotFound )
|
|
|
|
{
|
|
|
|
auto formatting = tracy::PlotValueFormatting::Number;
|
|
|
|
|
|
|
|
// NOTE: With C++20 one could say metricName.ends_with( "_bytes" ) instead of rfind
|
|
|
|
auto metricNameLen = metricName.size();
|
|
|
|
if ( metricNameLen >= 6 && metricName.rfind( "_bytes" ) == metricNameLen - 6 ) {
|
|
|
|
formatting = tracy::PlotValueFormatting::Memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
plots.emplace_back( tracy::Worker::ImportEventPlots {
|
|
|
|
std::move( metricName ),
|
|
|
|
formatting,
|
|
|
|
{ dataPoint }
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 17:55:21 +00:00
|
|
|
}
|
2019-12-15 22:28:19 +00:00
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
std::stable_sort( timeline.begin(), timeline.end(), [] ( const auto& l, const auto& r ) { return l.timestamp < r.timestamp; } );
|
|
|
|
std::stable_sort( messages.begin(), messages.end(), [] ( const auto& l, const auto& r ) { return l.timestamp < r.timestamp; } );
|
2020-06-20 13:30:06 +00:00
|
|
|
for( auto& v : plots ) std::stable_sort( v.data.begin(), v.data.end(), [] ( const auto& l, const auto& r ) { return l.first < r.first; } );
|
2019-12-15 22:28:19 +00:00
|
|
|
|
2019-12-16 17:55:21 +00:00
|
|
|
uint64_t mts = 0;
|
|
|
|
if( !timeline.empty() )
|
2019-12-15 22:28:19 +00:00
|
|
|
{
|
2019-12-16 17:55:21 +00:00
|
|
|
mts = timeline[0].timestamp;
|
2019-12-15 22:28:19 +00:00
|
|
|
}
|
2019-12-16 17:55:21 +00:00
|
|
|
if( !messages.empty() )
|
2019-12-15 22:28:19 +00:00
|
|
|
{
|
2019-12-16 17:55:21 +00:00
|
|
|
if( mts > messages[0].timestamp ) mts = messages[0].timestamp;
|
2019-12-15 22:28:19 +00:00
|
|
|
}
|
2019-12-16 17:55:21 +00:00
|
|
|
for( auto& v : timeline ) v.timestamp -= mts;
|
|
|
|
for( auto& v : messages ) v.timestamp -= mts;
|
2020-06-20 21:57:30 +00:00
|
|
|
for( auto& plot : plots )
|
|
|
|
for( auto& v : plot.data )
|
|
|
|
v.first -= mts;
|
2019-12-16 17:55:21 +00:00
|
|
|
|
|
|
|
printf( "\33[2KProcessing...\r" );
|
|
|
|
fflush( stdout );
|
|
|
|
|
|
|
|
auto program = input;
|
|
|
|
while( *program ) program++;
|
|
|
|
program--;
|
|
|
|
while( program > input && ( *program != '/' || *program != '\\' ) ) program--;
|
2020-06-20 13:30:06 +00:00
|
|
|
tracy::Worker worker( program, timeline, messages, plots );
|
2019-12-16 17:55:21 +00:00
|
|
|
|
|
|
|
auto w = std::unique_ptr<tracy::FileWrite>( tracy::FileWrite::Open( output, clev ) );
|
|
|
|
if( !w )
|
2019-12-15 22:28:19 +00:00
|
|
|
{
|
2019-12-16 17:55:21 +00:00
|
|
|
fprintf( stderr, "Cannot open output file!\n" );
|
2019-12-15 22:28:19 +00:00
|
|
|
exit( 1 );
|
|
|
|
}
|
2019-12-19 16:25:22 +00:00
|
|
|
printf( "\33[2KSaving...\r" );
|
2019-12-16 17:55:21 +00:00
|
|
|
fflush( stdout );
|
|
|
|
worker.Write( *w );
|
2019-12-15 22:28:19 +00:00
|
|
|
|
2019-12-19 16:25:22 +00:00
|
|
|
printf( "\33[2KCleanup...\n" );
|
|
|
|
fflush( stdout );
|
|
|
|
|
2019-12-15 22:28:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|