mirror of
https://github.com/wolfpld/tracy.git
synced 2024-11-27 00:04:35 +00:00
Implement getting CPU usage in linux.
This commit is contained in:
parent
0c75a5178c
commit
bafc8a1330
@ -4,6 +4,10 @@
|
|||||||
|
|
||||||
# if defined _WIN32 || defined __CYGWIN__
|
# if defined _WIN32 || defined __CYGWIN__
|
||||||
# include <windows.h>
|
# include <windows.h>
|
||||||
|
# elif defined __linux__
|
||||||
|
# include <assert.h>
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <inttypes.h>
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
namespace tracy
|
namespace tracy
|
||||||
@ -16,7 +20,7 @@ static inline uint64_t ConvertTime( const FILETIME& t )
|
|||||||
return ( uint64_t( t.dwHighDateTime ) << 32 ) | uint64_t( t.dwLowDateTime );
|
return ( uint64_t( t.dwHighDateTime ) << 32 ) | uint64_t( t.dwLowDateTime );
|
||||||
}
|
}
|
||||||
|
|
||||||
SysTime::SysTime()
|
void SysTime::ReadTimes()
|
||||||
{
|
{
|
||||||
FILETIME idleTime;
|
FILETIME idleTime;
|
||||||
FILETIME kernelTime;
|
FILETIME kernelTime;
|
||||||
@ -30,29 +34,42 @@ SysTime::SysTime()
|
|||||||
used = kernel + user;
|
used = kernel + user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# elif defined __linux__
|
||||||
|
|
||||||
|
void SysTime::ReadTimes()
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SysTime::SysTime()
|
||||||
|
{
|
||||||
|
ReadTimes();
|
||||||
|
}
|
||||||
|
|
||||||
float SysTime::Get()
|
float SysTime::Get()
|
||||||
{
|
{
|
||||||
FILETIME idleTime;
|
const auto oldUsed = used;
|
||||||
FILETIME kernelTime;
|
const auto oldIdle = idle;
|
||||||
FILETIME userTime;
|
|
||||||
|
|
||||||
GetSystemTimes( &idleTime, &kernelTime, &userTime );
|
ReadTimes();
|
||||||
|
|
||||||
const auto newIdle = ConvertTime( idleTime );
|
const auto diffIdle = idle - oldIdle;
|
||||||
const auto kernel = ConvertTime( kernelTime );
|
const auto diffUsed = used - oldUsed;
|
||||||
const auto user = ConvertTime( userTime );
|
|
||||||
const auto newUsed = kernel + user;
|
|
||||||
|
|
||||||
const auto diffIdle = newIdle - idle;
|
|
||||||
const auto diffUsed = newUsed - used;
|
|
||||||
|
|
||||||
idle = newIdle;
|
|
||||||
used = newUsed;
|
|
||||||
|
|
||||||
|
#if defined _WIN32 || defined __CYGWIN__
|
||||||
return diffUsed == 0 ? 0 : ( diffUsed - diffIdle ) * 100.f / diffUsed;
|
return diffUsed == 0 ? 0 : ( diffUsed - diffIdle ) * 100.f / diffUsed;
|
||||||
}
|
#elif defined __linux__
|
||||||
|
const auto total = diffUsed + diffIdle;
|
||||||
|
return total == 0 ? 0 : diffUsed * 100.f / total;
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef __TRACYSYSTIME_HPP__
|
#ifndef __TRACYSYSTIME_HPP__
|
||||||
#define __TRACYSYSTIME_HPP__
|
#define __TRACYSYSTIME_HPP__
|
||||||
|
|
||||||
#if defined _WIN32 || defined __CYGWIN__
|
#if defined _WIN32 || defined __CYGWIN__ || defined __linux__
|
||||||
# define TRACY_HAS_SYSTIME
|
# define TRACY_HAS_SYSTIME
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -18,10 +18,10 @@ public:
|
|||||||
SysTime();
|
SysTime();
|
||||||
float Get();
|
float Get();
|
||||||
|
|
||||||
|
void ReadTimes();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
# if defined _WIN32 || defined __CYGWIN__
|
|
||||||
uint64_t idle, used;
|
uint64_t idle, used;
|
||||||
# endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user