|
| 1 | +package clistat |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "tailscale.com/types/ptr" |
| 7 | + |
| 8 | + "golang.org/x/xerrors" |
| 9 | +) |
| 10 | + |
| 11 | +const () |
| 12 | + |
| 13 | +// CGroupCPU returns the CPU usage of the container cgroup. |
| 14 | +// On non-Linux platforms, this always returns nil. |
| 15 | +func (s *Statter) ContainerCPU() (*Result, error) { |
| 16 | + // Firstly, check if we are containerized. |
| 17 | + if ok, err := IsContainerized(); err != nil || !ok { |
| 18 | + return nil, nil |
| 19 | + } |
| 20 | + |
| 21 | + used1, total1, err := cgroupCPU() |
| 22 | + if err != nil { |
| 23 | + return nil, xerrors.Errorf("get cgroup CPU usage: %w", err) |
| 24 | + } |
| 25 | + <-time.After(s.sampleInterval) |
| 26 | + used2, total2, err := cgroupCPU() |
| 27 | + if err != nil { |
| 28 | + return nil, xerrors.Errorf("get cgroup CPU usage: %w", err) |
| 29 | + } |
| 30 | + |
| 31 | + return &Result{ |
| 32 | + Unit: "cores", |
| 33 | + Used: used2 - used1, |
| 34 | + Total: ptr.To(total2 - total1), |
| 35 | + }, nil |
| 36 | +} |
| 37 | + |
| 38 | +func cgroupCPU() (used, total float64, err error) { |
| 39 | + if isCGroupV2() { |
| 40 | + return cGroupv2CPU() |
| 41 | + } |
| 42 | + |
| 43 | + // Fall back to CGroupv1 |
| 44 | + return cGroupv1CPU() |
| 45 | +} |
| 46 | + |
| 47 | +func isCGroupV2() bool { |
| 48 | + // TODO implement |
| 49 | + return false |
| 50 | +} |
| 51 | + |
| 52 | +func cGroupv2CPU() (float64, float64, error) { |
| 53 | + // TODO: implement |
| 54 | + return 0, 0, nil |
| 55 | +} |
| 56 | + |
| 57 | +func cGroupv1CPU() (float64, float64, error) { |
| 58 | + // TODO: implement |
| 59 | + return 0, 0, nil |
| 60 | +} |
| 61 | + |
| 62 | +func (s *Statter) ContainerMemory() (*Result, error) { |
| 63 | + if ok, err := IsContainerized(); err != nil || !ok { |
| 64 | + return nil, nil |
| 65 | + } |
| 66 | + |
| 67 | + if isCGroupV2() { |
| 68 | + return cGroupv2Memory() |
| 69 | + } |
| 70 | + |
| 71 | + // Fall back to CGroupv1 |
| 72 | + return cGroupv1Memory() |
| 73 | +} |
| 74 | + |
| 75 | +func cGroupv2Memory() (*Result, error) { |
| 76 | + // TODO implement |
| 77 | + return nil, nil |
| 78 | +} |
| 79 | + |
| 80 | +func cGroupv1Memory() (*Result, error) { |
| 81 | + // TODO implement |
| 82 | + return nil, nil |
| 83 | +} |
0 commit comments