Extract text printing functions.

This commit is contained in:
Bartosz Taudul 2019-06-18 20:43:28 +02:00
parent aa5259b20a
commit 1a32edebf2
8 changed files with 361 additions and 397 deletions

View File

@ -134,6 +134,7 @@
<ClCompile Include="..\..\..\common\tracy_lz4.cpp" /> <ClCompile Include="..\..\..\common\tracy_lz4.cpp" />
<ClCompile Include="..\..\..\common\tracy_lz4hc.cpp" /> <ClCompile Include="..\..\..\common\tracy_lz4hc.cpp" />
<ClCompile Include="..\..\..\server\TracyMemory.cpp" /> <ClCompile Include="..\..\..\server\TracyMemory.cpp" />
<ClCompile Include="..\..\..\server\TracyPrint.cpp" />
<ClCompile Include="..\..\..\server\TracyWorker.cpp" /> <ClCompile Include="..\..\..\server\TracyWorker.cpp" />
<ClCompile Include="..\..\src\capture.cpp" /> <ClCompile Include="..\..\src\capture.cpp" />
<ClCompile Include="..\..\src\getopt.c" /> <ClCompile Include="..\..\src\getopt.c" />
@ -156,6 +157,7 @@
<ClInclude Include="..\..\..\server\TracyFileWrite.hpp" /> <ClInclude Include="..\..\..\server\TracyFileWrite.hpp" />
<ClInclude Include="..\..\..\server\TracyMemory.hpp" /> <ClInclude Include="..\..\..\server\TracyMemory.hpp" />
<ClInclude Include="..\..\..\server\TracyPopcnt.hpp" /> <ClInclude Include="..\..\..\server\TracyPopcnt.hpp" />
<ClInclude Include="..\..\..\server\TracyPrint.hpp" />
<ClInclude Include="..\..\..\server\TracySlab.hpp" /> <ClInclude Include="..\..\..\server\TracySlab.hpp" />
<ClInclude Include="..\..\..\server\TracyVector.hpp" /> <ClInclude Include="..\..\..\server\TracyVector.hpp" />
<ClInclude Include="..\..\..\server\TracyWorker.hpp" /> <ClInclude Include="..\..\..\server\TracyWorker.hpp" />

View File

@ -36,6 +36,9 @@
<ClCompile Include="..\..\..\common\tracy_lz4hc.cpp"> <ClCompile Include="..\..\..\common\tracy_lz4hc.cpp">
<Filter>common</Filter> <Filter>common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\server\TracyPrint.cpp">
<Filter>server</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\common\tracy_lz4.hpp"> <ClInclude Include="..\..\..\common\tracy_lz4.hpp">
@ -104,5 +107,8 @@
<ClInclude Include="..\..\..\common\tracy_lz4hc.hpp"> <ClInclude Include="..\..\..\common\tracy_lz4hc.hpp">
<Filter>common</Filter> <Filter>common</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\server\TracyPrint.hpp">
<Filter>server</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -13,6 +13,7 @@
#include "../../common/TracyProtocol.hpp" #include "../../common/TracyProtocol.hpp"
#include "../../server/TracyFileWrite.hpp" #include "../../server/TracyFileWrite.hpp"
#include "../../server/TracyMemory.hpp" #include "../../server/TracyMemory.hpp"
#include "../../server/TracyPrint.hpp"
#include "../../server/TracyWorker.hpp" #include "../../server/TracyWorker.hpp"
#include "getopt.h" #include "getopt.h"
@ -26,87 +27,6 @@ void SigInt( int )
} }
#endif #endif
static const char* TimeToString( int64_t ns )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
const char* sign = "";
if( ns < 0 )
{
sign = "-";
ns = -ns;
}
if( ns < 1000 )
{
sprintf( buf, "%s%" PRIi64 " ns", sign, ns );
}
else if( ns < 1000ll * 1000 )
{
sprintf( buf, "%s%.2f us", sign, ns / 1000. );
}
else if( ns < 1000ll * 1000 * 1000 )
{
sprintf( buf, "%s%.2f ms", sign, ns / ( 1000. * 1000. ) );
}
else if( ns < 1000ll * 1000 * 1000 * 60 )
{
sprintf( buf, "%s%.2f s", sign, ns / ( 1000. * 1000. * 1000. ) );
}
else
{
const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) );
const auto s = int64_t( ns - m * ( 1000ll * 1000 * 1000 * 60 ) );
sprintf( buf, "%s%" PRIi64 ":%04.1f", sign, m, s / ( 1000. * 1000. * 1000. ) );
}
return buf;
}
static const char* RealToString( double val, bool separator )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
sprintf( buf, "%f", val );
auto ptr = buf;
if( *ptr == '-' ) ptr++;
const auto vbegin = ptr;
if( separator )
{
while( *ptr != '\0' && *ptr != ',' && *ptr != '.' ) ptr++;
auto end = ptr;
while( *end != '\0' ) end++;
auto sz = end - ptr;
while( ptr - vbegin > 3 )
{
ptr -= 3;
memmove( ptr+1, ptr, sz );
*ptr = ',';
sz += 4;
}
}
while( *ptr != '\0' && *ptr != ',' && *ptr != '.' ) ptr++;
if( *ptr == '\0' ) return buf;
while( *ptr != '\0' ) ptr++;
ptr--;
while( *ptr == '0' && *ptr != ',' && *ptr != '.' ) ptr--;
if( *ptr != '.' && *ptr != ',' ) ptr++;
*ptr = '\0';
return buf;
}
void Usage() void Usage()
{ {
@ -169,7 +89,7 @@ int main( int argc, char** argv )
} }
} }
while( !worker.HasData() ) std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) ); while( !worker.HasData() ) std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
printf( "\nQueue delay: %s\nTimer resolution: %s\n", TimeToString( worker.GetDelay() ), TimeToString( worker.GetResolution() ) ); printf( "\nQueue delay: %s\nTimer resolution: %s\n", tracy::TimeToString( worker.GetDelay() ), tracy::TimeToString( worker.GetResolution() ) );
#ifndef _MSC_VER #ifndef _MSC_VER
struct sigaction sigint; struct sigaction sigint;
@ -203,7 +123,7 @@ int main( int argc, char** argv )
{ {
printf( "\33[2K\r\033[36;1m%7.2f Mbps", mbps ); printf( "\33[2K\r\033[36;1m%7.2f Mbps", mbps );
} }
printf( " \033[0m /\033[36;1m%5.1f%% \033[0m=\033[33;1m%7.2f Mbps \033[0m| Mem: \033[31;1m%.2f MB\033[0m | \033[33mTime: %s\033[0m", compRatio * 100.f, mbps / compRatio, tracy::memUsage.load( std::memory_order_relaxed ) / ( 1024.f * 1024.f ), TimeToString( worker.GetLastTime() - worker.GetTimeBegin() ) ); printf( " \033[0m /\033[36;1m%5.1f%% \033[0m=\033[33;1m%7.2f Mbps \033[0m| Mem: \033[31;1m%.2f MB\033[0m | \033[33mTime: %s\033[0m", compRatio * 100.f, mbps / compRatio, tracy::memUsage.load( std::memory_order_relaxed ) / ( 1024.f * 1024.f ), tracy::TimeToString( worker.GetLastTime() - worker.GetTimeBegin() ) );
fflush( stdout ); fflush( stdout );
std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) ); std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
@ -215,7 +135,7 @@ int main( int argc, char** argv )
printf( "\n\033[31;1mInstrumentation failure: %s\033[0m", tracy::Worker::GetFailureString( failure ) ); printf( "\n\033[31;1mInstrumentation failure: %s\033[0m", tracy::Worker::GetFailureString( failure ) );
} }
printf( "\nFrames: %" PRIu64 "\nTime span: %s\nZones: %s\nSaving trace...", worker.GetFrameCount( *worker.GetFramesBase() ), TimeToString( worker.GetLastTime() - worker.GetTimeBegin() ), RealToString( worker.GetZoneCount(), true ) ); printf( "\nFrames: %" PRIu64 "\nTime span: %s\nZones: %s\nSaving trace...", worker.GetFrameCount( *worker.GetFramesBase() ), tracy::TimeToString( worker.GetLastTime() - worker.GetTimeBegin() ), tracy::RealToString( worker.GetZoneCount(), true ) );
fflush( stdout ); fflush( stdout );
auto f = std::unique_ptr<tracy::FileWrite>( tracy::FileWrite::Open( output ) ); auto f = std::unique_ptr<tracy::FileWrite>( tracy::FileWrite::Open( output ) );
if( f ) if( f )

View File

@ -113,6 +113,7 @@
<ClCompile Include="..\..\..\nfd\nfd_win.cpp" /> <ClCompile Include="..\..\..\nfd\nfd_win.cpp" />
<ClCompile Include="..\..\..\server\TracyBadVersion.cpp" /> <ClCompile Include="..\..\..\server\TracyBadVersion.cpp" />
<ClCompile Include="..\..\..\server\TracyMemory.cpp" /> <ClCompile Include="..\..\..\server\TracyMemory.cpp" />
<ClCompile Include="..\..\..\server\TracyPrint.cpp" />
<ClCompile Include="..\..\..\server\TracyStorage.cpp" /> <ClCompile Include="..\..\..\server\TracyStorage.cpp" />
<ClCompile Include="..\..\..\server\TracyTexture.cpp" /> <ClCompile Include="..\..\..\server\TracyTexture.cpp" />
<ClCompile Include="..\..\..\server\TracyView.cpp" /> <ClCompile Include="..\..\..\server\TracyView.cpp" />
@ -163,6 +164,7 @@
<ClInclude Include="..\..\..\server\TracyImGui.hpp" /> <ClInclude Include="..\..\..\server\TracyImGui.hpp" />
<ClInclude Include="..\..\..\server\TracyMemory.hpp" /> <ClInclude Include="..\..\..\server\TracyMemory.hpp" />
<ClInclude Include="..\..\..\server\TracyPopcnt.hpp" /> <ClInclude Include="..\..\..\server\TracyPopcnt.hpp" />
<ClInclude Include="..\..\..\server\TracyPrint.hpp" />
<ClInclude Include="..\..\..\server\TracySlab.hpp" /> <ClInclude Include="..\..\..\server\TracySlab.hpp" />
<ClInclude Include="..\..\..\server\TracyStorage.hpp" /> <ClInclude Include="..\..\..\server\TracyStorage.hpp" />
<ClInclude Include="..\..\..\server\TracyStringDiscovery.hpp" /> <ClInclude Include="..\..\..\server\TracyStringDiscovery.hpp" />

View File

@ -96,6 +96,9 @@
<ClCompile Include="..\..\..\server\TracyTexture.cpp"> <ClCompile Include="..\..\..\server\TracyTexture.cpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\..\server\TracyPrint.cpp">
<Filter>server</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\..\common\tracy_lz4.hpp"> <ClInclude Include="..\..\..\common\tracy_lz4.hpp">
@ -263,6 +266,9 @@
<ClInclude Include="..\..\..\server\TracyTexture.hpp"> <ClInclude Include="..\..\..\server\TracyTexture.hpp">
<Filter>server</Filter> <Filter>server</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\..\server\TracyPrint.hpp">
<Filter>server</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="DebugVis.natvis" /> <Natvis Include="DebugVis.natvis" />

327
server/TracyPrint.cpp Normal file
View File

@ -0,0 +1,327 @@
#include <assert.h>
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "TracyPrint.hpp"
namespace tracy
{
static const char* IntTable100 =
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899";
static inline void PrintTinyInt( char*& buf, uint64_t v )
{
if( v >= 10 )
{
*buf++ = '0' + v/10;
}
*buf++ = '0' + v%10;
}
static inline void PrintTinyInt0( char*& buf, uint64_t v )
{
if( v >= 10 )
{
*buf++ = '0' + v/10;
}
else
{
*buf++ = '0';
}
*buf++ = '0' + v%10;
}
static inline void PrintSmallInt( char*& buf, uint64_t v )
{
if( v >= 100 )
{
memcpy( buf, IntTable100 + v/10*2, 2 );
buf += 2;
}
else if( v >= 10 )
{
*buf++ = '0' + v/10;
}
*buf++ = '0' + v%10;
}
static inline void PrintFrac00( char*& buf, uint64_t v )
{
*buf++ = '.';
v += 5;
if( v/10%10 == 0 )
{
*buf++ = '0' + v/100;
}
else
{
memcpy( buf, IntTable100 + v/10*2, 2 );
buf += 2;
}
}
static inline void PrintFrac0( char*& buf, uint64_t v )
{
*buf++ = '.';
*buf++ = '0' + (v+50)/100;
}
static inline void PrintSmallIntFrac( char*& buf, uint64_t v )
{
uint64_t in = v / 1000;
uint64_t fr = v % 1000;
if( fr >= 995 )
{
PrintSmallInt( buf, in+1 );
}
else
{
PrintSmallInt( buf, in );
if( fr > 5 )
{
PrintFrac00( buf, fr );
}
}
}
static inline void PrintSecondsFrac( char*& buf, uint64_t v )
{
uint64_t in = v / 1000;
uint64_t fr = v % 1000;
if( fr >= 950 )
{
PrintTinyInt0( buf, in+1 );
}
else
{
PrintTinyInt0( buf, in );
if( fr > 50 )
{
PrintFrac0( buf, fr );
}
}
}
const char* TimeToString( int64_t _ns )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
char* bufstart = buf;
bufsel = ( bufsel + 1 ) % Pool;
uint64_t ns;
if( _ns < 0 )
{
*buf = '-';
buf++;
ns = -_ns;
}
else
{
ns = _ns;
}
if( ns < 1000 )
{
PrintSmallInt( buf, ns );
memcpy( buf, " ns", 4 );
}
else if( ns < 1000ll * 1000 )
{
PrintSmallIntFrac( buf, ns );
#ifdef TRACY_EXTENDED_FONT
memcpy( buf, " \xce\xbcs", 5 );
#else
memcpy( buf, " us", 4 );
#endif
}
else if( ns < 1000ll * 1000 * 1000 )
{
PrintSmallIntFrac( buf, ns / 1000 );
memcpy( buf, " ms", 4 );
}
else if( ns < 1000ll * 1000 * 1000 * 60 )
{
PrintSmallIntFrac( buf, ns / ( 1000ll * 1000 ) );
memcpy( buf, " s", 3 );
}
else if( ns < 1000ll * 1000 * 1000 * 60 * 60 )
{
const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) );
const auto s = int64_t( ns - m * ( 1000ll * 1000 * 1000 * 60 ) ) / ( 1000ll * 1000 );
PrintTinyInt( buf, m );
*buf++ = ':';
PrintSecondsFrac( buf, s );
*buf++ = '\0';
}
else if( ns < 1000ll * 1000 * 1000 * 60 * 60 * 24 )
{
const auto h = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 * 60 ) );
const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) - h * 60 );
const auto s = int64_t( ns / ( 1000ll * 1000 * 1000 ) - h * ( 60 * 60 ) - m * 60 );
PrintTinyInt( buf, h );
*buf++ = ':';
PrintTinyInt0( buf, m );
*buf++ = ':';
PrintTinyInt0( buf, s );
*buf++ = '\0';
}
else
{
const auto d = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 * 60 * 24 ) );
const auto h = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 * 60 ) - d * 24 );
const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) - d * ( 60 * 24 ) - h * 60 );
const auto s = int64_t( ns / ( 1000ll * 1000 * 1000 ) - d * ( 60 * 60 * 24 ) - h * ( 60 * 60 ) - m * 60 );
if( d < 1000 )
{
PrintSmallInt( buf, d );
*buf++ = 'd';
}
else
{
buf += sprintf( buf, "%" PRIi64 "d", d );
}
PrintTinyInt0( buf, h );
*buf++ = ':';
PrintTinyInt0( buf, m );
*buf++ = ':';
PrintTinyInt0( buf, s );
*buf++ = '\0';
}
return bufstart;
}
const char* RealToString( double val, bool separator )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
sprintf( buf, "%f", val );
auto ptr = buf;
if( *ptr == '-' ) ptr++;
const auto vbegin = ptr;
if( separator )
{
while( *ptr != '\0' && *ptr != ',' && *ptr != '.' ) ptr++;
auto end = ptr;
while( *end != '\0' ) end++;
auto sz = end - ptr;
while( ptr - vbegin > 3 )
{
ptr -= 3;
memmove( ptr+1, ptr, sz );
*ptr = ',';
sz += 4;
}
}
while( *ptr != '\0' && *ptr != ',' && *ptr != '.' ) ptr++;
if( *ptr == '\0' ) return buf;
while( *ptr != '\0' ) ptr++;
ptr--;
while( *ptr == '0' ) ptr--;
if( *ptr != '.' && *ptr != ',' ) ptr++;
*ptr = '\0';
return buf;
}
const char* MemSizeToString( int64_t val )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
const auto aval = abs( val );
if( aval < 10000ll )
{
sprintf( buf, "%" PRIi64 " bytes", val );
return buf;
}
enum class Unit
{
Kilobyte,
Megabyte,
Gigabyte,
Terabyte
};
Unit unit;
if( aval < 10000ll * 1024 )
{
sprintf( buf, "%.2f", val / 1024. );
unit = Unit::Kilobyte;
}
else if( aval < 10000ll * 1024 * 1024 )
{
sprintf( buf, "%.2f", val / ( 1024. * 1024 ) );
unit = Unit::Megabyte;
}
else if( aval < 10000ll * 1024 * 1024 * 1024 )
{
sprintf( buf, "%.2f", val / ( 1024. * 1024 * 1024 ) );
unit = Unit::Gigabyte;
}
else
{
sprintf( buf, "%.2f", val / ( 1024. * 1024 * 1024 * 1024 ) );
unit = Unit::Terabyte;
}
auto ptr = buf;
while( *ptr ) ptr++;
ptr--;
while( ptr >= buf && *ptr == '0' ) ptr--;
if( *ptr != '.' ) ptr++;
*ptr++ = ' ';
switch( unit )
{
case Unit::Kilobyte:
*ptr++ = 'K';
break;
case Unit::Megabyte:
*ptr++ = 'M';
break;
case Unit::Gigabyte:
*ptr++ = 'G';
break;
case Unit::Terabyte:
*ptr++ = 'T';
break;
default:
assert( false );
break;
}
*ptr++ = 'B';
*ptr++ = '\0';
return buf;
}
}

13
server/TracyPrint.hpp Normal file
View File

@ -0,0 +1,13 @@
#ifndef __TRACYPRINT_HPP__
#define __TRACYPRINT_HPP__
namespace tracy
{
const char* TimeToString( int64_t ns );
const char* RealToString( double val, bool separator );
const char* MemSizeToString( int64_t val );
}
#endif

View File

@ -23,6 +23,7 @@
#include "TracyFilesystem.hpp" #include "TracyFilesystem.hpp"
#include "TracyImGui.hpp" #include "TracyImGui.hpp"
#include "TracyPopcnt.hpp" #include "TracyPopcnt.hpp"
#include "TracyPrint.hpp"
#include "TracyView.hpp" #include "TracyView.hpp"
#include "../imguicolortextedit/TextEditor.h" #include "../imguicolortextedit/TextEditor.h"
@ -58,319 +59,6 @@ static const char* s_tracyStackFrames[] = {
nullptr nullptr
}; };
static const char* IntTable100 =
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899";
static inline void PrintTinyInt( char*& buf, uint64_t v )
{
if( v >= 10 )
{
*buf++ = '0' + v/10;
}
*buf++ = '0' + v%10;
}
static inline void PrintTinyInt0( char*& buf, uint64_t v )
{
if( v >= 10 )
{
*buf++ = '0' + v/10;
}
else
{
*buf++ = '0';
}
*buf++ = '0' + v%10;
}
static inline void PrintSmallInt( char*& buf, uint64_t v )
{
if( v >= 100 )
{
memcpy( buf, IntTable100 + v/10*2, 2 );
buf += 2;
}
else if( v >= 10 )
{
*buf++ = '0' + v/10;
}
*buf++ = '0' + v%10;
}
static inline void PrintFrac00( char*& buf, uint64_t v )
{
*buf++ = '.';
v += 5;
if( v/10%10 == 0 )
{
*buf++ = '0' + v/100;
}
else
{
memcpy( buf, IntTable100 + v/10*2, 2 );
buf += 2;
}
}
static inline void PrintFrac0( char*& buf, uint64_t v )
{
*buf++ = '.';
*buf++ = '0' + (v+50)/100;
}
static inline void PrintSmallIntFrac( char*& buf, uint64_t v )
{
uint64_t in = v / 1000;
uint64_t fr = v % 1000;
if( fr >= 995 )
{
PrintSmallInt( buf, in+1 );
}
else
{
PrintSmallInt( buf, in );
if( fr > 5 )
{
PrintFrac00( buf, fr );
}
}
}
static inline void PrintSecondsFrac( char*& buf, uint64_t v )
{
uint64_t in = v / 1000;
uint64_t fr = v % 1000;
if( fr >= 950 )
{
PrintTinyInt0( buf, in+1 );
}
else
{
PrintTinyInt0( buf, in );
if( fr > 50 )
{
PrintFrac0( buf, fr );
}
}
}
static const char* TimeToString( int64_t _ns )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
char* bufstart = buf;
bufsel = ( bufsel + 1 ) % Pool;
uint64_t ns;
if( _ns < 0 )
{
*buf = '-';
buf++;
ns = -_ns;
}
else
{
ns = _ns;
}
if( ns < 1000 )
{
PrintSmallInt( buf, ns );
memcpy( buf, " ns", 4 );
}
else if( ns < 1000ll * 1000 )
{
PrintSmallIntFrac( buf, ns );
#ifdef TRACY_EXTENDED_FONT
memcpy( buf, " \xce\xbcs", 5 );
#else
memcpy( buf, " us", 4 );
#endif
}
else if( ns < 1000ll * 1000 * 1000 )
{
PrintSmallIntFrac( buf, ns / 1000 );
memcpy( buf, " ms", 4 );
}
else if( ns < 1000ll * 1000 * 1000 * 60 )
{
PrintSmallIntFrac( buf, ns / ( 1000ll * 1000 ) );
memcpy( buf, " s", 3 );
}
else if( ns < 1000ll * 1000 * 1000 * 60 * 60 )
{
const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) );
const auto s = int64_t( ns - m * ( 1000ll * 1000 * 1000 * 60 ) ) / ( 1000ll * 1000 );
PrintTinyInt( buf, m );
*buf++ = ':';
PrintSecondsFrac( buf, s );
*buf++ = '\0';
}
else if( ns < 1000ll * 1000 * 1000 * 60 * 60 * 24 )
{
const auto h = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 * 60 ) );
const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) - h * 60 );
const auto s = int64_t( ns / ( 1000ll * 1000 * 1000 ) - h * ( 60 * 60 ) - m * 60 );
PrintTinyInt( buf, h );
*buf++ = ':';
PrintTinyInt0( buf, m );
*buf++ = ':';
PrintTinyInt0( buf, s );
*buf++ = '\0';
}
else
{
const auto d = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 * 60 * 24 ) );
const auto h = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 * 60 ) - d * 24 );
const auto m = int64_t( ns / ( 1000ll * 1000 * 1000 * 60 ) - d * ( 60 * 24 ) - h * 60 );
const auto s = int64_t( ns / ( 1000ll * 1000 * 1000 ) - d * ( 60 * 60 * 24 ) - h * ( 60 * 60 ) - m * 60 );
if( d < 1000 )
{
PrintSmallInt( buf, d );
*buf++ = 'd';
}
else
{
buf += sprintf( buf, "%" PRIi64 "d", d );
}
PrintTinyInt0( buf, h );
*buf++ = ':';
PrintTinyInt0( buf, m );
*buf++ = ':';
PrintTinyInt0( buf, s );
*buf++ = '\0';
}
return bufstart;
}
static const char* RealToString( double val, bool separator )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
sprintf( buf, "%f", val );
auto ptr = buf;
if( *ptr == '-' ) ptr++;
const auto vbegin = ptr;
if( separator )
{
while( *ptr != '\0' && *ptr != ',' && *ptr != '.' ) ptr++;
auto end = ptr;
while( *end != '\0' ) end++;
auto sz = end - ptr;
while( ptr - vbegin > 3 )
{
ptr -= 3;
memmove( ptr+1, ptr, sz );
*ptr = ',';
sz += 4;
}
}
while( *ptr != '\0' && *ptr != ',' && *ptr != '.' ) ptr++;
if( *ptr == '\0' ) return buf;
while( *ptr != '\0' ) ptr++;
ptr--;
while( *ptr == '0' ) ptr--;
if( *ptr != '.' && *ptr != ',' ) ptr++;
*ptr = '\0';
return buf;
}
static const char* MemSizeToString( int64_t val )
{
enum { Pool = 8 };
static char bufpool[Pool][64];
static int bufsel = 0;
char* buf = bufpool[bufsel];
bufsel = ( bufsel + 1 ) % Pool;
const auto aval = abs( val );
if( aval < 10000ll )
{
sprintf( buf, "%" PRIi64 " bytes", val );
return buf;
}
enum class Unit
{
Kilobyte,
Megabyte,
Gigabyte,
Terabyte
};
Unit unit;
if( aval < 10000ll * 1024 )
{
sprintf( buf, "%.2f", val / 1024. );
unit = Unit::Kilobyte;
}
else if( aval < 10000ll * 1024 * 1024 )
{
sprintf( buf, "%.2f", val / ( 1024. * 1024 ) );
unit = Unit::Megabyte;
}
else if( aval < 10000ll * 1024 * 1024 * 1024 )
{
sprintf( buf, "%.2f", val / ( 1024. * 1024 * 1024 ) );
unit = Unit::Gigabyte;
}
else
{
sprintf( buf, "%.2f", val / ( 1024. * 1024 * 1024 * 1024 ) );
unit = Unit::Terabyte;
}
auto ptr = buf;
while( *ptr ) ptr++;
ptr--;
while( ptr >= buf && *ptr == '0' ) ptr--;
if( *ptr != '.' ) ptr++;
*ptr++ = ' ';
switch( unit )
{
case Unit::Kilobyte:
*ptr++ = 'K';
break;
case Unit::Megabyte:
*ptr++ = 'M';
break;
case Unit::Gigabyte:
*ptr++ = 'G';
break;
case Unit::Terabyte:
*ptr++ = 'T';
break;
default:
assert( false );
break;
}
*ptr++ = 'B';
*ptr++ = '\0';
return buf;
}
static void SetButtonHighlightColor() static void SetButtonHighlightColor()
{ {