Skip to content

Commit 520aadd

Browse files
committed
libcontainer/intelrdt: add support for EnableMonitoring field
The linux.intelRdt.enableMonitoring field enables the creation of a per-container monitoring group. The monitoring group is removed when the container is destroyed. Signed-off-by: Markus Lehtonen <[email protected]>
1 parent b90ea53 commit 520aadd

File tree

5 files changed

+198
-29
lines changed

5 files changed

+198
-29
lines changed

libcontainer/configs/intelrdt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ type IntelRdt struct {
1313
// The unit of memory bandwidth is specified in "percentages" by
1414
// default, and in "MBps" if MBA Software Controller is enabled.
1515
MemBwSchema string `json:"memBwSchema,omitempty"`
16+
17+
// Create a monitoring group for the container.
18+
EnableMonitoring bool `json:"enableMonitoring,omitempty"`
1619
}

libcontainer/container_linux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ type State struct {
7373

7474
// Intel RDT "resource control" filesystem path.
7575
IntelRdtPath string `json:"intel_rdt_path,omitempty"`
76+
77+
// Path of the container spefific monitoring group in resctrl filesystem.
78+
// Empty if the container does not have aindividual dedicated monitoring
79+
// group.
80+
IntelRdtMonPath string `json:"intel_rdt_mon_path,omitempty"`
7681
}
7782

7883
// ID returns the container's unique ID
@@ -938,8 +943,10 @@ func (c *Container) currentState() *State {
938943
}
939944

940945
intelRdtPath := ""
946+
intelRdtMonPath := ""
941947
if c.intelRdtManager != nil {
942948
intelRdtPath = c.intelRdtManager.GetPath()
949+
intelRdtMonPath = c.intelRdtManager.GetMonPath()
943950
}
944951
state := &State{
945952
BaseState: BaseState{
@@ -952,6 +959,7 @@ func (c *Container) currentState() *State {
952959
Rootless: c.config.RootlessEUID && c.config.RootlessCgroups,
953960
CgroupPaths: c.cgroupManager.GetPaths(),
954961
IntelRdtPath: intelRdtPath,
962+
IntelRdtMonPath: intelRdtMonPath,
955963
NamespacePaths: make(map[configs.NamespaceType]string),
956964
ExternalDescriptors: externalDescriptors,
957965
}

libcontainer/intelrdt/intelrdt.go

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,16 @@ func (m *Manager) Apply(pid int) (err error) {
474474
return newLastCmdError(err)
475475
}
476476

477+
// Create MON group
478+
if monPath := m.GetMonPath(); monPath != "" {
479+
if err := os.MkdirAll(monPath, 0o755); err != nil {
480+
return newLastCmdError(err)
481+
}
482+
if err := WriteIntelRdtTasks(monPath, pid); err != nil {
483+
return newLastCmdError(err)
484+
}
485+
}
486+
477487
m.path = path
478488
return nil
479489
}
@@ -483,13 +493,21 @@ func (m *Manager) Destroy() error {
483493
// Don't remove resctrl group if closid has been explicitly specified. The
484494
// group is likely externally managed, i.e. by some other entity than us.
485495
// There are probably other containers/tasks sharing the same group.
486-
if m.config.IntelRdt != nil && m.config.IntelRdt.ClosID == "" {
487-
m.mu.Lock()
488-
defer m.mu.Unlock()
489-
if err := os.RemoveAll(m.GetPath()); err != nil {
490-
return err
496+
if m.config.IntelRdt != nil {
497+
if m.config.IntelRdt.ClosID == "" {
498+
m.mu.Lock()
499+
defer m.mu.Unlock()
500+
if err := os.RemoveAll(m.GetPath()); err != nil {
501+
return err
502+
}
503+
m.path = ""
504+
} else if monPath := m.GetMonPath(); monPath != "" {
505+
// If ClosID is not specified the possible monintoring group was
506+
// removed with the CLOS above.
507+
if err := os.RemoveAll(monPath); err != nil {
508+
return err
509+
}
491510
}
492-
m.path = ""
493511
}
494512
return nil
495513
}
@@ -503,6 +521,16 @@ func (m *Manager) GetPath() string {
503521
return m.path
504522
}
505523

524+
// GetMonPath returns path of the monitoring group of the container. Returns an
525+
// empty string if the container does not have a individual dedicated
526+
// monitoring group.
527+
func (m *Manager) GetMonPath() string {
528+
if closPath := m.GetPath(); closPath != "" && m.config.IntelRdt.EnableMonitoring {
529+
return filepath.Join(closPath, "mon_groups", m.id)
530+
}
531+
return ""
532+
}
533+
506534
// GetStats returns statistics for Intel RDT.
507535
func (m *Manager) GetStats() (*Stats, error) {
508536
// If intelRdt is not specified in config
@@ -580,7 +608,16 @@ func (m *Manager) GetStats() (*Stats, error) {
580608
}
581609

582610
if IsMBMEnabled() || IsCMTEnabled() {
583-
err = getMonitoringStats(containerPath, stats)
611+
monPath := m.GetMonPath()
612+
if monPath == "" {
613+
// NOTE: If per-container monitoring is not enabled, the monitoring
614+
// data we get here might have little to do with this container as
615+
// there might be anything from this single container to the half
616+
// of the system assigned in the group. Should consider not
617+
// exposing stats in this case(?)
618+
monPath = containerPath
619+
}
620+
err = getMonitoringStats(monPath, stats)
584621
if err != nil {
585622
return nil, err
586623
}

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 139 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"path/filepath"
66
"strings"
77
"testing"
8+
9+
"github.com/opencontainers/runc/libcontainer/configs"
810
)
911

1012
func TestIntelRdtSetL3CacheSchema(t *testing.T) {
@@ -98,30 +100,148 @@ func TestIntelRdtSetMemBwScSchema(t *testing.T) {
98100
}
99101

100102
func TestApply(t *testing.T) {
101-
helper := NewIntelRdtTestUtil(t)
102-
103103
const closID = "test-clos"
104-
105-
helper.config.IntelRdt.ClosID = closID
106-
intelrdt := newManager(helper.config, "", helper.IntelRdtPath)
107-
if err := intelrdt.Apply(1234); err == nil {
108-
t.Fatal("unexpected success when applying pid")
104+
// TC-1: failure because non-pre-existing CLOS
105+
{
106+
helper := NewIntelRdtTestUtil(t)
107+
helper.config.IntelRdt = &configs.IntelRdt{
108+
ClosID: closID,
109+
}
110+
111+
intelrdt := newManager(helper.config, "", "")
112+
if err := intelrdt.Apply(1234); err == nil {
113+
t.Fatal("unexpected success when applying pid")
114+
}
115+
closPath := filepath.Join(intelRdtRoot, closID)
116+
if _, err := os.Stat(closPath); err == nil {
117+
t.Fatal("closid dir should not exist")
118+
}
109119
}
110-
if _, err := os.Stat(filepath.Join(helper.IntelRdtPath, closID)); err == nil {
111-
t.Fatal("closid dir should not exist")
120+
// TC-2: CLOS dir should be created if some schema has been specified
121+
{
122+
helper := NewIntelRdtTestUtil(t)
123+
helper.config.IntelRdt = &configs.IntelRdt{
124+
ClosID: closID,
125+
L3CacheSchema: "L3:0=f",
126+
}
127+
128+
intelrdt := newManager(helper.config, "", "")
129+
if err := intelrdt.Apply(1235); err != nil {
130+
t.Fatalf("Apply() failed: %v", err)
131+
}
132+
133+
closPath := filepath.Join(intelRdtRoot, closID)
134+
pids, err := getIntelRdtParamString(closPath, "tasks")
135+
if err != nil {
136+
t.Fatalf("failed to read tasks file: %v", err)
137+
}
138+
if pids != "1235" {
139+
t.Fatalf("unexpected tasks file, expected '1235', got %q", pids)
140+
}
112141
}
113-
114-
// Dir should be created if some schema has been specified
115-
intelrdt.config.IntelRdt.L3CacheSchema = "L3:0=f"
116-
if err := intelrdt.Apply(1235); err != nil {
117-
t.Fatalf("Apply() failed: %v", err)
142+
// TC-3: clos and monitoring group should be created if EnableMonitoring is true
143+
{
144+
helper := NewIntelRdtTestUtil(t)
145+
helper.config.IntelRdt = &configs.IntelRdt{
146+
EnableMonitoring: true,
147+
}
148+
id := "aaaa-bbbb"
149+
150+
intelrdt := newManager(helper.config, id, "")
151+
if err := intelrdt.Apply(1236); err != nil {
152+
t.Fatalf("Apply() failed: %v", err)
153+
}
154+
155+
closPath := filepath.Join(intelRdtRoot, id)
156+
pids, err := getIntelRdtParamString(closPath, "tasks")
157+
if err != nil {
158+
t.Fatalf("failed to read tasks file: %v", err)
159+
}
160+
if pids != "1236" {
161+
t.Fatalf("unexpected tasks file, expected '1236', got %q", pids)
162+
}
118163
}
164+
}
119165

120-
pids, err := getIntelRdtParamString(intelrdt.GetPath(), "tasks")
121-
if err != nil {
122-
t.Fatalf("failed to read tasks file: %v", err)
166+
func TestDestroy(t *testing.T) {
167+
const closID = "test-clos"
168+
169+
// TC-1: per-container CLOS dir should be removed
170+
{
171+
helper := NewIntelRdtTestUtil(t)
172+
id := "abcd-efgh"
173+
174+
intelrdt := newManager(helper.config, id, "")
175+
if err := intelrdt.Apply(1234); err != nil {
176+
t.Fatalf("Apply() failed: %v", err)
177+
}
178+
closPath := filepath.Join(intelRdtRoot, id)
179+
if _, err := os.Stat(closPath); err != nil {
180+
t.Fatal("CLOS dir should exist")
181+
}
182+
if err := intelrdt.Destroy(); err != nil {
183+
t.Fatalf("Destroy() failed: %v", err)
184+
}
185+
if _, err := os.Stat(closPath); err == nil {
186+
t.Fatal("CLOS dir should not exist")
187+
}
188+
}
189+
// TC-2: pre-existing CLOS should not be removed
190+
{
191+
helper := NewIntelRdtTestUtil(t)
192+
helper.config.IntelRdt = &configs.IntelRdt{
193+
ClosID: closID,
194+
}
195+
196+
closPath := filepath.Join(intelRdtRoot, closID)
197+
if err := os.MkdirAll(closPath, 0o755); err != nil {
198+
t.Fatal(err)
199+
}
200+
201+
intelrdt := newManager(helper.config, "", "")
202+
if err := intelrdt.Apply(1234); err != nil {
203+
t.Fatalf("Apply() failed: %v", err)
204+
}
205+
if _, err := os.Stat(closPath); err != nil {
206+
t.Fatal("CLOS dir should exist")
207+
}
208+
if err := intelrdt.Destroy(); err != nil {
209+
t.Fatalf("Destroy() failed: %v", err)
210+
}
211+
if _, err := os.Stat(closPath); err != nil {
212+
t.Fatal("CLOS dir should exist")
213+
}
123214
}
124-
if pids != "1235" {
125-
t.Fatalf("unexpected tasks file, expected '1235', got %q", pids)
215+
// TC-3: per-container MON dir in pre-existing CLOS should be removed
216+
{
217+
helper := NewIntelRdtTestUtil(t)
218+
helper.config.IntelRdt = &configs.IntelRdt{
219+
ClosID: closID,
220+
EnableMonitoring: true,
221+
}
222+
id := "abcd-efgh"
223+
224+
closPath := filepath.Join(intelRdtRoot, closID)
225+
if err := os.MkdirAll(closPath, 0o755); err != nil {
226+
t.Fatal(err)
227+
}
228+
229+
intelrdt := newManager(helper.config, id, "")
230+
if err := intelrdt.Apply(1234); err != nil {
231+
t.Fatalf("Apply() failed: %v", err)
232+
}
233+
monPath := filepath.Join(closPath, "mon_groups", id)
234+
if _, err := os.Stat(monPath); err != nil {
235+
t.Fatal("MON dir should exist")
236+
}
237+
if err := intelrdt.Destroy(); err != nil {
238+
t.Fatalf("Destroy() failed: %v", err)
239+
}
240+
if _, err := os.Stat(closPath); err != nil {
241+
t.Fatalf("CLOS dir should exist: %f", err)
242+
}
243+
if _, err := os.Stat(monPath); err == nil {
244+
t.Fatal("MON dir should not exist")
245+
}
126246
}
127247
}

libcontainer/specconv/spec_linux.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,10 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) {
462462
}
463463
if spec.Linux.IntelRdt != nil {
464464
config.IntelRdt = &configs.IntelRdt{
465-
ClosID: spec.Linux.IntelRdt.ClosID,
466-
L3CacheSchema: spec.Linux.IntelRdt.L3CacheSchema,
467-
MemBwSchema: spec.Linux.IntelRdt.MemBwSchema,
465+
ClosID: spec.Linux.IntelRdt.ClosID,
466+
L3CacheSchema: spec.Linux.IntelRdt.L3CacheSchema,
467+
MemBwSchema: spec.Linux.IntelRdt.MemBwSchema,
468+
EnableMonitoring: spec.Linux.IntelRdt.EnableMonitoring,
468469
}
469470
}
470471
if spec.Linux.Personality != nil {

0 commit comments

Comments
 (0)