SystemInfo.cpp 9.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.

G
groot 已提交
18
#include "metrics/SystemInfo.h"
Y
yu yunfeng 已提交
19

G
groot 已提交
20
#include <nvml.h>
Y
yu yunfeng 已提交
21 22 23
#include <sys/types.h>
#include <unistd.h>
#include <fstream>
G
groot 已提交
24
#include <iostream>
G
groot 已提交
25 26
#include <string>
#include <utility>
Y
yu yunfeng 已提交
27 28

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

G
groot 已提交
32 33
void
SystemInfo::Init() {
G
groot 已提交
34 35
    if (initialized_)
        return;
Y
yu yunfeng 已提交
36 37 38

    initialized_ = true;

Y
yu yunfeng 已提交
39
    // initialize CPU information
G
groot 已提交
40
    FILE* file;
Y
yu yunfeng 已提交
41
    struct tms time_sample;
Y
yu yunfeng 已提交
42
    char line[128];
Y
yu yunfeng 已提交
43 44 45
    last_cpu_ = times(&time_sample);
    last_sys_cpu_ = time_sample.tms_stime;
    last_user_cpu_ = time_sample.tms_utime;
Y
yu yunfeng 已提交
46
    file = fopen("/proc/cpuinfo", "r");
Y
yu yunfeng 已提交
47
    num_processors_ = 0;
G
groot 已提交
48
    while (fgets(line, 128, file) != NULL) {
G
groot 已提交
49 50
        if (strncmp(line, "processor", 9) == 0)
            num_processors_++;
K
kun yu 已提交
51 52 53
        if (strncmp(line, "physical", 8) == 0) {
            num_physical_processors_ = ParseLine(line);
        }
Y
yu yunfeng 已提交
54
    }
Y
yu yunfeng 已提交
55
    total_ram_ = GetPhysicalMemory();
Y
yu yunfeng 已提交
56 57
    fclose(file);

G
groot 已提交
58
    // initialize GPU information
Y
yu yunfeng 已提交
59 60
    nvmlReturn_t nvmlresult;
    nvmlresult = nvmlInit();
G
groot 已提交
61
    if (NVML_SUCCESS != nvmlresult) {
Y
yu yunfeng 已提交
62
        printf("System information initilization failed");
G
groot 已提交
63
        return;
Y
yu yunfeng 已提交
64
    }
Y
yu yunfeng 已提交
65
    nvmlresult = nvmlDeviceGetCount(&num_device_);
G
groot 已提交
66
    if (NVML_SUCCESS != nvmlresult) {
Y
yu yunfeng 已提交
67
        printf("Unable to get devidce number");
G
groot 已提交
68
        return;
Y
yu yunfeng 已提交
69 70
    }

G
groot 已提交
71
    // initialize network traffic information
G
groot 已提交
72
    std::pair<uint64_t, uint64_t> in_and_out_octets = Octets();
Y
yu yunfeng 已提交
73 74 75
    in_octets_ = in_and_out_octets.first;
    out_octets_ = in_and_out_octets.second;
    net_time_ = std::chrono::system_clock::now();
Y
yu yunfeng 已提交
76 77
}

G
groot 已提交
78
uint64_t
G
groot 已提交
79
SystemInfo::ParseLine(char* line) {
Y
yu yunfeng 已提交
80 81
    // This assumes that a digit will be found and the line ends in " Kb".
    int i = strlen(line);
G
groot 已提交
82
    const char* p = line;
Y
yu yunfeng 已提交
83 84 85
    while (*p < '0' || *p > '9') p++;
    line[i - 3] = '\0';
    i = atoi(p);
G
groot 已提交
86
    return static_cast<uint64_t>(i);
Y
yu yunfeng 已提交
87 88
}

G
groot 已提交
89
uint64_t
Y
yu yunfeng 已提交
90 91
SystemInfo::GetPhysicalMemory() {
    struct sysinfo memInfo;
G
groot 已提交
92 93
    sysinfo(&memInfo);
    uint64_t totalPhysMem = memInfo.totalram;
G
groot 已提交
94
    // Multiply in next statement to avoid int overflow on right hand side...
Y
yu yunfeng 已提交
95 96 97 98
    totalPhysMem *= memInfo.mem_unit;
    return totalPhysMem;
}

G
groot 已提交
99
uint64_t
Y
yu yunfeng 已提交
100
SystemInfo::GetProcessUsedMemory() {
G
groot 已提交
101 102
    // Note: this value is in KB!
    FILE* file = fopen("/proc/self/status", "r");
G
groot 已提交
103 104 105
    constexpr uint64_t line_length = 128;
    uint64_t result = -1;
    constexpr uint64_t KB_SIZE = 1024;
Y
fix  
yu yunfeng 已提交
106
    char line[line_length];
Y
yu yunfeng 已提交
107

G
groot 已提交
108 109
    while (fgets(line, line_length, file) != NULL) {
        if (strncmp(line, "VmRSS:", 6) == 0) {
Y
fix  
yu yunfeng 已提交
110
            result = ParseLine(line);
Y
yu yunfeng 已提交
111 112 113 114 115
            break;
        }
    }
    fclose(file);
    // return value in Byte
G
groot 已提交
116
    return (result * KB_SIZE);
Y
yu yunfeng 已提交
117 118 119 120
}

double
SystemInfo::MemoryPercent() {
G
groot 已提交
121 122
    if (!initialized_)
        Init();
G
groot 已提交
123
    return (double)(GetProcessUsedMemory() * 100) / (double)total_ram_;
Y
yu yunfeng 已提交
124 125
}

K
kun yu 已提交
126 127
std::vector<double>
SystemInfo::CPUCorePercent() {
G
groot 已提交
128 129
    std::vector<uint64_t> prev_work_time_array;
    std::vector<uint64_t> prev_total_time_array = getTotalCpuTime(prev_work_time_array);
K
kun yu 已提交
130
    usleep(100000);
G
groot 已提交
131 132
    std::vector<uint64_t> cur_work_time_array;
    std::vector<uint64_t> cur_total_time_array = getTotalCpuTime(cur_work_time_array);
K
kun yu 已提交
133 134

    std::vector<double> cpu_core_percent;
K
kun yu 已提交
135
    for (int i = 1; i < num_processors_; i++) {
K
kun yu 已提交
136 137 138 139 140 141 142
        double total_cpu_time = cur_total_time_array[i] - prev_total_time_array[i];
        double cpu_work_time = cur_work_time_array[i] - prev_work_time_array[i];
        cpu_core_percent.push_back((cpu_work_time / total_cpu_time) * 100);
    }
    return cpu_core_percent;
}

G
groot 已提交
143
std::vector<uint64_t>
G
groot 已提交
144
SystemInfo::getTotalCpuTime(std::vector<uint64_t>& work_time_array) {
G
groot 已提交
145
    std::vector<uint64_t> total_time_array;
G
groot 已提交
146
    FILE* file = fopen("/proc/stat", "r");
K
kun yu 已提交
147 148 149 150 151
    if (file == NULL) {
        perror("Could not open stat file");
        return total_time_array;
    }

G
groot 已提交
152 153
    uint64_t user = 0, nice = 0, system = 0, idle = 0;
    uint64_t iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guestnice = 0;
K
kun yu 已提交
154 155 156

    for (int i = 0; i < num_processors_; i++) {
        char buffer[1024];
G
groot 已提交
157
        char* ret = fgets(buffer, sizeof(buffer) - 1, file);
K
kun yu 已提交
158 159 160 161 162 163
        if (ret == NULL) {
            perror("Could not read stat file");
            fclose(file);
            return total_time_array;
        }

G
groot 已提交
164 165
        sscanf(buffer, "cpu  %16lu %16lu %16lu %16lu %16lu %16lu %16lu %16lu %16lu %16lu", &user, &nice, &system, &idle,
               &iowait, &irq, &softirq, &steal, &guest, &guestnice);
K
kun yu 已提交
166 167 168 169 170 171 172 173 174

        work_time_array.push_back(user + nice + system);
        total_time_array.push_back(user + nice + system + idle + iowait + irq + softirq + steal);
    }

    fclose(file);
    return total_time_array;
}

Y
yu yunfeng 已提交
175 176
double
SystemInfo::CPUPercent() {
G
groot 已提交
177 178
    if (!initialized_)
        Init();
Y
yu yunfeng 已提交
179
    struct tms time_sample;
Y
yu yunfeng 已提交
180 181 182
    clock_t now;
    double percent;

Y
yu yunfeng 已提交
183
    now = times(&time_sample);
G
groot 已提交
184 185
    if (now <= last_cpu_ || time_sample.tms_stime < last_sys_cpu_ || time_sample.tms_utime < last_user_cpu_) {
        // Overflow detection. Just skip this value.
Y
yu yunfeng 已提交
186
        percent = -1.0;
G
groot 已提交
187
    } else {
G
groot 已提交
188
        percent = (time_sample.tms_stime - last_sys_cpu_) + (time_sample.tms_utime - last_user_cpu_);
Y
yu yunfeng 已提交
189
        percent /= (now - last_cpu_);
Y
yu yunfeng 已提交
190 191
        percent *= 100;
    }
Y
yu yunfeng 已提交
192 193 194
    last_cpu_ = now;
    last_sys_cpu_ = time_sample.tms_stime;
    last_user_cpu_ = time_sample.tms_utime;
Y
yu yunfeng 已提交
195 196 197 198

    return percent;
}

G
groot 已提交
199
std::vector<uint64_t>
K
kun yu 已提交
200
SystemInfo::GPUMemoryTotal() {
Y
yu yunfeng 已提交
201
    // get GPU usage percent
G
groot 已提交
202 203
    if (!initialized_)
        Init();
G
groot 已提交
204
    std::vector<uint64_t> result;
K
kun yu 已提交
205
    nvmlMemory_t nvmlMemory;
Y
yu yunfeng 已提交
206
    for (int i = 0; i < num_device_; ++i) {
Y
yu yunfeng 已提交
207 208
        nvmlDevice_t device;
        nvmlDeviceGetHandleByIndex(i, &device);
K
kun yu 已提交
209 210
        nvmlDeviceGetMemoryInfo(device, &nvmlMemory);
        result.push_back(nvmlMemory.total);
Y
yu yunfeng 已提交
211 212 213 214
    }
    return result;
}

G
groot 已提交
215 216
std::vector<uint64_t>
SystemInfo::GPUTemperature() {
G
groot 已提交
217 218
    if (!initialized_)
        Init();
G
groot 已提交
219
    std::vector<uint64_t> result;
K
kun yu 已提交
220 221 222 223
    for (int i = 0; i < num_device_; i++) {
        nvmlDevice_t device;
        nvmlDeviceGetHandleByIndex(i, &device);
        unsigned int temp;
G
groot 已提交
224
        nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temp);
K
kun yu 已提交
225 226 227 228
        result.push_back(temp);
    }
    return result;
}
G
groot 已提交
229

K
kun yu 已提交
230
std::vector<float>
G
groot 已提交
231
SystemInfo::CPUTemperature() {
K
kun yu 已提交
232 233 234
    std::vector<float> result;
    for (int i = 0; i <= num_physical_processors_; ++i) {
        std::string path = "/sys/class/thermal/thermal_zone" + std::to_string(i) + "/temp";
G
groot 已提交
235
        FILE* file = fopen(path.data(), "r");
K
kun yu 已提交
236 237 238 239 240 241 242
        if (file == NULL) {
            perror("Could not open thermal file");
            return result;
        }
        float temp;
        fscanf(file, "%f", &temp);
        result.push_back(temp / 1000);
Y
Yu Kun 已提交
243
        fclose(file);
K
kun yu 已提交
244 245 246
    }
}

G
groot 已提交
247
std::vector<uint64_t>
Y
yu yunfeng 已提交
248 249
SystemInfo::GPUMemoryUsed() {
    // get GPU memory used
G
groot 已提交
250 251
    if (!initialized_)
        Init();
Y
yu yunfeng 已提交
252

G
groot 已提交
253
    std::vector<uint64_t> result;
Y
yu yunfeng 已提交
254
    nvmlMemory_t nvmlMemory;
Y
yu yunfeng 已提交
255
    for (int i = 0; i < num_device_; ++i) {
Y
yu yunfeng 已提交
256 257 258 259 260 261 262 263
        nvmlDevice_t device;
        nvmlDeviceGetHandleByIndex(i, &device);
        nvmlDeviceGetMemoryInfo(device, &nvmlMemory);
        result.push_back(nvmlMemory.used);
    }
    return result;
}

G
groot 已提交
264 265
std::pair<uint64_t, uint64_t>
SystemInfo::Octets() {
Y
yu yunfeng 已提交
266
    pid_t pid = getpid();
G
groot 已提交
267
    //    const std::string filename = "/proc/"+std::to_string(pid)+"/net/netstat";
Y
yu yunfeng 已提交
268 269 270 271
    const std::string filename = "/proc/net/netstat";
    std::ifstream file(filename);
    std::string lastline = "";
    std::string line = "";
G
groot 已提交
272
    while (file) {
Y
yu yunfeng 已提交
273
        getline(file, line);
G
groot 已提交
274
        if (file.fail()) {
Y
yu yunfeng 已提交
275 276 277 278 279 280
            break;
        }
        lastline = line;
    }
    std::vector<size_t> space_position;
    size_t space_pos = lastline.find(" ");
G
groot 已提交
281
    while (space_pos != std::string::npos) {
Y
yu yunfeng 已提交
282
        space_position.push_back(space_pos);
G
groot 已提交
283
        space_pos = lastline.find(" ", space_pos + 1);
Y
yu yunfeng 已提交
284 285
    }
    // InOctets is between 6th and 7th " " and OutOctets is between 7th and 8th " "
G
groot 已提交
286 287 288 289 290 291 292 293 294 295
    size_t inoctets_begin = space_position[6] + 1;
    size_t inoctets_length = space_position[7] - inoctets_begin;
    size_t outoctets_begin = space_position[7] + 1;
    size_t outoctets_length = space_position[8] - outoctets_begin;
    std::string inoctets = lastline.substr(inoctets_begin, inoctets_length);
    std::string outoctets = lastline.substr(outoctets_begin, outoctets_length);

    uint64_t inoctets_bytes = std::stoull(inoctets);
    uint64_t outoctets_bytes = std::stoull(outoctets);
    std::pair<uint64_t, uint64_t> res(inoctets_bytes, outoctets_bytes);
Y
yu yunfeng 已提交
296 297 298
    return res;
}

G
groot 已提交
299 300 301
}  // namespace server
}  // namespace milvus
}  // namespace zilliz