1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
package resources
import (
"bufio"
"context"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
resourcestypes "github.com/moby/buildkit/executor/resources/types"
"github.com/moby/buildkit/util/bklog"
"github.com/prometheus/procfs"
)
const (
cgroupProcsFile = "cgroup.procs"
cgroupControllersFile = "cgroup.controllers"
cgroupSubtreeFile = "cgroup.subtree_control"
defaultMountpoint = "/sys/fs/cgroup"
initGroup = "init"
)
var initOnce sync.Once
var isCgroupV2 bool
type cgroupRecord struct {
once sync.Once
ns string
sampler *Sub[*resourcestypes.Sample]
closeSampler func() error
samples []*resourcestypes.Sample
err error
done chan struct{}
monitor *Monitor
netSampler NetworkSampler
startCPUStat *procfs.CPUStat
sysCPUStat *resourcestypes.SysCPUStat
}
func (r *cgroupRecord) Wait() error {
go r.close()
<-r.done
return r.err
}
func (r *cgroupRecord) Start() {
if stat, err := r.monitor.proc.Stat(); err == nil {
r.startCPUStat = &stat.CPUTotal
}
s := NewSampler(2*time.Second, 10, r.sample)
r.sampler = s.Record()
r.closeSampler = s.Close
}
func (r *cgroupRecord) Close() {
r.close()
}
func (r *cgroupRecord) CloseAsync(next func(context.Context) error) error {
go func() {
r.close()
next(context.TODO())
}()
return nil
}
func (r *cgroupRecord) close() {
r.once.Do(func() {
defer close(r.done)
go func() {
r.monitor.mu.Lock()
delete(r.monitor.records, r.ns)
r.monitor.mu.Unlock()
}()
if r.sampler == nil {
return
}
s, err := r.sampler.Close(true)
if err != nil {
r.err = err
} else {
r.samples = s
}
r.closeSampler()
if r.startCPUStat != nil {
stat, err := r.monitor.proc.Stat()
if err == nil {
cpu := &resourcestypes.SysCPUStat{
User: stat.CPUTotal.User - r.startCPUStat.User,
Nice: stat.CPUTotal.Nice - r.startCPUStat.Nice,
System: stat.CPUTotal.System - r.startCPUStat.System,
Idle: stat.CPUTotal.Idle - r.startCPUStat.Idle,
Iowait: stat.CPUTotal.Iowait - r.startCPUStat.Iowait,
IRQ: stat.CPUTotal.IRQ - r.startCPUStat.IRQ,
SoftIRQ: stat.CPUTotal.SoftIRQ - r.startCPUStat.SoftIRQ,
Steal: stat.CPUTotal.Steal - r.startCPUStat.Steal,
Guest: stat.CPUTotal.Guest - r.startCPUStat.Guest,
GuestNice: stat.CPUTotal.GuestNice - r.startCPUStat.GuestNice,
}
r.sysCPUStat = cpu
}
}
})
}
func (r *cgroupRecord) sample(tm time.Time) (*resourcestypes.Sample, error) {
cpu, err := getCgroupCPUStat(filepath.Join(defaultMountpoint, r.ns))
if err != nil {
return nil, err
}
memory, err := getCgroupMemoryStat(filepath.Join(defaultMountpoint, r.ns))
if err != nil {
return nil, err
}
io, err := getCgroupIOStat(filepath.Join(defaultMountpoint, r.ns))
if err != nil {
return nil, err
}
pids, err := getCgroupPIDsStat(filepath.Join(defaultMountpoint, r.ns))
if err != nil {
return nil, err
}
sample := &resourcestypes.Sample{
Timestamp_: tm,
CPUStat: cpu,
MemoryStat: memory,
IOStat: io,
PIDsStat: pids,
}
if r.netSampler != nil {
net, err := r.netSampler.Sample()
if err != nil {
return nil, err
}
sample.NetStat = net
}
return sample, nil
}
func (r *cgroupRecord) Samples() (*resourcestypes.Samples, error) {
<-r.done
if r.err != nil {
return nil, r.err
}
return &resourcestypes.Samples{
Samples: r.samples,
SysCPUStat: r.sysCPUStat,
}, nil
}
type nopRecord struct {
}
func (r *nopRecord) Wait() error {
return nil
}
func (r *nopRecord) Samples() (*resourcestypes.Samples, error) {
return nil, nil
}
func (r *nopRecord) Close() {
}
func (r *nopRecord) CloseAsync(next func(context.Context) error) error {
return next(context.TODO())
}
func (r *nopRecord) Start() {
}
type Monitor struct {
mu sync.Mutex
closed chan struct{}
records map[string]*cgroupRecord
proc procfs.FS
}
type NetworkSampler interface {
Sample() (*resourcestypes.NetworkSample, error)
}
type RecordOpt struct {
NetworkSampler NetworkSampler
}
func (m *Monitor) RecordNamespace(ns string, opt RecordOpt) (resourcestypes.Recorder, error) {
isClosed := false
select {
case <-m.closed:
isClosed = true
default:
}
if !isCgroupV2 || isClosed {
return &nopRecord{}, nil
}
r := &cgroupRecord{
ns: ns,
done: make(chan struct{}),
monitor: m,
netSampler: opt.NetworkSampler,
}
m.mu.Lock()
m.records[ns] = r
m.mu.Unlock()
return r, nil
}
func (m *Monitor) Close() error {
close(m.closed)
m.mu.Lock()
defer m.mu.Unlock()
for _, r := range m.records {
r.close()
}
return nil
}
func NewMonitor() (*Monitor, error) {
initOnce.Do(func() {
isCgroupV2 = isCgroup2()
if !isCgroupV2 {
return
}
if err := prepareCgroupControllers(); err != nil {
bklog.L.Warnf("failed to prepare cgroup controllers: %+v", err)
}
})
fs, err := procfs.NewDefaultFS()
if err != nil {
return nil, err
}
return &Monitor{
closed: make(chan struct{}),
records: make(map[string]*cgroupRecord),
proc: fs,
}, nil
}
func prepareCgroupControllers() error {
v, ok := os.LookupEnv("BUILDKIT_SETUP_CGROUPV2_ROOT")
if !ok {
return nil
}
if b, _ := strconv.ParseBool(v); !b {
return nil
}
// move current process to init cgroup
if err := os.MkdirAll(filepath.Join(defaultMountpoint, initGroup), 0755); err != nil {
return err
}
f, err := os.OpenFile(filepath.Join(defaultMountpoint, cgroupProcsFile), os.O_RDONLY, 0)
if err != nil {
return err
}
s := bufio.NewScanner(f)
for s.Scan() {
if err := os.WriteFile(filepath.Join(defaultMountpoint, initGroup, cgroupProcsFile), s.Bytes(), 0); err != nil {
return err
}
}
if err := f.Close(); err != nil {
return err
}
dt, err := os.ReadFile(filepath.Join(defaultMountpoint, cgroupControllersFile))
if err != nil {
return err
}
for _, c := range strings.Split(string(dt), " ") {
if c == "" {
continue
}
if err := os.WriteFile(filepath.Join(defaultMountpoint, cgroupSubtreeFile), []byte("+"+c), 0); err != nil {
// ignore error
bklog.L.Warnf("failed to enable cgroup controller %q: %+v", c, err)
}
}
return nil
}
|