Skip to content

Commit

Permalink
feat(advisor): add cpu usage ratio for pod and node custom metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sun-yuliang authored and csfldf committed Sep 29, 2024
1 parent 875d630 commit 3f931e7
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/google/uuid v1.3.0
github.com/h2non/gock v1.2.0
github.com/klauspost/cpuid/v2 v2.2.6
github.com/kubewharf/katalyst-api v0.5.1-0.20240911051124-d5c09c115a19
github.com/kubewharf/katalyst-api v0.5.1-0.20240929080014-ae613a8935d8
github.com/montanaflynn/stats v0.7.1
github.com/opencontainers/runc v1.1.6
github.com/opencontainers/selinux v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
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/kubewharf/katalyst-api v0.5.1-0.20240911051124-d5c09c115a19 h1:5kz2u+tLNzW94ByL9yfsfkvE4N8n/r6C1TKDiFWK8rw=
github.com/kubewharf/katalyst-api v0.5.1-0.20240911051124-d5c09c115a19/go.mod h1:Y2IeIorxQamF2a3oa0+URztl5QCSty6Jj3zD83R8J9k=
github.com/kubewharf/katalyst-api v0.5.1-0.20240929080014-ae613a8935d8 h1:rIwZVD4iia7pTiB6h1xR8muc4jx4GgHWzhVCPKlEhXw=
github.com/kubewharf/katalyst-api v0.5.1-0.20240929080014-ae613a8935d8/go.mod h1:Y2IeIorxQamF2a3oa0+URztl5QCSty6Jj3zD83R8J9k=
github.com/kubewharf/kubelet v1.24.6-kubewharf.9 h1:jOTYZt7h/J7I8xQMKMUcJjKf5UFBv37jHWvNp5VRFGc=
github.com/kubewharf/kubelet v1.24.6-kubewharf.9/go.mod h1:MxbSZUx3wXztFneeelwWWlX7NAAStJ6expqq7gY2J3c=
github.com/kyoh86/exportloopref v0.1.7/go.mod h1:h1rDl2Kdj97+Kwh4gdz3ujE7XHmH51Q0lUiZ1z4NLj8=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import (
// nodeRawMetricNameMapping maps the raw metricName (collected from agent.MetricsFetcher)
// to the standard metricName (used by custom-metric-api-server)
var nodeRawMetricNameMapping = map[string]string{
consts.MetricLoad1MinSystem: apimetricnode.CustomMetricNodeCPULoad1Min,
consts.MetricLoad1MinSystem: apimetricnode.CustomMetricNodeCPULoad1Min,
consts.MetricCPUUsageRatioSystem: apimetricnode.CustomMetricNodeCPUUsageRatio,

consts.MetricMemFreeSystem: apimetricnode.CustomMetricNodeMemoryFree,
consts.MetricMemAvailableSystem: apimetricnode.CustomMetricNodeMemoryAvailable,
Expand Down
7 changes: 4 additions & 3 deletions pkg/agent/sysadvisor/plugin/metric-emitter/syncer/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ const (
// podRawMetricNameMapping maps the raw metricName (collected from agent.MetricsFetcher)
// to the standard metricName (used by custom-metric-api-server)
var podRawMetricNameMapping = map[string]string{
consts.MetricLoad1MinContainer: apimetricpod.CustomMetricPodCPULoad1Min,
consts.MetricCPUUsageContainer: apimetricpod.CustomMetricPodCPUUsage,
consts.MetricCPUCPIContainer: apimetricpod.CustomMetricPodCPUCPI,
consts.MetricLoad1MinContainer: apimetricpod.CustomMetricPodCPULoad1Min,
consts.MetricCPUUsageContainer: apimetricpod.CustomMetricPodCPUUsage,
consts.MetricCPUUsageRatioContainer: apimetricpod.CustomMetricPodCPUUsageRatio,
consts.MetricCPUCPIContainer: apimetricpod.CustomMetricPodCPUCPI,

consts.MetricMemRssContainer: apimetricpod.CustomMetricPodMemoryRSS,
consts.MetricMemUsageContainer: apimetricpod.CustomMetricPodMemoryUsage,
Expand Down
11 changes: 7 additions & 4 deletions pkg/consts/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (

// System compute metrics
const (
MetricCPUUsageRatioSystem = "cpu.usage.ratio.system"

MetricLoad1MinSystem = "cpu.load.1min.system"
MetricLoad5MinSystem = "cpu.load.5min.system"
MetricLoad15MinSystem = "cpu.load.15min.system"
Expand Down Expand Up @@ -170,10 +172,11 @@ const (

// container cpu metrics
const (
MetricCPULimitContainer = "cpu.limit.container"
MetricCPUUsageContainer = "cpu.usage.container"
MetricCPUUsageUserContainer = "cpu.usage.user.container"
MetricCPUUsageSysContainer = "cpu.usage.sys.container"
MetricCPULimitContainer = "cpu.limit.container"
MetricCPUUsageContainer = "cpu.usage.container"
MetricCPUUsageUserContainer = "cpu.usage.user.container"
MetricCPUUsageSysContainer = "cpu.usage.sys.container"
MetricCPUUsageRatioContainer = "cpu.usage.ratio.container"

MetricCPUShareContainer = "cpu.share.container"
MetricCPUQuotaContainer = "cpu.quota.container"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func (m *MalachiteMetricsProvisioner) processSystemComputeData(systemComputeData
// todo, currently we only get a unified data for the whole system compute data
updateTime := time.Unix(systemComputeData.UpdateTime, 0)

globalCPU := systemComputeData.GlobalCPU
m.metricStore.SetNodeMetric(consts.MetricCPUUsageRatioSystem,
utilmetric.MetricData{Value: globalCPU.CPUUsage / 100, Time: &updateTime})

load := systemComputeData.Load
m.metricStore.SetNodeMetric(consts.MetricLoad1MinSystem,
utilmetric.MetricData{Value: load.One, Time: &updateTime})
Expand Down Expand Up @@ -742,6 +746,8 @@ func (m *MalachiteMetricsProvisioner) processContainerCPUData(podUID, containerN
utilmetric.MetricData{Value: cpu.CPUUserUsageRatio, Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUUsageSysContainer,
utilmetric.MetricData{Value: cpu.CPUSysUsageRatio, Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUUsageRatioContainer,
utilmetric.MetricData{Value: cpu.CPUUsageRatio / (float64(cpu.CfsQuotaUs) / float64(cpu.CfsPeriodUs)), Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUShareContainer,
utilmetric.MetricData{Value: float64(cpu.CPUShares), Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUQuotaContainer,
Expand Down Expand Up @@ -811,6 +817,8 @@ func (m *MalachiteMetricsProvisioner) processContainerCPUData(podUID, containerN
utilmetric.MetricData{Value: cpu.CPUUserUsageRatio, Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUUsageSysContainer,
utilmetric.MetricData{Value: cpu.CPUSysUsageRatio, Time: &updateTime})
m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUUsageRatioContainer,
utilmetric.MetricData{Value: cpu.CPUUsageRatio / (float64(cpu.Max) / float64(cpu.MaxPeriod)), Time: &updateTime})

m.metricStore.SetContainerMetric(podUID, containerName, consts.MetricCPUNrRunnableContainer,
utilmetric.MetricData{Value: float64(cpu.TaskNrRunning), Time: &updateTime})
Expand Down

0 comments on commit 3f931e7

Please sign in to comment.