Skip to content

Commit

Permalink
Merge pull request google#3410 from filippog/get-spec-stat-optimization
Browse files Browse the repository at this point in the history
container: skip checking for files in non-existent directories.
  • Loading branch information
iwankgb committed Oct 29, 2023
2 parents bf2a7fe + 9a30197 commit d19df4f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions container/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ func getSpecInternal(cgroupPaths map[string]string, machineInfoFactory info.Mach
dir, err := os.Stat(cgroupPathDir)
if err == nil && dir.ModTime().Before(lowestTime) {
lowestTime = dir.ModTime()
} else if os.IsNotExist(err) {
// Directory does not exist, skip checking for files within.
continue
}

// The modified time of the cgroup directory sometimes changes whenever a subcontainer is created.
// eg. /docker will have creation time matching the creation of latest docker container.
// Use clone_children/events as a workaround as it isn't usually modified. It is only likely changed
Expand Down
37 changes: 37 additions & 0 deletions container/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,40 @@ func TestRemoveNetMetrics(t *testing.T) {
}
}
}

func BenchmarkGetSpecCgroupV2(b *testing.B) {
root, err := os.Getwd()
if err != nil {
b.Fatalf("getwd: %s", err)
}

cgroupPaths := map[string]string{
"": filepath.Join(root, "test_resources/cgroup_v2/test1"),
}

for i := 0; i < b.N; i++ {
_, err := getSpecInternal(cgroupPaths, &mockInfoProvider{}, false, false, true)
assert.Nil(b, err)
}

}

func BenchmarkGetSpecCgroupV1(b *testing.B) {
root, err := os.Getwd()
if err != nil {
b.Fatalf("getwd: %s", err)
}

cgroupPaths := map[string]string{
"memory": filepath.Join(root, "test_resources/cgroup_v1/test1/memory"),
"cpu": filepath.Join(root, "test_resources/cgroup_v1/test1/cpu"),
"cpuset": filepath.Join(root, "test_resources/cgroup_v1/test1/cpuset"),
"pids": filepath.Join(root, "test_resources/cgroup_v1/test1/pids"),
}

for i := 0; i < b.N; i++ {
_, err := getSpecInternal(cgroupPaths, &mockInfoProvider{}, false, false, false)
assert.Nil(b, err)
}

}

0 comments on commit d19df4f

Please sign in to comment.