From 130365f4ff45db3e532f5e49533b54bf6d0b2019 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 27 Sep 2019 15:51:29 +0200 Subject: [PATCH] Inject tracy_systrace into filesystem and use instead of cat. Statistics for a one-minute trace: Capture tool | Running time | Running regions ---------------+--------------+----------------- cat | 25.11 s | 392,300 tracy_systrace | 10.41 s | 12,249 --- client/TracySysTrace.cpp | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/client/TracySysTrace.cpp b/client/TracySysTrace.cpp index bac44e5b..3820b6d4 100644 --- a/client/TracySysTrace.cpp +++ b/client/TracySysTrace.cpp @@ -322,6 +322,7 @@ void SysTraceSendExternalName( uint64_t thread ) # include # include +# include # include # include # include @@ -333,6 +334,10 @@ void SysTraceSendExternalName( uint64_t thread ) # include "TracyProfiler.hpp" +# ifdef __ANDROID__ +# include "TracySysTracePayload.hpp" +# endif + namespace tracy { @@ -381,6 +386,43 @@ static bool TraceWrite( const char* path, size_t psz, const char* val, size_t vs } #endif +#ifdef __ANDROID__ +void SysTraceInjectPayload() +{ + int pipefd[2]; + if( pipe( pipefd ) == 0 ) + { + const auto pid = fork(); + if( pid == 0 ) + { + // child + close( pipefd[1] ); + if( dup2( pipefd[0], STDIN_FILENO ) >= 0 ) + { + close( pipefd[0] ); + execlp( "su", "su", "-c", "cat > /data/tracy_systrace", (char*)nullptr ); + exit( 1 ); + } + } + else if( pid > 0 ) + { + // parent + close( pipefd[0] ); + +#ifdef __aarch64__ + write( pipefd[1], tracy_systrace_aarch64_data, tracy_systrace_aarch64_size ); +#else + write( pipefd[1], tracy_systrace_armv7_data, tracy_systrace_armv7_size ); +#endif + close( pipefd[1] ); + waitpid( pid, nullptr, 0 ); + + system( "su -c 'chmod 700 /data/tracy_systrace'" ); + } + } +} +#endif + bool SysTraceStart() { if( !TraceWrite( TracingOn, sizeof( TracingOn ), "0", 2 ) ) return false; @@ -395,6 +437,11 @@ bool SysTraceStart() #endif if( !TraceWrite( SchedSwitch, sizeof( SchedSwitch ), "1", 2 ) ) return false; if( !TraceWrite( SchedWakeup, sizeof( SchedWakeup ), "1", 2 ) ) return false; + +#if defined __ANDROID__ && ( defined __aarch64__ || defined __ARM_ARCH ) + SysTraceInjectPayload(); +#endif + if( !TraceWrite( TracingOn, sizeof( TracingOn ), "1", 2 ) ) return false; return true; @@ -666,6 +713,9 @@ void SysTraceWorker( void* ptr ) if( dup2( pipefd[1], STDOUT_FILENO ) >= 0 ) { close( pipefd[1] ); +#if defined __ANDROID__ && ( defined __aarch64__ || defined __ARM_ARCH ) + execlp( "su", "su", "-c", "/data/tracy_systrace", (char*)nullptr ); +#endif execlp( "su", "su", "-c", "cat /sys/kernel/debug/tracing/trace_pipe", (char*)nullptr ); exit( 1 ); }