diff --git a/pkg/k8s/apis/system/v1beta1/common_types.go b/pkg/k8s/apis/system/v1beta1/common_types.go index 79936ebb..c2635d51 100644 --- a/pkg/k8s/apis/system/v1beta1/common_types.go +++ b/pkg/k8s/apis/system/v1beta1/common_types.go @@ -28,5 +28,6 @@ type TotalRunningLoad struct { type SystemResource struct { MaxCPU string `json:"MaxCPU"` + MeanCPU string `json:"MeanCPU"` MaxMemory string `json:"MaxMemory"` } diff --git a/pkg/resource/resource.go b/pkg/resource/resource.go index 96520857..664fb6b4 100644 --- a/pkg/resource/resource.go +++ b/pkg/resource/resource.go @@ -17,11 +17,13 @@ import ( ) type UsedResource struct { - mem uint64 // byte - cpu float64 // percent - l lock.RWMutex - ctx context.Context - stop chan struct{} + mem uint64 // byte + cpu float64 // percent + roundCount int // Count the number of rounds of cpu mem + totalCPU float64 //Total cpu usage statistics + l lock.RWMutex + ctx context.Context + stop chan struct{} } func InitResource(ctx context.Context) *UsedResource { @@ -35,7 +37,6 @@ func InitResource(ctx context.Context) *UsedResource { func (r *UsedResource) RunResourceCollector() { interval := time.Duration(types.AgentConfig.CollectResourceInSecond) * time.Second go func() { - for { select { case <-r.stop: @@ -50,6 +51,7 @@ func (r *UsedResource) RunResourceCollector() { if r.cpu < cpuStats[0] { r.cpu = cpuStats[0] } + r.totalCPU += cpuStats[0] r.l.Unlock() } m := &runtime.MemStats{} @@ -70,6 +72,7 @@ func (r *UsedResource) Stats() v1beta1.SystemResource { defer r.l.Unlock() resource := v1beta1.SystemResource{ MaxCPU: fmt.Sprintf("%.3f%%", r.cpu), + MeanCPU: fmt.Sprintf("%.3f%%", r.totalCPU/float64(r.roundCount)), MaxMemory: fmt.Sprintf("%.2fMB", float64(r.mem/(1024*1024))), }