|
| 1 | +/* |
| 2 | +Copyright 2022 The Katalyst Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package userwatermark |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + |
| 25 | + "github.com/kubewharf/katalyst-core/pkg/util/cgroup/common" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + TestCGroupPath = "/sys/fs/cgroup/test" |
| 30 | + |
| 31 | + TestWatermarkScaleFactor = 100 |
| 32 | + TestSingleReclaimFactor = 0.25 |
| 33 | + TestSingleReclaimSize = 4096 |
| 34 | +) |
| 35 | + |
| 36 | +var calculatorMutex sync.Mutex |
| 37 | + |
| 38 | +func NewDefaultMemoryWatermarkCalculator() *WatermarkCalculator { |
| 39 | + return NewMemoryWatermarkCalculator(TestCGroupPath, TestWatermarkScaleFactor, TestSingleReclaimFactor, TestSingleReclaimSize) |
| 40 | +} |
| 41 | + |
| 42 | +func TestNewMemoryWatermarkCalculator(t *testing.T) { |
| 43 | + t.Parallel() |
| 44 | + |
| 45 | + calc := NewMemoryWatermarkCalculator(TestCGroupPath, TestWatermarkScaleFactor, TestSingleReclaimFactor, TestSingleReclaimSize) |
| 46 | + assert.Equal(t, TestCGroupPath, calc.CGroupPath) |
| 47 | + assert.Equal(t, uint64(TestWatermarkScaleFactor), calc.WatermarkScaleFactor) |
| 48 | + assert.Equal(t, TestSingleReclaimFactor, calc.SingleReclaimFactor) |
| 49 | + assert.Equal(t, uint64(TestSingleReclaimSize), calc.SingleReclaimSize) |
| 50 | +} |
| 51 | + |
| 52 | +func TestWatermarkCalculator_GetLowAndHighWatermark(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + wmc := NewDefaultMemoryWatermarkCalculator() |
| 56 | + capacity := uint64(1024 * 1024 * 1024) // 1GiB |
| 57 | + low := wmc.GetLowWatermark(capacity) |
| 58 | + high := wmc.GetHighWatermark(capacity) |
| 59 | + |
| 60 | + expectedLow := uint64(float64(capacity * wmc.WatermarkScaleFactor / 10000)) |
| 61 | + expectedHigh := uint64(float64(capacity * 2 * wmc.WatermarkScaleFactor / 10000)) |
| 62 | + |
| 63 | + assert.Equal(t, expectedLow, low) |
| 64 | + assert.Equal(t, expectedHigh, high) |
| 65 | + assert.True(t, high >= low) |
| 66 | +} |
| 67 | + |
| 68 | +func TestWatermarkCalculator_GetReclaimTarget(t *testing.T) { |
| 69 | + t.Parallel() |
| 70 | + |
| 71 | + wmc := NewDefaultMemoryWatermarkCalculator() |
| 72 | + memLimit := uint64(1000) |
| 73 | + |
| 74 | + // case 1: reclaimTarget > reclaimableMax |
| 75 | + memUsage := uint64(950) |
| 76 | + reclaimableMax := uint64(300) |
| 77 | + |
| 78 | + high := wmc.GetHighWatermark(memLimit) |
| 79 | + free := memLimit - memUsage |
| 80 | + expected := high - free |
| 81 | + |
| 82 | + if expected > reclaimableMax { |
| 83 | + expected = reclaimableMax |
| 84 | + } |
| 85 | + got := wmc.GetReclaimTarget(memLimit, memUsage, reclaimableMax) |
| 86 | + assert.Equal(t, expected, got) |
| 87 | + |
| 88 | + // case 2: reclaimTarget < reclaimableMax |
| 89 | + memUsage = 900 |
| 90 | + reclaimableMax = 999 |
| 91 | + |
| 92 | + high = wmc.GetHighWatermark(memLimit) |
| 93 | + free = memLimit - memUsage |
| 94 | + expected = high - free |
| 95 | + |
| 96 | + if expected > reclaimableMax { |
| 97 | + expected = reclaimableMax |
| 98 | + } |
| 99 | + |
| 100 | + got = wmc.GetReclaimTarget(memLimit, memUsage, reclaimableMax) |
| 101 | + assert.Equal(t, expected, got) |
| 102 | + |
| 103 | + // case 3: reclaimTarget == reclaimableMax |
| 104 | + memUsage = 800 |
| 105 | + reclaimableMax = 900 |
| 106 | + |
| 107 | + high = wmc.GetHighWatermark(memLimit) |
| 108 | + free = memLimit - memUsage |
| 109 | + expected = high - free |
| 110 | + |
| 111 | + if expected > reclaimableMax { |
| 112 | + expected = reclaimableMax |
| 113 | + } |
| 114 | + |
| 115 | + got = wmc.GetReclaimTarget(memLimit, memUsage, reclaimableMax) |
| 116 | + assert.Equal(t, expected, got) |
| 117 | +} |
| 118 | + |
| 119 | +func TestWatermarkCalculator_GetWatermark(t *testing.T) { |
| 120 | + t.Parallel() |
| 121 | + |
| 122 | + wmc := &WatermarkCalculator{WatermarkScaleFactor: 100} |
| 123 | + |
| 124 | + capacity := uint64(1024) |
| 125 | + |
| 126 | + low, high := wmc.GetWatermark(capacity) |
| 127 | + assert.Equal(t, wmc.GetLowWatermark(capacity), low) |
| 128 | + assert.Equal(t, wmc.GetHighWatermark(capacity), high) |
| 129 | +} |
| 130 | + |
| 131 | +func TestWatermarkCalculator_GetReclaimMax(t *testing.T) { |
| 132 | + t.Parallel() |
| 133 | + |
| 134 | + memStats := common.MemoryStats{ |
| 135 | + InactiveFile: 100, |
| 136 | + ActiveFile: 50, |
| 137 | + InactiveAnno: 200, |
| 138 | + ActiveAnno: 70, |
| 139 | + } |
| 140 | + |
| 141 | + wmc := &WatermarkCalculator{SwapEnabled: false} |
| 142 | + assert.Equal(t, uint64(150), wmc.GetReclaimMax(memStats)) |
| 143 | + |
| 144 | + wmc.SwapEnabled = true |
| 145 | + assert.Equal(t, uint64(420), wmc.GetReclaimMax(memStats)) |
| 146 | +} |
0 commit comments