Skip to content

Commit

Permalink
Merge branch 'windows'
Browse files Browse the repository at this point in the history
  • Loading branch information
bjia56 committed Jan 16, 2025
2 parents f171236 + e91832b commit 182eff8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
8 changes: 8 additions & 0 deletions src/cosmotop_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ namespace Config {
{"proc_sorting", "#* Processes sorting, \"pid\" \"program\" \"arguments\" \"threads\" \"user\" \"memory\" \"cpu lazy\" \"cpu direct\",\n"
"#* \"cpu lazy\" sorts top process over time (easier to follow), \"cpu direct\" updates top process directly."},

{"proc_services", "#* Show services in the process box instead of processes."},

{"services_sorting", "#* Services sorting, \"service\" \"caption\" \"status\" \"memory\" \"cpu lazy\" \"cpu direct\",\n"
"#* \"cpu lazy\" sorts top service over time (easier to follow), \"cpu direct\" updates top service directly."},

{"proc_reversed", "#* Reverse sorting order, True or False."},

{"proc_tree", "#* Show processes as a tree."},
Expand Down Expand Up @@ -234,6 +239,7 @@ namespace Config {
{"graph_symbol_net", "default"},
{"graph_symbol_proc", "default"},
{"proc_sorting", "cpu lazy"},
{"services_sorting", "cpu lazy"},
{"cpu_graph_upper", "Auto"},
{"cpu_graph_lower", "Auto"},
{"cpu_sensor", "Auto"},
Expand All @@ -249,6 +255,7 @@ namespace Config {
{"proc_filter", ""},
{"proc_command", ""},
{"selected_name", ""},
{"detailed_name", ""},
#ifdef GPU_SUPPORT
{"custom_gpu_name0", ""},
{"custom_gpu_name1", ""},
Expand All @@ -266,6 +273,7 @@ namespace Config {
{"theme_background", true},
{"truecolor", true},
{"rounded_corners", true},
{"proc_services", false},
{"proc_reversed", false},
{"proc_tree", false},
{"proc_colors", true},
Expand Down
7 changes: 4 additions & 3 deletions src/cosmotop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ namespace Cpu {
{"softirq", {}},
{"steal", {}},
{"guest", {}},
{"guest_nice", {}}
{"guest_nice", {}},
{"dpc", {}}
};
vector<deque<long long>> core_percent;
vector<deque<long long>> temp;
Expand Down Expand Up @@ -326,10 +327,10 @@ namespace Mem {

struct mem_info {
std::unordered_map<string, uint64_t> stats =
{{"used", 0}, {"available", 0}, {"cached", 0}, {"free", 0},
{{"used", 0}, {"available", 0}, {"commit", 0}, {"commit_total", 0}, {"cached", 0}, {"free", 0},
{"swap_total", 0}, {"swap_used", 0}, {"swap_free", 0}};
std::unordered_map<string, deque<long long>> percent =
{{"used", {}}, {"available", {}}, {"cached", {}}, {"free", {}},
{{"used", {}}, {"available", {}}, {"commit", {}}, {"cached", {}}, {"free", {}},
{"swap_total", {}}, {"swap_used", {}}, {"swap_free", {}}};
std::unordered_map<string, disk_info> disks;
vector<string> disks_order;
Expand Down
48 changes: 29 additions & 19 deletions src/windows/cosmotop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,26 @@ namespace Shared {
IWbemServices* WbemServices;

void WMI_init() {
if (auto hr = CoInitializeEx(0, COINIT_MULTITHREADED); FAILED(hr))
throw std::runtime_error("Shared::WMI_init() -> CoInitializeEx() failed with code: " + to_string(hr));
if (auto hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); FAILED(hr) and hr != RPC_E_TOO_LATE)
Logger::warning("Shared::WMI_init() -> CoInitializeSecurity() failed with code: " + to_string(hr));
IWbemLocator* WbemLocator;
if (auto hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&WbemLocator); FAILED(hr))
throw std::runtime_error("Shared::WMI_init() -> CoCreateInstance() failed with code: " + to_string(hr));
if (auto hr = WbemLocator->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, NULL, 0, NULL, NULL, &WbemServices); FAILED(hr))
throw std::runtime_error("Shared::WMI_init() -> ConnectServer() failed with code: " + to_string(hr));
WbemLocator->Release();
if (auto hr = CoSetProxyBlanket(WbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHN_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); FAILED(hr))
Logger::warning("Shared::WMI_init() -> CoSetProxyBlanket() failed with code: " + to_string(hr));
volatile bool done = false;
std::thread([&] {
// Perform initialization in a separate thread to ensure loaded dlls
// do not get unloaded on thread exit
if (auto hr = CoInitializeEx(0, COINIT_MULTITHREADED); FAILED(hr))
throw std::runtime_error("Shared::WMI_init() -> CoInitializeEx() failed with code: " + to_string(hr));
if (auto hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); FAILED(hr) and hr != RPC_E_TOO_LATE)
Logger::warning("Shared::WMI_init() -> CoInitializeSecurity() failed with code: " + to_string(hr));
IWbemLocator* WbemLocator;
if (auto hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&WbemLocator); FAILED(hr))
throw std::runtime_error("Shared::WMI_init() -> CoCreateInstance() failed with code: " + to_string(hr));
if (auto hr = WbemLocator->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, NULL, 0, NULL, NULL, &WbemServices); FAILED(hr))
throw std::runtime_error("Shared::WMI_init() -> ConnectServer() failed with code: " + to_string(hr));
WbemLocator->Release();
if (auto hr = CoSetProxyBlanket(WbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHN_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); FAILED(hr))
Logger::warning("Shared::WMI_init() -> CoSetProxyBlanket() failed with code: " + to_string(hr));
done = true;
while (not Global::get_quitting()) std::this_thread::sleep_for(std::chrono::milliseconds(100));
}).detach();
while (!done) busy_wait();
}

class WbemEnumerator {
Expand Down Expand Up @@ -1016,14 +1024,14 @@ namespace Cpu {
tuple<int, float, long, string> current_bat;
string current_gpu = "";

const array<string, 6> time_names = { "kernel", "user", "dpc", "interrupt", "idle" };
const array<string, 6> time_names = { "system", "user", "dpc", "irq", "idle" };

std::unordered_map<string, long long> cpu_old = {
{"total", 0},
{"kernel", 0},
{"system", 0},
{"user", 0},
{"dpc", 0},
{"interrupt", 0},
{"irq", 0},
{"idle", 0},
{"totals", 0},
{"idles", 0}
Expand Down Expand Up @@ -1080,6 +1088,7 @@ namespace Cpu {
DWORD BufSize = sizeof(cpuName);
if (RegQueryValueEx(hKey, L"ProcessorNameString", NULL, NULL, (LPBYTE)cpuName, &BufSize) == ERROR_SUCCESS) {
name = string(CW2A(cpuName));
name = trim(name);
}
}

Expand Down Expand Up @@ -1387,6 +1396,7 @@ namespace Mem {
totalMem = static_cast<int64_t>(memstat.ullTotalPhys);
const int64_t totalCommit = perfinfo.CommitLimit * perfinfo.PageSize;
mem.stats.at("available") = static_cast<int64_t>(memstat.ullAvailPhys);
mem.stats.at("free") = static_cast<int64_t>(memstat.ullAvailPhys);
mem.stats.at("used") = totalMem * memstat.dwMemoryLoad / 100;
mem.stats.at("cached") = perfinfo.SystemCache * perfinfo.PageSize;
mem.stats.at("commit") = perfinfo.CommitTotal * perfinfo.PageSize;
Expand All @@ -1399,15 +1409,15 @@ namespace Mem {
mem.stats.at("swap_used") = mem.stats.at("swap_total") - mem.stats.at("swap_free");

//? Calculate percentages
for (const string name : { "used", "available", "cached", "commit"}) {
for (const string name : { "used", "available", "free", "cached", "commit"}) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / (name == "commit" ? totalCommit : totalMem)));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) mem.percent.at(name).pop_front();
}


if (show_swap and mem.stats.at("page_total") > 0) {
for (const auto name : {"page_used", "page_free"}) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / mem.stats.at("page_total")));
if (show_swap and mem.stats.at("swap_total") > 0) {
for (const auto name : {"swap_used", "swap_free"}) {
mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / mem.stats.at("swap_total")));
while (cmp_greater(mem.percent.at(name).size(), width * 2)) mem.percent.at(name).pop_front();
}
has_swap = true;
Expand Down
2 changes: 1 addition & 1 deletion third_party/libcosmo_plugin

0 comments on commit 182eff8

Please sign in to comment.