|
| 1 | +#include "../include/metrics_collector.hpp" |
| 2 | + |
| 3 | +#include <chrono> |
| 4 | +#include <fstream> |
| 5 | +#include <iostream> |
| 6 | +#include <thread> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +namespace { |
| 10 | +double GetMemoryUsed() { |
| 11 | + std::ifstream file("/proc/self/statm"); |
| 12 | + if (!file.is_open()) { |
| 13 | + return 0; |
| 14 | + } |
| 15 | + |
| 16 | + long mem_pages = 0; |
| 17 | + file >> mem_pages; |
| 18 | + file.close(); |
| 19 | + return mem_pages * (double)getpagesize(); |
| 20 | +} |
| 21 | +} // namespace |
| 22 | + |
| 23 | +MetricsCollector::MetricsCollector(const char *gateway_address, |
| 24 | + const char *gateway_port, |
| 25 | + const char *worker_name) |
| 26 | + : gateway(gateway_address, gateway_port, worker_name), |
| 27 | + registry(std::make_shared<prometheus::Registry>()) { |
| 28 | + auto &cpu_usage_family = prometheus::BuildGauge() |
| 29 | + .Name("cpu_usage") |
| 30 | + .Help("CPU Usage in percents") |
| 31 | + .Register(*registry); |
| 32 | + |
| 33 | + auto &memory_used_family = prometheus::BuildGauge() |
| 34 | + .Name("memory_used") |
| 35 | + .Help("Memory used by worker in bytes") |
| 36 | + .Register(*registry); |
| 37 | + |
| 38 | + auto &task_processing_time_family = |
| 39 | + prometheus::BuildGauge() |
| 40 | + .Name("task_processing_time") |
| 41 | + .Help("Task processing time (in seconds)") |
| 42 | + .Register(*registry); |
| 43 | + |
| 44 | + std::ifstream file("/proc/stat"); |
| 45 | + int ign; |
| 46 | + std::string cpu_name; |
| 47 | + |
| 48 | + while (true) { |
| 49 | + CPUInfo cpu; |
| 50 | + |
| 51 | + file >> cpu_name >> cpu.time.user >> cpu.time.user_low >> cpu.time.sys >> |
| 52 | + cpu.time.idle >> ign >> ign >> ign >> ign >> ign >> ign; |
| 53 | + |
| 54 | + if (cpu_name.find("cpu") != 0) |
| 55 | + break; |
| 56 | + |
| 57 | + cpu.gauge = &cpu_usage_family.Add({{"cpu", std::string(cpu_name)}}); |
| 58 | + cpu_usage.insert({std::string(cpu_name), cpu}); |
| 59 | + } |
| 60 | + |
| 61 | + file.close(); |
| 62 | + |
| 63 | + memory_used_gauge = &memory_used_family.Add({}); |
| 64 | + task_processing_time_gauge = &task_processing_time_family.Add({}); |
| 65 | + |
| 66 | + gateway.RegisterCollectable(registry); |
| 67 | + thread = std::thread(&MetricsCollector::MainLoop, this); |
| 68 | + is_task_running = false; |
| 69 | +} |
| 70 | + |
| 71 | +void MetricsCollector::MainLoop() { |
| 72 | + while (is_running) { |
| 73 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 74 | + |
| 75 | + memory_used_gauge->Set(::GetMemoryUsed()); |
| 76 | + GetCPUUsage(); |
| 77 | + |
| 78 | + if (is_task_running) { |
| 79 | + auto cur_time = std::chrono::high_resolution_clock::now(); |
| 80 | + task_processing_time_gauge->Set( |
| 81 | + std::chrono::duration<double>(cur_time - task_start).count()); |
| 82 | + } else { |
| 83 | + task_processing_time_gauge->Set(0); |
| 84 | + } |
| 85 | + |
| 86 | + int status = gateway.PushAdd(); |
| 87 | + if (status != 200) { |
| 88 | + std::cerr << "[ERROR] Failed to push metrics. Status " << status |
| 89 | + << std::endl; |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +MetricsCollector::~MetricsCollector() { |
| 95 | + is_running = false; |
| 96 | + thread.join(); |
| 97 | +} |
| 98 | + |
| 99 | +void MetricsCollector::GetCPUUsage() { |
| 100 | + std::ifstream file("/proc/stat"); |
| 101 | + CPUInfo::Time cur_time; |
| 102 | + double percent; |
| 103 | + |
| 104 | + std::string cpu_name; |
| 105 | + int ign; |
| 106 | + |
| 107 | + while (true) { |
| 108 | + file >> cpu_name >> cur_time.user >> cur_time.user_low >> cur_time.sys >> |
| 109 | + cur_time.idle >> ign >> ign >> ign >> ign >> ign >> ign; |
| 110 | + |
| 111 | + if (cpu_name.find("cpu") != 0) |
| 112 | + break; |
| 113 | + |
| 114 | + CPUInfo &cpu = cpu_usage[cpu_name]; |
| 115 | + if (cur_time.user < cpu.time.user || |
| 116 | + cur_time.user_low < cpu.time.user_low || cur_time.sys < cpu.time.sys || |
| 117 | + cur_time.idle < cpu.time.idle) { |
| 118 | + // overflow detection |
| 119 | + percent = -1.0; |
| 120 | + } else { |
| 121 | + uint64_t total = (cur_time.user - cpu.time.user) + |
| 122 | + (cur_time.user_low - cpu.time.user_low) + |
| 123 | + (cur_time.sys - cpu.time.sys); |
| 124 | + |
| 125 | + percent = total; |
| 126 | + total += (cur_time.idle - cpu.time.idle); |
| 127 | + percent = (total == 0) ? -1.0 : (percent / total) * 100.0; |
| 128 | + } |
| 129 | + |
| 130 | + cpu.time = cur_time; |
| 131 | + cpu.gauge->Set(percent); |
| 132 | + } |
| 133 | + |
| 134 | + file.close(); |
| 135 | +} |
| 136 | + |
| 137 | +void MetricsCollector::StartTask() { |
| 138 | + is_task_running = true; |
| 139 | + task_start = std::chrono::high_resolution_clock::now(); |
| 140 | + task_processing_time_gauge->Set(0); |
| 141 | + gateway.PushAdd(); |
| 142 | +} |
| 143 | + |
| 144 | +void MetricsCollector::StopTask() { |
| 145 | + is_task_running = false; |
| 146 | + task_processing_time_gauge->Set(0); |
| 147 | + gateway.PushAdd(); |
| 148 | +} |
0 commit comments