Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide workspace resource information #10836

Merged
merged 6 commits into from
Jun 28, 2022
Merged
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
59 changes: 59 additions & 0 deletions components/common-go/cgroups/cgroup.go
Original file line number Diff line number Diff line change
@@ -5,10 +5,17 @@
package cgroups

import (
"math"
"os"
"strconv"
"strings"

"github.com/containerd/cgroups"
v2 "github.com/containerd/cgroups/v2"
)

const DefaultMountPoint = "/sys/fs/cgroup"

func IsUnifiedCgroupSetup() (bool, error) {
return cgroups.Mode() == cgroups.Unified, nil
}
@@ -26,3 +33,55 @@ func EnsureCpuControllerEnabled(basePath, cgroupPath string) error {

return nil
}

type CpuStats struct {
UsageTotal uint64
UsageUser uint64
UsageSystem uint64
}

type MemoryStats struct {
InactiveFileTotal uint64
}

func ReadSingleValue(path string) (uint64, error) {
content, err := os.ReadFile(path)
if err != nil {
return 0, err
}

value := strings.TrimSpace(string(content))
if value == "max" || value == "-1" {
return math.MaxUint64, nil
}

max, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, err
}

return max, nil
}

func ReadFlatKeyedFile(path string) (map[string]uint64, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}

entries := strings.Split(strings.TrimSpace(string(content)), "\n")
kv := make(map[string]uint64, len(entries))
for _, entry := range entries {
tokens := strings.Split(entry, " ")
if len(tokens) < 2 {
continue
}
v, err := strconv.ParseUint(tokens[1], 10, 64)
if err != nil {
continue
}
kv[tokens[0]] = v
}

return kv, nil
}
47 changes: 47 additions & 0 deletions components/common-go/cgroups/cgroups_test.go
Original file line number Diff line number Diff line change
@@ -5,9 +5,12 @@
package cgroups

import (
"math"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

var cgroupPath = []string{"/kubepods", "burstable", "pods234sdf", "234as8df34"}
@@ -87,3 +90,47 @@ func verifyCpuControllerToggled(t *testing.T, path string, enabled bool) {
t.Fatalf("%s should not have enabled cpu controller", path)
}
}

func TestReadSingleValue(t *testing.T) {
scenarios := []struct {
name string
content string
expected uint64
}{
{
name: "cgroup2 max value",
content: "max",
expected: math.MaxUint64,
},
{
name: "cgroup1 max value",
content: "-1",
expected: math.MaxUint64,
},
{
name: "valid value",
content: "100000",
expected: 100_000,
},
}

for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
f, err := os.CreateTemp("", "cgroup_test*")
if err != nil {
t.Fatal(err)
}

if _, err := f.Write([]byte(s.content)); err != nil {
t.Fatal(err)
}

v, err := ReadSingleValue(f.Name())
if err != nil {
t.Fatal(err)
}

assert.Equal(t, s.expected, v)
})
}
}
47 changes: 47 additions & 0 deletions components/common-go/cgroups/v1/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package v1

import (
"path/filepath"

"github.com/gitpod-io/gitpod/common-go/cgroups"
)

type Cpu struct {
path string
}

func NewCpuControllerWithMount(mountPoint, path string) *Cpu {
fullPath := filepath.Join(mountPoint, "cpu", path)
return &Cpu{
path: fullPath,
}
}

func NewCpuController(path string) *Cpu {
path = filepath.Join(cgroups.DefaultMountPoint, "cpu", path)
return &Cpu{
path: path,
}
}

// Quota returns the cpu quota in microseconds
func (c *Cpu) Quota() (uint64, error) {
path := filepath.Join(c.path, "cpu.cfs_quota_us")
return cgroups.ReadSingleValue(path)
}

// Period returns the cpu period in microseconds
func (c *Cpu) Period() (uint64, error) {
path := filepath.Join(c.path, "cpu.cfs_period_us")
return cgroups.ReadSingleValue(path)
}

// Usage returns the cpu usage in nanoseconds
func (c *Cpu) Usage() (uint64, error) {
path := filepath.Join(c.path, "cpuacct.usage")
return cgroups.ReadSingleValue(path)
}
54 changes: 54 additions & 0 deletions components/common-go/cgroups/v1/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package v1

import (
"path/filepath"

"github.com/gitpod-io/gitpod/common-go/cgroups"
)

type Memory struct {
path string
}

func NewMemoryControllerWithMount(mountPoint, path string) *Memory {
fullPath := filepath.Join(mountPoint, "memory", path)
return &Memory{
path: fullPath,
}
}

func NewMemoryController(path string) *Memory {
path = filepath.Join(cgroups.DefaultMountPoint, "memory", path)
return &Memory{
path: path,
}
}

// Limit returns the memory limit in bytes
func (m *Memory) Limit() (uint64, error) {
path := filepath.Join(m.path, "memory.limit_in_bytes")
return cgroups.ReadSingleValue(path)
}

// Usage returns the memory usage in bytes
func (m *Memory) Usage() (uint64, error) {
path := filepath.Join(m.path, "memory.usage_in_bytes")
return cgroups.ReadSingleValue(path)
}

// Stat returns cpu statistics
func (m *Memory) Stat() (*cgroups.MemoryStats, error) {
path := filepath.Join(m.path, "memory.stat")
statMap, err := cgroups.ReadFlatKeyedFile(path)
if err != nil {
return nil, err
}

return &cgroups.MemoryStats{
InactiveFileTotal: statMap["total_inactive_file"],
}, nil
}
86 changes: 86 additions & 0 deletions components/common-go/cgroups/v2/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package v2

import (
"math"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/gitpod-io/gitpod/common-go/cgroups"
"golang.org/x/xerrors"
)

const (
StatUsageTotal = "usage_usec"
StatUsageUser = "user_usec"
StatUsageSystem = "system_usec"
)

type Cpu struct {
path string
}

func NewCpuControllerWithMount(mountPoint, path string) *Cpu {
fullPath := filepath.Join(mountPoint, path)
return &Cpu{
path: fullPath,
}
}

func NewCpuController(path string) *Cpu {
return &Cpu{
path: path,
}
}

// Max return the quota and period in microseconds
func (c *Cpu) Max() (quota uint64, period uint64, err error) {
path := filepath.Join(c.path, "cpu.max")
content, err := os.ReadFile(path)
if err != nil {
return 0, 0, nil
}

values := strings.Split(strings.TrimSpace(string(content)), " ")
if len(values) < 2 {
return 0, 0, xerrors.Errorf("%s has less than 2 values", path)
}

if values[0] == "max" {
quota = math.MaxUint64
} else {
quota, err = strconv.ParseUint(values[0], 10, 64)
if err != nil {
return 0, 0, err
}
}

period, err = strconv.ParseUint(values[1], 10, 64)
if err != nil {
return 0, 0, err
}

return quota, period, nil
}

// Stat returns cpu statistics (all values are in microseconds)
func (c *Cpu) Stat() (*cgroups.CpuStats, error) {
path := filepath.Join(c.path, "cpu.stat")
statMap, err := cgroups.ReadFlatKeyedFile(path)
if err != nil {
return nil, err
}

stats := cgroups.CpuStats{
UsageTotal: statMap[StatUsageTotal],
UsageUser: statMap[StatUsageUser],
UsageSystem: statMap[StatUsageSystem],
}

return &stats, nil
}
43 changes: 43 additions & 0 deletions components/common-go/cgroups/v2/cpu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package v2

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMax(t *testing.T) {
mountPoint := createMaxFile(t)

cpu := NewCpuControllerWithMount(mountPoint, "cgroup")
quota, period, err := cpu.Max()
if err != nil {
t.Fatal(err)
}

assert.Equal(t, uint64(200_000), quota)
assert.Equal(t, uint64(100_000), period)
}

func createMaxFile(t *testing.T) string {
mountPoint, err := os.MkdirTemp("", "test.max")
if err != nil {
t.Fatal(err)
}
cgroupPath := filepath.Join(mountPoint, "cgroup")
if err := os.MkdirAll(cgroupPath, 0755); err != nil {
t.Fatal(err)
}

if err := os.WriteFile(filepath.Join(cgroupPath, "cpu.max"), []byte("200000 100000\n"), 0755); err != nil {
t.Fatalf("failed to create cpu.max file: %v", err)
}

return mountPoint
}
Loading