From 2971db21e33e168b9ed2665564db93957035c4ca Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Fri, 10 Mar 2023 00:23:09 +0100 Subject: [PATCH] Read and report power usage. --- public/client/TracySysPower.cpp | 33 +++++++++++++++++++++++++++++++++ public/client/TracySysPower.hpp | 1 + public/common/TracyProtocol.hpp | 2 +- public/common/TracyQueue.hpp | 10 ++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/public/client/TracySysPower.cpp b/public/client/TracySysPower.cpp index d26eab19..bd5939da 100644 --- a/public/client/TracySysPower.cpp +++ b/public/client/TracySysPower.cpp @@ -4,11 +4,13 @@ #include #include +#include #include #include #include #include "TracyDebug.hpp" +#include "TracyProfiler.hpp" #include "../common/TracyAlloc.hpp" namespace tracy @@ -16,6 +18,7 @@ namespace tracy SysPower::SysPower() : m_domains( 4 ) + , m_lastTime( 0 ) { ScanDirectory( "/sys/devices/virtual/powercap/intel-rapl", -1 ); } @@ -31,6 +34,36 @@ SysPower::~SysPower() void SysPower::Tick() { + auto t = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + if( t - m_lastTime > 10000000 ) // 10 ms + { + m_lastTime = t; + for( auto& v : m_domains ) + { + char tmp[32]; + if( fread( tmp, 1, 32, v.handle ) > 0 ) + { + rewind( v.handle ); + auto p = (uint64_t)atoll( tmp ); + uint64_t delta; + if( p >= v.value ) + { + delta = p - v.value; + } + else + { + delta = v.overflow - v.value + p; + } + v.value = p; + + TracyLfqPrepare( QueueType::SysPowerReport ); + MemWrite( &item->sysPower.time, Profiler::GetTime() ); + MemWrite( &item->sysPower.delta, delta ); + MemWrite( &item->sysPower.name, (uint64_t)v.name ); + TracyLfqCommit; + } + } + } } void SysPower::ScanDirectory( const char* path, int parent ) diff --git a/public/client/TracySysPower.hpp b/public/client/TracySysPower.hpp index 01fa75c9..210123bc 100644 --- a/public/client/TracySysPower.hpp +++ b/public/client/TracySysPower.hpp @@ -35,6 +35,7 @@ private: void ScanDirectory( const char* path, int parent ); FastVector m_domains; + uint64_t m_lastTime; }; } diff --git a/public/common/TracyProtocol.hpp b/public/common/TracyProtocol.hpp index dd30e539..04c98690 100644 --- a/public/common/TracyProtocol.hpp +++ b/public/common/TracyProtocol.hpp @@ -9,7 +9,7 @@ namespace tracy constexpr unsigned Lz4CompressBound( unsigned isize ) { return isize + ( isize / 255 ) + 16; } -enum : uint32_t { ProtocolVersion = 63 }; +enum : uint32_t { ProtocolVersion = 64 }; enum : uint16_t { BroadcastVersion = 3 }; using lz4sz_t = uint32_t; diff --git a/public/common/TracyQueue.hpp b/public/common/TracyQueue.hpp index 8443193a..051d412a 100644 --- a/public/common/TracyQueue.hpp +++ b/public/common/TracyQueue.hpp @@ -90,6 +90,7 @@ enum class QueueType : uint8_t GpuNewContext, CallstackFrame, SysTimeReport, + SysPowerReport, TidToPid, HwSampleCpuCycle, HwSampleInstructionRetired, @@ -563,6 +564,13 @@ struct QueueSysTime float sysTime; }; +struct QueueSysPower +{ + int64_t time; + uint64_t delta; + uint64_t name; // ptr +}; + struct QueueContextSwitch { int64_t time; @@ -729,6 +737,7 @@ struct QueueItem QueueCrashReport crashReport; QueueCrashReportThread crashReportThread; QueueSysTime sysTime; + QueueSysPower sysPower; QueueContextSwitch contextSwitch; QueueThreadWakeup threadWakeup; QueueTidToPid tidToPid; @@ -832,6 +841,7 @@ static constexpr size_t QueueDataSize[] = { sizeof( QueueHeader ) + sizeof( QueueGpuNewContext ), sizeof( QueueHeader ) + sizeof( QueueCallstackFrame ), sizeof( QueueHeader ) + sizeof( QueueSysTime ), + sizeof( QueueHeader ) + sizeof( QueueSysPower ), sizeof( QueueHeader ) + sizeof( QueueTidToPid ), sizeof( QueueHeader ) + sizeof( QueueHwSample ), // cpu cycle sizeof( QueueHeader ) + sizeof( QueueHwSample ), // instruction retired