diff --git a/cpp/src/db/DBImpl.cpp b/cpp/src/db/DBImpl.cpp index 068e2a8d24..050cd4a87b 100644 --- a/cpp/src/db/DBImpl.cpp +++ b/cpp/src/db/DBImpl.cpp @@ -323,6 +323,7 @@ void DBImpl::BackgroundTimerTask(int interval) { server::Metrics::GetInstance().RAMUsagePercentSet(); server::Metrics::GetInstance().GPUPercentGaugeSet(); server::Metrics::GetInstance().GPUMemoryUsageGaugeSet(); + server::Metrics::GetInstance().OctetsSet(); TrySchedule(); } } diff --git a/cpp/src/metrics/MetricBase.h b/cpp/src/metrics/MetricBase.h index 96dcf22ed6..8c6602d05c 100644 --- a/cpp/src/metrics/MetricBase.h +++ b/cpp/src/metrics/MetricBase.h @@ -82,6 +82,7 @@ class MetricsBase{ virtual void ConnectionGaugeIncrement() {}; virtual void ConnectionGaugeDecrement() {}; virtual void KeepingAliveCounterIncrement(double value = 1) {}; + virtual void OctetsSet() {}; }; diff --git a/cpp/src/metrics/PrometheusMetrics.cpp b/cpp/src/metrics/PrometheusMetrics.cpp index 8672817428..03c923eebe 100644 --- a/cpp/src/metrics/PrometheusMetrics.cpp +++ b/cpp/src/metrics/PrometheusMetrics.cpp @@ -33,6 +33,8 @@ PrometheusMetrics::Init() { return SERVER_UNEXPECTED_ERROR; } + // + return SERVER_SUCCESS; } @@ -110,15 +112,39 @@ void PrometheusMetrics::QueryIndexTypePerSecondSet(std::string type, double valu } } + void PrometheusMetrics::ConnectionGaugeIncrement() { if(!startup_) return; connection_gauge_.Increment(); } + void PrometheusMetrics::ConnectionGaugeDecrement() { if(!startup_) return; connection_gauge_.Decrement(); } +void PrometheusMetrics::OctetsSet() { + if(!startup_) return; + + // get old stats and reset them + std::pair 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(); + + // + constexpr int micro_to_second = 1e-6; + auto now_time = std::chrono::system_clock::now(); + 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(); + 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); +} + //void PrometheusMetrics::GpuPercentInit() { // int num_device = SystemInfo::GetInstance().num_device(); // constexpr char device_number[] = "DeviceNum"; diff --git a/cpp/src/metrics/PrometheusMetrics.h b/cpp/src/metrics/PrometheusMetrics.h index fc2bef6f60..1cf487731e 100644 --- a/cpp/src/metrics/PrometheusMetrics.h +++ b/cpp/src/metrics/PrometheusMetrics.h @@ -116,6 +116,7 @@ class PrometheusMetrics: public MetricsBase { void ConnectionGaugeIncrement() override ; void ConnectionGaugeDecrement() override ; void KeepingAliveCounterIncrement(double value = 1) override {if(startup_) keeping_alive_counter_.Increment(value);}; + void OctetsSet() override ; @@ -480,6 +481,13 @@ class PrometheusMetrics: public MetricsBase { .Register(*registry_); prometheus::Counter &keeping_alive_counter_ = keeping_alive_.Add({}); + prometheus::Family &octets_ = prometheus::BuildGauge() + .Name("octets_bytes_per_second") + .Help("octets bytes per second") + .Register(*registry_); + prometheus::Gauge &inoctets_gauge_ = octets_.Add({{"type", "inoctets"}}); + prometheus::Gauge &outoctets_gauge_ = octets_.Add({{"type", "outoctets"}}); + }; diff --git a/cpp/src/metrics/SystemInfo.cpp b/cpp/src/metrics/SystemInfo.cpp index 210817f856..4f223bd89e 100644 --- a/cpp/src/metrics/SystemInfo.cpp +++ b/cpp/src/metrics/SystemInfo.cpp @@ -53,6 +53,11 @@ void SystemInfo::Init() { return ; } + //initialize network traffic information + std::pair in_and_out_octets = Octets(); + in_octets_ = in_and_out_octets.first; + out_octets_ = in_and_out_octets.second; + net_time_ = std::chrono::system_clock::now(); } long long @@ -202,6 +207,42 @@ SystemInfo::GPUMemoryUsed() { return result; } +std::pair +SystemInfo::Octets(){ + pid_t pid = getpid(); +// const std::string filename = "/proc/"+std::to_string(pid)+"/net/netstat"; + const std::string filename = "/proc/net/netstat"; + std::ifstream file(filename); + std::string lastline = ""; + std::string line = ""; + while(file){ + getline(file, line); + if(file.fail()){ + break; + } + lastline = line; + } + std::vector space_position; + size_t space_pos = lastline.find(" "); + while(space_pos != std::string::npos){ + space_position.push_back(space_pos); + space_pos = lastline.find(" ",space_pos+1); + } + // InOctets is between 6th and 7th " " and OutOctets is between 7th and 8th " " + 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); + + + unsigned long long inoctets_bytes = std::stoull(inoctets); + unsigned long long outoctets_bytes = std::stoull(outoctets); + std::pair res(inoctets_bytes, outoctets_bytes); + return res; +} + } } } \ No newline at end of file diff --git a/cpp/src/metrics/SystemInfo.h b/cpp/src/metrics/SystemInfo.h index 042358c3df..a8a8263ffb 100644 --- a/cpp/src/metrics/SystemInfo.h +++ b/cpp/src/metrics/SystemInfo.h @@ -13,6 +13,7 @@ #include "string.h" #include "sys/times.h" #include "sys/vtimes.h" +#include #include #include @@ -29,9 +30,12 @@ class SystemInfo { clock_t last_cpu_ = clock_t(); clock_t last_sys_cpu_ = clock_t(); clock_t last_user_cpu_ = clock_t(); + std::chrono::system_clock::time_point net_time_ = std::chrono::system_clock::now(); int num_processors_ = 0; //number of GPU unsigned int num_device_ = 0; + unsigned long long in_octets_ = 0; + unsigned long long out_octets_ = 0; bool initialized_ = false; public: @@ -43,11 +47,18 @@ class SystemInfo { void Init(); int num_device() const {return num_device_;}; + unsigned long long get_inoctets() { return in_octets_;}; + unsigned long long get_octets() { return out_octets_;}; + std::chrono::system_clock::time_point get_nettime() { return net_time_;}; + void set_inoctets(unsigned long long value) { in_octets_ = value;}; + void set_outoctets(unsigned long long value) { out_octets_ = value;}; + void set_nettime() {net_time_ = std::chrono::system_clock::now();}; long long ParseLine(char* line); unsigned long GetPhysicalMemory(); unsigned long GetProcessUsedMemory(); double MemoryPercent(); double CPUPercent(); + std::pair Octets(); // std::unordered_map> GetGPUMemPercent() {}; // std::vector split(std::string input) {}; std::vector GPUPercent(); diff --git a/cpp/src/server/Server.cpp b/cpp/src/server/Server.cpp index fa093c1bca..ef3a90b09e 100644 --- a/cpp/src/server/Server.cpp +++ b/cpp/src/server/Server.cpp @@ -16,7 +16,7 @@ #include #include #include -#include +//#include #include #include diff --git a/cpp/unittest/metrics/metrics_test.cpp b/cpp/unittest/metrics/metrics_test.cpp index f6d7f02feb..f4d896cb12 100644 --- a/cpp/unittest/metrics/metrics_test.cpp +++ b/cpp/unittest/metrics/metrics_test.cpp @@ -27,12 +27,13 @@ using namespace zilliz::vecwise; TEST_F(DBTest, Metric_Tes) { - + server::SystemInfo::GetInstance().Init(); // server::Metrics::GetInstance().Init(); // server::Metrics::GetInstance().exposer_ptr()->RegisterCollectable(server::Metrics::GetInstance().registry_ptr()); server::Metrics::GetInstance().Init(); + // server::PrometheusMetrics::GetInstance().exposer_ptr()->RegisterCollectable(server::PrometheusMetrics::GetInstance().registry_ptr()); - zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->SetCapacity(2UL*1024*1024*1024); + zilliz::vecwise::cache::CpuCacheMgr::GetInstance()->SetCapacity(4UL*1024*1024*1024); std::cout<CacheCapacity()<