Skip to content

Commit 95fd3af

Browse files
authored
Merge pull request #2806 from iwankgb/fixes_2798_2792
Porting fixes to #2792 and #2798 to v0.38
2 parents 57a2c80 + da26e37 commit 95fd3af

File tree

4 files changed

+202
-141
lines changed

4 files changed

+202
-141
lines changed

container/libcontainer/handler.go

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ import (
2929
"strings"
3030
"time"
3131

32-
"github.com/google/cadvisor/container"
33-
info "github.com/google/cadvisor/info/v1"
34-
"golang.org/x/sys/unix"
35-
3632
"github.com/opencontainers/runc/libcontainer"
3733
"github.com/opencontainers/runc/libcontainer/cgroups"
38-
fs2 "github.com/opencontainers/runc/libcontainer/cgroups/fs2"
34+
"github.com/opencontainers/runc/libcontainer/cgroups/fs2"
3935
"k8s.io/klog/v2"
36+
37+
"github.com/google/cadvisor/container"
38+
info "github.com/google/cadvisor/info/v1"
4039
)
4140

4241
var (
@@ -758,16 +757,6 @@ func (h *Handler) GetProcesses() ([]int, error) {
758757
return pids, nil
759758
}
760759

761-
func minUint32(x, y uint32) uint32 {
762-
if x < y {
763-
return x
764-
}
765-
return y
766-
}
767-
768-
// var to allow unit tests to stub it out
769-
var numCpusFunc = getNumberOnlineCPUs
770-
771760
// Convert libcontainer stats to info.ContainerStats.
772761
func setCPUStats(s *cgroups.Stats, ret *info.ContainerStats, withPerCPU bool) {
773762
ret.Cpu.Usage.User = s.CpuStats.CpuUsage.UsageInUsermode
@@ -785,37 +774,7 @@ func setCPUStats(s *cgroups.Stats, ret *info.ContainerStats, withPerCPU bool) {
785774
// cpuacct subsystem.
786775
return
787776
}
788-
789-
numPossible := uint32(len(s.CpuStats.CpuUsage.PercpuUsage))
790-
// Note that as of https://patchwork.kernel.org/patch/8607101/ (kernel v4.7),
791-
// the percpu usage information includes extra zero values for all additional
792-
// possible CPUs. This is to allow statistic collection after CPU-hotplug.
793-
// We intentionally ignore these extra zeroes.
794-
numActual, err := numCpusFunc()
795-
if err != nil {
796-
klog.Errorf("unable to determine number of actual cpus; defaulting to maximum possible number: errno %v", err)
797-
numActual = numPossible
798-
}
799-
if numActual > numPossible {
800-
// The real number of cores should never be greater than the number of
801-
// datapoints reported in cpu usage.
802-
klog.Errorf("PercpuUsage had %v cpus, but the actual number is %v; ignoring extra CPUs", numPossible, numActual)
803-
}
804-
numActual = minUint32(numPossible, numActual)
805-
ret.Cpu.Usage.PerCpu = make([]uint64, numActual)
806-
807-
for i := uint32(0); i < numActual; i++ {
808-
ret.Cpu.Usage.PerCpu[i] = s.CpuStats.CpuUsage.PercpuUsage[i]
809-
}
810-
811-
}
812-
813-
func getNumberOnlineCPUs() (uint32, error) {
814-
var availableCPUs unix.CPUSet
815-
if err := unix.SchedGetaffinity(0, &availableCPUs); err != nil {
816-
return 0, err
817-
}
818-
return uint32(availableCPUs.Count()), nil
777+
ret.Cpu.Usage.PerCpu = s.CpuStats.CpuUsage.PercpuUsage
819778
}
820779

821780
func setDiskIoStats(s *cgroups.Stats, ret *info.ContainerStats) {

container/libcontainer/handler_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,11 @@ const nanosecondsInSeconds = 1000000000
9696
// https://github.com/containerd/cgroups/pull/12
9797
const clockTicks = 100
9898

99-
func TestMorePossibleCPUs(t *testing.T) {
100-
realNumCPUs := uint32(8)
101-
numCpusFunc = func() (uint32, error) {
102-
return realNumCPUs, nil
103-
}
104-
possibleCPUs := uint32(31)
105-
106-
perCPUUsage := make([]uint64, possibleCPUs)
107-
for i := uint32(0); i < realNumCPUs; i++ {
99+
func TestSetCPUStats(t *testing.T) {
100+
perCPUUsage := make([]uint64, 31)
101+
for i := uint32(0); i < 31; i++ {
108102
perCPUUsage[i] = 8562955455524
109103
}
110-
111104
s := &cgroups.Stats{
112105
CpuStats: cgroups.CpuStats{
113106
CpuUsage: cgroups.CpuUsage{
@@ -124,7 +117,7 @@ func TestMorePossibleCPUs(t *testing.T) {
124117
expected := info.ContainerStats{
125118
Cpu: info.CpuStats{
126119
Usage: info.CpuUsage{
127-
PerCpu: perCPUUsage[0:realNumCPUs],
120+
PerCpu: perCPUUsage,
128121
User: s.CpuStats.CpuUsage.UsageInUsermode,
129122
System: s.CpuStats.CpuUsage.UsageInKernelmode,
130123
Total: 33802947350272,

utils/sysinfo/sysinfo.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func getCoresInfo(sysFs sysfs.SysFs, cpuDirs []string) ([]info.Core, error) {
383383
for _, cpuDir := range cpuDirs {
384384
cpuID, err := getMatchedInt(cpuDirRegExp, cpuDir)
385385
if err != nil {
386-
return nil, fmt.Errorf("Unexpected format of CPU directory, cpuDirRegExp %s, cpuDir: %s", cpuDirRegExp, cpuDir)
386+
return nil, fmt.Errorf("unexpected format of CPU directory, cpuDirRegExp %s, cpuDir: %s", cpuDirRegExp, cpuDir)
387387
}
388388
if !sysFs.IsCPUOnline(cpuDir) {
389389
continue
@@ -401,9 +401,22 @@ func getCoresInfo(sysFs sysfs.SysFs, cpuDirs []string) ([]info.Core, error) {
401401
return nil, err
402402
}
403403

404+
rawPhysicalPackageID, err := sysFs.GetCPUPhysicalPackageID(cpuDir)
405+
if os.IsNotExist(err) {
406+
klog.Warningf("Cannot read physical package id for %s, physical_package_id file does not exist, err: %s", cpuDir, err)
407+
continue
408+
} else if err != nil {
409+
return nil, err
410+
}
411+
412+
physicalPackageID, err := strconv.Atoi(rawPhysicalPackageID)
413+
if err != nil {
414+
return nil, err
415+
}
416+
404417
coreIDx := -1
405418
for id, core := range cores {
406-
if core.Id == physicalID {
419+
if core.Id == physicalID && core.SocketID == physicalPackageID {
407420
coreIDx = id
408421
}
409422
}
@@ -414,25 +427,14 @@ func getCoresInfo(sysFs sysfs.SysFs, cpuDirs []string) ([]info.Core, error) {
414427
desiredCore := &cores[coreIDx]
415428

416429
desiredCore.Id = physicalID
430+
desiredCore.SocketID = physicalPackageID
431+
417432
if len(desiredCore.Threads) == 0 {
418433
desiredCore.Threads = []int{cpuID}
419434
} else {
420435
desiredCore.Threads = append(desiredCore.Threads, cpuID)
421436
}
422437

423-
rawPhysicalPackageID, err := sysFs.GetCPUPhysicalPackageID(cpuDir)
424-
if os.IsNotExist(err) {
425-
klog.Warningf("Cannot read physical package id for %s, physical_package_id file does not exist, err: %s", cpuDir, err)
426-
continue
427-
} else if err != nil {
428-
return nil, err
429-
}
430-
431-
physicalPackageID, err := strconv.Atoi(rawPhysicalPackageID)
432-
if err != nil {
433-
return nil, err
434-
}
435-
desiredCore.SocketID = physicalPackageID
436438
}
437439
return cores, nil
438440
}

0 commit comments

Comments
 (0)