-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add resource usage monitoring for build steps #3860
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6e87e4b
resources: add build step resource tracking via cgroups
tonistiigi 963f161
resources: CNI network usage sampling support
tonistiigi 6a2f92d
resources: add sampler for periodic stat reads
tonistiigi 32dcdff
resources: store sys cpu usage per step
tonistiigi 509cfa3
llbsolver: add systemusage samples to provenance attestation
tonistiigi 262b708
resources: make maxsamples configurable
tonistiigi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package resources | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/moby/buildkit/executor/resources/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
cpuUsageUsec = "usage_usec" | ||
cpuUserUsec = "user_usec" | ||
cpuSystemUsec = "system_usec" | ||
cpuNrPeriods = "nr_periods" | ||
cpuNrThrottled = "nr_throttled" | ||
cpuThrottledUsec = "throttled_usec" | ||
) | ||
|
||
func getCgroupCPUStat(cgroupPath string) (*types.CPUStat, error) { | ||
cpuStat := &types.CPUStat{} | ||
|
||
// Read cpu.stat file | ||
cpuStatFile, err := os.Open(filepath.Join(cgroupPath, "cpu.stat")) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
defer cpuStatFile.Close() | ||
|
||
scanner := bufio.NewScanner(cpuStatFile) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
fields := strings.Fields(line) | ||
|
||
if len(fields) < 2 { | ||
continue | ||
} | ||
|
||
key := fields[0] | ||
value, err := strconv.ParseUint(fields[1], 10, 64) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
switch key { | ||
case cpuUsageUsec: | ||
cpuStat.UsageNanos = uint64Ptr(value * 1000) | ||
case cpuUserUsec: | ||
cpuStat.UserNanos = uint64Ptr(value * 1000) | ||
case cpuSystemUsec: | ||
cpuStat.SystemNanos = uint64Ptr(value * 1000) | ||
case cpuNrPeriods: | ||
cpuStat.NrPeriods = new(uint32) | ||
*cpuStat.NrPeriods = uint32(value) | ||
case cpuNrThrottled: | ||
cpuStat.NrThrottled = new(uint32) | ||
*cpuStat.NrThrottled = uint32(value) | ||
case cpuThrottledUsec: | ||
cpuStat.ThrottledNanos = uint64Ptr(value * 1000) | ||
} | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
// Read cpu.pressure file | ||
pressure, err := parsePressureFile(filepath.Join(cgroupPath, "cpu.pressure")) | ||
if err == nil { | ||
cpuStat.Pressure = pressure | ||
} | ||
|
||
return cpuStat, nil | ||
} | ||
func parsePressureFile(filename string) (*types.Pressure, error) { | ||
content, err := os.ReadFile(filename) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { // pressure file requires CONFIG_PSI | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
|
||
lines := strings.Split(string(content), "\n") | ||
|
||
pressure := &types.Pressure{} | ||
for _, line := range lines { | ||
// Skip empty lines | ||
if len(strings.TrimSpace(line)) == 0 { | ||
continue | ||
} | ||
|
||
fields := strings.Fields(line) | ||
prefix := fields[0] | ||
pressureValues := &types.PressureValues{} | ||
|
||
for i := 1; i < len(fields); i++ { | ||
keyValue := strings.Split(fields[i], "=") | ||
key := keyValue[0] | ||
valueStr := keyValue[1] | ||
jedevc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if key == "total" { | ||
totalValue, err := strconv.ParseUint(valueStr, 10, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pressureValues.Total = &totalValue | ||
} else { | ||
value, err := strconv.ParseFloat(valueStr, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch key { | ||
case "avg10": | ||
pressureValues.Avg10 = &value | ||
case "avg60": | ||
pressureValues.Avg60 = &value | ||
case "avg300": | ||
pressureValues.Avg300 = &value | ||
} | ||
} | ||
} | ||
|
||
switch prefix { | ||
case "some": | ||
pressure.Some = pressureValues | ||
case "full": | ||
pressure.Full = pressureValues | ||
} | ||
} | ||
|
||
return pressure, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add comments linking to the specifications for the underlying file formats in https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html.