Skip to content

Commit

Permalink
[refactor] Extract useful functions/constants for capture
Browse files Browse the repository at this point in the history
  • Loading branch information
Grégoire Roussel committed Jul 19, 2024
1 parent f81e089 commit b960872
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 83 deletions.
2 changes: 1 addition & 1 deletion capture/src/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
1 change: 1 addition & 0 deletions cmake/server.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(TRACY_SERVER_SOURCES
TracyMemory.cpp
TracyMmap.cpp
TracyPrint.cpp
TracyProtocolServer.cpp
TracySysUtil.cpp
TracyTaskDispatch.cpp
TracyTextureCompression.cpp
Expand Down
86 changes: 13 additions & 73 deletions profiler/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -85,7 +86,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;
Expand Down Expand Up @@ -452,76 +453,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() )
{
Expand All @@ -538,16 +478,16 @@ 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() )
Expand Down
2 changes: 1 addition & 1 deletion profiler/src/profiler/TracyView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
4 changes: 2 additions & 2 deletions public/client/TracyProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1629,12 +1629,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 ) );
Expand Down
3 changes: 3 additions & 0 deletions public/common/TracyProtocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 66 };
Expand Down
2 changes: 1 addition & 1 deletion public/common/TracySocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion public/common/TracySocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class UdpBroadcast
class IpAddress
{
public:
static constexpr size_t TEXT_SIZE = 17;

IpAddress();
~IpAddress();

Expand All @@ -127,7 +129,7 @@ class IpAddress

private:
uint32_t m_number;
char m_text[17];
char m_text[TEXT_SIZE];
};

class UdpListen
Expand Down
107 changes: 107 additions & 0 deletions server/TracyProtocolServer.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 17 additions & 0 deletions server/TracyProtocolServer.hpp
Original file line number Diff line number Diff line change
@@ -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);
}

#endif
8 changes: 4 additions & 4 deletions server/TracyWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,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 );
Expand Down Expand Up @@ -561,7 +561,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();
Expand Down Expand Up @@ -717,7 +717,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;
Expand Down Expand Up @@ -5918,7 +5918,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];
Expand Down
2 changes: 2 additions & 0 deletions server/TracyWorker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {}
Expand Down

0 comments on commit b960872

Please sign in to comment.