From c55a70dc9423fa1a32deec51def6ad020d468741 Mon Sep 17 00:00:00 2001 From: yu yunfeng Date: Mon, 10 Jun 2019 10:33:24 +0800 Subject: [PATCH] fix codestyle Former-commit-id: 10caa1a57e07115fd3eab3f3a23eaebda6b8ecf2 --- cpp/src/metrics/PrometheusMetrics.cpp | 1 + cpp/src/metrics/SystemInfo.cpp | 66 ++++++++++++--------------- cpp/src/metrics/SystemInfo.h | 14 +++--- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/cpp/src/metrics/PrometheusMetrics.cpp b/cpp/src/metrics/PrometheusMetrics.cpp index 39462e80ea..ae45a22fea 100644 --- a/cpp/src/metrics/PrometheusMetrics.cpp +++ b/cpp/src/metrics/PrometheusMetrics.cpp @@ -87,6 +87,7 @@ void PrometheusMetrics::AddVectorsPerSecondGaugeSet(int num_vector, int dim, dou } void PrometheusMetrics::QueryIndexTypePerSecondSet(std::string type, double value) { + if(!startup_) return; if(type == "IVF"){ query_index_IVF_type_per_second_gauge_.Set(value); } else if(type == "IDMap"){ diff --git a/cpp/src/metrics/SystemInfo.cpp b/cpp/src/metrics/SystemInfo.cpp index 24c6688786..a90e206a93 100644 --- a/cpp/src/metrics/SystemInfo.cpp +++ b/cpp/src/metrics/SystemInfo.cpp @@ -21,23 +21,23 @@ namespace vecwise { namespace server { void SystemInfo::Init() { - if(initialized) return; -// mutex.lock(); - initialized = true; -// mutex.unlock(); + if(initialized_) return; + + initialized_ = true; + // initialize CPU information FILE* file; - struct tms timeSample; + struct tms time_sample; char line[128]; - lastCPU_ = times(&timeSample); - lastSysCPU_ = timeSample.tms_stime; - lastUserCPU_ = timeSample.tms_utime; + last_cpu_ = times(&time_sample); + last_sys_cpu_ = time_sample.tms_stime; + last_user_cpu_ = time_sample.tms_utime; file = fopen("/proc/cpuinfo", "r"); - numProcessors = 0; + num_processors_ = 0; while(fgets(line, 128, file) != NULL){ - if (strncmp(line, "processor", 9) == 0) numProcessors++; + if (strncmp(line, "processor", 9) == 0) num_processors_++; } - total_RAM_ = GetPhysicalMemory(); + total_ram_ = GetPhysicalMemory(); fclose(file); //initialize GPU information @@ -47,7 +47,7 @@ void SystemInfo::Init() { printf("System information initilization failed"); return ; } - nvmlresult = nvmlDeviceGetCount(&numDevice); + nvmlresult = nvmlDeviceGetCount(&num_device_); if(NVML_SUCCESS != nvmlresult) { printf("Unable to get devidce number"); return ; @@ -90,7 +90,6 @@ SystemInfo::GetProcessUsedMemory() { } } fclose(file); -// printf("RAM is %d",result); // return value in Byte return (result*1024); @@ -98,33 +97,33 @@ SystemInfo::GetProcessUsedMemory() { double SystemInfo::MemoryPercent() { - if (!initialized) Init(); - return GetProcessUsedMemory()*100/total_RAM_; + if (!initialized_) Init(); + return GetProcessUsedMemory()*100/total_ram_; } double SystemInfo::CPUPercent() { - if (!initialized) Init(); - struct tms timeSample; + if (!initialized_) Init(); + struct tms time_sample; clock_t now; double percent; - now = times(&timeSample); - if (now <= lastCPU_ || timeSample.tms_stime < lastSysCPU_ || - timeSample.tms_utime < lastUserCPU_){ + now = times(&time_sample); + if (now <= last_cpu_ || time_sample.tms_stime < last_sys_cpu_ || + time_sample.tms_utime < last_user_cpu_){ //Overflow detection. Just skip this value. percent = -1.0; } else{ - percent = (timeSample.tms_stime - lastSysCPU_) + - (timeSample.tms_utime - lastUserCPU_); - percent /= (now - lastCPU_); - percent /= numProcessors; + percent = (time_sample.tms_stime - last_sys_cpu_) + + (time_sample.tms_utime - last_user_cpu_); + percent /= (now - last_cpu_); + percent /= num_processors_; percent *= 100; } - lastCPU_ = now; - lastSysCPU_ = timeSample.tms_stime; - lastUserCPU_ = timeSample.tms_utime; + last_cpu_ = now; + last_sys_cpu_ = time_sample.tms_stime; + last_user_cpu_ = time_sample.tms_utime; return percent; } @@ -173,31 +172,26 @@ SystemInfo::split(std::string input) { std::vector SystemInfo::GPUPercent() { // get GPU usage percent - if(!initialized) Init(); + if(!initialized_) Init(); std::vector result; nvmlUtilization_t utilization; - for (int i = 0; i < numDevice; ++i) { + for (int i = 0; i < num_device_; ++i) { nvmlDevice_t device; nvmlDeviceGetHandleByIndex(i, &device); nvmlDeviceGetUtilizationRates(device, &utilization); result.push_back(utilization.gpu); } return result; -// nvmlDevice_t device; -// nvmlUtilization_t utilization; -// nvmlDeviceGetHandleByIndex(device_index, &device); -// nvmlDeviceGetUtilizationRates(device, &utilization); -// return utilization.gpu; } std::vector SystemInfo::GPUMemoryUsed() { // get GPU memory used - if(!initialized) Init(); + if(!initialized_) Init(); std::vector result; nvmlMemory_t nvmlMemory; - for (int i = 0; i < numDevice; ++i) { + for (int i = 0; i < num_device_; ++i) { nvmlDevice_t device; nvmlDeviceGetHandleByIndex(i, &device); nvmlDeviceGetMemoryInfo(device, &nvmlMemory); diff --git a/cpp/src/metrics/SystemInfo.h b/cpp/src/metrics/SystemInfo.h index 8d2eeb131e..d173f4f3af 100644 --- a/cpp/src/metrics/SystemInfo.h +++ b/cpp/src/metrics/SystemInfo.h @@ -25,12 +25,14 @@ namespace server { class SystemInfo { private: - unsigned long total_RAM_ ; - clock_t lastCPU_, lastSysCPU_, lastUserCPU_; - int numProcessors; + unsigned long total_ram_ = 0; + clock_t last_cpu_ = clock_t(); + clock_t last_sys_cpu_ = clock_t(); + clock_t last_user_cpu_ = clock_t(); + int num_processors_ = 0; //number of GPU - unsigned int numDevice; - bool initialized = false; + unsigned int num_device_ = 0; + bool initialized_ = false; public: static SystemInfo & @@ -40,7 +42,7 @@ class SystemInfo { } void Init(); - int NumDevice() {return numDevice;}; + int NumDevice() {return num_device_;}; long long parseLine(char* line); unsigned long GetPhysicalMemory(); unsigned long GetProcessUsedMemory();