Skip to content

Commit ea2adae

Browse files
committed
[1.1] libct: rm intelrtd.Manager interface, NewIntelRdtManager
[This is a manual port of commit dbd990d to release-1.1 branch. Original description follows.] Remove intelrtd.Manager interface, since we only have a single implementation, and do not expect another one. Rename intelRdtManager to Manager, and modify its users accordingly. Remove NewIntelRdtManager from factory. Remove IntelRdtfs. Instead, make intelrdt.NewManager return nil if the feature is not available. Remove TestFactoryNewIntelRdt as it is now identical to TestFactoryNew. Add internal function newManager to be used for tests (to make sure some testing is done even when the feature is not available in kernel/hardware). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent b4f7bf2 commit ea2adae

File tree

7 files changed

+41
-93
lines changed

7 files changed

+41
-93
lines changed

libcontainer/container_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type linuxContainer struct {
4040
root string
4141
config *configs.Config
4242
cgroupManager cgroups.Manager
43-
intelRdtManager intelrdt.Manager
43+
intelRdtManager *intelrdt.Manager
4444
initPath string
4545
initArgs []string
4646
initProcess parentProcess

libcontainer/factory_linux.go

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,6 @@ func InitArgs(args ...string) func(*LinuxFactory) error {
4848
}
4949
}
5050

51-
// IntelRdtfs is an options func to configure a LinuxFactory to return
52-
// containers that use the Intel RDT "resource control" filesystem to
53-
// create and manage Intel RDT resources (e.g., L3 cache, memory bandwidth).
54-
func IntelRdtFs(l *LinuxFactory) error {
55-
if !intelrdt.IsCATEnabled() && !intelrdt.IsMBAEnabled() {
56-
l.NewIntelRdtManager = nil
57-
} else {
58-
l.NewIntelRdtManager = func(config *configs.Config, id string, path string) intelrdt.Manager {
59-
return intelrdt.NewManager(config, id, path)
60-
}
61-
}
62-
return nil
63-
}
64-
6551
// TmpfsRoot is an option func to mount LinuxFactory.Root to tmpfs.
6652
func TmpfsRoot(l *LinuxFactory) error {
6753
mounted, err := mountinfo.Mounted(l.Root)
@@ -136,9 +122,6 @@ type LinuxFactory struct {
136122

137123
// Validator provides validation to container configurations.
138124
Validator validate.Validator
139-
140-
// NewIntelRdtManager returns an initialized Intel RDT manager for a single container.
141-
NewIntelRdtManager func(config *configs.Config, id string, path string) intelrdt.Manager
142125
}
143126

144127
func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, error) {
@@ -208,18 +191,16 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
208191
return nil, err
209192
}
210193
c := &linuxContainer{
211-
id: id,
212-
root: containerRoot,
213-
config: config,
214-
initPath: l.InitPath,
215-
initArgs: l.InitArgs,
216-
criuPath: l.CriuPath,
217-
newuidmapPath: l.NewuidmapPath,
218-
newgidmapPath: l.NewgidmapPath,
219-
cgroupManager: cm,
220-
}
221-
if l.NewIntelRdtManager != nil {
222-
c.intelRdtManager = l.NewIntelRdtManager(config, id, "")
194+
id: id,
195+
root: containerRoot,
196+
config: config,
197+
initPath: l.InitPath,
198+
initArgs: l.InitArgs,
199+
criuPath: l.CriuPath,
200+
newuidmapPath: l.NewuidmapPath,
201+
newgidmapPath: l.NewgidmapPath,
202+
cgroupManager: cm,
203+
intelRdtManager: intelrdt.NewManager(config, id, ""),
223204
}
224205
c.state = &stoppedState{c: c}
225206
return c, nil
@@ -261,12 +242,10 @@ func (l *LinuxFactory) Load(id string) (Container, error) {
261242
newuidmapPath: l.NewuidmapPath,
262243
newgidmapPath: l.NewgidmapPath,
263244
cgroupManager: cm,
245+
intelRdtManager: intelrdt.NewManager(&state.Config, id, state.IntelRdtPath),
264246
root: containerRoot,
265247
created: state.Created,
266248
}
267-
if l.NewIntelRdtManager != nil {
268-
c.intelRdtManager = l.NewIntelRdtManager(&state.Config, id, state.IntelRdtPath)
269-
}
270249
c.state = &loadedState{c: c}
271250
if err := c.refreshState(); err != nil {
272251
return nil, err

libcontainer/factory_linux_test.go

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,6 @@ func TestFactoryNew(t *testing.T) {
3737
}
3838
}
3939

40-
func TestFactoryNewIntelRdt(t *testing.T) {
41-
root := t.TempDir()
42-
factory, err := New(root, IntelRdtFs)
43-
if err != nil {
44-
t.Fatal(err)
45-
}
46-
if factory == nil {
47-
t.Fatal("factory should not be nil")
48-
}
49-
lfactory, ok := factory.(*LinuxFactory)
50-
if !ok {
51-
t.Fatal("expected linux factory returned on linux based systems")
52-
}
53-
if lfactory.Root != root {
54-
t.Fatalf("expected factory root to be %q but received %q", root, lfactory.Root)
55-
}
56-
57-
if factory.Type() != "libcontainer" {
58-
t.Fatalf("unexpected factory type: %q, expected %q", factory.Type(), "libcontainer")
59-
}
60-
}
61-
6240
func TestFactoryNewTmpfs(t *testing.T) {
6341
root := t.TempDir()
6442
factory, err := New(root, TmpfsRoot)
@@ -157,7 +135,7 @@ func TestFactoryLoadContainer(t *testing.T) {
157135
if err := marshal(filepath.Join(root, id, stateFilename), expectedState); err != nil {
158136
t.Fatal(err)
159137
}
160-
factory, err := New(root, IntelRdtFs)
138+
factory, err := New(root)
161139
if err != nil {
162140
t.Fatal(err)
163141
}

libcontainer/intelrdt/intelrdt.go

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,34 +146,27 @@ import (
146146
* }
147147
*/
148148

149-
type Manager interface {
150-
// Applies Intel RDT configuration to the process with the specified pid
151-
Apply(pid int) error
152-
153-
// Returns statistics for Intel RDT
154-
GetStats() (*Stats, error)
155-
156-
// Destroys the Intel RDT container-specific 'container_id' group
157-
Destroy() error
158-
159-
// Returns Intel RDT path to save in a state file and to be able to
160-
// restore the object later
161-
GetPath() string
162-
163-
// Set Intel RDT "resource control" filesystem as configured.
164-
Set(container *configs.Config) error
165-
}
166-
167-
// This implements interface Manager
168-
type intelRdtManager struct {
149+
// NewManager returns a new instance of Manager, or nil, if the Intel RDT
150+
// functionality is not available from hardware or not enabled in the kernel.
151+
type Manager struct {
169152
mu sync.Mutex
170153
config *configs.Config
171154
id string
172155
path string
173156
}
174157

175-
func NewManager(config *configs.Config, id string, path string) Manager {
176-
return &intelRdtManager{
158+
func NewManager(config *configs.Config, id string, path string) *Manager {
159+
if _, err := Root(); err != nil {
160+
// Intel RDT is not available.
161+
return nil
162+
}
163+
return newManager(config, id, path)
164+
}
165+
166+
// newManager is the same as NewManager, except it does not check if the feature
167+
// is actually available. Used by unit tests that mock intelrdt paths.
168+
func newManager(config *configs.Config, id string, path string) *Manager {
169+
return &Manager{
177170
config: config,
178171
id: id,
179172
path: path,
@@ -507,7 +500,7 @@ func IsMBAScEnabled() bool {
507500
}
508501

509502
// Get the path of the clos group in "resource control" filesystem that the container belongs to
510-
func (m *intelRdtManager) getIntelRdtPath() (string, error) {
503+
func (m *Manager) getIntelRdtPath() (string, error) {
511504
rootPath, err := Root()
512505
if err != nil {
513506
return "", err
@@ -522,7 +515,7 @@ func (m *intelRdtManager) getIntelRdtPath() (string, error) {
522515
}
523516

524517
// Applies Intel RDT configuration to the process with the specified pid
525-
func (m *intelRdtManager) Apply(pid int) (err error) {
518+
func (m *Manager) Apply(pid int) (err error) {
526519
// If intelRdt is not specified in config, we do nothing
527520
if m.config.IntelRdt == nil {
528521
return nil
@@ -557,7 +550,7 @@ func (m *intelRdtManager) Apply(pid int) (err error) {
557550
}
558551

559552
// Destroys the Intel RDT container-specific 'container_id' group
560-
func (m *intelRdtManager) Destroy() error {
553+
func (m *Manager) Destroy() error {
561554
// Don't remove resctrl group if closid has been explicitly specified. The
562555
// group is likely externally managed, i.e. by some other entity than us.
563556
// There are probably other containers/tasks sharing the same group.
@@ -574,15 +567,15 @@ func (m *intelRdtManager) Destroy() error {
574567

575568
// Returns Intel RDT path to save in a state file and to be able to
576569
// restore the object later
577-
func (m *intelRdtManager) GetPath() string {
570+
func (m *Manager) GetPath() string {
578571
if m.path == "" {
579572
m.path, _ = m.getIntelRdtPath()
580573
}
581574
return m.path
582575
}
583576

584577
// Returns statistics for Intel RDT
585-
func (m *intelRdtManager) GetStats() (*Stats, error) {
578+
func (m *Manager) GetStats() (*Stats, error) {
586579
// If intelRdt is not specified in config
587580
if m.config.IntelRdt == nil {
588581
return nil, nil
@@ -668,7 +661,7 @@ func (m *intelRdtManager) GetStats() (*Stats, error) {
668661
}
669662

670663
// Set Intel RDT "resource control" filesystem as configured.
671-
func (m *intelRdtManager) Set(container *configs.Config) error {
664+
func (m *Manager) Set(container *configs.Config) error {
672665
// About L3 cache schema:
673666
// It has allocation bitmasks/values for L3 cache on each socket,
674667
// which contains L3 cache id and capacity bitmask (CBM).

libcontainer/intelrdt/intelrdt_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestIntelRdtSetL3CacheSchema(t *testing.T) {
2020
})
2121

2222
helper.config.IntelRdt.L3CacheSchema = l3CacheSchemeAfter
23-
intelrdt := NewManager(helper.config, "", helper.IntelRdtPath)
23+
intelrdt := newManager(helper.config, "", helper.IntelRdtPath)
2424
if err := intelrdt.Set(helper.config); err != nil {
2525
t.Fatal(err)
2626
}
@@ -50,7 +50,7 @@ func TestIntelRdtSetMemBwSchema(t *testing.T) {
5050
})
5151

5252
helper.config.IntelRdt.MemBwSchema = memBwSchemeAfter
53-
intelrdt := NewManager(helper.config, "", helper.IntelRdtPath)
53+
intelrdt := newManager(helper.config, "", helper.IntelRdtPath)
5454
if err := intelrdt.Set(helper.config); err != nil {
5555
t.Fatal(err)
5656
}
@@ -80,7 +80,7 @@ func TestIntelRdtSetMemBwScSchema(t *testing.T) {
8080
})
8181

8282
helper.config.IntelRdt.MemBwSchema = memBwScSchemeAfter
83-
intelrdt := NewManager(helper.config, "", helper.IntelRdtPath)
83+
intelrdt := newManager(helper.config, "", helper.IntelRdtPath)
8484
if err := intelrdt.Set(helper.config); err != nil {
8585
t.Fatal(err)
8686
}
@@ -103,7 +103,7 @@ func TestApply(t *testing.T) {
103103
const closID = "test-clos"
104104

105105
helper.config.IntelRdt.ClosID = closID
106-
intelrdt := NewManager(helper.config, "", helper.IntelRdtPath)
106+
intelrdt := newManager(helper.config, "", helper.IntelRdtPath)
107107
if err := intelrdt.Apply(1234); err == nil {
108108
t.Fatal("unexpected success when applying pid")
109109
}
@@ -112,7 +112,7 @@ func TestApply(t *testing.T) {
112112
}
113113

114114
// Dir should be created if some schema has been specified
115-
intelrdt.(*intelRdtManager).config.IntelRdt.L3CacheSchema = "L3:0=f"
115+
intelrdt.config.IntelRdt.L3CacheSchema = "L3:0=f"
116116
if err := intelrdt.Apply(1235); err != nil {
117117
t.Fatalf("Apply() failed: %v", err)
118118
}

libcontainer/process_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ type initProcess struct {
299299
logFilePair filePair
300300
config *initConfig
301301
manager cgroups.Manager
302-
intelRdtManager intelrdt.Manager
302+
intelRdtManager *intelrdt.Manager
303303
container *linuxContainer
304304
fds []string
305305
process *Process

utils_linux.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
3232
return nil, err
3333
}
3434

35-
intelRdtManager := libcontainer.IntelRdtFs
36-
3735
// We resolve the paths for {newuidmap,newgidmap} from the context of runc,
3836
// to avoid doing a path lookup in the nsexec context. TODO: The binary
3937
// names are not currently configurable.
@@ -46,7 +44,7 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
4644
newgidmap = ""
4745
}
4846

49-
return libcontainer.New(abs, intelRdtManager,
47+
return libcontainer.New(abs,
5048
libcontainer.CriuPath(context.GlobalString("criu")),
5149
libcontainer.NewuidmapPath(newuidmap),
5250
libcontainer.NewgidmapPath(newgidmap))

0 commit comments

Comments
 (0)