Skip to content

Commit

Permalink
chore(cgroup): fix memory policy build not in linux
Browse files Browse the repository at this point in the history
  • Loading branch information
luomingmeng committed Oct 30, 2024
1 parent 0c234bf commit 73f602d
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 105 deletions.
70 changes: 0 additions & 70 deletions pkg/util/cgroup/manager/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@ import (
"math"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"syscall"
"time"
"unsafe"

"k8s.io/klog/v2"

"github.com/kubewharf/katalyst-core/pkg/consts"
"github.com/kubewharf/katalyst-core/pkg/metrics"
"github.com/kubewharf/katalyst-core/pkg/util/asyncworker"
"github.com/kubewharf/katalyst-core/pkg/util/bitmask"
"github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
"github.com/kubewharf/katalyst-core/pkg/util/eventbus"
"github.com/kubewharf/katalyst-core/pkg/util/general"
Expand Down Expand Up @@ -492,70 +486,6 @@ func DisableSwapMaxWithAbsolutePathRecursive(absCgroupPath string) error {
return nil
}

func GetMemPolicy(addr unsafe.Pointer, flags int) (mode int, nodemask bitmask.BitMask, err error) {
var mask uint64
_, _, errno := syscall.Syscall6(syscall.SYS_GET_MEMPOLICY,
uintptr(unsafe.Pointer(&mode)), uintptr(unsafe.Pointer(&mask)), 64, uintptr(addr), uintptr(flags), 0)
if errno != 0 {
err = errno
}
nodemask = bitmask.NewEmptyBitMask()
bit := 0
for mask != 0 {
if mask&1 == 1 {
nodemask.Add(bit)
}
mask >>= 1
bit++
}
return
}

func SetMemPolicy(mode int, nodemask bitmask.BitMask) (err error) {
var mask uint64
for _, bit := range nodemask.GetBits() {
mask |= 1 << uint64(bit)
}
_, _, errno := syscall.Syscall(syscall.SYS_SET_MEMPOLICY, uintptr(mode), uintptr(unsafe.Pointer(&mask)), 64)
if errno != 0 {
err = errno
}
return
}

func doReclaimMemory(cmd string, mems machine.CPUSet) error {
// When memory is reclaimed by calling memory.reclaim interface and offloaded to zram,
// kernel will allocate additional zram memory at the NUMAs allowed for the caller process to store the compressed memory.
// However, the allowed NUMA nodes of katalyst and business processes may not be consistent,
// which will lead to the allocation of zram memory on NUMAs that are not affinity to the business processes,
// bringing unpredictable problems. Therefore, before reclaiming business memory,
// we have to temporarily set the allowed NUMAs of katalyst same as business workload, and restore it after reclaiming.
runtime.LockOSThread()
defer runtime.UnlockOSThread()

// save original memory policy
mode, mask, err := GetMemPolicy(nil, 0)
if err != nil {
return err
}

newMask := bitmask.NewEmptyBitMask()
newMask.Add(mems.ToSliceInt()...)

if err := SetMemPolicy(MPOL_BIND, newMask); err != nil {
return err
}

_, err = exec.Command("bash", "-c", cmd).Output()
klog.ErrorS(err, "failed to exec %v", cmd)

// restore original memory policy
if err := SetMemPolicy(mode, mask); err != nil {
return err
}
return nil
}

func MemoryOffloadingWithAbsolutePath(ctx context.Context, absCgroupPath string, nbytes int64, mems machine.CPUSet) error {
startTime := time.Now()

Expand Down
35 changes: 0 additions & 35 deletions pkg/util/cgroup/manager/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ import (
"math"
"os"
"path/filepath"
"runtime"
"testing"

"bou.ke/monkey"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/stretchr/testify/assert"

"github.com/kubewharf/katalyst-core/pkg/util/bitmask"
"github.com/kubewharf/katalyst-core/pkg/util/cgroup/common"
v1 "github.com/kubewharf/katalyst-core/pkg/util/cgroup/manager/v1"
v2 "github.com/kubewharf/katalyst-core/pkg/util/cgroup/manager/v2"
Expand Down Expand Up @@ -236,39 +234,6 @@ func testMemPressureV1(t *testing.T) {
assert.Equal(t, "0", fmt.Sprint(some.Avg300))
}

func TestMemoryPolicy(t *testing.T) {
t.Parallel()

runtime.LockOSThread()
defer runtime.UnlockOSThread()

mode, mask, err := GetMemPolicy(nil, 0)
assert.NoError(t, err)

assert.Equal(t, MPOL_DEFAULT, mode)

t.Logf("mask: %v", mask)

mems := machine.NewCPUSet(0)
newMask := bitmask.NewEmptyBitMask()
newMask.Add(mems.ToSliceInt()...)

err = SetMemPolicy(MPOL_BIND, newMask)
assert.NoError(t, err)

mode, mask, err = GetMemPolicy(nil, 0)
assert.NoError(t, err)

assert.Equal(t, MPOL_BIND, mode)

expectMask, err := bitmask.NewBitMask(0)
assert.NoError(t, err)

assert.Equal(t, expectMask, mask)

t.Logf("mask: %v", mask)
}

func testMemoryOffloadingWithAbsolutePath(t *testing.T) {
defer monkey.UnpatchAll()
monkey.Patch(common.CheckCgroup2UnifiedMode, func() bool { return true })
Expand Down
96 changes: 96 additions & 0 deletions pkg/util/cgroup/manager/memory_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//go:build linux
// +build linux

/*
Copyright 2022 The Katalyst 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 manager

import (
"os/exec"
"runtime"
"syscall"
"unsafe"

"k8s.io/klog/v2"

"github.com/kubewharf/katalyst-core/pkg/util/bitmask"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)

func GetMemPolicy(addr unsafe.Pointer, flags int) (mode int, nodemask bitmask.BitMask, err error) {
var mask uint64
_, _, errno := syscall.Syscall6(syscall.SYS_GET_MEMPOLICY,
uintptr(unsafe.Pointer(&mode)), uintptr(unsafe.Pointer(&mask)), 64, uintptr(addr), uintptr(flags), 0)
if errno != 0 {
err = errno
}
nodemask = bitmask.NewEmptyBitMask()
bit := 0
for mask != 0 {
if mask&1 == 1 {
nodemask.Add(bit)
}
mask >>= 1
bit++
}
return
}

func SetMemPolicy(mode int, nodemask bitmask.BitMask) (err error) {
var mask uint64
for _, bit := range nodemask.GetBits() {
mask |= 1 << uint64(bit)
}
_, _, errno := syscall.Syscall(syscall.SYS_SET_MEMPOLICY, uintptr(mode), uintptr(unsafe.Pointer(&mask)), 64)
if errno != 0 {
err = errno
}
return
}

func doReclaimMemory(cmd string, mems machine.CPUSet) error {
// When memory is reclaimed by calling memory.reclaim interface and offloaded to zram,
// kernel will allocate additional zram memory at the NUMAs allowed for the caller process to store the compressed memory.
// However, the allowed NUMA nodes of katalyst and business processes may not be consistent,
// which will lead to the allocation of zram memory on NUMAs that are not affinity to the business processes,
// bringing unpredictable problems. Therefore, before reclaiming business memory,
// we have to temporarily set the allowed NUMAs of katalyst same as business workload, and restore it after reclaiming.
runtime.LockOSThread()
defer runtime.UnlockOSThread()

// save original memory policy
mode, mask, err := GetMemPolicy(nil, 0)
if err != nil {
return err
}

newMask := bitmask.NewEmptyBitMask()
newMask.Add(mems.ToSliceInt()...)

if err := SetMemPolicy(MPOL_BIND, newMask); err != nil {
return err
}

_, err = exec.Command("bash", "-c", cmd).Output()
klog.ErrorS(err, "failed to exec %v", cmd)

// restore original memory policy
if err := SetMemPolicy(mode, mask); err != nil {
return err
}
return nil
}
63 changes: 63 additions & 0 deletions pkg/util/cgroup/manager/memory_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//go:build linux
// +build linux

/*
Copyright 2022 The Katalyst 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 manager

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"

"github.com/kubewharf/katalyst-core/pkg/util/bitmask"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)

func TestMemoryPolicy(t *testing.T) {
t.Parallel()

runtime.LockOSThread()
defer runtime.UnlockOSThread()

mode, mask, err := GetMemPolicy(nil, 0)
assert.NoError(t, err)

assert.Equal(t, MPOL_DEFAULT, mode)

t.Logf("mask: %v", mask)

mems := machine.NewCPUSet(0)
newMask := bitmask.NewEmptyBitMask()
newMask.Add(mems.ToSliceInt()...)

err = SetMemPolicy(MPOL_BIND, newMask)
assert.NoError(t, err)

mode, mask, err = GetMemPolicy(nil, 0)
assert.NoError(t, err)

assert.Equal(t, MPOL_BIND, mode)

expectMask, err := bitmask.NewBitMask(0)
assert.NoError(t, err)

assert.Equal(t, expectMask, mask)

t.Logf("mask: %v", mask)
}
40 changes: 40 additions & 0 deletions pkg/util/cgroup/manager/memory_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build !linux && !windows
// +build !linux,!windows

/*
Copyright 2022 The Katalyst 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 manager

import (
"fmt"
"unsafe"

"github.com/kubewharf/katalyst-core/pkg/util/bitmask"
"github.com/kubewharf/katalyst-core/pkg/util/machine"
)

func GetMemPolicy(addr unsafe.Pointer, flags int) (mode int, nodemask bitmask.BitMask, err error) {
return 0, bitmask.NewEmptyBitMask(), fmt.Errorf("GetMemPolicy is unsupported in this build")
}

func SetMemPolicy(mode int, nodemask bitmask.BitMask) (err error) {
return fmt.Errorf("GetMemPolicy is unsupported in this build")
}

func doReclaimMemory(cmd string, mems machine.CPUSet) error {
return fmt.Errorf("doReclaimMemory is unsupported in this build")
}

0 comments on commit 73f602d

Please sign in to comment.