SystemInfo.h 2.9 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.

Y
yu yunfeng 已提交
18 19
#pragma once

S
starlord 已提交
20 21 22
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
S
starlord 已提交
23 24
#include <sys/sysinfo.h>
#include <sys/times.h>
S
starlord 已提交
25
#include <sys/types.h>
S
starlord 已提交
26
#include <sys/vtimes.h>
Y
yu yunfeng 已提交
27
#include <chrono>
Y
yu yunfeng 已提交
28
#include <unordered_map>
S
starlord 已提交
29
#include <utility>
S
starlord 已提交
30
#include <vector>
Y
yu yunfeng 已提交
31

J
jinhai 已提交
32
namespace milvus {
Y
yu yunfeng 已提交
33 34 35 36
namespace server {

class SystemInfo {
 private:
S
starlord 已提交
37
    uint64_t total_ram_ = 0;
Y
yu yunfeng 已提交
38 39 40
    clock_t last_cpu_ = clock_t();
    clock_t last_sys_cpu_ = clock_t();
    clock_t last_user_cpu_ = clock_t();
Y
yu yunfeng 已提交
41
    std::chrono::system_clock::time_point net_time_ = std::chrono::system_clock::now();
Y
yu yunfeng 已提交
42
    int num_processors_ = 0;
K
kun yu 已提交
43
    int num_physical_processors_ = 0;
S
starlord 已提交
44
    // number of GPU
S
starlord 已提交
45 46 47
    uint32_t num_device_ = 0;
    uint64_t in_octets_ = 0;
    uint64_t out_octets_ = 0;
Y
yu yunfeng 已提交
48
    bool initialized_ = false;
Y
yu yunfeng 已提交
49 50

 public:
S
starlord 已提交
51
    static SystemInfo&
S
starlord 已提交
52
    GetInstance() {
Y
yu yunfeng 已提交
53 54 55 56
        static SystemInfo instance;
        return instance;
    }

S
starlord 已提交
57 58
    void
    Init();
S
starlord 已提交
59

S
starlord 已提交
60 61
    int
    num_processor() const {
S
starlord 已提交
62 63 64
        return num_processors_;
    }

S
starlord 已提交
65 66
    int
    num_physical_processors() const {
S
starlord 已提交
67 68 69
        return num_physical_processors_;
    }

S
starlord 已提交
70 71
    uint32_t
    num_device() const {
S
starlord 已提交
72 73 74
        return num_device_;
    }

S
starlord 已提交
75 76
    uint64_t
    get_inoctets() {
S
starlord 已提交
77 78 79
        return in_octets_;
    }

S
starlord 已提交
80 81
    uint64_t
    get_octets() {
S
starlord 已提交
82 83 84
        return out_octets_;
    }

S
starlord 已提交
85 86
    std::chrono::system_clock::time_point
    get_nettime() {
S
starlord 已提交
87 88 89
        return net_time_;
    }

S
starlord 已提交
90 91
    void
    set_inoctets(uint64_t value) {
S
starlord 已提交
92 93 94
        in_octets_ = value;
    }

S
starlord 已提交
95 96
    void
    set_outoctets(uint64_t value) {
S
starlord 已提交
97 98 99
        out_octets_ = value;
    }

S
starlord 已提交
100 101
    void
    set_nettime() {
S
starlord 已提交
102 103 104
        net_time_ = std::chrono::system_clock::now();
    }

S
starlord 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    uint64_t
    ParseLine(char* line);
    uint64_t
    GetPhysicalMemory();
    uint64_t
    GetProcessUsedMemory();
    double
    MemoryPercent();
    double
    CPUPercent();
    std::pair<uint64_t, uint64_t>
    Octets();
    std::vector<uint64_t>
    GPUMemoryTotal();
    std::vector<uint64_t>
    GPUMemoryUsed();

    std::vector<double>
    CPUCorePercent();
    std::vector<uint64_t>
    getTotalCpuTime(std::vector<uint64_t>& workTime);
    std::vector<uint64_t>
    GPUTemperature();
    std::vector<float>
    CPUTemperature();
Y
yu yunfeng 已提交
130 131
};

S
starlord 已提交
132 133
}  // namespace server
}  // namespace milvus