From ccd41322bd5ddda8e05aa0d7ef317a907494fe00 Mon Sep 17 00:00:00 2001 From: Kris Moore Date: Mon, 25 Jan 2016 15:51:43 -0500 Subject: [PATCH] Replace the 'memorypercentage' API call with 'memorystats', which returns much more meaningfull information to let us do graphs and whatnot REST Request: ------------------------------- PUT /sysadm/systeminfo { "action" : "memorystats" } REST Response: ------------------------------- { "args": { "memorystats": { "active": "818", "cache": "69", "free": "4855", "inactive": "2504", "wired": "1598" } } } WebSocket Request: ------------------------------- { "args" : { "action" : "memorystats" }, "namespace" : "sysadm", "id" : "fooid", "name" : "systeminfo" } WebSocket Response: ------------------------------- { "args": { "memorystats": { "active": "826", "cache": "69", "free": "4847", "inactive": "2505", "wired": "1598" } }, "id": "fooid", "name": "response", "namespace": "sysadm" } --- src/library/sysadm-systeminfo.cpp | 59 +++++++++++++++++++++++++------ src/library/sysadm-systeminfo.h | 2 +- src/server/WebBackend.cpp | 4 +-- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/library/sysadm-systeminfo.cpp b/src/library/sysadm-systeminfo.cpp index a580093..235da49 100644 --- a/src/library/sysadm-systeminfo.cpp +++ b/src/library/sysadm-systeminfo.cpp @@ -161,16 +161,52 @@ QJsonObject SysInfo::externalDevicePaths() { return retObject; } -// KPM 1-21-2016 -// This needs to beefed up as well, so we return more stats on arc, wired, etc -QJsonObject SysInfo::memoryPercentage() { +// Return information about memory +QJsonObject SysInfo::memoryStats() { QJsonObject retObject; - //SYSCTL: vm.stats.vm.v__count - QStringList info = General::RunCommand("sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_wire_count vm.stats.vm.v_active_count").split("\n"); - if(info.length()<3){ return retObject; } //error in fetching information - //List output: [total, wired, active] - double perc = 100.0* (info[1].toLong()+info[2].toLong())/(info[0].toDouble()); - retObject.insert("memoryused", qRound(perc)); + + QString tmp; + long pageSize; + bool ok; + + // Get the page size + tmp = General::RunCommand("sysctl -n vm.stats.vm.v_page_size").simplified(); + tmp.toLong(&ok); + if ( ok ) + pageSize = tmp.toLong(); + else + return retObject; + + // Get the free size + tmp = General::RunCommand("sysctl -n vm.stats.vm.v_free_count").simplified(); + tmp.toLong(&ok); + if ( ok ) + retObject.insert("free", tmp.setNum((tmp.toLong() * pageSize) / 1024 / 1024)); + + // Get the inactive size + tmp = General::RunCommand("sysctl -n vm.stats.vm.v_inactive_count").simplified(); + tmp.toLong(&ok); + if ( ok ) + retObject.insert("inactive", tmp.setNum((tmp.toLong() * pageSize) / 1024 / 1024)); + + // Get the cache size + tmp = General::RunCommand("sysctl -n vm.stats.vm.v_cache_count").simplified(); + tmp.toLong(&ok); + if ( ok ) + retObject.insert("cache", tmp.setNum((tmp.toLong() * pageSize) / 1024 / 1024)); + + // Get the wired size + tmp = General::RunCommand("sysctl -n vm.stats.vm.v_wire_count").simplified(); + tmp.toLong(&ok); + if ( ok ) + retObject.insert("wired", tmp.setNum((tmp.toLong() * pageSize) / 1024 / 1024)); + + // Get the active size + tmp = General::RunCommand("sysctl -n vm.stats.vm.v_active_count").simplified(); + tmp.toLong(&ok); + if ( ok ) + retObject.insert("active", tmp.setNum((tmp.toLong() * pageSize) / 1024 / 1024)); + return retObject; } @@ -203,10 +239,11 @@ QJsonObject SysInfo::systemInfo() { retObject.insert("cpucores", cpucores); bool ok; + QString tmp; QString totalmem = General::RunCommand("sysctl -n hw.realmem").simplified(); - totalmem.toDouble(&ok); + totalmem.toLong(&ok); if ( ok ) { - retObject.insert("totalmem", totalmem.toDouble() / 1024 / 1024); + retObject.insert("totalmem", tmp.setNum(totalmem.toLong() / 1024 / 1024)); } return retObject; diff --git a/src/library/sysadm-systeminfo.h b/src/library/sysadm-systeminfo.h index 4c46bab..a9c1c4a 100644 --- a/src/library/sysadm-systeminfo.h +++ b/src/library/sysadm-systeminfo.h @@ -18,7 +18,7 @@ public: static QJsonObject cpuPercentage(); static QJsonObject cpuTemps(); static QJsonObject externalDevicePaths(); - static QJsonObject memoryPercentage(); + static QJsonObject memoryStats(); static QJsonObject systemInfo(); }; diff --git a/src/server/WebBackend.cpp b/src/server/WebBackend.cpp index 8733fca..6b76486 100644 --- a/src/server/WebBackend.cpp +++ b/src/server/WebBackend.cpp @@ -271,9 +271,9 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmSystemInfoRequest(const QJso ok = true; out->insert("externalmounts", sysadm::SysInfo::externalDevicePaths()); } - if(act=="memorypercentage"){ + if(act=="memorystats"){ ok = true; - out->insert("memorypercentage", sysadm::SysInfo::memoryPercentage()); + out->insert("memorystats", sysadm::SysInfo::memoryStats()); } if(act=="systeminfo"){ ok = true;