This commit is contained in:
Grégoire Roussel 2024-08-09 01:20:40 +02:00
parent 0848bb1603
commit 4f769258b4
3 changed files with 108 additions and 104 deletions

View File

@ -14,7 +14,8 @@
static bool s_isStdoutATerminal = false; static bool s_isStdoutATerminal = false;
void InitIsStdoutATerminal() { void InitIsStdoutATerminal()
{
#ifdef _WIN32 #ifdef _WIN32
s_isStdoutATerminal = _isatty( fileno( stdout ) ); s_isStdoutATerminal = _isatty( fileno( stdout ) );
#else #else
@ -24,7 +25,6 @@ void InitIsStdoutATerminal() {
bool IsStdoutATerminal() { return s_isStdoutATerminal; } bool IsStdoutATerminal() { return s_isStdoutATerminal; }
#define ANSI_RESET "\033[0m" #define ANSI_RESET "\033[0m"
#define ANSI_BOLD "\033[1m" #define ANSI_BOLD "\033[1m"
#define ANSI_BLACK "\033[30m" #define ANSI_BLACK "\033[30m"
@ -40,9 +40,10 @@ bool IsStdoutATerminal() { return s_isStdoutATerminal; }
// Like printf, but if stdout is a terminal, prepends the output with // Like printf, but if stdout is a terminal, prepends the output with
// the given `ansiEscape` and appends ANSI_RESET. // the given `ansiEscape` and appends ANSI_RESET.
#ifdef __GNUC__ #ifdef __GNUC__
[[gnu::format(__printf__, 2, 3)]] [[gnu::format( __printf__, 2, 3 )]]
#endif #endif
void AnsiPrintf( const char* ansiEscape, const char* format, ... ) { void AnsiPrintf( const char* ansiEscape, const char* format, ... )
{
if( IsStdoutATerminal() ) if( IsStdoutATerminal() )
{ {
// Prepend ansiEscape and append ANSI_RESET. // Prepend ansiEscape and append ANSI_RESET.
@ -65,27 +66,30 @@ void AnsiPrintf( const char* ansiEscape, const char* format, ... ) {
// Check handshake status // Check handshake status
// If failure, printf helpful message and return non-zero // If failure, printf helpful message and return non-zero
int checkHandshake(tracy::HandshakeStatus handshake) int checkHandshake( tracy::HandshakeStatus handshake )
{ {
if( handshake == tracy::HandshakeProtocolMismatch ) if( handshake == tracy::HandshakeProtocolMismatch )
{ {
printf( "\nThe client you are trying to connect to uses incompatible protocol version.\nMake sure you are using the same Tracy version on both client and server.\n" ); printf(
"\nThe client you are trying to connect to uses incompatible protocol version.\nMake sure you are using the same Tracy version on both client and server.\n" );
return 1; return 1;
} }
if( handshake == tracy::HandshakeNotAvailable ) if( handshake == tracy::HandshakeNotAvailable )
{ {
printf( "\nThe client you are trying to connect to is no longer able to sent profiling data,\nbecause another server was already connected to it.\nYou can do the following:\n\n 1. Restart the client application.\n 2. Rebuild the client application with on-demand mode enabled.\n" ); printf(
"\nThe client you are trying to connect to is no longer able to sent profiling data,\nbecause another server was already connected to it.\nYou can do the following:\n\n 1. Restart the client application.\n 2. Rebuild the client application with on-demand mode enabled.\n" );
return 2; return 2;
} }
if( handshake == tracy::HandshakeDropped ) if( handshake == tracy::HandshakeDropped )
{ {
printf( "\nThe client you are trying to connect to has disconnected during the initial\nconnection handshake. Please check your network configuration.\n" ); printf(
"\nThe client you are trying to connect to has disconnected during the initial\nconnection handshake. Please check your network configuration.\n" );
return 3; return 3;
} }
return 0; return 0;
} }
void printCurrentMemoryUsage(int64_t memoryLimit) void printCurrentMemoryUsage( int64_t memoryLimit )
{ {
AnsiPrintf( ANSI_RED ANSI_BOLD, "%s", tracy::MemSizeToString( tracy::memUsage.load( std::memory_order_relaxed ) ) ); AnsiPrintf( ANSI_RED ANSI_BOLD, "%s", tracy::MemSizeToString( tracy::memUsage.load( std::memory_order_relaxed ) ) );
if( memoryLimit > 0 ) if( memoryLimit > 0 )
@ -95,7 +99,7 @@ void printCurrentMemoryUsage(int64_t memoryLimit)
} }
} }
void printWorkerUpdate(tracy::Worker& worker, int64_t memoryLimit, bool erase, bool memoryUsage) void printWorkerUpdate( tracy::Worker& worker, int64_t memoryLimit, bool erase, bool memoryUsage )
{ {
auto& lock = worker.GetMbpsDataLock(); auto& lock = worker.GetMbpsDataLock();
lock.lock(); lock.lock();
@ -111,30 +115,30 @@ void printWorkerUpdate(tracy::Worker& worker, int64_t memoryLimit, bool erase, b
unit = "Kbps"; unit = "Kbps";
unitsPerMbps = 1000.f; unitsPerMbps = 1000.f;
} }
if(erase) if( erase )
{ {
AnsiPrintf(ANSI_ERASE_LINE, "\r"); AnsiPrintf( ANSI_ERASE_LINE, "\r" );
} }
AnsiPrintf( ANSI_CYAN ANSI_BOLD, "%7.2f %s", mbps * unitsPerMbps, unit ); AnsiPrintf( ANSI_CYAN ANSI_BOLD, "%7.2f %s", mbps * unitsPerMbps, unit );
printf( " /"); printf( " /" );
AnsiPrintf( ANSI_CYAN ANSI_BOLD, "%5.1f%%", compRatio * 100.f ); AnsiPrintf( ANSI_CYAN ANSI_BOLD, "%5.1f%%", compRatio * 100.f );
printf( " ="); printf( " =" );
AnsiPrintf( ANSI_YELLOW ANSI_BOLD, "%7.2f Mbps", mbps / compRatio ); AnsiPrintf( ANSI_YELLOW ANSI_BOLD, "%7.2f Mbps", mbps / compRatio );
printf( " | "); printf( " | " );
AnsiPrintf( ANSI_YELLOW, "Tx: "); AnsiPrintf( ANSI_YELLOW, "Tx: " );
AnsiPrintf( ANSI_GREEN, "%s", tracy::MemSizeToString( netTotal ) ); AnsiPrintf( ANSI_GREEN, "%s", tracy::MemSizeToString( netTotal ) );
if (memoryUsage) if( memoryUsage )
{ {
printf( " | "); printf( " | " );
printCurrentMemoryUsage(memoryLimit); printCurrentMemoryUsage( memoryLimit );
} }
printf( " | "); printf( " | " );
AnsiPrintf( ANSI_RED, "%s", tracy::TimeToString( worker.GetLastTime() - worker.GetFirstTime() ) ); AnsiPrintf( ANSI_RED, "%s", tracy::TimeToString( worker.GetLastTime() - worker.GetFirstTime() ) );
fflush( stdout ); fflush( stdout );
} }
bool printWorkerFailure(tracy::Worker& worker, char const* prefix) bool printWorkerFailure( tracy::Worker& worker, char const* prefix )
{ {
auto const& failure = worker.GetFailureType(); auto const& failure = worker.GetFailureType();
if( failure == tracy::Worker::Failure::None ) if( failure == tracy::Worker::Failure::None )
@ -143,7 +147,8 @@ bool printWorkerFailure(tracy::Worker& worker, char const* prefix)
} }
else else
{ {
AnsiPrintf( ANSI_RED ANSI_BOLD, "\n%s Instrumentation failure: %s", prefix, tracy::Worker::GetFailureString( failure ) ); AnsiPrintf( ANSI_RED ANSI_BOLD, "\n%s Instrumentation failure: %s", prefix,
tracy::Worker::GetFailureString( failure ) );
auto& fd = worker.GetFailureData(); auto& fd = worker.GetFailureData();
if( !fd.message.empty() ) if( !fd.message.empty() )
{ {
@ -164,12 +169,12 @@ bool printWorkerFailure(tracy::Worker& worker, char const* prefix)
else else
{ {
const auto fsz = frameData->size; const auto fsz = frameData->size;
for( uint8_t f=0; f<fsz; f++ ) for( uint8_t f = 0; f < fsz; f++ )
{ {
const auto& frame = frameData->data[f]; const auto& frame = frameData->data[f];
auto txt = worker.GetString( frame.name ); auto txt = worker.GetString( frame.name );
if( fidx == 0 && f != fsz-1 ) if( fidx == 0 && f != fsz - 1 )
{ {
auto test = tracy::s_tracyStackFrames; auto test = tracy::s_tracyStackFrames;
bool match = false; bool match = false;
@ -180,12 +185,11 @@ bool printWorkerFailure(tracy::Worker& worker, char const* prefix)
match = true; match = true;
break; break;
} }
} } while( *++test );
while( *++test );
if( match ) continue; if( match ) continue;
} }
if( f == fsz-1 ) if( f == fsz - 1 )
{ {
printf( "%3i. ", fidx++ ); printf( "%3i. ", fidx++ );
} }

View File

@ -5,103 +5,103 @@
namespace tracy namespace tracy
{ {
std::optional<tracy::BroadcastMessage> ParseBroadcastMessage(const char* msg, size_t msgLen) std::optional<tracy::BroadcastMessage> ParseBroadcastMessage( const char* msg, size_t msgLen )
{ {
if (msgLen < sizeof(uint16_t)) if( msgLen < sizeof( uint16_t ) )
{ {
std::cout << "Received too short broadcast message" << std::endl; std::cout << "Received too short broadcast message" << std::endl;
return std::nullopt; return std::nullopt;
} }
uint16_t broadcastVersion; uint16_t broadcastVersion;
memcpy(&broadcastVersion, msg, sizeof(uint16_t)); memcpy( &broadcastVersion, msg, sizeof( uint16_t ) );
if (broadcastVersion > tracy::BroadcastVersion) if( broadcastVersion > tracy::BroadcastVersion )
{ {
std::cout << "Received broadcast message with unsupported version: " << broadcastVersion << std::endl; std::cout << "Received broadcast message with unsupported version: " << broadcastVersion << std::endl;
return std::nullopt; return std::nullopt;
} }
switch (broadcastVersion) switch( broadcastVersion )
{ {
case 3: case 3:
{
if( msgLen > sizeof( tracy::BroadcastMessage ) )
{ {
if (msgLen > sizeof(tracy::BroadcastMessage)) std::cout << "Received unexpected size broadcast v3 message" << std::endl;
{ return std::nullopt;
std::cout << "Received unexpected size broadcast v3 message" << std::endl;
return std::nullopt;
}
tracy::BroadcastMessage bm;
memcpy(&bm, msg, msgLen);
return bm;
break;
} }
case 2: tracy::BroadcastMessage bm;
memcpy( &bm, msg, msgLen );
return bm;
break;
}
case 2:
{
if( msgLen > sizeof( tracy::BroadcastMessage_v2 ) )
{ {
if (msgLen > sizeof(tracy::BroadcastMessage_v2)) std::cout << "Received unexpected size broadcast v2 message" << std::endl;
{ return std::nullopt;
std::cout << "Received unexpected size broadcast v2 message" << std::endl; }
return std::nullopt; tracy::BroadcastMessage_v2 bm;
} memcpy( &bm, msg, msgLen );
tracy::BroadcastMessage_v2 bm;
memcpy(&bm, msg, msgLen);
tracy::BroadcastMessage out; tracy::BroadcastMessage out;
out.broadcastVersion = broadcastVersion; out.broadcastVersion = broadcastVersion;
out.protocolVersion = bm.protocolVersion; out.protocolVersion = bm.protocolVersion;
out.activeTime = bm.activeTime; out.activeTime = bm.activeTime;
out.listenPort = bm.listenPort; out.listenPort = bm.listenPort;
strcpy(out.programName, bm.programName); strcpy( out.programName, bm.programName );
out.pid = 0; out.pid = 0;
return out; return out;
break; break;
} }
case 1: case 1:
{
if( msgLen > sizeof( tracy::BroadcastMessage_v1 ) )
{ {
if (msgLen > sizeof(tracy::BroadcastMessage_v1)) std::cout << "Received unexpected size broadcast v1 message" << std::endl;
{ return std::nullopt;
std::cout << "Received unexpected size broadcast v1 message" << std::endl;
return std::nullopt;
}
tracy::BroadcastMessage_v1 bm;
memcpy(&bm, msg, msgLen);
tracy::BroadcastMessage out;
out.broadcastVersion = broadcastVersion;
out.protocolVersion = bm.protocolVersion;
out.activeTime = bm.activeTime;
out.listenPort = bm.listenPort;
strcpy(out.programName, bm.programName);
out.pid = 0;
return out;
break;
} }
case 0: tracy::BroadcastMessage_v1 bm;
memcpy( &bm, msg, msgLen );
tracy::BroadcastMessage out;
out.broadcastVersion = broadcastVersion;
out.protocolVersion = bm.protocolVersion;
out.activeTime = bm.activeTime;
out.listenPort = bm.listenPort;
strcpy( out.programName, bm.programName );
out.pid = 0;
return out;
break;
}
case 0:
{
if( msgLen > sizeof( tracy::BroadcastMessage_v0 ) )
{ {
if (msgLen > sizeof(tracy::BroadcastMessage_v0)) std::cout << "Received unexpected size broadcast v0 message" << std::endl;
{ return std::nullopt;
std::cout << "Received unexpected size broadcast v0 message" << std::endl;
return std::nullopt;
}
tracy::BroadcastMessage_v0 bm;
memcpy(&bm, msg, msgLen);
tracy::BroadcastMessage out;
out.broadcastVersion = broadcastVersion;
out.protocolVersion = bm.protocolVersion;
out.activeTime = bm.activeTime;
out.listenPort = tracy::DEFAULT_CLIENT_DATA_TCP_PORT;
strcpy(out.programName, bm.programName);
out.pid = 0;
return out;
break;
} }
default: tracy::BroadcastMessage_v0 bm;
assert(false); memcpy( &bm, msg, msgLen );
break;
tracy::BroadcastMessage out;
out.broadcastVersion = broadcastVersion;
out.protocolVersion = bm.protocolVersion;
out.activeTime = bm.activeTime;
out.listenPort = tracy::DEFAULT_CLIENT_DATA_TCP_PORT;
strcpy( out.programName, bm.programName );
out.pid = 0;
return out;
break;
}
default:
assert( false );
break;
} }
return std::nullopt; return std::nullopt;
} }
uint64_t ClientUniqueID(tracy::IpAddress const& addr, uint16_t port) uint64_t ClientUniqueID( tracy::IpAddress const& addr, uint16_t port )
{ {
return uint64_t(addr.GetNumber()) | (uint64_t(port) << 32); return uint64_t( addr.GetNumber() ) | ( uint64_t( port ) << 32 );
} }
} }

View File

@ -2,16 +2,16 @@
#ifndef __TRACYPROTOCOLSERVER_HPP__ #ifndef __TRACYPROTOCOLSERVER_HPP__
#define __TRACYPROTOCOLSERVER_HPP__ #define __TRACYPROTOCOLSERVER_HPP__
#include <optional>
#include "TracyProtocol.hpp" #include "TracyProtocol.hpp"
#include "TracySocket.hpp" #include "TracySocket.hpp"
#include <optional>
namespace tracy namespace tracy
{ {
// create the latest version of broadcast message, migrating older versions if possible // create the latest version of broadcast message, migrating older versions if possible
std::optional<tracy::BroadcastMessage> ParseBroadcastMessage(const char* msg, size_t msgLen); std::optional<tracy::BroadcastMessage> ParseBroadcastMessage( const char* msg, size_t msgLen );
// internal unique ID for a client // internal unique ID for a client
uint64_t ClientUniqueID(tracy::IpAddress const& addr, uint16_t port); uint64_t ClientUniqueID( tracy::IpAddress const& addr, uint16_t port );
} }
#endif #endif