86 lines
2.5 KiB
C++
86 lines
2.5 KiB
C++
#include "system_metrics.h"
|
|
|
|
#include "lib/config/config.h"
|
|
#include "lib/config/protos/system_config.pb.h"
|
|
|
|
#include <fstream>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <unistd.h>
|
|
|
|
namespace bts {
|
|
namespace metrics {
|
|
|
|
struct ProcessStats {
|
|
int pid;
|
|
std::string name, state, ppid, pgrp, session, tty_nr;
|
|
std::string tpgid, flags, minflt, cminflt, majflt, cmajflt;
|
|
// utime - user time
|
|
// stime - kernel time
|
|
// cutime - cumulative user time
|
|
// cstime - cumulative system time
|
|
long utime, stime, cutime, cstime, priority, nice;
|
|
std::string O, itrealvalue, starttime;
|
|
// virtual size
|
|
uint64_t vsize;
|
|
// resident set
|
|
int64_t rss;
|
|
};
|
|
|
|
void read_stats(ProcessStats &stats) {
|
|
std::ifstream stat_stream("/proc/self/stat", std::ios_base::in);
|
|
stat_stream >> stats.pid >> stats.name >> stats.state >> stats.ppid >>
|
|
stats.pgrp >> stats.session >> stats.tty_nr >> stats.tpgid >>
|
|
stats.flags >> stats.minflt >> stats.cminflt >> stats.majflt >>
|
|
stats.cmajflt >> stats.utime >> stats.stime >> stats.cutime >>
|
|
stats.cstime >> stats.priority >> stats.nice >> stats.O >>
|
|
stats.itrealvalue >> stats.starttime >> stats.vsize >> stats.rss;
|
|
stat_stream.close();
|
|
}
|
|
|
|
void process_mem_usage(double &vm, double &resident) {
|
|
using std::ifstream;
|
|
using std::ios_base;
|
|
vm = resident = 0.0;
|
|
|
|
ProcessStats stats;
|
|
read_stats(stats);
|
|
|
|
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024;
|
|
vm = stats.vsize / 1024.0;
|
|
resident = stats.rss * page_size_kb;
|
|
}
|
|
|
|
void publish_system_metrics(MetricsContainer &stats) {
|
|
auto version = std::make_shared<CounterMetric<int>>(
|
|
"spark_version",
|
|
std::vector<Label>(
|
|
{{"version", config::get<config::SystemConfig>().version()}}));
|
|
version->set(1);
|
|
stats.publish(version);
|
|
|
|
// publish system cpu use
|
|
|
|
// publish sparkd cpu use and thread use (/proc/self/task/*)
|
|
|
|
//
|
|
// sparkd memory use
|
|
// Note: It would be nice to have 2 metrics work on the same 'read' call
|
|
stats.publish(
|
|
std::make_shared<LazyCounterMetricL>("spark_memory_res_kb", []() {
|
|
double vm, res;
|
|
process_mem_usage(vm, res);
|
|
return res;
|
|
}));
|
|
|
|
stats.publish(
|
|
std::make_shared<LazyCounterMetricL>("spark_memory_vm_kb", []() {
|
|
double vm, res;
|
|
process_mem_usage(vm, res);
|
|
return vm;
|
|
}));
|
|
}
|
|
|
|
} // namespace metrics
|
|
} // namespace bts
|