Extract shared capture-side tool code into TracyUtil.

This commit is contained in:
Bartosz Taudul
2026-09-02 01:21:59 +02:00
parent ebf5ebd9bd
commit db6a46cf3e
8 changed files with 124 additions and 106 deletions

View File

@@ -20,7 +20,6 @@ include(${CMAKE_CURRENT_LIST_DIR}/../cmake/util.cmake)
set(PROGRAM_FILES
src/capture.cpp
src/CaptureOutput.cpp
)
add_executable(${PROJECT_NAME} ${PROGRAM_FILES} ${COMMON_FILES} ${SERVER_FILES})
@@ -30,8 +29,8 @@ set_property(DIRECTORY ${CMAKE_CURRENT_LIST_DIR} PROPERTY VS_STARTUP_PROJECT ${P
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
add_executable(tracy-capture-daemon src/capturedaemon.cpp src/CaptureOutput.cpp ${COMMON_FILES} ${SERVER_FILES})
add_executable(tracy-capture-daemon src/capturedaemon.cpp ${COMMON_FILES} ${SERVER_FILES})
add_git_ref(tracy-capture-daemon)
target_link_libraries(tracy-capture-daemon PRIVATE TracyServer TracyGetOpt)
target_link_libraries(tracy-capture-daemon PRIVATE TracyServer TracyUtil TracyGetOpt)
install(TARGETS tracy-capture-daemon DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@@ -19,6 +19,7 @@
#include "../../public/common/TracyVersion.hpp"
#include "GitRef.hpp"
#include "CaptureFileBackup.hpp"
#include "CaptureConsole.hpp"
#include "CaptureOutput.hpp"

View File

@@ -28,6 +28,7 @@
#include "../../server/TracyWorker.hpp"
#include "GitRef.hpp"
#include "CaptureConsole.hpp"
#include "CaptureOutput.hpp"
static std::atomic<bool> g_shutdown{false};

View File

@@ -1,10 +1,13 @@
set(TRACY_UTIL_DIR ${CMAKE_CURRENT_LIST_DIR}/../util)
set(TRACY_UTIL_SOURCES
CaptureConsole.cpp
CaptureFileBackup.cpp
CaptureOutput.cpp
)
list(TRANSFORM TRACY_UTIL_SOURCES PREPEND "${TRACY_UTIL_DIR}/")
add_library(TracyUtil STATIC EXCLUDE_FROM_ALL ${TRACY_UTIL_SOURCES})
target_link_libraries(TracyUtil PUBLIC TracyServer)
target_include_directories(TracyUtil PUBLIC ${TRACY_UTIL_DIR})

97
util/CaptureConsole.cpp Normal file
View File

@@ -0,0 +1,97 @@
#ifdef _WIN32
# include <io.h>
# include <windows.h>
#else
# include <unistd.h>
#endif
#include <cstdarg>
#include <cstdio>
#include "CaptureConsole.hpp"
#include "../server/TracyMemory.hpp"
#include "../server/TracyPrint.hpp"
#include "../server/TracyWorker.hpp"
static bool s_isTerminal = false;
void InitTerminalDetection()
{
#ifdef _WIN32
s_isTerminal = _isatty( fileno( stdout ) );
#else
s_isTerminal = isatty( fileno( stdout ) );
#endif
}
bool IsTerminal()
{
return s_isTerminal;
}
void AnsiPrintf( const char* ansiEscape, const char* format, ... )
{
if( IsTerminal() )
{
char buf[256];
va_list args;
va_start( args, format );
vsnprintf( buf, sizeof buf, format, args );
va_end( args );
printf( "%s%s" ANSI_RESET, ansiEscape, buf );
}
else
{
va_list args;
va_start( args, format );
vfprintf( stdout, format, args );
va_end( args );
}
}
void PrintCaptureProgress( tracy::Worker& worker, int64_t firstTime, int64_t memoryLimit )
{
if( !IsTerminal() ) return;
auto& lock = worker.GetMbpsDataLock();
lock.lock();
const auto& mbpsData = worker.GetMbpsData();
if( mbpsData.empty() )
{
lock.unlock();
return;
}
const auto mbps = mbpsData.back();
const auto compRatio = worker.GetCompRatio();
const auto netTotal = worker.GetDataTransferred();
const auto queueSize = worker.GetSendQueueSize();
lock.unlock();
const char* unit = "Mbps";
float unitsPerMbps = 1.f;
if( mbps < 0.1f )
{
unit = "Kbps";
unitsPerMbps = 1000.f;
}
AnsiPrintf( ANSI_ERASE_LINE ANSI_CYAN ANSI_BOLD, "\r%7.2f %s", mbps * unitsPerMbps, unit );
printf( " /" );
AnsiPrintf( ANSI_CYAN ANSI_BOLD, "%5.1f%%", compRatio * 100.f );
printf( " =" );
AnsiPrintf( ANSI_YELLOW ANSI_BOLD, "%7.2f Mbps", mbps / compRatio );
printf( " | " );
AnsiPrintf( ANSI_YELLOW, "Tx: " );
AnsiPrintf( ANSI_GREEN, "%s", tracy::MemSizeToString( netTotal ) );
printf( " | " );
AnsiPrintf( ANSI_RED ANSI_BOLD, "%s", tracy::MemSizeToString( tracy::memUsage.load( std::memory_order_relaxed ) ) );
if( memoryLimit > 0 )
{
printf( " / " );
AnsiPrintf( ANSI_BLUE ANSI_BOLD, "%s", tracy::MemSizeToString( memoryLimit ) );
}
printf( " | " );
AnsiPrintf( ANSI_RED, "%s", tracy::TimeToString( worker.GetLastTime() - firstTime ) );
printf( " | " );
AnsiPrintf( ANSI_RED ANSI_BOLD, "%s query backlog", tracy::RealToString( queueSize ) );
fflush( stdout );
}

View File

@@ -1,5 +1,5 @@
#ifndef __CAPTUREOUTPUT_HPP__
#define __CAPTUREOUTPUT_HPP__
#ifndef __CAPTURECONSOLE_HPP__
#define __CAPTURECONSOLE_HPP__
#include <stdint.h>
@@ -24,10 +24,9 @@ bool IsTerminal();
#endif
void AnsiPrintf( const char* ansiEscape, const char* format, ... );
int WaitForConnection( tracy::Worker& worker );
void PrintWorkerFailure( tracy::Worker& worker );
// Renders the live capture status line (rate / compression / transferred /
// memory / time / query backlog) on the current line. Does nothing when
// stdout is not a terminal or until the worker reports its first rate sample.
void PrintCaptureProgress( tracy::Worker& worker, int64_t firstTime, int64_t memoryLimit );
#endif

View File

@@ -1,60 +1,13 @@
#ifdef _WIN32
# include <io.h>
# include <windows.h>
#else
# include <unistd.h>
#endif
#include <atomic>
#include <chrono>
#include <cstdarg>
#include <cstdio>
#include <cstring>
#include <inttypes.h>
#include <thread>
#include "CaptureOutput.hpp"
#include "../../public/common/TracyProtocol.hpp"
#include "../../public/common/TracyStackFrames.hpp"
#include "../../server/TracyMemory.hpp"
#include "../../server/TracyPrint.hpp"
#include "../../server/TracyWorker.hpp"
static bool s_isTerminal = false;
void InitTerminalDetection()
{
#ifdef _WIN32
s_isTerminal = _isatty( fileno( stdout ) );
#else
s_isTerminal = isatty( fileno( stdout ) );
#endif
}
bool IsTerminal()
{
return s_isTerminal;
}
void AnsiPrintf( const char* ansiEscape, const char* format, ... )
{
if( IsTerminal() )
{
char buf[256];
va_list args;
va_start( args, format );
vsnprintf( buf, sizeof buf, format, args );
va_end( args );
printf( "%s%s" ANSI_RESET, ansiEscape, buf );
}
else
{
va_list args;
va_start( args, format );
vfprintf( stdout, format, args );
va_end( args );
}
}
#include "../public/common/TracyProtocol.hpp"
#include "../public/common/TracyStackFrames.hpp"
#include "../server/TracyWorker.hpp"
int WaitForConnection( tracy::Worker& worker )
{
@@ -159,50 +112,3 @@ void PrintWorkerFailure( tracy::Worker& worker )
}
}
}
void PrintCaptureProgress( tracy::Worker& worker, int64_t firstTime, int64_t memoryLimit )
{
if( !IsTerminal() ) return;
auto& lock = worker.GetMbpsDataLock();
lock.lock();
const auto& mbpsData = worker.GetMbpsData();
if( mbpsData.empty() )
{
lock.unlock();
return;
}
const auto mbps = mbpsData.back();
const auto compRatio = worker.GetCompRatio();
const auto netTotal = worker.GetDataTransferred();
const auto queueSize = worker.GetSendQueueSize();
lock.unlock();
const char* unit = "Mbps";
float unitsPerMbps = 1.f;
if( mbps < 0.1f )
{
unit = "Kbps";
unitsPerMbps = 1000.f;
}
AnsiPrintf( ANSI_ERASE_LINE ANSI_CYAN ANSI_BOLD, "\r%7.2f %s", mbps * unitsPerMbps, unit );
printf( " /" );
AnsiPrintf( ANSI_CYAN ANSI_BOLD, "%5.1f%%", compRatio * 100.f );
printf( " =" );
AnsiPrintf( ANSI_YELLOW ANSI_BOLD, "%7.2f Mbps", mbps / compRatio );
printf( " | " );
AnsiPrintf( ANSI_YELLOW, "Tx: " );
AnsiPrintf( ANSI_GREEN, "%s", tracy::MemSizeToString( netTotal ) );
printf( " | " );
AnsiPrintf( ANSI_RED ANSI_BOLD, "%s", tracy::MemSizeToString( tracy::memUsage.load( std::memory_order_relaxed ) ) );
if( memoryLimit > 0 )
{
printf( " / " );
AnsiPrintf( ANSI_BLUE ANSI_BOLD, "%s", tracy::MemSizeToString( memoryLimit ) );
}
printf( " | " );
AnsiPrintf( ANSI_RED, "%s", tracy::TimeToString( worker.GetLastTime() - firstTime ) );
printf( " | " );
AnsiPrintf( ANSI_RED ANSI_BOLD, "%s query backlog", tracy::RealToString( queueSize ) );
fflush( stdout );
}

12
util/CaptureOutput.hpp Normal file
View File

@@ -0,0 +1,12 @@
#ifndef __CAPTUREOUTPUT_HPP__
#define __CAPTUREOUTPUT_HPP__
#include "CaptureConsole.hpp"
namespace tracy { class Worker; }
int WaitForConnection( tracy::Worker& worker );
void PrintWorkerFailure( tracy::Worker& worker );
#endif