2019-02-21 20:59:02 +00:00
|
|
|
#include "TracySysTime.hpp"
|
|
|
|
|
|
|
|
#ifdef TRACY_HAS_SYSTIME
|
|
|
|
|
2019-02-21 22:11:09 +00:00
|
|
|
# if defined _WIN32 || defined __CYGWIN__
|
2019-02-21 20:59:02 +00:00
|
|
|
# include <windows.h>
|
2019-02-24 18:02:49 +00:00
|
|
|
# elif defined __linux__
|
|
|
|
# include <assert.h>
|
|
|
|
# include <stdio.h>
|
|
|
|
# include <inttypes.h>
|
2019-02-25 14:04:06 +00:00
|
|
|
# elif defined __APPLE__
|
|
|
|
# include <mach/mach_host.h>
|
|
|
|
# include <mach/host_info.h>
|
2019-02-21 20:59:02 +00:00
|
|
|
# endif
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2019-02-21 22:11:09 +00:00
|
|
|
# if defined _WIN32 || defined __CYGWIN__
|
2019-02-21 20:59:02 +00:00
|
|
|
|
|
|
|
static inline uint64_t ConvertTime( const FILETIME& t )
|
|
|
|
{
|
|
|
|
return ( uint64_t( t.dwHighDateTime ) << 32 ) | uint64_t( t.dwLowDateTime );
|
|
|
|
}
|
|
|
|
|
2019-02-24 18:02:49 +00:00
|
|
|
void SysTime::ReadTimes()
|
2019-02-21 20:59:02 +00:00
|
|
|
{
|
|
|
|
FILETIME idleTime;
|
|
|
|
FILETIME kernelTime;
|
|
|
|
FILETIME userTime;
|
|
|
|
|
|
|
|
GetSystemTimes( &idleTime, &kernelTime, &userTime );
|
|
|
|
|
|
|
|
idle = ConvertTime( idleTime );
|
|
|
|
const auto kernel = ConvertTime( kernelTime );
|
|
|
|
const auto user = ConvertTime( userTime );
|
|
|
|
used = kernel + user;
|
|
|
|
}
|
|
|
|
|
2019-02-24 18:02:49 +00:00
|
|
|
# elif defined __linux__
|
|
|
|
|
|
|
|
void SysTime::ReadTimes()
|
2019-02-21 20:59:02 +00:00
|
|
|
{
|
2019-02-24 18:02:49 +00:00
|
|
|
uint64_t user, nice, system;
|
|
|
|
FILE* f = fopen( "/proc/stat", "r" );
|
|
|
|
assert( f );
|
|
|
|
fscanf( f, "cpu %" PRIu64 " %" PRIu64 " %" PRIu64" %" PRIu64, &user, &nice, &system, &idle );
|
|
|
|
fclose( f );
|
|
|
|
used = user + nice + system;
|
|
|
|
}
|
2019-02-21 20:59:02 +00:00
|
|
|
|
2019-02-25 14:04:06 +00:00
|
|
|
# elif defined __APPLE__
|
|
|
|
|
|
|
|
void SysTime::ReadTimes()
|
|
|
|
{
|
|
|
|
host_cpu_load_info_data_t info;
|
|
|
|
mach_msg_type_number_t cnt = HOST_CPU_LOAD_INFO_COUNT;
|
|
|
|
host_statistics( mach_host_self(), HOST_CPU_LOAD_INFO, reinterpret_cast<host_info_t>( &info ), &cnt );
|
|
|
|
used = info.cpu_ticks[CPU_STATE_USER] + info.cpu_ticks[CPU_STATE_NICE] + info.cpu_ticks[CPU_STATE_SYSTEM];
|
|
|
|
idle = info.cpu_ticks[CPU_STATE_IDLE];
|
|
|
|
}
|
|
|
|
|
2019-02-24 18:02:49 +00:00
|
|
|
#endif
|
2019-02-21 20:59:02 +00:00
|
|
|
|
2019-02-24 18:02:49 +00:00
|
|
|
SysTime::SysTime()
|
|
|
|
{
|
|
|
|
ReadTimes();
|
|
|
|
}
|
|
|
|
|
|
|
|
float SysTime::Get()
|
|
|
|
{
|
|
|
|
const auto oldUsed = used;
|
|
|
|
const auto oldIdle = idle;
|
2019-02-21 20:59:02 +00:00
|
|
|
|
2019-02-24 18:02:49 +00:00
|
|
|
ReadTimes();
|
2019-02-21 20:59:02 +00:00
|
|
|
|
2019-02-24 18:02:49 +00:00
|
|
|
const auto diffIdle = idle - oldIdle;
|
|
|
|
const auto diffUsed = used - oldUsed;
|
2019-02-21 20:59:02 +00:00
|
|
|
|
2019-02-24 18:02:49 +00:00
|
|
|
#if defined _WIN32 || defined __CYGWIN__
|
2019-02-25 14:11:35 +00:00
|
|
|
return diffUsed == 0 ? -1 : ( diffUsed - diffIdle ) * 100.f / diffUsed;
|
2019-02-25 14:04:06 +00:00
|
|
|
#elif defined __linux__ || defined __APPLE__
|
2019-02-24 18:02:49 +00:00
|
|
|
const auto total = diffUsed + diffIdle;
|
2019-02-25 14:11:35 +00:00
|
|
|
return total == 0 ? -1 : diffUsed * 100.f / total;
|
2019-02-24 18:02:49 +00:00
|
|
|
#endif
|
2019-02-21 20:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|