Get memory metric and write it into mem_metric.
26 {
27 static const std::string system_mem_stat_file = "/proc/meminfo";
28 const int mem_total = 0, available = 2, line_count = 3;
29 const auto stat_lines =
GetStatsLines(system_mem_stat_file, line_count);
30 if (stat_lines.size() < line_count) {
31 AERROR <<
"failed to load contents from " << system_mem_stat_file;
32 return false;
33 }
34 const auto total_memory = GetMemoryValueFromLine(stat_lines[mem_total]);
35 const auto available_memory = GetMemoryValueFromLine(stat_lines[available]);
36 if (available_memory > total_memory) {
37 AERROR <<
"available memory is larger than total memory";
38 return false;
39 }
40 const auto used_memory = total_memory - available_memory;
41 mem_metric->set_total_mb(total_memory >> 10);
42 mem_metric->set_used_mb(used_memory >> 10);
43 mem_metric->set_available_mb((total_memory - used_memory) >> 10);
44 mem_metric->set_usage(100.f * (static_cast<float>(used_memory) / total_memory));
45 return true;
46}
std::vector< std::string > GetStatsLines(const std::string &stat_file, const int line_count)
Get stats lines from file