cgroupv1: remove subsystemSet.Get()

Instead of iterating over m.paths, iterate over subsystems and look up
the path for each. This is faster since a map lookup is faster than
iterating over the names in Get. A quick benchmark shows that the new
way is 2.5x faster than the old one.

Note though that this is not done to make things faster, as savings are
negligible, but to make things simpler by removing some code.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2020-06-03 09:11:20 -07:00
parent daf30cb7ca
commit 2a322e91ec
2 changed files with 8 additions and 30 deletions

View File

@ -18,7 +18,7 @@ import (
) )
var ( var (
subsystems = subsystemSet{ subsystems = []subsystem{
&CpusetGroup{}, &CpusetGroup{},
&DevicesGroup{}, &DevicesGroup{},
&MemoryGroup{}, &MemoryGroup{},
@ -38,17 +38,6 @@ var (
var errSubsystemDoesNotExist = errors.New("cgroup: subsystem does not exist") var errSubsystemDoesNotExist = errors.New("cgroup: subsystem does not exist")
type subsystemSet []subsystem
func (s subsystemSet) Get(name string) (subsystem, error) {
for _, ss := range s {
if ss.Name() == name {
return ss, nil
}
}
return nil, errSubsystemDoesNotExist
}
type subsystem interface { type subsystem interface {
// Name returns the name of the subsystem. // Name returns the name of the subsystem.
Name() string Name() string
@ -244,9 +233,9 @@ func (m *manager) GetStats() (*cgroups.Stats, error) {
m.mu.Lock() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
stats := cgroups.NewStats() stats := cgroups.NewStats()
for name, path := range m.paths { for _, sys := range subsystems {
sys, err := subsystems.Get(name) path := m.paths[sys.Name()]
if err == errSubsystemDoesNotExist { if path == "" {
continue continue
} }
if err := sys.GetStats(path, stats); err != nil { if err := sys.GetStats(path, stats); err != nil {

View File

@ -41,18 +41,7 @@ type subsystem interface {
var errSubsystemDoesNotExist = errors.New("cgroup: subsystem does not exist") var errSubsystemDoesNotExist = errors.New("cgroup: subsystem does not exist")
type subsystemSet []subsystem var legacySubsystems = []subsystem{
func (s subsystemSet) Get(name string) (subsystem, error) {
for _, ss := range s {
if ss.Name() == name {
return ss, nil
}
}
return nil, errSubsystemDoesNotExist
}
var legacySubsystems = subsystemSet{
&fs.CpusetGroup{}, &fs.CpusetGroup{},
&fs.DevicesGroup{}, &fs.DevicesGroup{},
&fs.MemoryGroup{}, &fs.MemoryGroup{},
@ -355,9 +344,9 @@ func (m *legacyManager) GetStats() (*cgroups.Stats, error) {
m.mu.Lock() m.mu.Lock()
defer m.mu.Unlock() defer m.mu.Unlock()
stats := cgroups.NewStats() stats := cgroups.NewStats()
for name, path := range m.paths { for _, sys := range legacySubsystems {
sys, err := legacySubsystems.Get(name) path := m.paths[sys.Name()]
if err == errSubsystemDoesNotExist || !cgroups.PathExists(path) { if path == "" {
continue continue
} }
if err := sys.GetStats(path, stats); err != nil { if err := sys.GetStats(path, stats); err != nil {