PrometheusMetrics.cpp 6.3 KB
Newer Older
Y
yu yunfeng 已提交
1 2 3 4 5 6
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/

Y
Yu Kun 已提交
7
#include <cache/GpuCacheMgr.h>
Y
yu yunfeng 已提交
8
#include "PrometheusMetrics.h"
G
groot 已提交
9
#include "utils/Log.h"
Y
yu yunfeng 已提交
10
#include "SystemInfo.h"
Y
yu yunfeng 已提交
11

12

Y
yu yunfeng 已提交
13
namespace zilliz {
J
jinhai 已提交
14
namespace milvus {
Y
yu yunfeng 已提交
15 16 17 18
namespace server {

ServerError
PrometheusMetrics::Init() {
G
groot 已提交
19 20
    try {
        ConfigNode &configNode = ServerConfig::GetInstance().GetConfig(CONFIG_METRIC);
Y
yu yunfeng 已提交
21 22
        startup_ = configNode.GetValue(CONFIG_METRIC_IS_STARTUP) == "on";
        if(!startup_) return SERVER_SUCCESS;
G
groot 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
        // Following should be read from config file.
        const std::string bind_address = configNode.GetChild(CONFIG_PROMETHEUS).GetValue(CONFIG_METRIC_PROMETHEUS_PORT);
        const std::string uri = std::string("/metrics");
        const std::size_t num_threads = 2;

        // Init Exposer
        exposer_ptr_ = std::make_shared<prometheus::Exposer>(bind_address, uri, num_threads);

        // Exposer Registry
        exposer_ptr_->RegisterCollectable(registry_);
    } catch (std::exception& ex) {
        SERVER_LOG_ERROR << "Failed to connect prometheus server: " << std::string(ex.what());
        return SERVER_UNEXPECTED_ERROR;
    }
Y
yu yunfeng 已提交
37 38

    return SERVER_SUCCESS;
Y
yu yunfeng 已提交
39 40 41

}

Y
fix  
yu yunfeng 已提交
42

Y
yu yunfeng 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
void
PrometheusMetrics::CPUUsagePercentSet()  {
    if(!startup_) return ;
    double usage_percent = server::SystemInfo::GetInstance().CPUPercent();
    CPU_usage_percent_.Set(usage_percent);
}

void
PrometheusMetrics::RAMUsagePercentSet() {
    if(!startup_) return ;
    double usage_percent = server::SystemInfo::GetInstance().MemoryPercent();
    RAM_usage_percent_.Set(usage_percent);
}

void
PrometheusMetrics::GPUPercentGaugeSet() {
    if(!startup_) return;
K
kun yu 已提交
60
    int numDevice = server::SystemInfo::GetInstance().num_device();
K
kun yu 已提交
61
    std::vector<unsigned long long > used_total = server::SystemInfo::GetInstance().GPUMemoryTotal();
K
kun yu 已提交
62 63
    std::vector<unsigned long long > used_memory = server::SystemInfo::GetInstance().GPUMemoryUsed();

K
kun yu 已提交
64
    for (int i = 0; i < numDevice; ++i) {
K
kun yu 已提交
65
        prometheus::Gauge &GPU_percent = GPU_percent_.Add({{"DeviceNum", std::to_string(i)}});
K
kun yu 已提交
66 67
        double percent = (double)used_memory[i] / (double)used_total[i];
        GPU_percent.Set(percent * 100);
K
kun yu 已提交
68
    }
Y
yu yunfeng 已提交
69 70 71 72 73
}

void PrometheusMetrics::GPUMemoryUsageGaugeSet() {
    if(!startup_) return;
    std::vector<unsigned long long> values = server::SystemInfo::GetInstance().GPUMemoryUsed();
Y
fix  
yu yunfeng 已提交
74
    constexpr unsigned long long MtoB = 1024*1024;
K
kun yu 已提交
75
    int numDevice = server::SystemInfo::GetInstance().num_device();
Y
yu yunfeng 已提交
76

K
kun yu 已提交
77
    for (int i = 0; i < numDevice; ++i) {
K
kun yu 已提交
78 79 80 81
        prometheus::Gauge &GPU_memory = GPU_memory_usage_.Add({{"DeviceNum", std::to_string(i)}});
        GPU_memory.Set(values[i] / MtoB);
    }

Y
yu yunfeng 已提交
82 83 84 85 86 87 88 89 90
}
void PrometheusMetrics::AddVectorsPerSecondGaugeSet(int num_vector, int dim, double time) {
    // MB/s
    if(!startup_) return;

    long long MtoB = 1024*1024;
    long long size = num_vector * dim * 4;
    add_vectors_per_second_gauge_.Set(size/time/MtoB);

Y
yu yunfeng 已提交
91
}
Y
yu yunfeng 已提交
92
void PrometheusMetrics::QueryIndexTypePerSecondSet(std::string type, double value) {
Y
yu yunfeng 已提交
93
    if(!startup_) return;
Y
yu yunfeng 已提交
94 95 96 97 98 99 100
    if(type == "IVF"){
        query_index_IVF_type_per_second_gauge_.Set(value);
    } else if(type == "IDMap"){
        query_index_IDMAP_type_per_second_gauge_.Set(value);
    }

}
Y
yu yunfeng 已提交
101

Y
yu yunfeng 已提交
102 103 104 105
void PrometheusMetrics::ConnectionGaugeIncrement() {
    if(!startup_) return;
    connection_gauge_.Increment();
}
Y
yu yunfeng 已提交
106

Y
yu yunfeng 已提交
107 108 109 110 111
void PrometheusMetrics::ConnectionGaugeDecrement() {
    if(!startup_) return;
    connection_gauge_.Decrement();
}

Y
yu yunfeng 已提交
112 113 114 115
void PrometheusMetrics::OctetsSet() {
    if(!startup_) return;

    // get old stats and reset them
Y
yu yunfeng 已提交
116 117 118
    unsigned long long old_inoctets = SystemInfo::GetInstance().get_inoctets();
    unsigned long long old_outoctets = SystemInfo::GetInstance().get_octets();
    auto old_time = SystemInfo::GetInstance().get_nettime();
Y
yu yunfeng 已提交
119 120 121 122 123 124
    std::pair<unsigned long long, unsigned long long> in_and_out_octets = SystemInfo::GetInstance().Octets();
    SystemInfo::GetInstance().set_inoctets(in_and_out_octets.first);
    SystemInfo::GetInstance().set_outoctets(in_and_out_octets.second);
    SystemInfo::GetInstance().set_nettime();

    //
Y
yu yunfeng 已提交
125
    constexpr double micro_to_second = 1e-6;
Y
yu yunfeng 已提交
126 127 128 129 130 131 132 133
    auto now_time = std::chrono::system_clock::now();
    auto total_microsecond = METRICS_MICROSECONDS(old_time, now_time);
    auto total_second = total_microsecond*micro_to_second;
    if(total_second == 0) return;
    inoctets_gauge_.Set((in_and_out_octets.first-old_inoctets)/total_second);
    outoctets_gauge_.Set((in_and_out_octets.second-old_outoctets)/total_second);
}

K
kun yu 已提交
134 135 136 137 138 139
void PrometheusMetrics::CPUCoreUsagePercentSet() {
    if (!startup_)
        return;

    std::vector<double> cpu_core_percent = server::SystemInfo::GetInstance().CPUCorePercent();

K
kun yu 已提交
140
    for (int i = 0; i < cpu_core_percent.size(); ++i) {
K
kun yu 已提交
141 142 143 144
        prometheus::Gauge &core_percent = CPU_.Add({{"CPU", std::to_string(i)}});
        core_percent.Set(cpu_core_percent[i]);
    }
}
Y
fix  
yu yunfeng 已提交
145

K
kun yu 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
void PrometheusMetrics::GPUTemperature() {
    if (!startup_)
        return;

    std::vector<unsigned int> GPU_temperatures = server::SystemInfo::GetInstance().GPUTemperature();

    for (int i = 0; i < GPU_temperatures.size(); ++i) {
        prometheus::Gauge &gpu_temp = GPU_temperature_.Add({{"GPU", std::to_string(i)}});
        gpu_temp.Set(GPU_temperatures[i]);
    }
}

void PrometheusMetrics::CPUTemperature() {
    if (!startup_)
        return;

    std::vector<float> CPU_temperatures = server::SystemInfo::GetInstance().CPUTemperature();

    for (int i = 0; i < CPU_temperatures.size(); ++i) {
        prometheus::Gauge &cpu_temp = CPU_temperature_.Add({{"CPU", std::to_string(i)}});
        cpu_temp.Set(CPU_temperatures[i]);
    }
}
Y
yu yunfeng 已提交
169

Y
Yu Kun 已提交
170 171 172 173 174 175 176 177 178 179 180 181
void PrometheusMetrics::GpuCacheUsageGaugeSet(double value) {
    if(!startup_) return;
    int64_t num_processors = server::SystemInfo::GetInstance().num_processor();

    for (auto i = 0; i < num_processors; ++i) {
//        int gpu_cache_usage = cache::GpuCacheMgr::GetInstance(i)->CacheUsage();
//        int gpu_cache_total = cache::GpuCacheMgr::GetInstance(i)->CacheCapacity();
//        prometheus::Gauge &gpu_cache = gpu_cache_usage_.Add({{"GPU_Cache", std::to_string(i)}});
//        gpu_cache.Set(gpu_cache_usage * 100 / gpu_cache_total);
    }
}

Y
yu yunfeng 已提交
182 183 184
}
}
}