From 1e74a899247ae8a70424524e5104745f2cad36a0 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 24 Aug 2019 01:06:21 +0200 Subject: [PATCH] Check if there's data to read from kernel. Reading from kernel pipe, while being a blocking operation, spin locks the thread. --- client/TracySysTrace.cpp | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/client/TracySysTrace.cpp b/client/TracySysTrace.cpp index 8c709660..b06ae482 100644 --- a/client/TracySysTrace.cpp +++ b/client/TracySysTrace.cpp @@ -320,6 +320,8 @@ void SysTraceSendExternalName( uint64_t thread ) # include # include # include +# include +# include # include # include # include @@ -674,24 +676,43 @@ void SysTraceWorker( void* ptr ) } } #else -static void ProcessTraceLines( FILE* f ) +static void ProcessTraceLines( int fd ) { - size_t lsz = 1024; - auto line = (char*)malloc( lsz ); + char* buf = (char*)tracy_malloc( 64*1024 ); + + struct pollfd pfd; + pfd.fd = fd; + pfd.events = POLLIN | POLLERR; for(;;) { - auto rd = getline( &line, &lsz, f ); - if( rd < 0 ) break; + while( poll( &pfd, 1, 0 ) <= 0 ) std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); + + const auto rd = read( fd, buf, 64*1024 ); + if( rd <= 0 ) break; #ifdef TRACY_ON_DEMAND if( !GetProfiler().IsConnected() ) continue; #endif - HandleTraceLine( line ); + auto line = buf; + const auto end = buf + rd; + for(;;) + { + auto next = line; + while( next < end && *next != '\n' ) next++; + next++; + if( next >= end ) + { + break; + } + + HandleTraceLine( line ); + line = next; + } } - free( line ); + tracy_free( buf ); } void SysTraceWorker( void* ptr ) @@ -701,10 +722,10 @@ void SysTraceWorker( void* ptr ) memcpy( tmp, BasePath, sizeof( BasePath ) - 1 ); memcpy( tmp + sizeof( BasePath ) - 1, TracePipe, sizeof( TracePipe ) ); - FILE* f = fopen( tmp, "rb" ); - if( !f ) return; - ProcessTraceLines( f ); - fclose( f ); + int fd = open( tmp, O_RDONLY ); + if( fd < 0 ) return; + ProcessTraceLines( fd ); + close( fd ); } #endif