Move get_proc_meminfo to system metrics.

This commit is contained in:
Kishore Nallan 2024-11-16 16:42:07 +05:30
parent ce276e1606
commit 5b900be858
5 changed files with 45 additions and 42 deletions

@ -477,8 +477,6 @@ public:
const bool& return_doc, const bool& return_id, const size_t remote_embedding_batch_size = 200,
const size_t remote_embedding_timeout_ms = 60000, const size_t remote_embedding_num_tries = 2);
bool is_exceeding_memory_threshold() const;
void parse_search_query(const std::string &query, std::vector<std::string>& q_include_tokens, std::vector<std::string>& q_include_tokens_non_stemmed,
std::vector<std::vector<std::string>>& q_exclude_tokens,
std::vector<std::vector<std::string>>& q_phrases,

@ -177,4 +177,7 @@ public:
static uint64_t get_memory_free_bytes() {
return get_memory_total_bytes() - get_memory_used_bytes();
}
static void get_proc_meminfo(uint64_t& memory_total_bytes, uint64_t& memory_available_bytes,
uint64_t& swap_total_bytes, uint64_t& swap_free_bytes);
};

@ -1,5 +1,6 @@
#include "cached_resource_stat.h"
#include <fstream>
#include <system_metrics.h>
#include "logger.h"
cached_resource_stat_t::resource_check_t
@ -43,42 +44,7 @@ cached_resource_stat_t::get_resource_status(const std::string& data_dir_path, co
disk_total_bytes = st.f_blocks * st.f_frsize;
disk_used_bytes = (st.f_blocks - st.f_bavail) * st.f_frsize;
// get memory and swap usage
std::string token;
std::ifstream file("/proc/meminfo");
while(file >> token) {
if(token == "MemTotal:") {
uint64_t value_kb;
if(file >> value_kb) {
memory_total_bytes = value_kb * 1024;
}
}
else if(token == "MemAvailable:") {
uint64_t value_kb;
if(file >> value_kb) {
memory_available_bytes = value_kb * 1024;
}
}
else if(token == "SwapTotal:") {
uint64_t value_kb;
if(file >> value_kb) {
swap_total_bytes = value_kb * 1024;
}
}
else if(token == "SwapFree:") {
uint64_t value_kb;
if(file >> value_kb) {
swap_free_bytes = value_kb * 1024;
}
// since "SwapFree" appears last in the file
break;
}
}
SystemMetrics::get_proc_meminfo(memory_total_bytes, memory_available_bytes, swap_total_bytes, swap_free_bytes);
if(memory_total_bytes == 0) {
// if there is an error in fetching the stat, we will return `OK`

@ -687,10 +687,6 @@ Option<nlohmann::json> Collection::update_matching_filter(const std::string& fil
return Option(resp_summary);
}
bool Collection::is_exceeding_memory_threshold() const {
return SystemMetrics::used_memory_ratio() > max_memory_ratio;
}
void Collection::batch_index(std::vector<index_record>& index_records, std::vector<std::string>& json_out,
size_t &num_indexed, const bool& return_doc, const bool& return_id, const size_t remote_embedding_batch_size,
const size_t remote_embedding_timeout_ms, const size_t remote_embedding_num_tries) {

@ -234,3 +234,43 @@ void SystemMetrics::linux_get_network_data(const std::string & stat_path,
}
}
}
void SystemMetrics::get_proc_meminfo(uint64_t& memory_total_bytes, uint64_t& memory_available_bytes,
uint64_t& swap_total_bytes,
uint64_t& swap_free_bytes) {
std::string token;
std::ifstream file("/proc/meminfo");
while(file >> token) {
if(token == "MemTotal:") {
uint64_t value_kb;
if(file >> value_kb) {
memory_total_bytes = value_kb * 1024;
}
}
else if(token == "MemAvailable:") {
uint64_t value_kb;
if(file >> value_kb) {
memory_available_bytes = value_kb * 1024;
}
}
else if(token == "SwapTotal:") {
uint64_t value_kb;
if(file >> value_kb) {
swap_total_bytes = value_kb * 1024;
}
}
else if(token == "SwapFree:") {
uint64_t value_kb;
if(file >> value_kb) {
swap_free_bytes = value_kb * 1024;
}
// since "SwapFree" appears last in the file
break;
}
}
}