1
0
mirror of https://github.com/wolfpld/tracy.git synced 2025-03-20 07:40:02 +08:00

Merge b9323c1029e7de9b9b328c526c6a17ef1e8fc82b into 9074461ffbb44ad77244a493c1090eeaa6f20322

This commit is contained in:
Grégoire Roussel 2025-03-14 14:19:04 +00:00 committed by GitHub
commit 1e2cae2f3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 163 additions and 83 deletions

View File

@ -113,7 +113,7 @@ int main( int argc, char** argv )
const char* output = nullptr;
int port = 8086;
int seconds = -1;
int64_t memoryLimit = -1;
int64_t memoryLimit = tracy::NO_WORKER_MEMORY_LIMIT;
int c;
while( ( c = getopt( argc, argv, "a:o:p:fs:m:" ) ) != -1 )

View File

@ -17,6 +17,7 @@ set(TRACY_SERVER_SOURCES
TracyMemory.cpp
TracyMmap.cpp
TracyPrint.cpp
TracyProtocolServer.cpp
TracySysUtil.cpp
TracyTaskDispatch.cpp
TracyTextureCompression.cpp

View File

@ -46,6 +46,7 @@
#include "../../server/tracy_robin_hood.h"
#include "../../server/TracyFileHeader.hpp"
#include "../../server/TracyFileRead.hpp"
#include "../../server/TracyProtocolServer.hpp"
#include "../../server/TracyPrint.hpp"
#include "../../server/TracySysUtil.hpp"
#include "../../server/TracyWorker.hpp"
@ -87,7 +88,7 @@ enum class ViewShutdown { False, True, Join };
static tracy::unordered_flat_map<uint64_t, ClientData> clients;
static std::unique_ptr<tracy::View> view;
static tracy::BadVersionState badVer;
static uint16_t port = 8086;
static uint16_t port = tracy::DEFAULT_BROADCAST_UDP_PORT;
static const char* connectTo = nullptr;
static char title[128];
static std::thread loadThread, updateThread, updateNotesThread;
@ -458,76 +459,15 @@ static void UpdateBroadcastClients()
{
auto msg = broadcastListen->Read( len, addr, 0 );
if( !msg ) break;
if( len > sizeof( tracy::BroadcastMessage ) ) continue;
uint16_t broadcastVersion;
memcpy( &broadcastVersion, msg, sizeof( uint16_t ) );
if( broadcastVersion <= tracy::BroadcastVersion )
auto parsedMessageOpt = tracy::ParseBroadcastMessage(msg, len);
if (parsedMessageOpt.has_value())
{
uint32_t protoVer;
char procname[tracy::WelcomeMessageProgramNameSize];
int32_t activeTime;
uint16_t listenPort;
uint64_t pid;
switch( broadcastVersion )
{
case 3:
{
tracy::BroadcastMessage bm;
memcpy( &bm, msg, len );
protoVer = bm.protocolVersion;
strcpy( procname, bm.programName );
activeTime = bm.activeTime;
listenPort = bm.listenPort;
pid = bm.pid;
break;
}
case 2:
{
if( len > sizeof( tracy::BroadcastMessage_v2 ) ) continue;
tracy::BroadcastMessage_v2 bm;
memcpy( &bm, msg, len );
protoVer = bm.protocolVersion;
strcpy( procname, bm.programName );
activeTime = bm.activeTime;
listenPort = bm.listenPort;
pid = 0;
break;
}
case 1:
{
if( len > sizeof( tracy::BroadcastMessage_v1 ) ) continue;
tracy::BroadcastMessage_v1 bm;
memcpy( &bm, msg, len );
protoVer = bm.protocolVersion;
strcpy( procname, bm.programName );
activeTime = bm.activeTime;
listenPort = bm.listenPort;
pid = 0;
break;
}
case 0:
{
if( len > sizeof( tracy::BroadcastMessage_v0 ) ) continue;
tracy::BroadcastMessage_v0 bm;
memcpy( &bm, msg, len );
protoVer = bm.protocolVersion;
strcpy( procname, bm.programName );
activeTime = bm.activeTime;
listenPort = 8086;
pid = 0;
break;
}
default:
assert( false );
break;
}
auto parsedMessage = parsedMessageOpt.value();
auto address = addr.GetText();
const auto clientId = tracy::ClientUniqueID(addr, parsedMessage.listenPort);
const auto ipNumerical = addr.GetNumber();
const auto clientId = uint64_t( ipNumerical ) | ( uint64_t( listenPort ) << 32 );
auto it = clients.find( clientId );
if( activeTime >= 0 )
if( parsedMessage.activeTime >= 0 )
{
if( it == clients.end() )
{
@ -544,16 +484,24 @@ static void UpdateBroadcastClients()
} );
}
resolvLock.unlock();
clients.emplace( clientId, ClientData { time, protoVer, activeTime, listenPort, pid, procname, std::move( ip ) } );
clients.emplace(clientId,
ClientData{time,
parsedMessage.protocolVersion,
parsedMessage.activeTime,
parsedMessage.listenPort,
parsedMessage.pid,
parsedMessage.programName,
std::move(ip)});
}
else
{
it->second.time = time;
it->second.activeTime = activeTime;
it->second.port = listenPort;
it->second.pid = pid;
it->second.protocolVersion = protoVer;
if( strcmp( it->second.procName.c_str(), procname ) != 0 ) it->second.procName = procname;
it->second.activeTime = parsedMessage.activeTime;
it->second.port = parsedMessage.listenPort;
it->second.pid = parsedMessage.pid;
it->second.protocolVersion = parsedMessage.protocolVersion;
if (strcmp(it->second.procName.c_str(), parsedMessage.programName) != 0)
it->second.procName = parsedMessage.programName;
}
}
else if( it != clients.end() )

View File

@ -36,7 +36,7 @@ namespace tracy
double s_time = 0;
View::View( void(*cbMainThread)(const std::function<void()>&, bool), const char* addr, uint16_t port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, SetScaleCallback sscb, AttentionCallback acb, const Config& config, AchievementsMgr* amgr )
: m_worker( addr, port, config.memoryLimit == 0 ? -1 : ( config.memoryLimitPercent * tracy::GetPhysicalMemorySize() / 100 ) )
: m_worker( addr, port, config.memoryLimit == 0 ? NO_WORKER_MEMORY_LIMIT : ( config.memoryLimitPercent * tracy::GetPhysicalMemorySize() / 100 ) )
, m_staticView( false )
, m_viewMode( ViewMode::LastFrames )
, m_viewModeHeuristicTry( true )

View File

@ -1679,12 +1679,12 @@ void Profiler::Worker()
auto dataPort = m_userPort != 0 ? m_userPort : TRACY_DATA_PORT;
#else
const bool dataPortSearch = m_userPort == 0;
auto dataPort = m_userPort != 0 ? m_userPort : 8086;
auto dataPort = m_userPort != 0 ? m_userPort : DEFAULT_CLIENT_DATA_TCP_PORT;
#endif
#ifdef TRACY_BROADCAST_PORT
const auto broadcastPort = TRACY_BROADCAST_PORT;
#else
const auto broadcastPort = 8086;
const auto broadcastPort = DEFAULT_BROADCAST_UDP_PORT;
#endif
while( m_timeBegin.load( std::memory_order_relaxed ) == 0 ) std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) );

View File

@ -7,6 +7,9 @@
namespace tracy
{
constexpr uint16_t DEFAULT_BROADCAST_UDP_PORT = 8086;
constexpr uint16_t DEFAULT_CLIENT_DATA_TCP_PORT = 8086;
constexpr unsigned Lz4CompressBound( unsigned isize ) { return isize + ( isize / 255 ) + 16; }
enum : uint32_t { ProtocolVersion = 72 };

View File

@ -651,7 +651,7 @@ void IpAddress::Set( const struct sockaddr& addr )
#else
auto ai = (const struct sockaddr_in*)&addr;
#endif
inet_ntop( AF_INET, &ai->sin_addr, m_text, 17 );
inet_ntop(AF_INET, &ai->sin_addr, m_text, TEXT_SIZE);
m_number = ai->sin_addr.s_addr;
}

View File

@ -112,6 +112,8 @@ private:
class IpAddress
{
public:
static constexpr size_t TEXT_SIZE = 17;
IpAddress();
~IpAddress();
@ -127,7 +129,7 @@ public:
private:
uint32_t m_number;
char m_text[17];
char m_text[TEXT_SIZE];
};
class UdpListen

View File

@ -0,0 +1,107 @@
#include "TracyProtocolServer.hpp"
#include <cassert>
#include <cstring>
#include <iostream>
namespace tracy
{
std::optional<tracy::BroadcastMessage> ParseBroadcastMessage(const char* msg, size_t msgLen)
{
if (msgLen < sizeof(uint16_t))
{
std::cout << "Received too short broadcast message" << std::endl;
return std::nullopt;
}
uint16_t broadcastVersion;
memcpy(&broadcastVersion, msg, sizeof(uint16_t));
if (broadcastVersion > tracy::BroadcastVersion)
{
std::cout << "Received broadcast message with unsupported version: " << broadcastVersion << std::endl;
return std::nullopt;
}
switch (broadcastVersion)
{
case 3:
{
if (msgLen > sizeof(tracy::BroadcastMessage))
{
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:
{
if (msgLen > sizeof(tracy::BroadcastMessage_v2))
{
std::cout << "Received unexpected size broadcast v2 message" << std::endl;
return std::nullopt;
}
tracy::BroadcastMessage_v2 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 1:
{
if (msgLen > sizeof(tracy::BroadcastMessage_v1))
{
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:
{
if (msgLen > sizeof(tracy::BroadcastMessage_v0))
{
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:
assert(false);
break;
}
return std::nullopt;
}
uint64_t ClientUniqueID(tracy::IpAddress const& addr, uint16_t port)
{
return uint64_t(addr.GetNumber()) | (uint64_t(port) << 32);
}
} // namespace tracy

View File

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

View File

@ -313,7 +313,7 @@ Worker::Worker( const char* name, const char* program, const std::vector<ImportE
, m_buffer( nullptr )
, m_onDemand( false )
, m_inconsistentSamples( false )
, m_memoryLimit( -1 )
, m_memoryLimit( NO_WORKER_MEMORY_LIMIT )
, m_traceVersion( CurrentVersion )
{
m_data.sourceLocationExpand.push_back( 0 );
@ -556,7 +556,7 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks, bool allow
, m_stream( nullptr )
, m_buffer( nullptr )
, m_inconsistentSamples( false )
, m_memoryLimit( -1 )
, m_memoryLimit( NO_WORKER_MEMORY_LIMIT )
, m_allowStringModification( allowStringModification )
{
auto loadStart = std::chrono::high_resolution_clock::now();
@ -740,7 +740,7 @@ Worker::Worker( FileRead& f, EventType::Type eventMask, bool bgTasks, bool allow
{
m_data.stringData.reserve_exact( sz, m_slab );
}
for( uint64_t i=0; i<sz; i++ )
{
uint64_t ptr, ssz;
@ -5960,7 +5960,7 @@ void Worker::ProcessGpuCalibration( const QueueGpuCalibration& ev )
ctx->calibratedGpuTime = gpuTime;
ctx->calibratedCpuTime = TscTime( ev.cpuTime );
}
void Worker::ProcessGpuTimeSync( const QueueGpuTimeSync& ev )
{
auto ctx = m_gpuCtxMap[ev.context];

View File

@ -52,6 +52,8 @@ namespace EventType
};
}
constexpr int64_t NO_WORKER_MEMORY_LIMIT = -1; // anything <0 would do
struct UnsupportedVersion : public std::exception
{
UnsupportedVersion( int version ) : version( version ) {}