From 920ef14a23e60dbdd0bcb43fe289007bcb36b01a Mon Sep 17 00:00:00 2001 From: Hanish Gogada <2924108@FVFG70VQQ05N.local> Date: Sun, 5 Jun 2022 21:21:26 +0200 Subject: [PATCH 1/5] Initial commit for cpu stats --- go.mod | 9 ++- go.sum | 22 ++++-- metrics/replicacpumem.go | 69 +++++++++++++++++++ metrics/types/types.pb.go | 139 +++++++++++++++++++++++++++++++++----- metrics/types/types.proto | 8 +++ 5 files changed, 226 insertions(+), 21 deletions(-) create mode 100644 metrics/replicacpumem.go diff --git a/go.mod b/go.mod index 68b767675..0a92742a1 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/relab/gorums v0.7.0 github.com/relab/iago v0.0.0-20220416090249-bf984205c7a8 github.com/relab/wrfs v0.0.0-20220416082020-a641cd350078 + github.com/shirou/gopsutil/v3 v3.22.5 github.com/spf13/cobra v1.4.0 github.com/spf13/viper v1.11.0 go-hep.org/x/hep v0.31.1 @@ -38,12 +39,13 @@ require ( github.com/fsnotify/fsnotify v1.5.3 // indirect github.com/go-fonts/liberation v0.2.0 // indirect github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-pdf/fpdf v0.6.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/gonuts/binary v0.2.0 // indirect - github.com/google/go-cmp v0.5.7 // indirect + github.com/google/go-cmp v0.5.8 // indirect github.com/google/pprof v0.0.0-20220412212628-83db2b799d1f // indirect github.com/gorilla/mux v1.7.3 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -51,6 +53,7 @@ require ( github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/kr/fs v0.1.0 // indirect github.com/kr/text v0.2.0 // indirect + github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/magiconair/properties v1.8.6 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect @@ -61,12 +64,16 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pkg/sftp v1.13.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/spf13/afero v1.8.2 // indirect github.com/spf13/cast v1.4.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.2.0 // 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 go.uber.org/atomic v1.9.0 // indirect go.uber.org/goleak v1.1.12 // indirect golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect diff --git a/go.sum b/go.sum index 72ec351a1..e59c721df 100644 --- a/go.sum +++ b/go.sum @@ -321,6 +321,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= +github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-openapi/jsonpointer v0.19.2/go.mod h1:3akKfEdA7DF1sugOqz1dVQHBcuDBPKZGEoHC/NkiQRg= github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonreference v0.19.2/go.mod h1:jMjeRr2HHw6nAVajTXJ4eiUwohSTlpa0o73RUL1owJc= @@ -395,8 +397,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= -github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.1.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -496,6 +498,8 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4= +github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= @@ -604,6 +608,8 @@ github.com/pkg/sftp v1.13.4 h1:Lb0RYJCmgUcBgZosfoi9Y9sbl6+LJgOIgk/2Y4YjMFg= github.com/pkg/sftp v1.13.4/go.mod h1:LzqnAvaD5TWeNBsZpfKxSYn1MbjWwOsCIAFFJbpIsK8= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw= +github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA= github.com/prometheus/client_golang v0.0.0-20180209125602-c332b6f63c06/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= @@ -650,6 +656,8 @@ github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZ github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo= +github.com/shirou/gopsutil/v3 v3.22.5 h1:atX36I/IXgFiB81687vSiBI5zrMsxcIBkP9cQMJQoJA= +github.com/shirou/gopsutil/v3 v3.22.5/go.mod h1:so9G9VzeHt/hsd0YwqprnjHnfARAUktauykSbr+y2gA= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.0.4-0.20170822132746-89742aefa4b2/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc= @@ -708,6 +716,10 @@ github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I= +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= +github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= @@ -734,6 +746,8 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= +github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs= github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA= github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg= @@ -968,6 +982,7 @@ golang.org/x/sys v0.0.0-20201117170446-d9b008d0a637/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201202213521-69691e467435/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -983,6 +998,7 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -1002,8 +1018,6 @@ golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs= -golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20220411224347-583f2d630306 h1:+gHMid33q6pen7kv9xvT+JRinntgeXO2AeZVd0AWD3w= golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/metrics/replicacpumem.go b/metrics/replicacpumem.go new file mode 100644 index 000000000..2898a5e99 --- /dev/null +++ b/metrics/replicacpumem.go @@ -0,0 +1,69 @@ +package metrics + +import ( + "time" + + "github.com/relab/hotstuff/metrics/types" + "github.com/relab/hotstuff/modules" + "github.com/shirou/gopsutil/v3/cpu" + "github.com/shirou/gopsutil/v3/mem" +) + +func init() { + RegisterReplicaMetric("cpumem", func() interface{} { + return &CPUMemStat{} + }) + RegisterClientMetric("cpumem", func() interface{} { + return &CPUMemStat{} + }) +} + +// CPUMemStat measures CPU usage and Memory usage and record in the metric logs. +type CPUMemStat struct { + mods *modules.Modules +} + +// InitModule gives the module access to the other modules. +func (c *CPUMemStat) InitModule(mods *modules.Modules) { + c.mods = mods + c.mods.EventLoop().RegisterObserver(types.TickEvent{}, func(event interface{}) { + c.tick(event.(types.TickEvent)) + }) + c.mods.Logger().Info("CPU-Memory stats metric enabled") + cpu.Percent(0, false) +} + +func (c *CPUMemStat) getCPUsage() (float64, uint32) { + cores, err := cpu.Counts(false) + if err != nil { + return 0, 0 + } + usage, err := cpu.Percent(0, false) + if err != nil { + return 0, uint32(cores) + } else { + return usage[0], uint32(cores) + } +} + +func (c *CPUMemStat) getMemoryPercentage() (uint64, float64) { + v, err := mem.VirtualMemory() + if err != nil { + return 0, 0 + } + return v.Available, v.UsedPercent +} + +func (c *CPUMemStat) tick(tick types.TickEvent) { + now := time.Now() + cpusage, cores := c.getCPUsage() + availablemem, memusage := c.getMemoryPercentage() + event := &types.CPUMemoryStats{ + Event: types.NewReplicaEvent(uint32(c.mods.ID()), now), + CPUsagePercentage: cpusage, + NumCores: uint32(cores), + MemoryUsagePercentage: memusage, + AvailMemory: availablemem, + } + c.mods.MetricsLogger().Log(event) +} diff --git a/metrics/types/types.pb.go b/metrics/types/types.pb.go index 77849739f..aecd74afb 100644 --- a/metrics/types/types.pb.go +++ b/metrics/types/types.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.1 -// protoc v3.19.4 +// protoc v3.17.3 // source: metrics/types/types.proto package types @@ -342,6 +342,85 @@ func (x *ViewTimeouts) GetTimeouts() uint64 { return 0 } +type CPUMemoryStats struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Event *Event `protobuf:"bytes,1,opt,name=Event,proto3" json:"Event,omitempty"` + CPUsagePercentage float64 `protobuf:"fixed64,2,opt,name=CPUsagePercentage,proto3" json:"CPUsagePercentage,omitempty"` + MemoryUsagePercentage float64 `protobuf:"fixed64,3,opt,name=MemoryUsagePercentage,proto3" json:"MemoryUsagePercentage,omitempty"` + NumCores uint32 `protobuf:"varint,4,opt,name=NumCores,proto3" json:"NumCores,omitempty"` + AvailMemory uint64 `protobuf:"varint,5,opt,name=AvailMemory,proto3" json:"AvailMemory,omitempty"` +} + +func (x *CPUMemoryStats) Reset() { + *x = CPUMemoryStats{} + if protoimpl.UnsafeEnabled { + mi := &file_metrics_types_types_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CPUMemoryStats) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CPUMemoryStats) ProtoMessage() {} + +func (x *CPUMemoryStats) ProtoReflect() protoreflect.Message { + mi := &file_metrics_types_types_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use CPUMemoryStats.ProtoReflect.Descriptor instead. +func (*CPUMemoryStats) Descriptor() ([]byte, []int) { + return file_metrics_types_types_proto_rawDescGZIP(), []int{5} +} + +func (x *CPUMemoryStats) GetEvent() *Event { + if x != nil { + return x.Event + } + return nil +} + +func (x *CPUMemoryStats) GetCPUsagePercentage() float64 { + if x != nil { + return x.CPUsagePercentage + } + return 0 +} + +func (x *CPUMemoryStats) GetMemoryUsagePercentage() float64 { + if x != nil { + return x.MemoryUsagePercentage + } + return 0 +} + +func (x *CPUMemoryStats) GetNumCores() uint32 { + if x != nil { + return x.NumCores + } + return 0 +} + +func (x *CPUMemoryStats) GetAvailMemory() uint64 { + if x != nil { + return x.AvailMemory + } + return 0 +} + var File_metrics_types_types_proto protoreflect.FileDescriptor var file_metrics_types_types_proto_rawDesc = []byte{ @@ -386,10 +465,24 @@ var file_metrics_types_types_proto_rawDesc = []byte{ 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x56, 0x69, 0x65, 0x77, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x05, 0x56, 0x69, 0x65, 0x77, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, 0x08, - 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, - 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x72, 0x65, 0x6c, 0x61, 0x62, 0x2f, 0x68, 0x6f, 0x74, - 0x73, 0x74, 0x75, 0x66, 0x66, 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x74, 0x79, - 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x22, 0xd6, 0x01, 0x0a, 0x0e, 0x43, 0x50, 0x55, + 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x73, 0x12, 0x22, 0x0a, 0x05, 0x45, + 0x76, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x74, 0x79, 0x70, + 0x65, 0x73, 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, + 0x2c, 0x0a, 0x11, 0x43, 0x50, 0x55, 0x73, 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, + 0x74, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x11, 0x43, 0x50, 0x55, 0x73, + 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, 0x61, 0x67, 0x65, 0x12, 0x34, 0x0a, + 0x15, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x55, 0x73, 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, + 0x65, 0x6e, 0x74, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x15, 0x4d, 0x65, + 0x6d, 0x6f, 0x72, 0x79, 0x55, 0x73, 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, + 0x61, 0x67, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x75, 0x6d, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x4e, 0x75, 0x6d, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x12, + 0x20, 0x0a, 0x0b, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x04, 0x52, 0x0b, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x4d, 0x65, 0x6d, 0x6f, 0x72, + 0x79, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x72, 0x65, 0x6c, 0x61, 0x62, 0x2f, 0x68, 0x6f, 0x74, 0x73, 0x74, 0x75, 0x66, 0x66, 0x2f, 0x6d, + 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -404,28 +497,30 @@ func file_metrics_types_types_proto_rawDescGZIP() []byte { return file_metrics_types_types_proto_rawDescData } -var file_metrics_types_types_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_metrics_types_types_proto_msgTypes = make([]protoimpl.MessageInfo, 6) var file_metrics_types_types_proto_goTypes = []interface{}{ (*StartEvent)(nil), // 0: types.StartEvent (*Event)(nil), // 1: types.Event (*ThroughputMeasurement)(nil), // 2: types.ThroughputMeasurement (*LatencyMeasurement)(nil), // 3: types.LatencyMeasurement (*ViewTimeouts)(nil), // 4: types.ViewTimeouts - (*timestamppb.Timestamp)(nil), // 5: google.protobuf.Timestamp - (*durationpb.Duration)(nil), // 6: google.protobuf.Duration + (*CPUMemoryStats)(nil), // 5: types.CPUMemoryStats + (*timestamppb.Timestamp)(nil), // 6: google.protobuf.Timestamp + (*durationpb.Duration)(nil), // 7: google.protobuf.Duration } var file_metrics_types_types_proto_depIdxs = []int32{ 1, // 0: types.StartEvent.Event:type_name -> types.Event - 5, // 1: types.Event.Timestamp:type_name -> google.protobuf.Timestamp + 6, // 1: types.Event.Timestamp:type_name -> google.protobuf.Timestamp 1, // 2: types.ThroughputMeasurement.Event:type_name -> types.Event - 6, // 3: types.ThroughputMeasurement.Duration:type_name -> google.protobuf.Duration + 7, // 3: types.ThroughputMeasurement.Duration:type_name -> google.protobuf.Duration 1, // 4: types.LatencyMeasurement.Event:type_name -> types.Event 1, // 5: types.ViewTimeouts.Event:type_name -> types.Event - 6, // [6:6] is the sub-list for method output_type - 6, // [6:6] is the sub-list for method input_type - 6, // [6:6] is the sub-list for extension type_name - 6, // [6:6] is the sub-list for extension extendee - 0, // [0:6] is the sub-list for field type_name + 1, // 6: types.CPUMemoryStats.Event:type_name -> types.Event + 7, // [7:7] is the sub-list for method output_type + 7, // [7:7] is the sub-list for method input_type + 7, // [7:7] is the sub-list for extension type_name + 7, // [7:7] is the sub-list for extension extendee + 0, // [0:7] is the sub-list for field type_name } func init() { file_metrics_types_types_proto_init() } @@ -494,6 +589,18 @@ func file_metrics_types_types_proto_init() { return nil } } + file_metrics_types_types_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*CPUMemoryStats); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -501,7 +608,7 @@ func file_metrics_types_types_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_metrics_types_types_proto_rawDesc, NumEnums: 0, - NumMessages: 5, + NumMessages: 6, NumExtensions: 0, NumServices: 0, }, diff --git a/metrics/types/types.proto b/metrics/types/types.proto index 696020295..060c51db7 100644 --- a/metrics/types/types.proto +++ b/metrics/types/types.proto @@ -39,3 +39,11 @@ message ViewTimeouts { // Number of view timeouts. uint64 Timeouts = 3; } + +message CPUMemoryStats { + Event Event = 1; + double CPUsagePercentage = 2; + double MemoryUsagePercentage = 3; + uint32 NumCores = 4; + uint64 AvailMemory = 5; +} \ No newline at end of file From 4559a6e8cd30964d10af6712e51a7e5d8da67819 Mon Sep 17 00:00:00 2001 From: Hanish Gogada <2924108@FVFG70VQQ05N.local> Date: Mon, 6 Jun 2022 12:02:06 +0200 Subject: [PATCH 2/5] fixed some lint issues --- metrics/replicacpumem.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/metrics/replicacpumem.go b/metrics/replicacpumem.go index 2898a5e99..b1de78aa6 100644 --- a/metrics/replicacpumem.go +++ b/metrics/replicacpumem.go @@ -30,7 +30,11 @@ func (c *CPUMemStat) InitModule(mods *modules.Modules) { c.tick(event.(types.TickEvent)) }) c.mods.Logger().Info("CPU-Memory stats metric enabled") - cpu.Percent(0, false) + // Percent with 0 interval returns 0 usage when called first time. + _, err := cpu.Percent(0, false) + if err != nil { + c.mods.Logger().Info("Unable to fetch the CPU usage") + } } func (c *CPUMemStat) getCPUsage() (float64, uint32) { @@ -41,9 +45,8 @@ func (c *CPUMemStat) getCPUsage() (float64, uint32) { usage, err := cpu.Percent(0, false) if err != nil { return 0, uint32(cores) - } else { - return usage[0], uint32(cores) } + return usage[0], uint32(cores) } func (c *CPUMemStat) getMemoryPercentage() (uint64, float64) { From 81679171c3eb3c59b0ca961d785018d962e40084 Mon Sep 17 00:00:00 2001 From: Hanish Gogada <2924108@FVFG70VQQ05N.local> Date: Tue, 7 Jun 2022 15:23:42 +0200 Subject: [PATCH 3/5] Addressed few review comments --- metrics/replicacpumem.go | 6 +++--- metrics/types/types.pb.go | 32 ++++++++++++++++---------------- metrics/types/types.proto | 6 +++--- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/metrics/replicacpumem.go b/metrics/replicacpumem.go index b1de78aa6..20ca478cf 100644 --- a/metrics/replicacpumem.go +++ b/metrics/replicacpumem.go @@ -57,16 +57,16 @@ func (c *CPUMemStat) getMemoryPercentage() (uint64, float64) { return v.Available, v.UsedPercent } -func (c *CPUMemStat) tick(tick types.TickEvent) { +func (c *CPUMemStat) tick(_ types.TickEvent) { now := time.Now() cpusage, cores := c.getCPUsage() availablemem, memusage := c.getMemoryPercentage() event := &types.CPUMemoryStats{ Event: types.NewReplicaEvent(uint32(c.mods.ID()), now), CPUsagePercentage: cpusage, - NumCores: uint32(cores), + Cores: uint32(cores), MemoryUsagePercentage: memusage, - AvailMemory: availablemem, + AvailableMemory: availablemem, } c.mods.MetricsLogger().Log(event) } diff --git a/metrics/types/types.pb.go b/metrics/types/types.pb.go index aecd74afb..e7b7a4344 100644 --- a/metrics/types/types.pb.go +++ b/metrics/types/types.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.1 -// protoc v3.17.3 +// protoc v3.19.4 // source: metrics/types/types.proto package types @@ -350,8 +350,8 @@ type CPUMemoryStats struct { Event *Event `protobuf:"bytes,1,opt,name=Event,proto3" json:"Event,omitempty"` CPUsagePercentage float64 `protobuf:"fixed64,2,opt,name=CPUsagePercentage,proto3" json:"CPUsagePercentage,omitempty"` MemoryUsagePercentage float64 `protobuf:"fixed64,3,opt,name=MemoryUsagePercentage,proto3" json:"MemoryUsagePercentage,omitempty"` - NumCores uint32 `protobuf:"varint,4,opt,name=NumCores,proto3" json:"NumCores,omitempty"` - AvailMemory uint64 `protobuf:"varint,5,opt,name=AvailMemory,proto3" json:"AvailMemory,omitempty"` + Cores uint32 `protobuf:"varint,4,opt,name=Cores,proto3" json:"Cores,omitempty"` + AvailableMemory uint64 `protobuf:"varint,5,opt,name=AvailableMemory,proto3" json:"AvailableMemory,omitempty"` } func (x *CPUMemoryStats) Reset() { @@ -407,16 +407,16 @@ func (x *CPUMemoryStats) GetMemoryUsagePercentage() float64 { return 0 } -func (x *CPUMemoryStats) GetNumCores() uint32 { +func (x *CPUMemoryStats) GetCores() uint32 { if x != nil { - return x.NumCores + return x.Cores } return 0 } -func (x *CPUMemoryStats) GetAvailMemory() uint64 { +func (x *CPUMemoryStats) GetAvailableMemory() uint64 { if x != nil { - return x.AvailMemory + return x.AvailableMemory } return 0 } @@ -465,7 +465,7 @@ var file_metrics_types_types_proto_rawDesc = []byte{ 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x56, 0x69, 0x65, 0x77, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x05, 0x56, 0x69, 0x65, 0x77, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, 0x08, - 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x22, 0xd6, 0x01, 0x0a, 0x0e, 0x43, 0x50, 0x55, + 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x22, 0xd8, 0x01, 0x0a, 0x0e, 0x43, 0x50, 0x55, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x73, 0x12, 0x22, 0x0a, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, @@ -475,14 +475,14 @@ var file_metrics_types_types_proto_rawDesc = []byte{ 0x15, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x55, 0x73, 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x15, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x55, 0x73, 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, - 0x61, 0x67, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x4e, 0x75, 0x6d, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x18, - 0x04, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x4e, 0x75, 0x6d, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x12, - 0x20, 0x0a, 0x0b, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x18, 0x05, - 0x20, 0x01, 0x28, 0x04, 0x52, 0x0b, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x4d, 0x65, 0x6d, 0x6f, 0x72, - 0x79, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, - 0x72, 0x65, 0x6c, 0x61, 0x62, 0x2f, 0x68, 0x6f, 0x74, 0x73, 0x74, 0x75, 0x66, 0x66, 0x2f, 0x6d, - 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x18, 0x04, 0x20, 0x01, + 0x28, 0x0d, 0x52, 0x05, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x12, 0x28, 0x0a, 0x0f, 0x41, 0x76, 0x61, + 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x04, 0x52, 0x0f, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x4d, 0x65, 0x6d, + 0x6f, 0x72, 0x79, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x72, 0x65, 0x6c, 0x61, 0x62, 0x2f, 0x68, 0x6f, 0x74, 0x73, 0x74, 0x75, 0x66, 0x66, + 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/metrics/types/types.proto b/metrics/types/types.proto index 060c51db7..0fcbbeb6c 100644 --- a/metrics/types/types.proto +++ b/metrics/types/types.proto @@ -44,6 +44,6 @@ message CPUMemoryStats { Event Event = 1; double CPUsagePercentage = 2; double MemoryUsagePercentage = 3; - uint32 NumCores = 4; - uint64 AvailMemory = 5; -} \ No newline at end of file + uint32 Cores = 4; + uint64 AvailableMemory = 5; +} From f4c54f0ff24c8524adc69ba4803e09f1389071a7 Mon Sep 17 00:00:00 2001 From: Hanish Gogada <2924108@FVFG70VQQ05N.local> Date: Tue, 7 Jun 2022 19:17:10 +0200 Subject: [PATCH 4/5] Added comments to the methods and description of the metrics. --- metrics/replicacpumem.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/metrics/replicacpumem.go b/metrics/replicacpumem.go index 20ca478cf..f086185df 100644 --- a/metrics/replicacpumem.go +++ b/metrics/replicacpumem.go @@ -9,6 +9,12 @@ import ( "github.com/shirou/gopsutil/v3/mem" ) +// CPUMem metics measures the percentage of cpu and memory utilization on the node. +// If multiple replicas are run on the same node, then the data may be duplicated. +// This is not enabled by default, to enable this metric add "cpumem" string to --metrics option. +// Since it can interfere with the performance of the protocol, do not enable this metics unless required. +// Interval for measuring the cpu and memory utilization should be above 100 milliseconds, for valid data collection. +// This limitation is due to the gopsutil package. func init() { RegisterReplicaMetric("cpumem", func() interface{} { return &CPUMemStat{} @@ -37,8 +43,11 @@ func (c *CPUMemStat) InitModule(mods *modules.Modules) { } } +// getCPUsage Method returns the average CPU per core and the number of cores, including logical ones. func (c *CPUMemStat) getCPUsage() (float64, uint32) { - cores, err := cpu.Counts(false) + // Counts return the number of cores as our bbchain cluster has hyper-threading enabled, + // logical parameter is set to true. + cores, err := cpu.Counts(true) if err != nil { return 0, 0 } @@ -49,6 +58,7 @@ func (c *CPUMemStat) getCPUsage() (float64, uint32) { return usage[0], uint32(cores) } +// getMemoryPercentage returns total memory available on the node and the currently utilized percentage. func (c *CPUMemStat) getMemoryPercentage() (uint64, float64) { v, err := mem.VirtualMemory() if err != nil { @@ -57,6 +67,7 @@ func (c *CPUMemStat) getMemoryPercentage() (uint64, float64) { return v.Available, v.UsedPercent } +// tick method is invoked periodically based on the configured measuring interval of metrics func (c *CPUMemStat) tick(_ types.TickEvent) { now := time.Now() cpusage, cores := c.getCPUsage() From 035df7d3843bf29e4fb76cca5ae6f62fec9a64ac Mon Sep 17 00:00:00 2001 From: Hanish Gogada <2924108@FVFG70VQQ05N.local> Date: Mon, 25 Jul 2022 15:02:08 +0200 Subject: [PATCH 5/5] Resolved the comments. --- metrics/replicacpumem.go | 35 +++++++++++++++-------------- metrics/types/types.pb.go | 47 +++++++++++++++++++-------------------- metrics/types/types.proto | 4 ++-- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/metrics/replicacpumem.go b/metrics/replicacpumem.go index f086185df..e572cc8d0 100644 --- a/metrics/replicacpumem.go +++ b/metrics/replicacpumem.go @@ -17,34 +17,35 @@ import ( // This limitation is due to the gopsutil package. func init() { RegisterReplicaMetric("cpumem", func() interface{} { - return &CPUMemStat{} + return &CPUMem{} }) RegisterClientMetric("cpumem", func() interface{} { - return &CPUMemStat{} + return &CPUMem{} }) } -// CPUMemStat measures CPU usage and Memory usage and record in the metric logs. -type CPUMemStat struct { +// CPUMem measures CPU usage and Memory usage and record in the metric logs. +type CPUMem struct { mods *modules.Modules } // InitModule gives the module access to the other modules. -func (c *CPUMemStat) InitModule(mods *modules.Modules) { +func (c *CPUMem) InitModule(mods *modules.Modules) { c.mods = mods c.mods.EventLoop().RegisterObserver(types.TickEvent{}, func(event interface{}) { c.tick(event.(types.TickEvent)) }) c.mods.Logger().Info("CPU-Memory stats metric enabled") - // Percent with 0 interval returns 0 usage when called first time. + // The cpu.Percent function returns the CPU usage since the last call when called with an interval of 0. + // This initial call ensures that the first measurement of the CPU usage is nonzero. _, err := cpu.Percent(0, false) if err != nil { c.mods.Logger().Info("Unable to fetch the CPU usage") } } -// getCPUsage Method returns the average CPU per core and the number of cores, including logical ones. -func (c *CPUMemStat) getCPUsage() (float64, uint32) { +// getCPUPercentage Method returns the average CPU per core and the number of cores, including logical ones. +func (c *CPUMem) getCPUPercentage() (float64, uint32) { // Counts return the number of cores as our bbchain cluster has hyper-threading enabled, // logical parameter is set to true. cores, err := cpu.Counts(true) @@ -59,7 +60,7 @@ func (c *CPUMemStat) getCPUsage() (float64, uint32) { } // getMemoryPercentage returns total memory available on the node and the currently utilized percentage. -func (c *CPUMemStat) getMemoryPercentage() (uint64, float64) { +func (c *CPUMem) getMemoryPercentage() (uint64, float64) { v, err := mem.VirtualMemory() if err != nil { return 0, 0 @@ -68,16 +69,16 @@ func (c *CPUMemStat) getMemoryPercentage() (uint64, float64) { } // tick method is invoked periodically based on the configured measuring interval of metrics -func (c *CPUMemStat) tick(_ types.TickEvent) { +func (c *CPUMem) tick(_ types.TickEvent) { now := time.Now() - cpusage, cores := c.getCPUsage() - availablemem, memusage := c.getMemoryPercentage() + cpu, cores := c.getCPUPercentage() + availableMemory, memoryUsage := c.getMemoryPercentage() event := &types.CPUMemoryStats{ - Event: types.NewReplicaEvent(uint32(c.mods.ID()), now), - CPUsagePercentage: cpusage, - Cores: uint32(cores), - MemoryUsagePercentage: memusage, - AvailableMemory: availablemem, + Event: types.NewReplicaEvent(uint32(c.mods.ID()), now), + CPUPercentage: cpu, + Cores: uint32(cores), + MemoryPercentage: memoryUsage, + AvailableMemory: availableMemory, } c.mods.MetricsLogger().Log(event) } diff --git a/metrics/types/types.pb.go b/metrics/types/types.pb.go index e7b7a4344..ada941bfc 100644 --- a/metrics/types/types.pb.go +++ b/metrics/types/types.pb.go @@ -347,11 +347,11 @@ type CPUMemoryStats struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Event *Event `protobuf:"bytes,1,opt,name=Event,proto3" json:"Event,omitempty"` - CPUsagePercentage float64 `protobuf:"fixed64,2,opt,name=CPUsagePercentage,proto3" json:"CPUsagePercentage,omitempty"` - MemoryUsagePercentage float64 `protobuf:"fixed64,3,opt,name=MemoryUsagePercentage,proto3" json:"MemoryUsagePercentage,omitempty"` - Cores uint32 `protobuf:"varint,4,opt,name=Cores,proto3" json:"Cores,omitempty"` - AvailableMemory uint64 `protobuf:"varint,5,opt,name=AvailableMemory,proto3" json:"AvailableMemory,omitempty"` + Event *Event `protobuf:"bytes,1,opt,name=Event,proto3" json:"Event,omitempty"` + CPUPercentage float64 `protobuf:"fixed64,2,opt,name=CPUPercentage,proto3" json:"CPUPercentage,omitempty"` + MemoryPercentage float64 `protobuf:"fixed64,3,opt,name=MemoryPercentage,proto3" json:"MemoryPercentage,omitempty"` + Cores uint32 `protobuf:"varint,4,opt,name=Cores,proto3" json:"Cores,omitempty"` + AvailableMemory uint64 `protobuf:"varint,5,opt,name=AvailableMemory,proto3" json:"AvailableMemory,omitempty"` } func (x *CPUMemoryStats) Reset() { @@ -393,16 +393,16 @@ func (x *CPUMemoryStats) GetEvent() *Event { return nil } -func (x *CPUMemoryStats) GetCPUsagePercentage() float64 { +func (x *CPUMemoryStats) GetCPUPercentage() float64 { if x != nil { - return x.CPUsagePercentage + return x.CPUPercentage } return 0 } -func (x *CPUMemoryStats) GetMemoryUsagePercentage() float64 { +func (x *CPUMemoryStats) GetMemoryPercentage() float64 { if x != nil { - return x.MemoryUsagePercentage + return x.MemoryPercentage } return 0 } @@ -465,24 +465,23 @@ var file_metrics_types_types_proto_rawDesc = []byte{ 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x56, 0x69, 0x65, 0x77, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x05, 0x56, 0x69, 0x65, 0x77, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, 0x08, - 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x22, 0xd8, 0x01, 0x0a, 0x0e, 0x43, 0x50, 0x55, + 0x54, 0x69, 0x6d, 0x65, 0x6f, 0x75, 0x74, 0x73, 0x22, 0xc6, 0x01, 0x0a, 0x0e, 0x43, 0x50, 0x55, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x53, 0x74, 0x61, 0x74, 0x73, 0x12, 0x22, 0x0a, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0c, 0x2e, 0x74, 0x79, 0x70, 0x65, 0x73, 0x2e, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x52, 0x05, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, - 0x2c, 0x0a, 0x11, 0x43, 0x50, 0x55, 0x73, 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, - 0x74, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x11, 0x43, 0x50, 0x55, 0x73, - 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, 0x61, 0x67, 0x65, 0x12, 0x34, 0x0a, - 0x15, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x55, 0x73, 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, - 0x65, 0x6e, 0x74, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x15, 0x4d, 0x65, - 0x6d, 0x6f, 0x72, 0x79, 0x55, 0x73, 0x61, 0x67, 0x65, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, - 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x18, 0x04, 0x20, 0x01, - 0x28, 0x0d, 0x52, 0x05, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x12, 0x28, 0x0a, 0x0f, 0x41, 0x76, 0x61, - 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x18, 0x05, 0x20, 0x01, - 0x28, 0x04, 0x52, 0x0f, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x4d, 0x65, 0x6d, - 0x6f, 0x72, 0x79, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, - 0x6d, 0x2f, 0x72, 0x65, 0x6c, 0x61, 0x62, 0x2f, 0x68, 0x6f, 0x74, 0x73, 0x74, 0x75, 0x66, 0x66, - 0x2f, 0x6d, 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x24, 0x0a, 0x0d, 0x43, 0x50, 0x55, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, 0x61, 0x67, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x0d, 0x43, 0x50, 0x55, 0x50, 0x65, 0x72, 0x63, 0x65, + 0x6e, 0x74, 0x61, 0x67, 0x65, 0x12, 0x2a, 0x0a, 0x10, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x50, + 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, + 0x10, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x50, 0x65, 0x72, 0x63, 0x65, 0x6e, 0x74, 0x61, 0x67, + 0x65, 0x12, 0x14, 0x0a, 0x05, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0d, + 0x52, 0x05, 0x43, 0x6f, 0x72, 0x65, 0x73, 0x12, 0x28, 0x0a, 0x0f, 0x41, 0x76, 0x61, 0x69, 0x6c, + 0x61, 0x62, 0x6c, 0x65, 0x4d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x18, 0x05, 0x20, 0x01, 0x28, 0x04, + 0x52, 0x0f, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x4d, 0x65, 0x6d, 0x6f, 0x72, + 0x79, 0x42, 0x29, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x72, 0x65, 0x6c, 0x61, 0x62, 0x2f, 0x68, 0x6f, 0x74, 0x73, 0x74, 0x75, 0x66, 0x66, 0x2f, 0x6d, + 0x65, 0x74, 0x72, 0x69, 0x63, 0x73, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x73, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/metrics/types/types.proto b/metrics/types/types.proto index 0fcbbeb6c..be4c1a13a 100644 --- a/metrics/types/types.proto +++ b/metrics/types/types.proto @@ -42,8 +42,8 @@ message ViewTimeouts { message CPUMemoryStats { Event Event = 1; - double CPUsagePercentage = 2; - double MemoryUsagePercentage = 3; + double CPUPercentage = 2; + double MemoryPercentage = 3; uint32 Cores = 4; uint64 AvailableMemory = 5; }