Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
4 changes: 4 additions & 0 deletions internal/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const (
// SystemdCgroupsManager represents systemd native cgroup manager
SystemdCgroupsManager = "systemd"

// CrioScopePrefix is the crio specific scope prefix
// used for container cgroups
CrioScopePrefix = "crio"

// killContainerTimeout is the timeout that we wait for the container to
// be SIGKILLed.
killContainerTimeout = 2 * time.Minute
Expand Down
16 changes: 9 additions & 7 deletions internal/oci/oci_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,33 @@ func newPipe() (parent, child *os.File, err error) {
return os.NewFile(uintptr(fds[1]), "parent"), os.NewFile(uintptr(fds[0]), "child"), nil
}

func (r *runtimeOCI) containerStats(ctr *Container, cgroup string) (stats *ContainerStats, err error) {
func (r *runtimeOCI) containerStats(ctr *Container, sandboxParent string) (stats *ContainerStats, err error) {
stats = &ContainerStats{}
stats.Container = ctr.ID()
stats.SystemNano = time.Now().UnixNano()

// technically, the CRI does not mandate a CgroupParent is given to a pod
// this situation should never happen in production, but some test suites
// (such as critest) assume we can call stats on a cgroupless container
if cgroup == "" {
if sandboxParent == "" {
return stats, nil
}

var suffix string
// this correction has to be made because the libpod cgroups package can't find a
// systemd cgroup that isn't converted to a fully qualified cgroup path
if r.config.CgroupManager == SystemdCgroupsManager {
logrus.Debugf("Expanding systemd cgroup slice %v", cgroup)
cgroup, err = systemd.ExpandSlice(cgroup)
logrus.Debugf("Expanding systemd cgroup slice %v", sandboxParent)
sandboxParent, err = systemd.ExpandSlice(sandboxParent)
if err != nil {
return nil, errors.Wrapf(err, "error expanding systemd slice to get container %s stats", ctr.ID())
}
suffix = ".scope"
}

cg, err := cgroups.Load(cgroup)
cgroupPath := filepath.Join(sandboxParent, CrioScopePrefix+"-"+ctr.ID()+suffix)
cg, err := cgroups.Load(cgroupPath)
if err != nil {
return nil, errors.Wrapf(err, "unable to load cgroup at %s", cgroup)
return nil, errors.Wrapf(err, "unable to load cgroup at %s", cgroupPath)
}

cgroupStats, err := cg.Stat()
Expand Down
1 change: 0 additions & 1 deletion server/container_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const (
seccompDockerDefault = "docker/default"
seccompLocalhostPrefix = "localhost/"

scopePrefix = "crio"
defaultCgroupfsParent = "/crio"
defaultSystemdParent = "system.slice"
)
Expand Down
4 changes: 2 additions & 2 deletions server/container_create_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,9 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID, contai
parent = sb.CgroupParent()
}
if useSystemd {
cgPath = parent + ":" + scopePrefix + ":" + containerID
cgPath = parent + ":" + oci.CrioScopePrefix + ":" + containerID
} else {
cgPath = filepath.Join(parent, scopePrefix+"-"+containerID)
cgPath = filepath.Join(parent, oci.CrioScopePrefix+"-"+containerID)
}
specgen.SetLinuxCgroupsPath(cgPath)

Expand Down
2 changes: 1 addition & 1 deletion server/sandbox_run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func AddCgroupAnnotation(ctx context.Context, g generate.Generator, mountPath, c
if strings.HasSuffix(path.Base(cgroupParent), ".slice") {
return "", fmt.Errorf("cri-o configured with cgroupfs cgroup manager, but received systemd slice as parent: %s", cgroupParent)
}
cgPath := filepath.Join(cgroupParent, scopePrefix+"-"+id)
cgPath := filepath.Join(cgroupParent, oci.CrioScopePrefix+"-"+id)
g.SetLinuxCgroupsPath(cgPath)
}
}
Expand Down
47 changes: 37 additions & 10 deletions test/stats.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,53 @@ function teardown() {
# given
run crictl run "$TESTDATA"/container_redis.json "$TESTDATA"/sandbox_config.json
[ "$status" -eq 0 ]
id="$output"

# when
run crictl stats -o json
echo "$output"
[ "$status" -eq 0 ]

# then
JSON="$output"
echo $JSON | jq -e '.stats[0].attributes.id != ""'
[ "$status" -eq 0 ]
jq -e '.stats[0].attributes.id = "'$id'"' <<< "$output"
jq -e '.stats[0].cpu.timestamp > 0' <<< "$output"
jq -e '.stats[0].cpu.usageCoreNanoSeconds.value > 0' <<< "$output"
jq -e '.stats[0].memory.timestamp > 0' <<< "$output"
jq -e '.stats[0].memory.workingSetBytes.value > 0' <<< "$output"
}

echo $JSON | jq -e '.stats[0].cpu.timestamp > 0'
@test "container stats" {
# given
container2config=$(cat "$TESTDATA"/container_config_sleep.json | python -c 'import json,sys;obj=json.load(sys.stdin);obj["name"] = ["container10000"];obj["metadata"]["name"] = "container10000"; json.dump(obj, sys.stdout)')
echo "$container2config" > "$TESTDIR"/container_config_sleep2.json
run crictl runp "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]

echo $JSON | jq -e '.stats[0].cpu.usageCoreNanoSeconds.value > 0'
pod_id="$output"
run crictl create "$pod_id" "$TESTDATA"/container_config_sleep.json "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]

echo $JSON | jq -e '.stats[0].memory.timestamp > 0'
ctr1_id="$output"
run crictl create "$pod_id" "$TESTDIR"/container_config_sleep2.json "$TESTDATA"/sandbox_config.json
echo "$output"
[ "$status" -eq 0 ]

echo $JSON | jq -e '.stats[0].memory.workingSetBytes.value > 0'
ctr2_id="$output"
run crictl start "$ctr1_id"
echo "$output"
[ "$status" -eq 0 ]
run crictl start "$ctr2_id"
echo "$output"
[ "$status" -eq 0 ]

# when
ctr1_stats_JSON=$(crictl stats -o json --id "$ctr1_id")
echo $ctr1_stats_JSON
ctr2_stats_JSON=$(crictl stats -o json --id "$ctr2_id")
echo $ctr2_stats_JSON

ctr1_memory_bytes=$(echo $ctr1_stats_JSON | jq -e '.stats[0].memory.workingSetBytes.value')
ctr2_memory_bytes=$(echo $ctr2_stats_JSON | jq -e '.stats[0].memory.workingSetBytes.value')

# then
[[ $ctr1_memory_bytes != $ctr2_memory_bytes ]]
}