Skip to content

Commit

Permalink
Use protobufs as Stats return result
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <[email protected]>
  • Loading branch information
crosbymichael committed Sep 6, 2017
1 parent d6e23fb commit 1423fc2
Show file tree
Hide file tree
Showing 14 changed files with 4,636 additions and 160 deletions.
39 changes: 39 additions & 0 deletions Protobuild.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version = "unstable"
generator = "gogoctrd"
plugins = ["grpc"]

# Control protoc include paths. Below are usually some good defaults, but feel
# free to try it without them if it works for your project.
[includes]
# Include paths that will be added before all others. Typically, you want to
# treat the root of the project as an include, but this may not be necessary.
# before = ["."]

# Paths that should be treated as include roots in relation to the vendor
# directory. These will be calculated with the vendor directory nearest the
# target package.
# vendored = ["github.com/gogo/protobuf"]
packages = ["github.com/gogo/protobuf"]

# 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"]

# This section maps protobuf imports to Go packages. These will become
# `-M` directives in the call to the go protobuf generator.
[packages]
"gogoproto/gogo.proto" = "github.com/gogo/protobuf/gogoproto"
"google/protobuf/any.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/descriptor.proto" = "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"google/protobuf/field_mask.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/timestamp.proto" = "github.com/gogo/protobuf/types"

# Aggregrate the API descriptors to lock down API changes.
[[descriptors]]
prefix = "github.com/containerd/cgroups"
target = "metrics.pb.txt"
ignore_files = [
"google/protobuf/descriptor.proto",
"gogoproto/gogo.proto"
]
14 changes: 7 additions & 7 deletions blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (b *blkioController) Update(path string, resources *specs.LinuxResources) e
return b.Create(path, resources)
}

func (b *blkioController) Stat(path string, stats *Stats) error {
stats.Blkio = &BlkioStat{}
func (b *blkioController) Stat(path string, stats *Metrics) error {
stats.Blkio = &BlkIOStat{}
settings := []blkioStatSettings{
{
name: "throttle.io_serviced",
Expand Down Expand Up @@ -119,7 +119,7 @@ func (b *blkioController) Stat(path string, stats *Stats) error {
return nil
}

func (b *blkioController) readEntry(devices map[deviceKey]string, path, name string, entry *[]BlkioEntry) error {
func (b *blkioController) readEntry(devices map[deviceKey]string, path, name string, entry *[]*BlkIOEntry) error {
f, err := os.Open(filepath.Join(b.Path(path), fmt.Sprintf("blkio.%s", name)))
if err != nil {
return err
Expand All @@ -131,7 +131,7 @@ func (b *blkioController) readEntry(devices map[deviceKey]string, path, name str
return err
}
// format: dev type amount
fields := strings.FieldsFunc(sc.Text(), splitBlkioStatLine)
fields := strings.FieldsFunc(sc.Text(), splitBlkIOStatLine)
if len(fields) < 3 {
if len(fields) == 2 && fields[0] == "Total" {
// skip total line
Expand All @@ -158,7 +158,7 @@ func (b *blkioController) readEntry(devices map[deviceKey]string, path, name str
if err != nil {
return err
}
*entry = append(*entry, BlkioEntry{
*entry = append(*entry, &BlkIOEntry{
Device: devices[deviceKey{major, minor}],
Major: major,
Minor: minor,
Expand Down Expand Up @@ -235,7 +235,7 @@ type blkioSettings struct {

type blkioStatSettings struct {
name string
entry *[]BlkioEntry
entry *[]*BlkIOEntry
}

func uintf(v interface{}) []byte {
Expand All @@ -257,7 +257,7 @@ func throttleddev(v interface{}) []byte {
return []byte(fmt.Sprintf("%d:%d %d", td.Major, td.Minor, td.Rate))
}

func splitBlkioStatLine(r rune) bool {
func splitBlkIOStatLine(r rune) bool {
return r == ' ' || r == ':'
}

Expand Down
15 changes: 10 additions & 5 deletions cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ func (c *cgroup) Delete() error {
return nil
}

// Stat returns the current stats for the cgroup
func (c *cgroup) Stat(handlers ...ErrorHandler) (*Stats, error) {
// Stat returns the current metrics for the cgroup
func (c *cgroup) Stat(handlers ...ErrorHandler) (*Metrics, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.err != nil {
Expand All @@ -165,9 +165,14 @@ func (c *cgroup) Stat(handlers ...ErrorHandler) (*Stats, error) {
handlers = append(handlers, errPassthrough)
}
var (
stats = &Stats{}
wg = &sync.WaitGroup{}
errs = make(chan error, len(c.subsystems))
stats = &Metrics{
CPU: &CPUStat{
Throttling: &Throttle{},
Usage: &CPUUsage{},
},
}
wg = &sync.WaitGroup{}
errs = make(chan error, len(c.subsystems))
)
for _, s := range c.subsystems {
if ss, ok := s.(stater); ok {
Expand Down
2 changes: 1 addition & 1 deletion control.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Cgroup interface {
// subsystems are moved one at a time
MoveTo(Cgroup) error
// Stat returns the stats for all subsystems in the cgroup
Stat(...ErrorHandler) (*Stats, error)
Stat(...ErrorHandler) (*Metrics, error)
// Update updates all the subsystems with the provided resource changes
Update(resources *specs.LinuxResources) error
// Processes returns all the processes in a select subsystem for the cgroup
Expand Down
15 changes: 4 additions & 11 deletions cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,13 @@ func (c *cpuController) Update(path string, resources *specs.LinuxResources) err
return c.Create(path, resources)
}

func (c *cpuController) Stat(path string, stats *Stats) error {
func (c *cpuController) Stat(path string, stats *Metrics) error {
f, err := os.Open(filepath.Join(c.Path(path), "cpu.stat"))
if err != nil {
return err
}
defer f.Close()
// get or create the cpu field because cpuacct can also set values on this struct
stats.cpuMu.Lock()
cpu := stats.Cpu
if cpu == nil {
cpu = &CpuStat{}
stats.Cpu = cpu
}
stats.cpuMu.Unlock()
sc := bufio.NewScanner(f)
for sc.Scan() {
if err := sc.Err(); err != nil {
Expand All @@ -109,11 +102,11 @@ func (c *cpuController) Stat(path string, stats *Stats) error {
}
switch key {
case "nr_periods":
cpu.Throttling.Periods = v
stats.CPU.Throttling.Periods = v
case "nr_throttled":
cpu.Throttling.ThrottledPeriods = v
stats.CPU.Throttling.ThrottledPeriods = v
case "throttled_time":
cpu.Throttling.ThrottledTime = v
stats.CPU.Throttling.ThrottledTime = v
}
}
return nil
Expand Down
17 changes: 5 additions & 12 deletions cpuacct.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *cpuacctController) Path(path string) string {
return filepath.Join(c.root, path)
}

func (c *cpuacctController) Stat(path string, stats *Stats) error {
func (c *cpuacctController) Stat(path string, stats *Metrics) error {
user, kernel, err := c.getUsage(path)
if err != nil {
return err
Expand All @@ -43,17 +43,10 @@ func (c *cpuacctController) Stat(path string, stats *Stats) error {
if err != nil {
return err
}
stats.cpuMu.Lock()
cpu := stats.Cpu
if cpu == nil {
cpu = &CpuStat{}
stats.Cpu = cpu
}
stats.cpuMu.Unlock()
cpu.Usage.Total = total
cpu.Usage.User = user
cpu.Usage.Kernel = kernel
cpu.Usage.PerCpu = percpu
stats.CPU.Usage.Total = total
stats.CPU.Usage.User = user
stats.CPU.Usage.Kernel = kernel
stats.CPU.Usage.PerCPU = percpu
return nil
}

Expand Down
15 changes: 8 additions & 7 deletions hugetlb.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@ func (h *hugetlbController) Create(path string, resources *specs.LinuxResources)
return nil
}

func (h *hugetlbController) Stat(path string, stats *Stats) error {
stats.Hugetlb = make(map[string]HugetlbStat)
func (h *hugetlbController) Stat(path string, stats *Metrics) error {
for _, size := range h.sizes {
s, err := h.readSizeStat(path, size)
if err != nil {
return err
}
stats.Hugetlb[size] = s
stats.Hugetlb = append(stats.Hugetlb, s)
}
return nil
}

func (h *hugetlbController) readSizeStat(path, size string) (HugetlbStat, error) {
var s HugetlbStat
func (h *hugetlbController) readSizeStat(path, size string) (*HugetlbStat, error) {
s := HugetlbStat{
Pagesize: size,
}
for _, t := range []struct {
name string
value *uint64
Expand All @@ -84,9 +85,9 @@ func (h *hugetlbController) readSizeStat(path, size string) (HugetlbStat, error)
} {
v, err := readUint(filepath.Join(h.Path(path), strings.Join([]string{"hugetlb", size, t.name}, ".")))
if err != nil {
return s, err
return nil, err
}
*t.value = v
}
return s, nil
return &s, nil
}
17 changes: 11 additions & 6 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,18 @@ func (m *memoryController) Update(path string, resources *specs.LinuxResources)
return m.set(path, settings)
}

func (m *memoryController) Stat(path string, stats *Stats) error {
func (m *memoryController) Stat(path string, stats *Metrics) error {
f, err := os.Open(filepath.Join(m.Path(path), "memory.stat"))
if err != nil {
return err
}
defer f.Close()
stats.Memory = &MemoryStat{}
stats.Memory = &MemoryStat{
Usage: &MemoryEntry{},
Swap: &MemoryEntry{},
Kernel: &MemoryEntry{},
KernelTCP: &MemoryEntry{},
}
if err := m.parseStats(f, stats.Memory); err != nil {
return err
}
Expand All @@ -97,19 +102,19 @@ func (m *memoryController) Stat(path string, stats *Stats) error {
}{
{
module: "",
entry: &stats.Memory.Usage,
entry: stats.Memory.Usage,
},
{
module: "memsw",
entry: &stats.Memory.Swap,
entry: stats.Memory.Swap,
},
{
module: "kmem",
entry: &stats.Memory.Kernel,
entry: stats.Memory.Kernel,
},
{
module: "kmem.tcp",
entry: &stats.Memory.KernelTCP,
entry: stats.Memory.KernelTCP,
},
} {
for _, tt := range []struct {
Expand Down
Loading

0 comments on commit 1423fc2

Please sign in to comment.