-
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.
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>
- Loading branch information
Showing
6 changed files
with
178 additions
and
0 deletions.
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,78 @@ | ||
package fs2 | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
) | ||
|
||
func statPSI(dirPath string, file string, stats *cgroups.PSIStats) error { | ||
f, err := cgroups.OpenFile(dirPath, file, os.O_RDONLY) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
sc := bufio.NewScanner(f) | ||
for sc.Scan() { | ||
parts := strings.Fields(sc.Text()) | ||
switch parts[0] { | ||
case "some": | ||
data, err := parsePSIData(parts[1:]) | ||
if err != nil { | ||
return err | ||
} | ||
stats.Some = data | ||
case "full": | ||
data, err := parsePSIData(parts[1:]) | ||
if err != nil { | ||
return err | ||
} | ||
stats.Full = data | ||
} | ||
} | ||
if err := sc.Err(); err != nil { | ||
return &parseError{Path: dirPath, File: file, Err: err} | ||
} | ||
return nil | ||
} | ||
|
||
func parsePSIData(psi []string) (data cgroups.PSIData, err error) { | ||
for i := 0; i < len(psi); i++ { | ||
kv := strings.SplitN(psi[i], "=", 2) | ||
if len(kv) != 2 { | ||
return data, fmt.Errorf("invalid psi data: %q", psi[i]) | ||
} | ||
switch kv[0] { | ||
case "avg10": | ||
v, err := strconv.ParseFloat(kv[1], 64) | ||
if err != nil { | ||
return data, fmt.Errorf("invalid psi value: %q", psi[i]) | ||
} | ||
data.Avg10 = v | ||
case "avg60": | ||
v, err := strconv.ParseFloat(kv[1], 64) | ||
if err != nil { | ||
return data, fmt.Errorf("invalid psi value: %q", psi[i]) | ||
} | ||
data.Avg60 = v | ||
case "avg300": | ||
v, err := strconv.ParseFloat(kv[1], 64) | ||
if err != nil { | ||
return data, fmt.Errorf("invalid psi value: %q", psi[i]) | ||
} | ||
data.Avg300 = v | ||
case "total": | ||
v, err := strconv.ParseUint(kv[1], 10, 64) | ||
if err != nil { | ||
return data, fmt.Errorf("invalid psi value: %q", psi[i]) | ||
} | ||
data.Total = 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" | ||
) | ||
|
||
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` | ||
|
||
func TestStatCPUPsi(t *testing.T) { | ||
// 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) | ||
} | ||
|
||
var psi cgroups.PSIStats | ||
if err := statPSI(fakeCgroupDir, "cpu.pressure", &psi); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if !reflect.DeepEqual(psi, 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", psi) | ||
} | ||
} |
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