-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
libct/cg/stats: support PSI for cgroup v2 #3900
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package fs2 | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"golang.org/x/sys/unix" | ||
|
||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
) | ||
|
||
func statPSI(dirPath string, file string) (*cgroups.PSIStats, error) { | ||
f, err := cgroups.OpenFile(dirPath, file, os.O_RDONLY) | ||
if err != nil { | ||
if errors.Is(err, os.ErrNotExist) { | ||
// Kernel < 4.20, or CONFIG_PSI is not set, | ||
// or PSI stats are turned off for the cgroup | ||
// ("echo 0 > cgroup.pressure", kernel >= 6.1). | ||
return nil, nil | ||
} | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
var psistats cgroups.PSIStats | ||
sc := bufio.NewScanner(f) | ||
for sc.Scan() { | ||
parts := strings.Fields(sc.Text()) | ||
var pv *cgroups.PSIData | ||
switch parts[0] { | ||
case "some": | ||
pv = &psistats.Some | ||
case "full": | ||
pv = &psistats.Full | ||
} | ||
if pv != nil { | ||
*pv, err = parsePSIData(parts[1:]) | ||
if err != nil { | ||
return nil, &parseError{Path: dirPath, File: file, Err: err} | ||
} | ||
} | ||
} | ||
if err := sc.Err(); err != nil { | ||
if errors.Is(err, unix.ENOTSUP) { | ||
// Some kernels (e.g. CS9) may return ENOTSUP on read | ||
// if psi=1 kernel cmdline parameter is required. | ||
return nil, nil | ||
} | ||
return nil, &parseError{Path: dirPath, File: file, Err: err} | ||
} | ||
return &psistats, nil | ||
} | ||
|
||
func parsePSIData(psi []string) (cgroups.PSIData, error) { | ||
data := cgroups.PSIData{} | ||
for _, f := range psi { | ||
kv := strings.SplitN(f, "=", 2) | ||
if len(kv) != 2 { | ||
return data, fmt.Errorf("invalid psi data: %q", f) | ||
} | ||
var pv *float64 | ||
switch kv[0] { | ||
case "avg10": | ||
pv = &data.Avg10 | ||
case "avg60": | ||
pv = &data.Avg60 | ||
case "avg300": | ||
pv = &data.Avg300 | ||
case "total": | ||
v, err := strconv.ParseUint(kv[1], 10, 64) | ||
if err != nil { | ||
return data, fmt.Errorf("invalid %s PSI value: %w", kv[0], err) | ||
} | ||
data.Total = v | ||
} | ||
if pv != nil { | ||
v, err := strconv.ParseFloat(kv[1], 64) | ||
if err != nil { | ||
return data, fmt.Errorf("invalid %s PSI value: %w", kv[0], err) | ||
} | ||
*pv = v | ||
} | ||
} | ||
return data, nil | ||
} |
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,47 @@ | ||
package fs2 | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
) | ||
|
||
func TestStatCPUPSI(t *testing.T) { | ||
const examplePSIData = `some avg10=1.71 avg60=2.36 avg300=2.57 total=230548833 | ||
full avg10=1.00 avg60=1.01 avg300=1.00 total=157622356` | ||
|
||
// We're using a fake cgroupfs. | ||
cgroups.TestMode = true | ||
|
||
fakeCgroupDir := t.TempDir() | ||
statPath := filepath.Join(fakeCgroupDir, "cpu.pressure") | ||
|
||
if err := os.WriteFile(statPath, []byte(examplePSIData), 0o644); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
st, err := statPSI(fakeCgroupDir, "cpu.pressure") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(*st, cgroups.PSIStats{ | ||
Some: cgroups.PSIData{ | ||
Avg10: 1.71, | ||
Avg60: 2.36, | ||
Avg300: 2.57, | ||
Total: 230548833, | ||
}, | ||
Full: cgroups.PSIData{ | ||
Avg10: 1.00, | ||
Avg60: 1.01, | ||
Avg300: 1.00, | ||
Total: 157622356, | ||
}, | ||
}) { | ||
t.Errorf("unexpected PSI result: %+v", st) | ||
} | ||
} |
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
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.
@kolyshkin I think this one could be optimized. We should write their own PSI state function for
cpu
,memory
andio
. For example: addstatCpuPressure
incpu.go
, addstatMemoryPressure
inmemory.go
, addstatIOPressure
inio.go
. Then, we can keep the code style likestatCpu
.But this one could be done in a seperate PR if the upstream projects are very anxious to need this feature.