-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account for kernel reserved memory in capacity calculations
- Loading branch information
Showing
25 changed files
with
824 additions
and
517 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright 2016 The Kubernetes 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 gce | ||
|
||
// There should be no imports as it is used standalone in e2e tests | ||
|
||
const ( | ||
// MiB - MebiByte size (2^20) | ||
MiB = 1024 * 1024 | ||
// GiB - GibiByte size (2^30) | ||
GiB = 1024 * 1024 * 1024 | ||
|
||
// KubeletEvictionHardMemory is subtracted from capacity | ||
// when calculating allocatable (on top of kube-reserved). | ||
// Equals kubelet "evictionHard: {memory.available}" | ||
// We don't have a good place to get it from, but it has been hard-coded | ||
// to 100Mi since at least k8s 1.4. | ||
KubeletEvictionHardMemory = 100 * MiB | ||
|
||
// Kernel reserved memory is subtracted when calculating total memory. | ||
kernelReservedRatio = 64 | ||
kernelReservedMemory = 16 * MiB | ||
// Reserved memory for software IO TLB | ||
swiotlbReservedMemory = 64 * MiB | ||
swiotlbThresholdMemory = 3 * GiB | ||
) | ||
|
||
// CalculateKernelReserved computes how much memory Linux kernel will reserve. | ||
// TODO(jkaniuk): account for crashkernel reservation on RHEL / CentOS | ||
func CalculateKernelReserved(physicalMemory int64) int64 { | ||
// Account for memory reserved by kernel | ||
reserved := int64(physicalMemory / kernelReservedRatio) | ||
reserved += kernelReservedMemory | ||
// Account for software IO TLB allocation if memory requires 64bit addressing | ||
if physicalMemory > swiotlbThresholdMemory { | ||
reserved += swiotlbReservedMemory | ||
} | ||
return reserved | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2016 The Kubernetes 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 gce | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCalculateKernelReserved(t *testing.T) { | ||
type testCase struct { | ||
physicalMemory int64 | ||
reservedMemory int64 | ||
} | ||
testCases := []testCase{ | ||
{ | ||
physicalMemory: 256 * MiB, | ||
reservedMemory: 4*MiB + kernelReservedMemory, | ||
}, | ||
{ | ||
physicalMemory: 2 * GiB, | ||
reservedMemory: 32*MiB + kernelReservedMemory, | ||
}, | ||
{ | ||
physicalMemory: 3 * GiB, | ||
reservedMemory: 48*MiB + kernelReservedMemory, | ||
}, | ||
{ | ||
physicalMemory: 3.25 * GiB, | ||
reservedMemory: 52*MiB + kernelReservedMemory + swiotlbReservedMemory, | ||
}, | ||
{ | ||
physicalMemory: 4 * GiB, | ||
reservedMemory: 64*MiB + kernelReservedMemory + swiotlbReservedMemory, | ||
}, | ||
{ | ||
physicalMemory: 128 * GiB, | ||
reservedMemory: 2*GiB + kernelReservedMemory + swiotlbReservedMemory, | ||
}, | ||
} | ||
for idx, tc := range testCases { | ||
t.Run(fmt.Sprintf("%v", idx), func(t *testing.T) { | ||
reserved := CalculateKernelReserved(tc.physicalMemory) | ||
assert.Equal(t, tc.reservedMemory, reserved) | ||
}) | ||
} | ||
} |
Oops, something went wrong.