Skip to content

Commit

Permalink
report resource add mean cpu
Browse files Browse the repository at this point in the history
Signed-off-by: ii2day <ji.li@daocloud.io>
  • Loading branch information
ii2day committed Nov 6, 2023
1 parent 8f17d25 commit 0680b89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions pkg/k8s/apis/system/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ type TotalRunningLoad struct {

type SystemResource struct {
MaxCPU string `json:"MaxCPU"`
MeanCPU string `json:"MeanCPU"`
MaxMemory string `json:"MaxMemory"`
}
15 changes: 9 additions & 6 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand All @@ -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{}
Expand All @@ -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))),
}

Expand Down

0 comments on commit 0680b89

Please sign in to comment.