Skip to content

Commit

Permalink
Merge pull request moby#4689 from crazy-max/prv-types
Browse files Browse the repository at this point in the history
provenance: move types to a dedicated package
  • Loading branch information
tonistiigi authored Feb 26, 2024
2 parents 596ef8f + 94e2370 commit f2546aa
Show file tree
Hide file tree
Showing 23 changed files with 240 additions and 234 deletions.
12 changes: 6 additions & 6 deletions executor/resources/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"syscall"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/pkg/errors"
)

Expand All @@ -21,8 +21,8 @@ const (
cpuThrottledUsec = "throttled_usec"
)

func getCgroupCPUStat(cgroupPath string) (*types.CPUStat, error) {
cpuStat := &types.CPUStat{}
func getCgroupCPUStat(cgroupPath string) (*resourcestypes.CPUStat, error) {
cpuStat := &resourcestypes.CPUStat{}

// Read cpu.stat file
cpuStatFile, err := os.Open(filepath.Join(cgroupPath, "cpu.stat"))
Expand Down Expand Up @@ -79,7 +79,7 @@ func getCgroupCPUStat(cgroupPath string) (*types.CPUStat, error) {

return cpuStat, nil
}
func parsePressureFile(filename string) (*types.Pressure, error) {
func parsePressureFile(filename string) (*resourcestypes.Pressure, error) {
content, err := os.ReadFile(filename)
if err != nil {
if errors.Is(err, os.ErrNotExist) || errors.Is(err, syscall.ENOTSUP) { // pressure file requires CONFIG_PSI
Expand All @@ -90,7 +90,7 @@ func parsePressureFile(filename string) (*types.Pressure, error) {

lines := strings.Split(string(content), "\n")

pressure := &types.Pressure{}
pressure := &resourcestypes.Pressure{}
for _, line := range lines {
// Skip empty lines
if len(strings.TrimSpace(line)) == 0 {
Expand All @@ -99,7 +99,7 @@ func parsePressureFile(filename string) (*types.Pressure, error) {

fields := strings.Fields(line)
prefix := fields[0]
pressureValues := &types.PressureValues{}
pressureValues := &resourcestypes.PressureValues{}

for i := 1; i < len(fields); i++ {
keyValue := strings.Split(fields[i], "=")
Expand Down
8 changes: 4 additions & 4 deletions executor/resources/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"testing"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -72,14 +72,14 @@ full avg10=0.12 avg60=0.34 avg300=0.56 total=9876`
full56 := 0.56
full9876 := uint64(9876)

expected := &types.Pressure{
Some: &types.PressureValues{
expected := &resourcestypes.Pressure{
Some: &resourcestypes.PressureValues{
Avg10: &some123,
Avg60: &some456,
Avg300: &some789,
Total: &some3031,
},
Full: &types.PressureValues{
Full: &resourcestypes.PressureValues{
Avg10: &full12,
Avg60: &full34,
Avg300: &full56,
Expand Down
6 changes: 3 additions & 3 deletions executor/resources/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"strings"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/pkg/errors"
)

Expand All @@ -24,7 +24,7 @@ const (
ioDiscardIOs = "dios"
)

func getCgroupIOStat(cgroupPath string) (*types.IOStat, error) {
func getCgroupIOStat(cgroupPath string) (*resourcestypes.IOStat, error) {
ioStatPath := filepath.Join(cgroupPath, ioStatFile)
data, err := os.ReadFile(ioStatPath)
if err != nil {
Expand All @@ -34,7 +34,7 @@ func getCgroupIOStat(cgroupPath string) (*types.IOStat, error) {
return nil, errors.Wrapf(err, "failed to read %s", ioStatPath)
}

ioStat := &types.IOStat{}
ioStat := &resourcestypes.IOStat{}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
parts := strings.Fields(line)
Expand Down
10 changes: 5 additions & 5 deletions executor/resources/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"testing"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/stretchr/testify/assert"
)

Expand All @@ -25,22 +25,22 @@ full avg10=0.12 avg60=0.34 avg300=0.56 total=9876`
ioStat, err := getCgroupIOStat(testDir)
assert.NoError(t, err)

var expectedPressure = &types.Pressure{
Some: &types.PressureValues{
var expectedPressure = &resourcestypes.Pressure{
Some: &resourcestypes.PressureValues{
Avg10: float64Ptr(1.23),
Avg60: float64Ptr(4.56),
Avg300: float64Ptr(7.89),
Total: uint64Ptr(3031),
},
Full: &types.PressureValues{
Full: &resourcestypes.PressureValues{
Avg10: float64Ptr(0.12),
Avg60: float64Ptr(0.34),
Avg300: float64Ptr(0.56),
Total: uint64Ptr(9876),
},
}

expectedIOStat := &types.IOStat{
expectedIOStat := &resourcestypes.IOStat{
ReadBytes: uint64Ptr(1024 + 512),
WriteBytes: uint64Ptr(2048 + 1024),
DiscardBytes: uint64Ptr(4096 + 2048),
Expand Down
6 changes: 3 additions & 3 deletions executor/resources/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"strings"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -41,8 +41,8 @@ const (
memoryOomKill = "oom_kill"
)

func getCgroupMemoryStat(path string) (*types.MemoryStat, error) {
memoryStat := &types.MemoryStat{}
func getCgroupMemoryStat(path string) (*resourcestypes.MemoryStat, error) {
memoryStat := &resourcestypes.MemoryStat{}

// Parse memory.stat
err := parseKeyValueFile(filepath.Join(path, memoryStatFile), func(key string, value uint64) {
Expand Down
10 changes: 5 additions & 5 deletions executor/resources/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"testing"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -51,22 +51,22 @@ oom_kill 5`
memoryStat, err := getCgroupMemoryStat(testDir)
assert.NoError(t, err)

var expectedPressure = &types.Pressure{
Some: &types.PressureValues{
var expectedPressure = &resourcestypes.Pressure{
Some: &resourcestypes.PressureValues{
Avg10: float64Ptr(1.23),
Avg60: float64Ptr(4.56),
Avg300: float64Ptr(7.89),
Total: uint64Ptr(3031),
},
Full: &types.PressureValues{
Full: &resourcestypes.PressureValues{
Avg10: float64Ptr(0.12),
Avg60: float64Ptr(0.34),
Avg300: float64Ptr(0.56),
Total: uint64Ptr(9876),
},
}

expectedMemoryStat := &types.MemoryStat{
expectedMemoryStat := &resourcestypes.MemoryStat{
SwapBytes: uint64Ptr(987654),
Anon: uint64Ptr(24576),
File: uint64Ptr(12791808),
Expand Down
22 changes: 11 additions & 11 deletions executor/resources/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sync"
"time"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/network"
"github.com/prometheus/procfs"
Expand All @@ -30,15 +30,15 @@ var isCgroupV2 bool
type cgroupRecord struct {
once sync.Once
ns string
sampler *Sub[*types.Sample]
sampler *Sub[*resourcestypes.Sample]
closeSampler func() error
samples []*types.Sample
samples []*resourcestypes.Sample
err error
done chan struct{}
monitor *Monitor
netSampler NetworkSampler
startCPUStat *procfs.CPUStat
sysCPUStat *types.SysCPUStat
sysCPUStat *resourcestypes.SysCPUStat
}

func (r *cgroupRecord) Wait() error {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (r *cgroupRecord) close() {
if r.startCPUStat != nil {
stat, err := r.monitor.proc.Stat()
if err == nil {
cpu := &types.SysCPUStat{
cpu := &resourcestypes.SysCPUStat{
User: stat.CPUTotal.User - r.startCPUStat.User,
Nice: stat.CPUTotal.Nice - r.startCPUStat.Nice,
System: stat.CPUTotal.System - r.startCPUStat.System,
Expand All @@ -108,7 +108,7 @@ func (r *cgroupRecord) close() {
})
}

func (r *cgroupRecord) sample(tm time.Time) (*types.Sample, error) {
func (r *cgroupRecord) sample(tm time.Time) (*resourcestypes.Sample, error) {
cpu, err := getCgroupCPUStat(filepath.Join(defaultMountpoint, r.ns))
if err != nil {
return nil, err
Expand All @@ -125,7 +125,7 @@ func (r *cgroupRecord) sample(tm time.Time) (*types.Sample, error) {
if err != nil {
return nil, err
}
sample := &types.Sample{
sample := &resourcestypes.Sample{
Timestamp_: tm,
CPUStat: cpu,
MemoryStat: memory,
Expand All @@ -142,12 +142,12 @@ func (r *cgroupRecord) sample(tm time.Time) (*types.Sample, error) {
return sample, nil
}

func (r *cgroupRecord) Samples() (*types.Samples, error) {
func (r *cgroupRecord) Samples() (*resourcestypes.Samples, error) {
<-r.done
if r.err != nil {
return nil, r.err
}
return &types.Samples{
return &resourcestypes.Samples{
Samples: r.samples,
SysCPUStat: r.sysCPUStat,
}, nil
Expand All @@ -160,7 +160,7 @@ func (r *nopRecord) Wait() error {
return nil
}

func (r *nopRecord) Samples() (*types.Samples, error) {
func (r *nopRecord) Samples() (*resourcestypes.Samples, error) {
return nil, nil
}

Expand Down Expand Up @@ -189,7 +189,7 @@ type RecordOpt struct {
NetworkSampler NetworkSampler
}

func (m *Monitor) RecordNamespace(ns string, opt RecordOpt) (types.Recorder, error) {
func (m *Monitor) RecordNamespace(ns string, opt RecordOpt) (resourcestypes.Recorder, error) {
isClosed := false
select {
case <-m.closed:
Expand Down
6 changes: 3 additions & 3 deletions executor/resources/pids.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"strconv"
"strings"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/pkg/errors"
)

const (
pidsCurrentFile = "pids.current"
)

func getCgroupPIDsStat(path string) (*types.PIDsStat, error) {
pidsStat := &types.PIDsStat{}
func getCgroupPIDsStat(path string) (*resourcestypes.PIDsStat, error) {
pidsStat := &resourcestypes.PIDsStat{}

v, err := parseSingleValueFile(filepath.Join(path, pidsCurrentFile))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions executor/resources/pids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"path/filepath"
"testing"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -16,7 +16,7 @@ func TestParsePidsStat(t *testing.T) {
err := os.WriteFile(filepath.Join(testDir, "pids.current"), []byte("123"), 0644)
assert.NoError(t, err)

expectedPidsStat := &types.PIDsStat{
expectedPidsStat := &resourcestypes.PIDsStat{
Current: uint64Ptr(123),
}
stats, err := getCgroupPIDsStat(filepath.Join(testDir))
Expand Down
6 changes: 3 additions & 3 deletions executor/resources/sys.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package resources

import "github.com/moby/buildkit/executor/resources/types"
import resourcestypes "github.com/moby/buildkit/executor/resources/types"

type SysSampler = Sub[*types.SysSample]
type SysSampler = Sub[*resourcestypes.SysSample]

func NewSysSampler() (*Sampler[*types.SysSample], error) {
func NewSysSampler() (*Sampler[*resourcestypes.SysSample], error) {
return newSysSampler()
}
16 changes: 8 additions & 8 deletions executor/resources/sys_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ import (
"os"
"time"

"github.com/moby/buildkit/executor/resources/types"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/prometheus/procfs"
)

func newSysSampler() (*Sampler[*types.SysSample], error) {
func newSysSampler() (*Sampler[*resourcestypes.SysSample], error) {
pfs, err := procfs.NewDefaultFS()
if err != nil {
return nil, err
}

return NewSampler(2*time.Second, 20, func(tm time.Time) (*types.SysSample, error) {
return NewSampler(2*time.Second, 20, func(tm time.Time) (*resourcestypes.SysSample, error) {
return sampleSys(pfs, tm)
}), nil
}

func sampleSys(proc procfs.FS, tm time.Time) (*types.SysSample, error) {
func sampleSys(proc procfs.FS, tm time.Time) (*resourcestypes.SysSample, error) {
stat, err := proc.Stat()
if err != nil {
return nil, err
}

s := &types.SysSample{
s := &resourcestypes.SysSample{
Timestamp_: tm,
}

s.CPUStat = &types.SysCPUStat{
s.CPUStat = &resourcestypes.SysCPUStat{
User: stat.CPUTotal.User,
Nice: stat.CPUTotal.Nice,
System: stat.CPUTotal.System,
Expand All @@ -42,7 +42,7 @@ func sampleSys(proc procfs.FS, tm time.Time) (*types.SysSample, error) {
GuestNice: stat.CPUTotal.GuestNice,
}

s.ProcStat = &types.ProcStat{
s.ProcStat = &resourcestypes.ProcStat{
ContextSwitches: stat.ContextSwitches,
ProcessCreated: stat.ProcessCreated,
ProcessesRunning: stat.ProcessesRunning,
Expand All @@ -53,7 +53,7 @@ func sampleSys(proc procfs.FS, tm time.Time) (*types.SysSample, error) {
return nil, err
}

s.MemoryStat = &types.SysMemoryStat{
s.MemoryStat = &resourcestypes.SysMemoryStat{
Total: mem.MemTotal,
Free: mem.MemFree,
Buffers: mem.Buffers,
Expand Down
4 changes: 2 additions & 2 deletions executor/resources/sys_nolinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

package resources

import "github.com/moby/buildkit/executor/resources/types"
import resourcestypes "github.com/moby/buildkit/executor/resources/types"

func newSysSampler() (*Sampler[*types.SysSample], error) {
func newSysSampler() (*Sampler[*resourcestypes.SysSample], error) {
return nil, nil
}
Loading

0 comments on commit f2546aa

Please sign in to comment.