PrometheusMetrics.cpp 7.1 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

S
starlord 已提交
18
#include "metrics/PrometheusMetrics.h"
S
starlord 已提交
19
#include "SystemInfo.h"
20 21
#include "cache/GpuCacheMgr.h"
#include "server/Config.h"
G
groot 已提交
22
#include "utils/Log.h"
Y
yu yunfeng 已提交
23

S
starlord 已提交
24 25
#include <string>
#include <utility>
26

Y
yu yunfeng 已提交
27
namespace zilliz {
J
jinhai 已提交
28
namespace milvus {
Y
yu yunfeng 已提交
29 30
namespace server {

31
ErrorCode
Y
yu yunfeng 已提交
32
PrometheusMetrics::Init() {
G
groot 已提交
33
    try {
S
starlord 已提交
34
        Config& config = Config::GetInstance();
Y
yudong.cai 已提交
35
        Status s = config.GetMetricConfigEnableMonitor(startup_);
S
starlord 已提交
36 37 38 39 40 41
        if (!s.ok()) {
            return s.code();
        }
        if (!startup_) {
            return SERVER_SUCCESS;
        }
42

G
groot 已提交
43
        // Following should be read from config file.
44 45
        std::string bind_address;
        s = config.GetMetricConfigPrometheusPort(bind_address);
S
starlord 已提交
46 47 48 49
        if (!s.ok()) {
            return s.code();
        }

50
        const std::string uri = std::string("/tmp/metrics");
G
groot 已提交
51 52 53 54 55 56 57
        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_);
S
starlord 已提交
58
    } catch (std::exception& ex) {
G
groot 已提交
59 60 61
        SERVER_LOG_ERROR << "Failed to connect prometheus server: " << std::string(ex.what());
        return SERVER_UNEXPECTED_ERROR;
    }
Y
yu yunfeng 已提交
62 63

    return SERVER_SUCCESS;
Y
yu yunfeng 已提交
64 65 66
}

void
S
starlord 已提交
67
PrometheusMetrics::CPUUsagePercentSet() {
S
starlord 已提交
68 69 70 71
    if (!startup_) {
        return;
    }

Y
yu yunfeng 已提交
72 73 74 75 76 77
    double usage_percent = server::SystemInfo::GetInstance().CPUPercent();
    CPU_usage_percent_.Set(usage_percent);
}

void
PrometheusMetrics::RAMUsagePercentSet() {
S
starlord 已提交
78 79 80 81
    if (!startup_) {
        return;
    }

Y
yu yunfeng 已提交
82 83 84 85 86 87
    double usage_percent = server::SystemInfo::GetInstance().MemoryPercent();
    RAM_usage_percent_.Set(usage_percent);
}

void
PrometheusMetrics::GPUPercentGaugeSet() {
S
starlord 已提交
88 89 90 91
    if (!startup_) {
        return;
    }

K
kun yu 已提交
92
    int numDevice = server::SystemInfo::GetInstance().num_device();
S
starlord 已提交
93 94
    std::vector<uint64_t> used_total = server::SystemInfo::GetInstance().GPUMemoryTotal();
    std::vector<uint64_t> used_memory = server::SystemInfo::GetInstance().GPUMemoryUsed();
K
kun yu 已提交
95

K
kun yu 已提交
96
    for (int i = 0; i < numDevice; ++i) {
S
starlord 已提交
97 98
        prometheus::Gauge& GPU_percent = GPU_percent_.Add({{"DeviceNum", std::to_string(i)}});
        double percent = (double)used_memory[i] / (double)used_total[i];
K
kun yu 已提交
99
        GPU_percent.Set(percent * 100);
K
kun yu 已提交
100
    }
Y
yu yunfeng 已提交
101 102
}

S
starlord 已提交
103 104
void
PrometheusMetrics::GPUMemoryUsageGaugeSet() {
S
starlord 已提交
105 106 107 108
    if (!startup_) {
        return;
    }

S
starlord 已提交
109 110
    std::vector<uint64_t> values = server::SystemInfo::GetInstance().GPUMemoryUsed();
    constexpr uint64_t MtoB = 1024 * 1024;
K
kun yu 已提交
111
    int numDevice = server::SystemInfo::GetInstance().num_device();
Y
yu yunfeng 已提交
112

K
kun yu 已提交
113
    for (int i = 0; i < numDevice; ++i) {
S
starlord 已提交
114
        prometheus::Gauge& GPU_memory = GPU_memory_usage_.Add({{"DeviceNum", std::to_string(i)}});
K
kun yu 已提交
115 116
        GPU_memory.Set(values[i] / MtoB);
    }
Y
yu yunfeng 已提交
117 118
}

S
starlord 已提交
119 120 121
void
PrometheusMetrics::AddVectorsPerSecondGaugeSet(int num_vector, int dim, double time) {
    // MB/s
S
starlord 已提交
122 123 124
    if (!startup_) {
        return;
    }
Y
yu yunfeng 已提交
125

S
starlord 已提交
126 127 128
    int64_t MtoB = 1024 * 1024;
    int64_t size = num_vector * dim * 4;
    add_vectors_per_second_gauge_.Set(size / time / MtoB);
Y
yu yunfeng 已提交
129
}
S
starlord 已提交
130 131 132

void
PrometheusMetrics::QueryIndexTypePerSecondSet(std::string type, double value) {
S
starlord 已提交
133 134 135 136
    if (!startup_) {
        return;
    }

S
starlord 已提交
137
    if (type == "IVF") {
Y
yu yunfeng 已提交
138
        query_index_IVF_type_per_second_gauge_.Set(value);
S
starlord 已提交
139
    } else if (type == "IDMap") {
Y
yu yunfeng 已提交
140 141 142
        query_index_IDMAP_type_per_second_gauge_.Set(value);
    }
}
Y
yu yunfeng 已提交
143

S
starlord 已提交
144 145
void
PrometheusMetrics::ConnectionGaugeIncrement() {
S
starlord 已提交
146 147 148 149
    if (!startup_) {
        return;
    }

Y
yu yunfeng 已提交
150 151
    connection_gauge_.Increment();
}
Y
yu yunfeng 已提交
152

S
starlord 已提交
153 154
void
PrometheusMetrics::ConnectionGaugeDecrement() {
S
starlord 已提交
155 156 157 158
    if (!startup_) {
        return;
    }

Y
yu yunfeng 已提交
159 160 161
    connection_gauge_.Decrement();
}

S
starlord 已提交
162 163
void
PrometheusMetrics::OctetsSet() {
S
starlord 已提交
164 165 166
    if (!startup_) {
        return;
    }
Y
yu yunfeng 已提交
167 168

    // get old stats and reset them
S
starlord 已提交
169 170
    uint64_t old_inoctets = SystemInfo::GetInstance().get_inoctets();
    uint64_t old_outoctets = SystemInfo::GetInstance().get_octets();
Y
yu yunfeng 已提交
171
    auto old_time = SystemInfo::GetInstance().get_nettime();
S
starlord 已提交
172
    std::pair<uint64_t, uint64_t> in_and_out_octets = SystemInfo::GetInstance().Octets();
Y
yu yunfeng 已提交
173 174 175 176 177
    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 已提交
178
    constexpr double micro_to_second = 1e-6;
Y
yu yunfeng 已提交
179 180
    auto now_time = std::chrono::system_clock::now();
    auto total_microsecond = METRICS_MICROSECONDS(old_time, now_time);
S
starlord 已提交
181
    auto total_second = total_microsecond * micro_to_second;
S
starlord 已提交
182 183 184 185
    if (total_second == 0) {
        return;
    }

S
starlord 已提交
186 187
    inoctets_gauge_.Set((in_and_out_octets.first - old_inoctets) / total_second);
    outoctets_gauge_.Set((in_and_out_octets.second - old_outoctets) / total_second);
Y
yu yunfeng 已提交
188 189
}

S
starlord 已提交
190 191
void
PrometheusMetrics::CPUCoreUsagePercentSet() {
S
starlord 已提交
192 193 194
    if (!startup_) {
        return;
    }
K
kun yu 已提交
195 196 197

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

K
kun yu 已提交
198
    for (int i = 0; i < cpu_core_percent.size(); ++i) {
S
starlord 已提交
199
        prometheus::Gauge& core_percent = CPU_.Add({{"CPU", std::to_string(i)}});
K
kun yu 已提交
200 201 202
        core_percent.Set(cpu_core_percent[i]);
    }
}
Y
fix  
yu yunfeng 已提交
203

S
starlord 已提交
204 205
void
PrometheusMetrics::GPUTemperature() {
S
starlord 已提交
206 207 208
    if (!startup_) {
        return;
    }
K
kun yu 已提交
209

S
starlord 已提交
210
    std::vector<uint64_t> GPU_temperatures = server::SystemInfo::GetInstance().GPUTemperature();
K
kun yu 已提交
211 212

    for (int i = 0; i < GPU_temperatures.size(); ++i) {
S
starlord 已提交
213
        prometheus::Gauge& gpu_temp = GPU_temperature_.Add({{"GPU", std::to_string(i)}});
K
kun yu 已提交
214 215 216 217
        gpu_temp.Set(GPU_temperatures[i]);
    }
}

S
starlord 已提交
218 219
void
PrometheusMetrics::CPUTemperature() {
S
starlord 已提交
220 221 222
    if (!startup_) {
        return;
    }
K
kun yu 已提交
223 224 225 226

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

    for (int i = 0; i < CPU_temperatures.size(); ++i) {
S
starlord 已提交
227
        prometheus::Gauge& cpu_temp = CPU_temperature_.Add({{"CPU", std::to_string(i)}});
K
kun yu 已提交
228 229 230
        cpu_temp.Set(CPU_temperatures[i]);
    }
}
Y
yu yunfeng 已提交
231

S
starlord 已提交
232 233
void
PrometheusMetrics::GpuCacheUsageGaugeSet() {
S
starlord 已提交
234 235 236 237 238 239 240
    //    std::vector<uint64_t > gpu_ids = {0};
    //    for(auto i = 0; i < gpu_ids.size(); ++i) {
    //        uint64_t cache_usage = cache::GpuCacheMgr::GetInstance(gpu_ids[i])->CacheUsage();
    //        uint64_t cache_capacity = cache::GpuCacheMgr::GetInstance(gpu_ids[i])->CacheCapacity();
    //        prometheus::Gauge &gpu_cache = gpu_cache_usage_.Add({{"GPU_Cache", std::to_string(i)}});
    //        gpu_cache.Set(cache_usage * 100 / cache_capacity);
    //    }
Y
Yu Kun 已提交
241 242
}

S
starlord 已提交
243 244 245
}  // namespace server
}  // namespace milvus
}  // namespace zilliz