Skip to content

Commit fd132e2

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 cff2336 commit fd132e2

File tree

6 files changed

+212
-29
lines changed

6 files changed

+212
-29
lines changed

features.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ var featuresCommand = cli.Command{
5656
Enabled: &t,
5757
},
5858
IntelRdt: &features.IntelRdt{
59-
Enabled: &t,
59+
Enabled: &t,
60+
Monitoring: &t,
6061
},
6162
MountExtensions: &features.MountExtensions{
6263
IDMap: &features.IDMap{

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 specific 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: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ func (m *Manager) Apply(pid int) (err error) {
458458
}
459459

460460
path := m.GetPath()
461+
fmt.Println("intelrdt: applying intelrdt path:", path, "pid:", pid)
461462

462463
m.mu.Lock()
463464
defer m.mu.Unlock()
@@ -478,6 +479,16 @@ func (m *Manager) Apply(pid int) (err error) {
478479
return newLastCmdError(err)
479480
}
480481

482+
// Create MON group
483+
if monPath := m.GetMonPath(); monPath != "" {
484+
if err := os.Mkdir(monPath, 0o755); err != nil && !os.IsExist(err) {
485+
return newLastCmdError(err)
486+
}
487+
if err := WriteIntelRdtTasks(monPath, pid); err != nil {
488+
return newLastCmdError(err)
489+
}
490+
}
491+
481492
m.path = path
482493
return nil
483494
}
@@ -487,13 +498,21 @@ func (m *Manager) Destroy() error {
487498
// Don't remove resctrl group if closid has been explicitly specified. The
488499
// group is likely externally managed, i.e. by some other entity than us.
489500
// There are probably other containers/tasks sharing the same group.
490-
if m.config.IntelRdt != nil && m.config.IntelRdt.ClosID == "" {
491-
m.mu.Lock()
492-
defer m.mu.Unlock()
493-
if err := os.Remove(m.GetPath()); err != nil && !os.IsNotExist(err) {
494-
return err
501+
if m.config.IntelRdt != nil {
502+
if m.config.IntelRdt.ClosID == "" {
503+
m.mu.Lock()
504+
defer m.mu.Unlock()
505+
if err := os.Remove(m.GetPath()); err != nil && !os.IsNotExist(err) {
506+
return err
507+
}
508+
m.path = ""
509+
} else if monPath := m.GetMonPath(); monPath != "" {
510+
// If ClosID is not specified the possible monintoring group was
511+
// removed with the CLOS above.
512+
if err := os.Remove(monPath); err != nil && !os.IsNotExist(err) {
513+
return err
514+
}
495515
}
496-
m.path = ""
497516
}
498517
return nil
499518
}
@@ -504,6 +523,20 @@ func (m *Manager) GetPath() string {
504523
return m.path
505524
}
506525

526+
// GetMonPath returns path of the monitoring group of the container. Returns an
527+
// empty string if the container does not have a individual dedicated
528+
// monitoring group.
529+
func (m *Manager) GetMonPath() string {
530+
if closPath := m.GetPath(); closPath != "" && m.config.IntelRdt.EnableMonitoring {
531+
path, err := securejoin.SecureJoin(filepath.Join(closPath, "mon_groups"), m.id)
532+
if err != nil {
533+
return ""
534+
}
535+
return path
536+
}
537+
return ""
538+
}
539+
507540
// GetStats returns statistics for Intel RDT.
508541
func (m *Manager) GetStats() (*Stats, error) {
509542
// If intelRdt is not specified in config
@@ -581,7 +614,16 @@ func (m *Manager) GetStats() (*Stats, error) {
581614
}
582615

583616
if IsMBMEnabled() || IsCMTEnabled() {
584-
err = getMonitoringStats(containerPath, stats)
617+
monPath := m.GetMonPath()
618+
if monPath == "" {
619+
// NOTE: If per-container monitoring is not enabled, the monitoring
620+
// data we get here might have little to do with this container as
621+
// there might be anything from this single container to the half
622+
// of the system assigned in the group. Should consider not
623+
// exposing stats in this case(?)
624+
monPath = containerPath
625+
}
626+
err = getMonitoringStats(monPath, stats)
585627
if err != nil {
586628
return nil, err
587629
}

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 146 additions & 18 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,31 +100,157 @@ func TestIntelRdtSetMemBwScSchema(t *testing.T) {
98100
}
99101

100102
func TestApply(t *testing.T) {
101-
helper := NewIntelRdtTestUtil(t)
102-
103103
const closID = "test-clos"
104-
closPath := filepath.Join(helper.IntelRdtPath, closID)
104+
// TC-1: failure because non-pre-existing CLOS
105+
{
106+
helper := NewIntelRdtTestUtil(t)
107+
helper.config.IntelRdt = &configs.IntelRdt{
108+
ClosID: closID,
109+
}
105110

106-
helper.config.IntelRdt.ClosID = closID
107-
intelrdt := newManager(helper.config, "container-1", closPath)
108-
if err := intelrdt.Apply(1234); err == nil {
109-
t.Fatal("unexpected success when applying pid")
111+
closPath := filepath.Join(intelRdtRoot, closID)
112+
intelrdt := newManager(helper.config, "", closPath)
113+
if err := intelrdt.Apply(1234); err == nil {
114+
t.Fatal("unexpected success when applying pid")
115+
}
116+
if _, err := os.Stat(closPath); err == nil {
117+
t.Fatal("closid dir should not exist")
118+
}
110119
}
111-
if _, err := os.Stat(closPath); err == nil {
112-
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+
closPath := filepath.Join(intelRdtRoot, closID)
129+
intelrdt := newManager(helper.config, "", closPath)
130+
if err := intelrdt.Apply(1235); err != nil {
131+
t.Fatalf("Apply() failed: %v", err)
132+
}
133+
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+
}
113141
}
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"
114149

115-
// Dir should be created if some schema has been specified
116-
intelrdt.config.IntelRdt.L3CacheSchema = "L3:0=f"
117-
if err := intelrdt.Apply(1235); err != nil {
118-
t.Fatalf("Apply() failed: %v", err)
150+
closPath := filepath.Join(intelRdtRoot, id)
151+
intelrdt := newManager(helper.config, id, closPath)
152+
// We need to pre-create the CLOS/mon_groups directory
153+
if err := os.MkdirAll(filepath.Join(closPath, "mon_groups"), 0o755); err != nil {
154+
t.Fatal(err)
155+
}
156+
157+
if err := intelrdt.Apply(1236); err != nil {
158+
t.Fatalf("Apply() failed: %v", err)
159+
}
160+
161+
pids, err := getIntelRdtParamString(closPath, "tasks")
162+
if err != nil {
163+
t.Fatalf("failed to read tasks file: %v", err)
164+
}
165+
if pids != "1236" {
166+
t.Fatalf("unexpected tasks file, expected '1236', got %q", pids)
167+
}
119168
}
169+
}
120170

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

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)