-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libct/cg/stats: support PSI for cgroup v2
We read output from the following files if they exists: - cpu.pressure - memory.pressure - io.pressure Each are in format: ``` some avg10=0.00 avg60=0.00 avg300=0.00 total=0 full avg10=0.00 avg60=0.00 avg300=0.00 total=0 ``` Signed-off-by: Daniel Dao <dqminh89@gmail.com> Co-authored-by: Sandor Szücs <sandor.szuecs@zalando.de> Co-authored-by: Kir Kolyshkin <kolyshkin@gmail.com> Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
- Loading branch information
1 parent
2f42992
commit 1aa7ca8
Showing
8 changed files
with
212 additions
and
1 deletion.
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