-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e2f36b
commit 92fc9b2
Showing
7 changed files
with
262 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#pragma once | ||
|
||
//----------------------------------------------------------------------------- | ||
// Engine Includes | ||
//----------------------------------------------------------------------------- | ||
#pragma region | ||
|
||
#include "script\system_usage_script.hpp" | ||
#include "system\system.hpp" | ||
|
||
#pragma endregion | ||
|
||
//----------------------------------------------------------------------------- | ||
// Engine Declarations | ||
//----------------------------------------------------------------------------- | ||
namespace mage { | ||
|
||
const double SystemUsageScript::resource_fetch_period = 0.25; | ||
|
||
void SystemUsageScript::Update(double elapsed_time, const Scene &scene) { | ||
UNUSED(elapsed_time); | ||
UNUSED(scene); | ||
|
||
m_time += elapsed_time; | ||
if (m_time > SystemUsageScript::resource_fetch_period) { | ||
const uint32_t cpu_usage = static_cast<uint32_t>(GetProcessorUsage()); | ||
const uint32_t ram_usage = static_cast<uint32_t>(GetPhysicalMemoryUsage() >> 20); | ||
|
||
m_cpu_usage = (cpu_usage) ? cpu_usage : m_cpu_usage; | ||
m_ram_usage = (ram_usage) ? ram_usage : m_ram_usage; | ||
|
||
m_time = 0.0; | ||
} | ||
|
||
wchar_t buffer[32]; | ||
_snwprintf_s(buffer, _countof(buffer), L"CPU: %u %%\nRAM: %u MB", m_cpu_usage, m_ram_usage); | ||
const wstring text = buffer; | ||
|
||
m_text->SetText(text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#pragma once | ||
|
||
//----------------------------------------------------------------------------- | ||
// Engine Includes | ||
//----------------------------------------------------------------------------- | ||
#pragma region | ||
|
||
#include "scripting\behavior_script.hpp" | ||
#include "text\sprite_text.hpp" | ||
|
||
#pragma endregion | ||
|
||
//----------------------------------------------------------------------------- | ||
// Engine Declarations | ||
//----------------------------------------------------------------------------- | ||
namespace mage { | ||
|
||
class SystemUsageScript final : public BehaviorScript { | ||
|
||
public: | ||
|
||
//--------------------------------------------------------------------- | ||
// Class Member Variables | ||
//--------------------------------------------------------------------- | ||
|
||
static const double resource_fetch_period; | ||
|
||
//--------------------------------------------------------------------- | ||
// Constructors and Destructors | ||
//--------------------------------------------------------------------- | ||
|
||
explicit SystemUsageScript(SharedPtr< SpriteText > text) | ||
: BehaviorScript(), m_time(0.0), | ||
m_cpu_usage(0), m_ram_usage(0), | ||
m_text(text) {} | ||
SystemUsageScript(const SystemUsageScript &script) = delete; | ||
SystemUsageScript(SystemUsageScript &&script) = default; | ||
virtual ~SystemUsageScript() = default; | ||
|
||
//--------------------------------------------------------------------- | ||
// Assignment Operators | ||
//--------------------------------------------------------------------- | ||
|
||
SystemUsageScript &operator=(const SystemUsageScript &script) = delete; | ||
SystemUsageScript &operator=(SystemUsageScript &&script) = delete; | ||
|
||
//--------------------------------------------------------------------- | ||
// Member Methods | ||
//--------------------------------------------------------------------- | ||
|
||
virtual void Update(double elapsed_time, const Scene &scene) override; | ||
|
||
private: | ||
|
||
//--------------------------------------------------------------------- | ||
// Member Variables | ||
//--------------------------------------------------------------------- | ||
|
||
double m_time; | ||
uint32_t m_cpu_usage; | ||
uint32_t m_ram_usage; | ||
SharedPtr< SpriteText > m_text; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//----------------------------------------------------------------------------- | ||
// Engine Includes | ||
//----------------------------------------------------------------------------- | ||
#pragma region | ||
|
||
#include "platform\windows.hpp" | ||
#include "system\system.hpp" | ||
#include "parallel\parallel.hpp" | ||
|
||
#pragma endregion | ||
|
||
//----------------------------------------------------------------------------- | ||
// System Includes | ||
//----------------------------------------------------------------------------- | ||
#pragma region | ||
|
||
#include <psapi.h> | ||
|
||
#pragma endregion | ||
|
||
//----------------------------------------------------------------------------- | ||
// Engine Declarations and Definitions | ||
//----------------------------------------------------------------------------- | ||
namespace mage { | ||
|
||
uint64_t GetVirtualMemoryUsage() { | ||
PROCESS_MEMORY_COUNTERS_EX memory_stats; | ||
ZeroMemory(&memory_stats, sizeof(memory_stats)); | ||
const BOOL result = GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&memory_stats, sizeof(memory_stats)); | ||
return (result == 0) ? 0 : static_cast< uint64_t >(memory_stats.PrivateUsage); | ||
} | ||
|
||
uint64_t GetPhysicalMemoryUsage() { | ||
PROCESS_MEMORY_COUNTERS_EX memory_stats; | ||
ZeroMemory(&memory_stats, sizeof(memory_stats)); | ||
const BOOL result = GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&memory_stats, sizeof(memory_stats)); | ||
return (result == 0) ? 0 : static_cast< uint64_t >(memory_stats.WorkingSetSize); | ||
} | ||
|
||
static bool IsInitialized = false; | ||
static ULARGE_INTEGER last_time; | ||
static ULARGE_INTEGER last_kernel_mode_time; | ||
static ULARGE_INTEGER last_user_mode_time; | ||
|
||
static void InitTimes() { | ||
FILETIME ftime; | ||
// Retrieves the current system date and time. | ||
// The information is in Coordinated Universal Time (UTC) format. | ||
GetSystemTimeAsFileTime(&ftime); | ||
|
||
memcpy(&last_time, &ftime, sizeof(FILETIME)); | ||
|
||
FILETIME kernel_mode_ftime; | ||
FILETIME user_mode_ftime; | ||
// Retrieves timing information for the specified process. | ||
// 1. A handle to the process whose timing information is sought. | ||
// 2. A pointer to a FILETIME structure that receives the creation time of the process. | ||
// 3. A pointer to a FILETIME structure that receives the exit time of the process. | ||
// 4. A pointer to a FILETIME structure that receives the amount of time that the process has executed in kernel mode. | ||
// 5. A pointer to a FILETIME structure that receives the amount of time that the process has executed in user mode. | ||
GetProcessTimes(GetCurrentProcess(), &ftime, &ftime, &kernel_mode_ftime, &user_mode_ftime); | ||
|
||
memcpy(&last_kernel_mode_time, &kernel_mode_ftime, sizeof(FILETIME)); | ||
memcpy(&last_user_mode_time, &user_mode_ftime, sizeof(FILETIME)); | ||
} | ||
|
||
double GetProcessorUsage() { | ||
if (!IsInitialized) { | ||
InitTimes(); | ||
IsInitialized = true; | ||
} | ||
|
||
FILETIME ftime; | ||
// Retrieves the current system date and time. | ||
// The information is in Coordinated Universal Time (UTC) format. | ||
GetSystemTimeAsFileTime(&ftime); | ||
|
||
ULARGE_INTEGER now_time; | ||
memcpy(&now_time, &ftime, sizeof(FILETIME)); | ||
|
||
FILETIME kernel_mode_ftime; | ||
FILETIME user_mode_ftime; | ||
// Retrieves timing information for the specified process. | ||
// 1. A handle to the process whose timing information is sought. | ||
// 2. A pointer to a FILETIME structure that receives the creation time of the process. | ||
// 3. A pointer to a FILETIME structure that receives the exit time of the process. | ||
// 4. A pointer to a FILETIME structure that receives the amount of time that the process has executed in kernel mode. | ||
// 5. A pointer to a FILETIME structure that receives the amount of time that the process has executed in user mode. | ||
const BOOL result = GetProcessTimes(GetCurrentProcess(), &ftime, &ftime, &kernel_mode_ftime, &user_mode_ftime); | ||
|
||
ULARGE_INTEGER kernel_mode_time; | ||
memcpy(&kernel_mode_time, &kernel_mode_ftime, sizeof(FILETIME)); | ||
|
||
ULARGE_INTEGER user_mode_time; | ||
memcpy(&user_mode_time, &user_mode_ftime, sizeof(FILETIME)); | ||
|
||
const double numerator = (kernel_mode_time.QuadPart - last_kernel_mode_time.QuadPart) + | ||
(user_mode_time.QuadPart - last_user_mode_time.QuadPart); | ||
const double denominator = (now_time.QuadPart - last_time.QuadPart) * NumberOfSystemCores(); | ||
const double percentage = 100.0 * numerator / denominator; | ||
|
||
last_time = now_time; | ||
last_kernel_mode_time = kernel_mode_time; | ||
last_user_mode_time = user_mode_time; | ||
|
||
return (result == 0 || denominator == 0) ? 0 : percentage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
//----------------------------------------------------------------------------- | ||
// System Includes | ||
//----------------------------------------------------------------------------- | ||
#pragma region | ||
|
||
#include <stdint.h> | ||
|
||
#pragma endregion | ||
|
||
//----------------------------------------------------------------------------- | ||
// Engine Declarations | ||
//----------------------------------------------------------------------------- | ||
namespace mage { | ||
|
||
uint64_t GetVirtualMemoryUsage(); | ||
|
||
uint64_t GetPhysicalMemoryUsage(); | ||
|
||
double GetProcessorUsage(); | ||
|
||
} |