Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Protobuild.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins = ["grpc"]
# Paths that will be added untouched to the end of the includes. We use
# `/usr/local/include` to pickup the common install location of protobuf.
# This is the default.
after = ["/usr/local/include"]
after = ["/usr/local/include", "/usr/include"]

# This section maps protobuf imports to Go packages. These will become
# `-M` directives in the call to the go protobuf generator.
Expand Down
6 changes: 2 additions & 4 deletions cmd/cgctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"encoding/json"
"fmt"
"os"

Expand Down Expand Up @@ -133,9 +134,6 @@ var statCommand = cli.Command{
if err != nil {
return err
}
for k, v := range stats {
fmt.Printf("%s->%d\n", k, v)
}
return nil
return json.NewEncoder(os.Stdout).Encode(stats)
},
}
64 changes: 64 additions & 0 deletions v2/io.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v2

import "fmt"

type IOType string

const (
ReadBPS IOType = "rbps"
WriteBPS IOType = "wbps"
ReadIOPS IOType = "riops"
WriteIOPS IOType = "wiops"
)

type BFQ struct {
Weight uint16
}

type Entry struct {
Type IOType
Major int64
Minor int64
Rate uint64
}

func (e Entry) String() string {
return fmt.Sprintf("%d:%d %s=%d", e.Major, e.Minor, e.Type, e.Rate)
}

type IO struct {
BFQ BFQ
Max []Entry
}

func (i *IO) Values() (o []Value) {
if i.BFQ.Weight != 0 {
o = append(o, Value{
filename: "io.bfq.weight",
value: i.BFQ.Weight,
})
}
for _, e := range i.Max {
o = append(o, Value{
filename: "io.max",
value: e.String(),
})
}
return o
}
111 changes: 107 additions & 4 deletions v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
"bufio"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/containerd/cgroups/v2/stats"
"github.com/pkg/errors"
)

Expand All @@ -43,6 +45,7 @@ type Resources struct {
CPU *CPU
Memory *Memory
Pids *Pids
IO *IO
}

// Values returns the raw filenames and values that
Expand All @@ -52,6 +55,7 @@ func (r *Resources) Values() (o []Value) {
r.CPU,
r.Memory,
r.Pids,
r.IO,
}
for _, v := range values {
if v == nil {
Expand All @@ -74,6 +78,8 @@ func (c *Value) write(path string, perm os.FileMode) error {
switch t := c.value.(type) {
case uint64:
data = []byte(strconv.FormatUint(t, 10))
case uint16:
data = []byte(strconv.FormatUint(uint64(t), 10))
case int64:
data = []byte(strconv.FormatInt(t, 10))
case []byte:
Expand Down Expand Up @@ -245,12 +251,17 @@ func (c *Manager) Procs(recursive bool) ([]uint64, error) {
return processes, err
}

func (c *Manager) Stat() (map[string]uint64, error) {
var singleValueFiles = []string{
"pids.current",
"pids.max",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be another PR, but we need memory.current and memory.swap.current?
https://facebookmicrosites.github.io/cgroup2/docs/memory-controller.html

}

func (c *Manager) Stat() (*stats.Metrics, error) {
controllers, err := c.ListControllers()
if err != nil {
return nil, err
}
out := make(map[string]uint64)
out := make(map[string]interface{})
for _, controller := range controllers {
filename := fmt.Sprintf("%s.stat", controller)
if err := readStatsFile(c.path, filename, out); err != nil {
Expand All @@ -260,10 +271,102 @@ func (c *Manager) Stat() (map[string]uint64, error) {
return nil, err
}
}
return out, nil
for _, name := range singleValueFiles {
if err := readSingleFile(c.path, name, out); err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
}
var metrics stats.Metrics

metrics.Pids = &stats.PidsStat{
Current: getPidValue("pids.current", out),
Limit: getPidValue("pids.max", out),
}
metrics.CPU = &stats.CPUStat{
UsageUsec: out["usage_usec"].(uint64),
UserUsec: out["user_usec"].(uint64),
SystemUsec: out["system_usec"].(uint64),
NrPeriods: out["nr_periods"].(uint64),
NrThrottled: out["nr_throttled"].(uint64),
ThrottledUsec: out["throttled_usec"].(uint64),
}
metrics.Memory = &stats.MemoryStat{
Anon: out["anon"].(uint64),
File: out["file"].(uint64),
KernelStack: out["kernel_stack"].(uint64),
Slab: out["slab"].(uint64),
Sock: out["sock"].(uint64),
Shmem: out["shmem"].(uint64),
FileMapped: out["file_mapped"].(uint64),
FileDirty: out["file_dirty"].(uint64),
FileWriteback: out["file_writeback"].(uint64),
AnonThp: out["anon_thp"].(uint64),
InactiveAnon: out["inactive_anon"].(uint64),
ActiveAnon: out["active_anon"].(uint64),
InactiveFile: out["inactive_file"].(uint64),
ActiveFile: out["active_file"].(uint64),
Unevictable: out["unevictable"].(uint64),
SlabReclaimable: out["slab_reclaimable"].(uint64),
SlabUnreclaimable: out["slab_unreclaimable"].(uint64),
Pgfault: out["pgfault"].(uint64),
Pgmajfault: out["pgmajfault"].(uint64),
WorkingsetRefault: out["workingset_refault"].(uint64),
WorkingsetActivate: out["workingset_activate"].(uint64),
WorkingsetNodereclaim: out["workingset_nodereclaim"].(uint64),
Pgrefill: out["pgrefill"].(uint64),
Pgscan: out["pgscan"].(uint64),
Pgsteal: out["pgsteal"].(uint64),
Pgactivate: out["pgactivate"].(uint64),
Pgdeactivate: out["pgdeactivate"].(uint64),
Pglazyfree: out["pglazyfree"].(uint64),
Pglazyfreed: out["pglazyfreed"].(uint64),
ThpFaultAlloc: out["thp_fault_alloc"].(uint64),
ThpCollapseAlloc: out["thp_collapse_alloc"].(uint64),
}
return &metrics, nil
}

func getPidValue(key string, out map[string]interface{}) uint64 {
v, ok := out[key]
if !ok {
return 0
}
switch t := v.(type) {
case uint64:
return t
case string:
if t == "max" {
return math.MaxUint64
}
}
return 0
}

func readSingleFile(path string, file string, out map[string]interface{}) error {
f, err := os.Open(filepath.Join(path, file))
if err != nil {
return err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
s := string(data)
v, err := parseUint(s, 10, 64)
if err != nil {
// if we cannot parse as a uint, parse as a string
out[file] = s
return nil
}
out[file] = v
return nil
}

func readStatsFile(path string, file string, out map[string]uint64) error {
func readStatsFile(path string, file string, out map[string]interface{}) error {
f, err := os.Open(filepath.Join(path, file))
if err != nil {
return err
Expand Down
Loading