Skip to content

Commit

Permalink
Added system usage script
Browse files Browse the repository at this point in the history
  • Loading branch information
matt77hias committed Apr 10, 2017
1 parent 4e2f36b commit 92fc9b2
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 3 deletions.
7 changes: 4 additions & 3 deletions MAGE/FPS/src/core/FPS.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "stdafx.h"

#include "script\fps_script.hpp"
#include "script\system_usage_script.hpp"

using namespace mage;

Expand Down Expand Up @@ -74,7 +75,7 @@ class TestScene final : public Scene {
GetWorld()->AddLight(light);

// Font
SharedPtr< SpriteFont > font = CreateFont(L"assets/fonts/comicsansms.spritefont", SpriteFontDescriptor());
SharedPtr< SpriteFont > font = CreateFont(L"assets/fonts/calibri.spritefont", SpriteFontDescriptor());
// Text
SharedPtr< SpriteText > text(new SpriteText("text", L"Hello World!", font));
GetWorld()->AddText(text);
Expand All @@ -89,8 +90,8 @@ class TestScene final : public Scene {
SharedPtr< BehaviorScript > script(new TestScript(model));
AddScript(script);

SharedPtr< BehaviorScript > fps(new FPSScript(text));
AddScript(fps);
SharedPtr< BehaviorScript > stats(new SystemUsageScript(text));
AddScript(stats);
}
};

Expand Down
4 changes: 4 additions & 0 deletions MAGE/MAGE.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
<ClInclude Include="MAGE\src\scripting\vs\vs_tokens.hpp" />
<ClInclude Include="MAGE\src\scripting\vs\vs_writer.hpp" />
<ClInclude Include="MAGE\src\script\fps_script.hpp" />
<ClInclude Include="MAGE\src\script\system_usage_script.hpp" />
<ClInclude Include="MAGE\src\shader\cso\lambertian_PS.h" />
<ClInclude Include="MAGE\src\shader\cso\lambertian_VS.h" />
<ClInclude Include="MAGE\src\shader\cso\sprite_PS.h" />
Expand All @@ -124,6 +125,7 @@
<ClInclude Include="MAGE\src\string\string.hpp" />
<ClInclude Include="MAGE\src\string\string_utils.hpp" />
<ClInclude Include="MAGE\src\string\writer.hpp" />
<ClInclude Include="MAGE\src\system\system.hpp" />
<ClInclude Include="MAGE\src\texture\dds\dds_loader.hpp" />
<ClInclude Include="MAGE\src\texture\texture.hpp" />
<ClInclude Include="MAGE\src\texture\texture_loader.hpp" />
Expand Down Expand Up @@ -180,6 +182,7 @@
<ClCompile Include="MAGE\src\scripting\vs\vs_reader.cpp" />
<ClCompile Include="MAGE\src\scripting\vs\vs_writer.cpp" />
<ClCompile Include="MAGE\src\script\fps_script.cpp" />
<ClCompile Include="MAGE\src\script\system_usage_script.cpp" />
<ClCompile Include="MAGE\src\shader\empty_shader.cpp" />
<ClCompile Include="MAGE\src\shader\lambertian_shader.cpp" />
<ClCompile Include="MAGE\src\shader\shader_utils.cpp" />
Expand All @@ -192,6 +195,7 @@
<ClCompile Include="MAGE\src\string\token.cpp" />
<ClCompile Include="MAGE\src\string\string_utils.cpp" />
<ClCompile Include="MAGE\src\string\writer.cpp" />
<ClCompile Include="MAGE\src\system\system.cpp" />
<ClCompile Include="MAGE\src\texture\dds\dds_loader.cpp" />
<ClCompile Include="MAGE\src\texture\texture.cpp" />
<ClCompile Include="MAGE\src\texture\texture_loader.cpp" />
Expand Down
18 changes: 18 additions & 0 deletions MAGE/MAGE.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@
<Filter Include="Source Files\script">
<UniqueIdentifier>{0ed63e00-b1e9-4c71-bbe7-abe117b09780}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\system">
<UniqueIdentifier>{a4d86b15-ef73-4b59-808c-66fbc132761e}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\system">
<UniqueIdentifier>{c6d373e4-d148-4ea4-8574-dcda34eb9f85}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MAGE\src\collection\collection.hpp">
Expand Down Expand Up @@ -546,6 +552,12 @@
<ClInclude Include="MAGE\src\script\fps_script.hpp">
<Filter>Header Files\script</Filter>
</ClInclude>
<ClInclude Include="MAGE\src\system\system.hpp">
<Filter>Header Files\system</Filter>
</ClInclude>
<ClInclude Include="MAGE\src\script\system_usage_script.hpp">
<Filter>Header Files\script</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MAGE\src\core\engine.cpp">
Expand Down Expand Up @@ -740,6 +752,12 @@
<ClCompile Include="MAGE\src\script\fps_script.cpp">
<Filter>Source Files\script</Filter>
</ClCompile>
<ClCompile Include="MAGE\src\system\system.cpp">
<Filter>Source Files\system</Filter>
</ClCompile>
<ClCompile Include="MAGE\src\script\system_usage_script.cpp">
<Filter>Source Files\script</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<FxCompile Include="MAGE\shaders\lambertian.fx">
Expand Down
41 changes: 41 additions & 0 deletions MAGE/MAGE/src/script/system_usage_script.cpp
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);
}
}
64 changes: 64 additions & 0 deletions MAGE/MAGE/src/script/system_usage_script.hpp
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;
};
}
108 changes: 108 additions & 0 deletions MAGE/MAGE/src/system/system.cpp
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;
}
}
23 changes: 23 additions & 0 deletions MAGE/MAGE/src/system/system.hpp
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();

}

0 comments on commit 92fc9b2

Please sign in to comment.