Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libcontainer/cgroups/fs: remove todo since strings.Fields performs well #4403

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs/cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func getCpuUsageBreakdown(path string) (uint64, uint64, error) {
if err != nil {
return 0, 0, err
}
// TODO: use strings.SplitN instead.

fields := strings.Fields(data)
if len(fields) < 4 || fields[0] != userField || fields[2] != systemField {
return 0, 0, malformedLine(path, file, data)
Expand Down
15 changes: 15 additions & 0 deletions libcontainer/cgroups/fs/cpuacct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,18 @@ func TestCpuacctStatsWithoutUsageAll(t *testing.T) {
expectedStats, actualStats.CpuStats.CpuUsage)
}
}

func BenchmarkGetCpuUsageBreakdown(b *testing.B) {
path := tempDir(b, "cpuacct")
writeFileContents(b, path, map[string]string{
"cpuacct.stat": cpuAcctStatContents,
})

b.ResetTimer()
for i := 0; i < b.N; i++ {
Stavrospanakakis marked this conversation as resolved.
Show resolved Hide resolved
_, _, err := getCpuUsageBreakdown(path)
if err != nil {
b.Fatal(err)
}
}
}
4 changes: 2 additions & 2 deletions libcontainer/cgroups/fs/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
}

// tempDir creates a new test directory for the specified subsystem.
func tempDir(t *testing.T, subsystem string) string {
func tempDir(t testing.TB, subsystem string) string {
path := filepath.Join(t.TempDir(), subsystem)
// Ensure the full mock cgroup path exists.
if err := os.Mkdir(path, 0o755); err != nil {
Expand All @@ -29,7 +29,7 @@ func tempDir(t *testing.T, subsystem string) string {

// writeFileContents writes the specified contents on the mock of the specified
// cgroup files.
func writeFileContents(t *testing.T, path string, fileContents map[string]string) {
func writeFileContents(t testing.TB, path string, fileContents map[string]string) {
for file, contents := range fileContents {
err := cgroups.WriteFile(path, file, contents)
if err != nil {
Expand Down
Loading