Skip to content

Commit 5ea59f9

Browse files
authored
Merge pull request #166 from askervin/5aW_mempolicy
Enable adjusting memory policy from NRI plugins
2 parents da6ea0e + b4b9795 commit 5ea59f9

File tree

14 files changed

+1575
-650
lines changed

14 files changed

+1575
-650
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ property was first available in.
276276
| Linux network devices | v0.11.0 | v2.2.1 | v1.35.0 |
277277
| Linux RDT CLOS adjustment | v0.11.0 | v2.2.1 | v1.35.0 |
278278
| Linux sysctl | v0.11.0 | v2.2.1 | v1.35.0 |
279+
| Linux memory policy | TODO | TODO | TODO |
279280

280281
### Container Updates
281282

pkg/adaptation/adaptation_suite_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,11 @@ var _ = Describe("Plugin container creation adjustments", func() {
593593
case "linux sysctl":
594594
a.SetLinuxSysctl("net.core.somaxconn", "256")
595595

596+
case "linux memory policy":
597+
a.SetLinuxMemoryPolicy(
598+
api.MpolMode_MPOL_INTERLEAVE, "0,1", api.MpolFlag_MPOL_F_STATIC_NODES,
599+
)
600+
596601
case "resources/cpu":
597602
a.SetLinuxCPUShares(123)
598603
a.SetLinuxCPUQuota(456)
@@ -889,6 +894,20 @@ var _ = Describe("Plugin container creation adjustments", func() {
889894
},
890895
),
891896

897+
Entry("adjust linux memory policy", "linux memory policy",
898+
&api.ContainerAdjustment{
899+
Linux: &api.LinuxContainerAdjustment{
900+
MemoryPolicy: &api.LinuxMemoryPolicy{
901+
Mode: api.MpolMode_MPOL_INTERLEAVE,
902+
Nodes: "0,1",
903+
Flags: []api.MpolFlag{
904+
api.MpolFlag_MPOL_F_STATIC_NODES,
905+
},
906+
},
907+
},
908+
},
909+
),
910+
892911
Entry("adjust CPU resources", "resources/cpu",
893912
&api.ContainerAdjustment{
894913
Linux: &api.LinuxContainerAdjustment{

pkg/adaptation/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type (
8888
LinuxResources = api.LinuxResources
8989
LinuxCPU = api.LinuxCPU
9090
LinuxMemory = api.LinuxMemory
91+
LinuxMemoryPolicy = api.LinuxMemoryPolicy
9192
LinuxDevice = api.LinuxDevice
9293
LinuxDeviceCgroup = api.LinuxDeviceCgroup
9394
LinuxIOPriority = api.LinuxIOPriority

pkg/adaptation/result.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ func (r *result) adjust(rpl *ContainerAdjustment, plugin string) error {
255255
if err := r.adjustRdt(rpl.Linux.Rdt, plugin); err != nil {
256256
return err
257257
}
258+
if err := r.adjustMemoryPolicy(rpl.Linux.MemoryPolicy, plugin); err != nil {
259+
return err
260+
}
258261
}
259262

260263
if err := r.adjustRlimits(rpl.Rlimits, plugin); err != nil {
@@ -1024,6 +1027,22 @@ func (r *result) adjustLinuxScheduler(sch *LinuxScheduler, plugin string) error
10241027
return nil
10251028
}
10261029

1030+
func (r *result) adjustMemoryPolicy(memoryPolicy *LinuxMemoryPolicy, plugin string) error {
1031+
if memoryPolicy == nil {
1032+
return nil
1033+
}
1034+
1035+
id := r.request.create.Container.Id
1036+
1037+
if err := r.owners.ClaimMemoryPolicy(id, plugin); err != nil {
1038+
return err
1039+
}
1040+
1041+
r.reply.adjust.Linux.MemoryPolicy = memoryPolicy
1042+
1043+
return nil
1044+
}
1045+
10271046
func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
10281047
create, id, adjust := r.request.create, r.request.create.Container.Id, r.reply.adjust
10291048
for _, l := range rlimits {

pkg/api/adjustment.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ func (a *ContainerAdjustment) RemoveLinuxNetDevice(hostDev string) {
180180
a.Linux.NetDevices[MarkForRemoval(hostDev)] = nil
181181
}
182182

183+
// SetLinuxMemoryPolicy records setting the Linux memory policy for a container.
184+
func (a *ContainerAdjustment) SetLinuxMemoryPolicy(mode MpolMode, nodes string, flags ...MpolFlag) {
185+
a.initLinuxMemoryPolicy()
186+
a.Linux.MemoryPolicy.Mode = mode
187+
a.Linux.MemoryPolicy.Nodes = nodes
188+
a.Linux.MemoryPolicy.Flags = slices.Clone(flags)
189+
}
190+
183191
// SetLinuxMemoryLimit records setting the memory limit for a container.
184192
func (a *ContainerAdjustment) SetLinuxMemoryLimit(value int64) {
185193
a.initLinuxResourcesMemory()
@@ -402,6 +410,13 @@ func (a *ContainerAdjustment) initLinuxNamespaces() {
402410
}
403411
}
404412

413+
func (a *ContainerAdjustment) initLinuxMemoryPolicy() {
414+
a.initLinux()
415+
if a.Linux.MemoryPolicy == nil {
416+
a.Linux.MemoryPolicy = &LinuxMemoryPolicy{}
417+
}
418+
}
419+
405420
func (a *ContainerAdjustment) initLinuxResources() {
406421
a.initLinux()
407422
if a.Linux.Resources == nil {

0 commit comments

Comments
 (0)