diff --git a/pkg/koordlet/metricsadvisor/collectors/coldmemoryresource/cold_page_collector_test.go b/pkg/koordlet/metricsadvisor/collectors/coldmemoryresource/cold_page_collector_test.go index 773a05118..71246acaf 100644 --- a/pkg/koordlet/metricsadvisor/collectors/coldmemoryresource/cold_page_collector_test.go +++ b/pkg/koordlet/metricsadvisor/collectors/coldmemoryresource/cold_page_collector_test.go @@ -82,7 +82,7 @@ func Test_NewColdPageCollector(t *testing.T) { appendableDB: opt.MetricCache, metricDB: opt.MetricCache, started: atomic.NewBool(false), - coldBoundary: 3, + coldBoundary: system.GetKidledColdBoundary(), }, }, } diff --git a/pkg/koordlet/metricsadvisor/collectors/coldmemoryresource/cold_page_kidled_test.go b/pkg/koordlet/metricsadvisor/collectors/coldmemoryresource/cold_page_kidled_test.go index 23b1e41c1..5282af421 100644 --- a/pkg/koordlet/metricsadvisor/collectors/coldmemoryresource/cold_page_kidled_test.go +++ b/pkg/koordlet/metricsadvisor/collectors/coldmemoryresource/cold_page_kidled_test.go @@ -196,53 +196,53 @@ func Test_collectColdPageInfo(t *testing.T) { total_unevictable 0 ` meminfo := `MemTotal: 1048576 kB - MemFree: 262144 kB - MemAvailable: 524288 kB - Buffers: 0 kB - Cached: 262144 kB - SwapCached: 0 kB - Active: 524288 kB - Inactive: 262144 kB - Active(anon): 262144 kB - Inactive(anon): 262144 kB - Active(file): 0 kB - Inactive(file): 262144 kB - Unevictable: 0 kB - Mlocked: 0 kB - SwapTotal: 0 kB - SwapFree: 0 kB - Dirty: 0 kB - Writeback: 0 kB - AnonPages: 0 kB - Mapped: 0 kB - Shmem: 0 kB - Slab: 0 kB - SReclaimable: 0 kB - SUnreclaim: 0 kB - KernelStack: 0 kB - PageTables: 0 kB - NFS_Unstable: 0 kB - Bounce: 0 kB - WritebackTmp: 0 kB - CommitLimit: 0 kB - Committed_AS: 0 kB - VmallocTotal: 0 kB - VmallocUsed: 0 kB - VmallocChunk: 0 kB - HardwareCorrupted: 0 kB - AnonHugePages: 0 kB - ShmemHugePages: 0 kB - ShmemPmdMapped: 0 kB - CmaTotal: 0 kB - CmaFree: 0 kB - HugePages_Total: 0 - HugePages_Free: 0 - HugePages_Rsvd: 0 - HugePages_Surp: 0 - Hugepagesize: 0 kB - DirectMap4k: 0 kB - DirectMap2M: 0 kB - DirectMap1G: 0 kB` +MemFree: 262144 kB +MemAvailable: 524288 kB +Buffers: 0 kB +Cached: 262144 kB +SwapCached: 0 kB +Active: 524288 kB +Inactive: 262144 kB +Active(anon): 262144 kB +Inactive(anon): 262144 kB +Active(file): 0 kB +Inactive(file): 262144 kB +Unevictable: 0 kB +Mlocked: 0 kB +SwapTotal: 0 kB +SwapFree: 0 kB +Dirty: 0 kB +Writeback: 0 kB +AnonPages: 0 kB +Mapped: 0 kB +Shmem: 0 kB +Slab: 0 kB +SReclaimable: 0 kB +SUnreclaim: 0 kB +KernelStack: 0 kB +PageTables: 0 kB +NFS_Unstable: 0 kB +Bounce: 0 kB +WritebackTmp: 0 kB +CommitLimit: 0 kB +Committed_AS: 0 kB +VmallocTotal: 0 kB +VmallocUsed: 0 kB +VmallocChunk: 0 kB +HardwareCorrupted: 0 kB +AnonHugePages: 0 kB +ShmemHugePages: 0 kB +ShmemPmdMapped: 0 kB +CmaTotal: 0 kB +CmaFree: 0 kB +HugePages_Total: 0 +HugePages_Free: 0 +HugePages_Rsvd: 0 +HugePages_Surp: 0 +Hugepagesize: 0 kB +DirectMap4k: 0 kB +DirectMap2M: 0 kB +DirectMap1G: 0 kB` testPod := &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: "test-pod", @@ -424,14 +424,14 @@ func Test_collectNodeColdPageInfo(t *testing.T) { # ||||| [1,2) [2,5) [5,15) [15,30) [30,60) [60,120) [120,240) [240,+inf) csei 2613248 4657152 18182144 293683200 0 0 0 0 dsei 2568192 5140480 15306752 48648192 0 0 0 0 - cfei 2633728 4640768 66531328 340172800 0 0 0 0 - dfei 0 0 4096 0 0 0 0 0 + cfei 2633728 4640768 66531328 340172800 52834304 0 0 0 + dfei 0 0 4096 0 52834304 52834304 0 0 csui 0 0 0 0 0 0 0 0 dsui 0 0 0 0 0 0 0 0 cfui 0 0 0 0 0 0 0 0 dfui 0 0 0 0 0 0 0 0 - csea 765952 1044480 3784704 52834304 0 0 0 0 - dsea 286720 270336 1564672 5390336 0 0 0 0 + csea 765952 1044480 3784704 52834304 52834304 0 0 0 + dsea 286720 270336 1564672 5390336 52834304 0 0 0 cfea 9273344 16609280 152109056 315121664 0 0 0 0 dfea 0 0 0 0 0 0 0 0 csua 0 0 0 0 0 0 0 0 @@ -440,53 +440,53 @@ func Test_collectNodeColdPageInfo(t *testing.T) { dfua 0 0 0 0 0 0 0 0 slab 0 0 0 0 0 0 0 0` meminfo := `MemTotal: 1048576 kB - MemFree: 262144 kB - MemAvailable: 524288 kB - Buffers: 0 kB - Cached: 262144 kB - SwapCached: 0 kB - Active: 524288 kB - Inactive: 262144 kB - Active(anon): 262144 kB - Inactive(anon): 262144 kB - Active(file): 0 kB - Inactive(file): 262144 kB - Unevictable: 0 kB - Mlocked: 0 kB - SwapTotal: 0 kB - SwapFree: 0 kB - Dirty: 0 kB - Writeback: 0 kB - AnonPages: 0 kB - Mapped: 0 kB - Shmem: 0 kB - Slab: 0 kB - SReclaimable: 0 kB - SUnreclaim: 0 kB - KernelStack: 0 kB - PageTables: 0 kB - NFS_Unstable: 0 kB - Bounce: 0 kB - WritebackTmp: 0 kB - CommitLimit: 0 kB - Committed_AS: 0 kB - VmallocTotal: 0 kB - VmallocUsed: 0 kB - VmallocChunk: 0 kB - HardwareCorrupted: 0 kB - AnonHugePages: 0 kB - ShmemHugePages: 0 kB - ShmemPmdMapped: 0 kB - CmaTotal: 0 kB - CmaFree: 0 kB - HugePages_Total: 0 - HugePages_Free: 0 - HugePages_Rsvd: 0 - HugePages_Surp: 0 - Hugepagesize: 0 kB - DirectMap4k: 0 kB - DirectMap2M: 0 kB - DirectMap1G: 0 kB` +MemFree: 262144 kB +MemAvailable: 524288 kB +Buffers: 0 kB +Cached: 262144 kB +SwapCached: 0 kB +Active: 524288 kB +Inactive: 262144 kB +Active(anon): 262144 kB +Inactive(anon): 262144 kB +Active(file): 0 kB +Inactive(file): 262144 kB +Unevictable: 0 kB +Mlocked: 0 kB +SwapTotal: 0 kB +SwapFree: 0 kB +Dirty: 0 kB +Writeback: 0 kB +AnonPages: 0 kB +Mapped: 0 kB +Shmem: 0 kB +Slab: 0 kB +SReclaimable: 0 kB +SUnreclaim: 0 kB +KernelStack: 0 kB +PageTables: 0 kB +NFS_Unstable: 0 kB +Bounce: 0 kB +WritebackTmp: 0 kB +CommitLimit: 0 kB +Committed_AS: 0 kB +VmallocTotal: 0 kB +VmallocUsed: 0 kB +VmallocChunk: 0 kB +HardwareCorrupted: 0 kB +AnonHugePages: 0 kB +ShmemHugePages: 0 kB +ShmemPmdMapped: 0 kB +CmaTotal: 0 kB +CmaFree: 0 kB +HugePages_Total: 0 +HugePages_Free: 0 +HugePages_Rsvd: 0 +HugePages_Surp: 0 +Hugepagesize: 0 kB +DirectMap4k: 0 kB +DirectMap2M: 0 kB +DirectMap1G: 0 kB` helper := system.NewFileTestUtil(t) defer helper.Cleanup() ctrl := gomock.NewController(t) @@ -530,8 +530,9 @@ func Test_collectNodeColdPageInfo(t *testing.T) { } assert.NoError(t, err) got1, got2 := testGetNodeMetrics(t, c.metricDB, testNow, 5*time.Second) - assert.Equal(t, float64(7.33569024e+08), got1) - assert.Equal(t, float64(3.401728e+08), got2) + // memInfo.MemTotal*1024 - memInfo.MemFree*1024 - coldPageUsageBytes + assert.Equal(t, float64((1048576-262144)<<10-52834304), got1) + assert.Equal(t, float64(52834304), got2) // test collect failed helper.WriteCgroupFileContents("", system.MemoryIdlePageStats, ``) helper.WriteProcSubFileContents(system.ProcMemInfoName, ``) @@ -563,8 +564,8 @@ func Test_collectPodColdPageInfo(t *testing.T) { # ||||| [1,2) [2,5) [5,15) [15,30) [30,60) [60,120) [120,240) [240,+inf) csei 2613248 4657152 18182144 293683200 0 0 0 0 dsei 2568192 5140480 15306752 48648192 0 0 0 0 - cfei 2633728 4640768 66531328 340172800 0 0 0 0 - dfei 0 0 4096 0 0 0 0 0 + cfei 2633728 4640768 66531328 340172800 0 52834304 0 0 + dfei 0 0 4096 0 0 52834304 0 0 csui 0 0 0 0 0 0 0 0 dsui 0 0 0 0 0 0 0 0 cfui 0 0 0 0 0 0 0 0 @@ -579,14 +580,14 @@ func Test_collectPodColdPageInfo(t *testing.T) { dfua 0 0 0 0 0 0 0 0 slab 0 0 0 0 0 0 0 0` testMemStat := ` - total_cache 104857600 - total_rss 104857600 - total_inactive_anon 104857600 - total_active_anon 0 - total_inactive_file 104857600 - total_active_file 0 - total_unevictable 0 - ` +total_cache 104857600 +total_rss 104857600 +total_inactive_anon 104857600 +total_active_anon 0 +total_inactive_file 104857600 +total_active_file 0 +total_unevictable 0 +` testPod := &corev1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: "test-pod", @@ -810,8 +811,8 @@ func Test_kidledcoldPageCollector_collectHostAppsColdPageInfo(t *testing.T) { # ||||| [1,2) [2,5) [5,15) [15,30) [30,60) [60,120) [120,240) [240,+inf) csei 2613248 4657152 18182144 293683200 0 0 0 0 dsei 2568192 5140480 15306752 48648192 0 0 0 0 - cfei 2633728 4640768 66531328 340172800 0 0 0 0 - dfei 0 0 4096 0 0 0 0 0 + cfei 2633728 4640768 66531328 340172800 0 52834304 0 0 + dfei 0 0 4096 0 0 52834304 0 0 csui 0 0 0 0 0 0 0 0 dsui 0 0 0 0 0 0 0 0 cfui 0 0 0 0 0 0 0 0 @@ -826,14 +827,14 @@ func Test_kidledcoldPageCollector_collectHostAppsColdPageInfo(t *testing.T) { dfua 0 0 0 0 0 0 0 0 slab 0 0 0 0 0 0 0 0` testMemStat := ` - total_cache 104857600 - total_rss 104857600 - total_inactive_anon 104857600 - total_active_anon 340172800 - total_inactive_file 104857600 - total_active_file 0 - total_unevictable 0 - ` +total_cache 104857600 +total_rss 104857600 +total_inactive_anon 104857600 +total_active_anon 340172800 +total_inactive_file 104857600 +total_active_file 0 +total_unevictable 0 +` type fields struct { getNodeSLO *slov1alpha1.NodeSLO SetSysUtil func(helper *system.FileTestUtil) @@ -884,8 +885,8 @@ func Test_kidledcoldPageCollector_collectHostAppsColdPageInfo(t *testing.T) { wants: wants{ hostMetric: map[string]wantMetric{ "test-host-app": { - coldPageSize: 340172800, - memWithHotCache: 209715200, + coldPageSize: 1.05668608e+08, + memWithHotCache: 4.44219392e+08, }, }, }, diff --git a/pkg/koordlet/metricsadvisor/collectors/pagecache/page_cache_collector_test.go b/pkg/koordlet/metricsadvisor/collectors/pagecache/page_cache_collector_test.go new file mode 100644 index 000000000..113794d44 --- /dev/null +++ b/pkg/koordlet/metricsadvisor/collectors/pagecache/page_cache_collector_test.go @@ -0,0 +1,341 @@ +/* +Copyright 2022 The Koordinator Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package pagecache + +import ( + "testing" + "time" + + "github.com/golang/mock/gomock" + gocache "github.com/patrickmn/go-cache" + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/koordinator-sh/koordinator/pkg/koordlet/metriccache" + "github.com/koordinator-sh/koordinator/pkg/koordlet/metricsadvisor/framework" + "github.com/koordinator-sh/koordinator/pkg/koordlet/resourceexecutor" + "github.com/koordinator-sh/koordinator/pkg/koordlet/statesinformer" + mock_statesinformer "github.com/koordinator-sh/koordinator/pkg/koordlet/statesinformer/mockstatesinformer" + "github.com/koordinator-sh/koordinator/pkg/koordlet/util/system" +) + +func Test_collectPageCache(t *testing.T) { + testNow := time.Now() + testContainerID := "containerd://123abc" + testPodMetaDir := "kubepods.slice/kubepods-podxxxxxxxx.slice" + testPodParentDir := "/kubepods.slice/kubepods-podxxxxxxxx.slice" + testContainerParentDir := "/kubepods.slice/kubepods-podxxxxxxxx.slice/cri-containerd-123abc.scope" + testMemStat := ` +total_cache 104857600 +total_rss 104857600 +total_inactive_anon 104857600 +total_active_anon 0 +total_inactive_file 104857600 +total_active_file 0 +total_unevictable 0 +` + meminfo := `MemTotal: 1048576 kB +MemFree: 262144 kB +MemAvailable: 524288 kB +Buffers: 0 kB +Cached: 262144 kB +SwapCached: 0 kB +Active: 524288 kB +Inactive: 262144 kB +Active(anon): 262144 kB +Inactive(anon): 262144 kB +Active(file): 0 kB +Inactive(file): 262144 kB +Unevictable: 0 kB +Mlocked: 0 kB +SwapTotal: 0 kB +SwapFree: 0 kB +Dirty: 0 kB +Writeback: 0 kB +AnonPages: 0 kB +Mapped: 0 kB +Shmem: 0 kB +Slab: 0 kB +SReclaimable: 0 kB +SUnreclaim: 0 kB +KernelStack: 0 kB +PageTables: 0 kB +NFS_Unstable: 0 kB +Bounce: 0 kB +WritebackTmp: 0 kB +CommitLimit: 0 kB +Committed_AS: 0 kB +VmallocTotal: 0 kB +VmallocUsed: 0 kB +VmallocChunk: 0 kB +HardwareCorrupted: 0 kB +AnonHugePages: 0 kB +ShmemHugePages: 0 kB +ShmemPmdMapped: 0 kB +CmaTotal: 0 kB +CmaFree: 0 kB +HugePages_Total: 0 +HugePages_Free: 0 +HugePages_Rsvd: 0 +HugePages_Surp: 0 +Hugepagesize: 0 kB +DirectMap4k: 0 kB +DirectMap2M: 0 kB +DirectMap1G: 0 kB` + testPod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pod", + Namespace: "test", + UID: "xxxxxxxx", + }, + Status: corev1.PodStatus{ + Phase: corev1.PodRunning, + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: "test-container", + ContainerID: testContainerID, + State: corev1.ContainerState{ + Running: &corev1.ContainerStateRunning{}, + }, + }, + { + Name: "test-no-running-container", + ContainerID: testContainerID, + State: corev1.ContainerState{ + Terminated: &corev1.ContainerStateTerminated{}, + }, + }, + }, + }, + } + testFailedPod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-failed-pod", + Namespace: "test", + UID: "yyyyyyyy", + }, + Status: corev1.PodStatus{ + Phase: corev1.PodFailed, + ContainerStatuses: []corev1.ContainerStatus{ + { + Name: "test-container", + ContainerID: testContainerID, + }, + }, + }, + } + + type fields struct { + podFilterOption framework.PodFilter + getPodMetas []*statesinformer.PodMeta + initPodLastStat func(lastState *gocache.Cache) + initContainerLastStat func(lastState *gocache.Cache) + SetSysUtil func(helper *system.FileTestUtil) + } + tests := []struct { + name string + fields fields + wantMetrics bool + wantEnable bool + wantStarted bool + wantNodeMetric float64 + wantPodMetric float64 + wantContainerMetric float64 + }{ + { + name: "collect pagecache info", + wantMetrics: true, + fields: fields{ + podFilterOption: framework.DefaultPodFilter, + getPodMetas: []*statesinformer.PodMeta{ + { + CgroupDir: testPodMetaDir, + Pod: testPod, + }, + }, + initPodLastStat: func(lastState *gocache.Cache) { + lastState.Set(string(testPod.UID), framework.CPUStat{ + CPUUsage: 0, + Timestamp: testNow.Add(-time.Second), + }, gocache.DefaultExpiration) + }, + initContainerLastStat: func(lastState *gocache.Cache) { + lastState.Set(testContainerID, framework.CPUStat{ + CPUUsage: 0, + Timestamp: testNow.Add(-time.Second), + }, gocache.DefaultExpiration) + }, + SetSysUtil: func(helper *system.FileTestUtil) { + helper.WriteProcSubFileContents(system.ProcMemInfoName, meminfo) + helper.WriteCgroupFileContents(testPodParentDir, system.MemoryStat, testMemStat) + helper.WriteCgroupFileContents(testContainerParentDir, system.MemoryStat, testMemStat) + }, + }, + wantEnable: true, + wantStarted: true, + wantNodeMetric: (1048576 - 262144) << 10, + wantPodMetric: 104857600 + 0 + 0 + 104857600 + 0, + wantContainerMetric: 104857600 + 0 + 0 + 104857600 + 0, + }, + { + name: "test failed pod and failed container", + wantMetrics: false, + fields: fields{ + podFilterOption: framework.DefaultPodFilter, + getPodMetas: []*statesinformer.PodMeta{ + { + CgroupDir: testPodMetaDir, + Pod: testPod, + }, + { + Pod: testFailedPod, + }, + }, + initPodLastStat: func(lastState *gocache.Cache) { + lastState.Set(string(testPod.UID), framework.CPUStat{ + CPUUsage: 0, + Timestamp: testNow.Add(-time.Second), + }, gocache.DefaultExpiration) + }, + initContainerLastStat: func(lastState *gocache.Cache) { + lastState.Set(testContainerID, framework.CPUStat{ + CPUUsage: 0, + Timestamp: testNow.Add(-time.Second), + }, gocache.DefaultExpiration) + }, + SetSysUtil: func(helper *system.FileTestUtil) { + helper.WriteProcSubFileContents(system.ProcMemInfoName, meminfo) + helper.WriteCgroupFileContents(testPodParentDir, system.MemoryStat, testMemStat) + }, + }, + wantEnable: true, + wantStarted: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + helper := system.NewFileTestUtil(t) + defer helper.Cleanup() + if tt.fields.SetSysUtil != nil { + tt.fields.SetSysUtil(helper) + } + ctrl := gomock.NewController(t) + defer ctrl.Finish() + metricCache, err := metriccache.NewMetricCache(&metriccache.Config{ + TSDBPath: t.TempDir(), + TSDBEnablePromMetrics: false, + }) + assert.NoError(t, err) + defer func() { + metricCache.Close() + }() + statesInformer := mock_statesinformer.NewMockStatesInformer(ctrl) + statesInformer.EXPECT().HasSynced().Return(true).AnyTimes() + statesInformer.EXPECT().GetAllPods().Return(tt.fields.getPodMetas).AnyTimes() + collector := New(&framework.Options{ + Config: &framework.Config{ + EnablePageCacheCollector: true, + }, + StatesInformer: statesInformer, + MetricCache: metricCache, + CgroupReader: resourceexecutor.NewCgroupReader(), + }) + c := collector.(*pageCacheCollector) + assert.NotPanics(t, func() { + c.collectPageCache() + }) + assert.NotPanics(t, func() { + c.Setup(&framework.Context{}) + }) + assert.Equal(t, tt.wantEnable, c.Enabled()) + assert.Equal(t, tt.wantStarted, c.Started()) + if tt.wantMetrics { + nodeGot := testGetNodePageCacheMetrics(t, metricCache, testNow, 5*time.Second) + assert.Equal(t, tt.wantNodeMetric, nodeGot) + podGot := testGetPodPageCacheMetrics(t, metricCache, string(c.statesInformer.GetAllPods()[0].Pod.UID), testNow, 5*time.Second) + assert.Equal(t, tt.wantPodMetric, podGot) + containerGot := testGetContainerPageCacheMetrics(t, metricCache, c.statesInformer.GetAllPods()[0].Pod.Status.ContainerStatuses[0].ContainerID, testNow, 5*time.Second) + assert.Equal(t, tt.wantContainerMetric, containerGot) + } + }) + + } +} + +func testGetNodePageCacheMetrics(t *testing.T, metricCache metriccache.TSDBStorage, testNow time.Time, d time.Duration) float64 { + testStart := testNow.Add(-d) + testEnd := testNow.Add(d) + queryParam := metriccache.QueryParam{ + Start: &testStart, + End: &testEnd, + Aggregate: metriccache.AggregationTypeAVG, + } + querier, err := metricCache.Querier(*queryParam.Start, *queryParam.End) + assert.NoError(t, err) + nodeMemWithPageCacheAggregateResult, err := testQuery(querier, metriccache.NodeMemoryUsageWithPageCacheMetric, nil) + assert.NoError(t, err) + nodeMemWithPageCacheUsed, err := nodeMemWithPageCacheAggregateResult.Value(queryParam.Aggregate) + assert.NoError(t, err) + return nodeMemWithPageCacheUsed +} + +func testGetPodPageCacheMetrics(t *testing.T, metricCache metriccache.TSDBStorage, podUID string, testNow time.Time, d time.Duration) float64 { + testStart := testNow.Add(-d) + testEnd := testNow.Add(d) + queryParam := metriccache.QueryParam{ + Start: &testStart, + End: &testEnd, + Aggregate: metriccache.AggregationTypeAVG, + } + querier, err := metricCache.Querier(*queryParam.Start, *queryParam.End) + assert.NoError(t, err) + podMemWithPageCacheAggregateResult, err := testQuery(querier, metriccache.PodMemoryUsageWithPageCacheMetric, metriccache.MetricPropertiesFunc.Pod(podUID)) + assert.NoError(t, err) + podMemWithPageCacheUsed, err := podMemWithPageCacheAggregateResult.Value(queryParam.Aggregate) + assert.NoError(t, err) + return podMemWithPageCacheUsed +} + +func testGetContainerPageCacheMetrics(t *testing.T, metricCache metriccache.TSDBStorage, containerUID string, testNow time.Time, d time.Duration) float64 { + testStart := testNow.Add(-d) + testEnd := testNow.Add(d) + queryParam := metriccache.QueryParam{ + Start: &testStart, + End: &testEnd, + Aggregate: metriccache.AggregationTypeAVG, + } + querier, err := metricCache.Querier(*queryParam.Start, *queryParam.End) + assert.NoError(t, err) + containerMemWithPageCacheAggregateResult, err := testQuery(querier, metriccache.ContainerMemoryUsageWithPageCacheMetric, metriccache.MetricPropertiesFunc.Container(containerUID)) + assert.NoError(t, err) + containerMemWithPageCacheUsed, err := containerMemWithPageCacheAggregateResult.Value(queryParam.Aggregate) + assert.NoError(t, err) + return containerMemWithPageCacheUsed +} + +func testQuery(querier metriccache.Querier, resource metriccache.MetricResource, properties map[metriccache.MetricProperty]string) (metriccache.AggregateResult, error) { + queryMeta, err := resource.BuildQueryMeta(properties) + if err != nil { + return nil, err + } + aggregateResult := metriccache.DefaultAggregateResultFactory.New(queryMeta) + if err = querier.Query(queryMeta, nil, aggregateResult); err != nil { + return nil, err + } + return aggregateResult, nil +} diff --git a/pkg/koordlet/resourceexecutor/reader_test.go b/pkg/koordlet/resourceexecutor/reader_test.go index b852c8612..87df0a6e2 100644 --- a/pkg/koordlet/resourceexecutor/reader_test.go +++ b/pkg/koordlet/resourceexecutor/reader_test.go @@ -1377,7 +1377,7 @@ func TestCgroupReader_ReadColdPageUsage(t *testing.T) { # ||||| [1,2) [2,5) [5,15) [15,30) [30,60) [60,120) [120,240) [240,+inf) csei 2613248 4657152 18182144 293683200 0 0 0 0 dsei 2568192 5140480 15306752 48648192 0 0 0 0 - cfei 2633728 4640768 66531328 340172800 0 0 0 0 + cfei 2633728 4640768 66531328 340172800 0 52834304 0 0 dfei 0 0 4096 0 0 0 0 0 csui 0 0 0 0 0 0 0 0 dsui 0 0 0 0 0 0 0 0 @@ -1396,7 +1396,7 @@ func TestCgroupReader_ReadColdPageUsage(t *testing.T) { args: args{ parentDir: "/kubepods.slice", }, - want: uint64(340172800), + want: uint64(52834304), wantErr: false, }, { diff --git a/pkg/koordlet/util/cold_page.go b/pkg/koordlet/util/cold_page.go index 3f44ab1c3..e6071adc9 100644 --- a/pkg/koordlet/util/cold_page.go +++ b/pkg/koordlet/util/cold_page.go @@ -25,7 +25,7 @@ func GetNodeMemUsageWithHotPageCache(coldPageUsageBytes uint64) (uint64, error) if err != nil { return 0, err } - return memInfo.MemTotal*1024 - memInfo.MemAvailable*1024 + memInfo.ActiveFile*1024 + memInfo.InactiveFile*1024 - coldPageUsageBytes, nil + return memInfo.MemTotal*1024 - memInfo.MemFree*1024 - coldPageUsageBytes, nil } func GetCgroupMemUsageWithHotPageCache(cgroupReader resourceexecutor.CgroupReader, parentDir string, coldPageUsageBytes uint64) (uint64, error) { diff --git a/pkg/koordlet/util/cold_page_test.go b/pkg/koordlet/util/cold_page_test.go index 4b531c705..ea44b894c 100644 --- a/pkg/koordlet/util/cold_page_test.go +++ b/pkg/koordlet/util/cold_page_test.go @@ -90,7 +90,7 @@ DirectMap1G: 261095424 kB` helper.WriteProcSubFileContents(system.ProcMemInfoName, testMemInfo) }, }, - want: uint64((263432804-256703236+2496524+2222452)<<10) - uint64(100), + want: uint64((263432804-254391744)<<10) - uint64(100), wantErr: false, }, { diff --git a/pkg/koordlet/util/meminfo.go b/pkg/koordlet/util/meminfo.go index 6d6279fd3..b4dd8edae 100644 --- a/pkg/koordlet/util/meminfo.go +++ b/pkg/koordlet/util/meminfo.go @@ -91,8 +91,8 @@ func (i *MemInfo) MemUsageBytes() uint64 { // MemWithPageCacheUsageBytes returns the usage of mem with page cache bytes. func (i *MemInfo) MemUsageWithPageCache() uint64 { - // total - available - return (i.MemTotal - i.MemAvailable + i.ActiveFile + i.InactiveFile) * 1024 + // total - free + return (i.MemTotal - i.MemFree) * 1024 } // readMemInfo reads and parses the meminfo from the given file. diff --git a/pkg/koordlet/util/meminfo_test.go b/pkg/koordlet/util/meminfo_test.go index df0b4db29..b576e0e6a 100644 --- a/pkg/koordlet/util/meminfo_test.go +++ b/pkg/koordlet/util/meminfo_test.go @@ -340,7 +340,7 @@ DirectMap1G: 261095424 kB` got = memInfo.MemUsageBytes() assert.Equal(t, uint64((263432804-256703236)<<10), got) got = memInfo.MemUsageWithPageCache() - assert.Equal(t, uint64((263432804-256703236+2496524+2222452)<<10), got) + assert.Equal(t, uint64((263432804-254391744)<<10), got) } func TestGetNUMAMemInfo(t *testing.T) { diff --git a/pkg/koordlet/util/system/kidled_util.go b/pkg/koordlet/util/system/kidled_util.go index 8b8827e17..6dc008d32 100644 --- a/pkg/koordlet/util/system/kidled_util.go +++ b/pkg/koordlet/util/system/kidled_util.go @@ -34,7 +34,7 @@ var ( kidledColdBoundary = defaultKidledColdBoundary defaultKidledScanPeriodInseconds uint32 = 5 defaultKidledUseHierarchy uint8 = 1 - defaultKidledColdBoundary int = 3 + defaultKidledColdBoundary int = 5 ) // the unit of Csei, Dsei, Cfei ... is byte diff --git a/pkg/koordlet/util/system/kidled_util_test.go b/pkg/koordlet/util/system/kidled_util_test.go index 59f9e3d08..3dc46f8ed 100644 --- a/pkg/koordlet/util/system/kidled_util_test.go +++ b/pkg/koordlet/util/system/kidled_util_test.go @@ -176,15 +176,15 @@ func Test_GetColdPageTotalBytes(t *testing.T) { # |||| / # ||||| [1,2) [2,5) [5,15) [15,30) [30,60) [60,120) [120,240) [240,+inf) csei 2613248 4657152 18182144 293683200 0 0 0 0 - dsei 2568192 5140480 15306752 48648192 0 0 0 0 - cfei 2633728 4640768 66531328 340172800 0 0 0 0 + dsei 2568192 5140480 15306752 48648192 52834304 52834304 0 0 + cfei 2633728 4640768 66531328 340172800 52834304 52834304 0 0 dfei 0 0 4096 0 0 0 0 0 csui 0 0 0 0 0 0 0 0 dsui 0 0 0 0 0 0 0 0 cfui 0 0 0 0 0 0 0 0 dfui 0 0 0 0 0 0 0 0 - csea 765952 1044480 3784704 52834304 0 0 0 0 - dsea 286720 270336 1564672 5390336 0 0 0 0 + csea 765952 1044480 3784704 52834304 52834304 0 0 0 + dsea 286720 270336 1564672 5390336 52834304 0 0 0 cfea 9273344 16609280 152109056 315121664 0 0 0 0 dfea 0 0 0 0 0 0 0 0 csua 0 0 0 0 0 0 0 0 @@ -196,7 +196,7 @@ func Test_GetColdPageTotalBytes(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, coldPageInfo) got := coldPageInfo.GetColdPageTotalBytes() - assert.Equal(t, uint64(340172800), got) + assert.Equal(t, uint64(52834304), got) } func Test_SetKidledScanPeriodInSeconds(t *testing.T) {