Skip to content

Commit

Permalink
feat: add more statistics to /status command
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 24, 2025
1 parent 1dfc717 commit 8a7109b
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 9 deletions.
3 changes: 3 additions & 0 deletions include/endstone/detail/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ std::string get_module_pathname();
void *get_executable_base();
std::string get_executable_pathname();
std::string_view get_platform();
std::size_t get_thread_count();
std::size_t get_used_physical_memory();
std::size_t get_total_virtual_memory();
} // namespace endstone::detail
38 changes: 29 additions & 9 deletions src/endstone/core/command/defaults/status_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "endstone/color_format.h"
#include "endstone/core/server.h"
#include "endstone/detail/platform.h"

namespace endstone::core {

Expand Down Expand Up @@ -55,18 +56,37 @@ bool StatusCommand::execute(CommandSender &sender, const std::vector<std::string
sender.sendMessage("{}Uptime: {}{} seconds", ColorFormat::Gold, ColorFormat::Red, seconds);
}

auto color = ColorFormat::Green;
auto average_tps = server.getAverageTicksPerSecond();
if (average_tps < 12) {
color = ColorFormat::Red;
auto tps_color = ColorFormat::Green;
if (server.getCurrentTicksPerSecond() < 12) {
tps_color = ColorFormat::Red;
}
else if (average_tps < 17) {
color = ColorFormat::Gold;
else if (server.getCurrentTicksPerSecond() < 17) {
tps_color = ColorFormat::Gold;
}

sender.sendMessage("{}MSPT: {}{:.2f}", ColorFormat::Gold, color, server.getAverageMillisecondsPerTick());
sender.sendMessage("{}TPS: {}{:.2f}", ColorFormat::Gold, color, server.getAverageTicksPerSecond());
sender.sendMessage("{}Usage: {}{:.2f}%", ColorFormat::Gold, color, server.getAverageTickUsage() * 100);
sender.sendMessage("{}Current TPS: {}{:.2f} ({:.2f}%)", ColorFormat::Gold, tps_color,
server.getCurrentTicksPerSecond(), server.getCurrentTickUsage());
sender.sendMessage("{}Average TPS: {}{:.2f} ({:.2f}%)", ColorFormat::Gold, tps_color,
server.getAverageTicksPerSecond(), server.getAverageTickUsage());

sender.sendMessage("{}Thread count: {}{}", ColorFormat::Gold, ColorFormat::Red, detail::get_thread_count());

sender.sendMessage("{}Used memory: {}{:.2f} MB", ColorFormat::Gold, ColorFormat::Red,
detail::get_used_physical_memory() / 1024.0F / 1024.0F);
sender.sendMessage("{}Total memory: {}{:.2f} MB", ColorFormat::Gold, ColorFormat::Red,
detail::get_total_virtual_memory() / 1024.0F / 1024.0F);

auto actors = server.getLevel()->getActors();
for (const auto &dimension : server.getLevel()->getDimensions()) {
auto actor_count = 0;
for (const auto &actor : actors) {
if (&actor->getDimension() == dimension) {
actor_count++;
}
}
sender.sendMessage("{}Dimension: \"{}\": {}{}{} actors", ColorFormat::Gold, dimension->getName(),
ColorFormat::Red, actor_count, ColorFormat::Green);
}

return true;
}
Expand Down
32 changes: 32 additions & 0 deletions src/endstone/core/platform_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,38 @@ std::string_view get_name()
return "Linux";
}

std::size_t get_proc_status(std::string_view key)
{
std::ifstream file("/proc/self/status");
if (!file.is_open()) {
throw std::runtime_error("Failed to open /proc/self/status");
}

std::string line;
while (std::getline(file, line)) {
if (line.size() > key.size() && line.substr(0, key.size()) == key && line[key.size()] == ':') {
return std::stoi(line.substr(key.size() + 1));
}
}

throw std::runtime_error(fmt::format("Key {} not found in {}", key, "/proc/self/status"));
}

std::size_t get_thread_count()
{
return get_proc_status("Threads");
}

std::size_t get_used_physical_memory()
{
return get_proc_status("VmRSS");
}

std::size_t get_total_virtual_memory()
{
return get_proc_status("VmSize");
}

} // namespace endstone::detail

#endif
44 changes: 44 additions & 0 deletions src/endstone/core/platform_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <Windows.h>
// Psapi.h must be included after Windows.h
#include <Psapi.h>
#include <TlHelp32.h>

#include <string>
#include <string_view>
Expand Down Expand Up @@ -90,6 +91,49 @@ std::string_view get_platform()
{
return "Windows";
}

std::size_t get_thread_count()
{
DWORD process_id = GetCurrentProcessId();
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (snapshot == INVALID_HANDLE_VALUE) {
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(),
"CreateToolhelp32Snapshot failed");
}

THREADENTRY32 te32;
te32.dwSize = sizeof(THREADENTRY32);
if (!Thread32First(snapshot, &te32)) {
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), "Thread32First failed");
}

int thread_count = 0;
do {
if (te32.th32OwnerProcessID == process_id) {
++thread_count;
}
} while (Thread32Next(snapshot, &te32));
CloseHandle(snapshot);
return thread_count;
}
std::size_t get_used_physical_memory()
{
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
return pmc.WorkingSetSize;
}
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), "GetProcessMemoryInfo failed");
}

std::size_t get_total_virtual_memory()
{
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
return pmc.PagefileUsage;
}
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), "GetProcessMemoryInfo failed");
}

} // namespace endstone::detail

#endif

0 comments on commit 8a7109b

Please sign in to comment.