mirror of
https://github.com/typesense/typesense.git
synced 2025-05-19 05:08:43 +08:00
export swap used in /metrics.json end-point (#1499)
* export swap used in /metrics.json end-point * remove logs * add macro to run on linux only * update the swap usage fetch approach * remove check to maintain consistent API response
This commit is contained in:
parent
37f8ea0dbd
commit
e463d2f0f1
@ -143,6 +143,8 @@ private:
|
||||
|
||||
static uint64_t get_memory_non_proc_bytes();
|
||||
|
||||
static uint64_t linux_get_swap_used_bytes();
|
||||
|
||||
public:
|
||||
|
||||
SystemMetrics() {
|
||||
|
@ -72,6 +72,10 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
|
||||
result["system_memory_total_bytes"] = std::to_string(get_memory_total_bytes());
|
||||
result["system_memory_used_bytes"] = std::to_string(get_memory_used_bytes());
|
||||
|
||||
#ifdef __linux__
|
||||
auto swap_bytes = linux_get_swap_used_bytes();
|
||||
result["system_memory_used_swap_bytes"] = std::to_string(swap_bytes);
|
||||
#endif
|
||||
// CPU and Network metrics
|
||||
#if __linux__
|
||||
const std::vector<cpu_stat_t>& cpu_stats = get_cpu_stats();
|
||||
@ -227,3 +231,18 @@ void SystemMetrics::linux_get_network_data(const std::string & stat_path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t SystemMetrics::linux_get_swap_used_bytes() {
|
||||
std::string filename = "/proc/" + std::to_string(::getpid()) + "/status";
|
||||
std::string token;
|
||||
std::ifstream file(filename);
|
||||
while(file >> token) {
|
||||
if(token == "VmSwap:") {
|
||||
uint64_t mem_kB;
|
||||
if(file >> mem_kB) {
|
||||
return mem_kB * 1024;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user