From 2a0ade6c62f93ca344c4e275d57d07eb87786554 Mon Sep 17 00:00:00 2001 From: David Levanon Date: Thu, 12 May 2022 19:36:11 +0300 Subject: [PATCH] measure cpu of current pid instead of globaly on the system --- agent/go.mod | 1 + agent/go.sum | 2 ++ tap/go.mod | 1 + tap/go.sum | 2 ++ tap/passive_tapper.go | 21 +++++++++++++-------- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/agent/go.mod b/agent/go.mod index 9988b6a6a..d51900a64 100644 --- a/agent/go.mod +++ b/agent/go.mod @@ -107,6 +107,7 @@ require ( github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/spf13/cobra v1.3.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/struCoder/pidusage v0.2.1 // indirect github.com/tidwall/gjson v1.14.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect diff --git a/agent/go.sum b/agent/go.sum index 9408421ad..90849a01b 100644 --- a/agent/go.sum +++ b/agent/go.sum @@ -668,6 +668,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5 github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/struCoder/pidusage v0.2.1 h1:dFiEgUDkubeIj0XA1NpQ6+8LQmKrLi7NiIQl86E6BoY= +github.com/struCoder/pidusage v0.2.1/go.mod h1:bewtP2KUA1TBUyza5+/PCpSQ6sc/H6jJbIKAzqW86BA= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tidwall/gjson v1.10.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.12.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= diff --git a/tap/go.mod b/tap/go.mod index f86e024d9..b8ec83594 100644 --- a/tap/go.mod +++ b/tap/go.mod @@ -25,6 +25,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect + github.com/struCoder/pidusage v0.2.1 // indirect github.com/tklauser/go-sysconf v0.3.10 // indirect github.com/tklauser/numcpus v0.4.0 // indirect github.com/yusufpapurcu/wmi v1.2.2 // indirect diff --git a/tap/go.sum b/tap/go.sum index 781c9bf86..4deeca6cd 100644 --- a/tap/go.sum +++ b/tap/go.sum @@ -130,6 +130,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/struCoder/pidusage v0.2.1 h1:dFiEgUDkubeIj0XA1NpQ6+8LQmKrLi7NiIQl86E6BoY= +github.com/struCoder/pidusage v0.2.1/go.mod h1:bewtP2KUA1TBUyza5+/PCpSQ6sc/H6jJbIKAzqW86BA= github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03OUqALw= github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk= github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o= diff --git a/tap/passive_tapper.go b/tap/passive_tapper.go index b19ab46fb..77871105b 100644 --- a/tap/passive_tapper.go +++ b/tap/passive_tapper.go @@ -18,6 +18,7 @@ import ( "time" "github.com/shirou/gopsutil/cpu" + "github.com/struCoder/pidusage" "github.com/up9inc/mizu/logger" "github.com/up9inc/mizu/tap/api" "github.com/up9inc/mizu/tap/diagnose" @@ -140,23 +141,27 @@ func printPeriodicStats(cleaner *Cleaner) { // At this moment memStats := runtime.MemStats{} runtime.ReadMemStats(&memStats) - cpuPercent, err := cpu.Percent(0, false) + + sysInfo, err := pidusage.GetStat(os.Getpid()) if err != nil { - logger.Log.Errorf("error getting cpu usage: %v", err) - cpuPercent = []float64{} + sysInfo = &pidusage.SysInfo{ + CPU: -1, + Memory: -1, + } } - numCores, _ := cpu.Counts(true) + + numCores, err := cpu.Counts(true) if err != nil { - logger.Log.Errorf("error getting cores count: %v", err) numCores = -1 } + logger.Log.Infof( - "mem: %d, goroutines: %d, cpu: %v, cores: %d", + "mem: %d, goroutines: %d, cpu: %f, cores: %d, rss: %f", memStats.HeapAlloc, runtime.NumGoroutine(), - cpuPercent, + sysInfo.CPU, numCores, - ) + sysInfo.Memory) // Since the last print cleanStats := cleaner.dumpStats()