dotcloud/docker — pull request #5328 by crosbymichael (2014-04-21 04:37:20+00:00)
Title: Refactor cgroups into subsystems and support Stat method

This pr does some refactoring of the cgroups pkg so that adding more features are easier. Then it adds support for a Stat method to get metrics from the various subsystems. You can test with this small go app with your docker containers.

This is just the low level work required for implementing container metrics in docker. More fun stuff to come.

Thanks to @ehazlett for doing the Stats work. I squashed most of his work in a merge because i'm a meanie.

```go
package main

import (
"flag"
"github.com/dotcloud/docker/pkg/cgroups"
"github.com/dotcloud/docker/pkg/cgroups/fs"
"github.com/samalba/dockerclient"
"log"
)

func init() {
flag.Parse()
if flag.NArg() != 2 {
log.Fatal("must specify a container id and subsystem")
}
}

func main() {
docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock")
if err != nil {
log.Fatal(err)
}
group := &cgroups.Cgroup{
Name: flag.Arg(0),
Parent: "docker",
}

container, err := docker.InspectContainer(flag.Arg(0))
if err != nil {
log.Fatal(err)
}

stats, err := fs.GetStats(group, flag.Arg(1), container.State.Pid)
if err != nil {
log.Fatal(err)
}
for k, v := range stats {
log.Printf("%s: %f\n", k, v)
}
}
```

You can run the app like:

```bash
# get a container id and run
go run cgroup.go c67bbc1b7dcaa8793fedfa55bfc3471b77123090c53ada28142cdadf8bedf94b cpu
go run cgroup.go c67bbc1b7dcaa8793fedfa55bfc3471b77123090c53ada28142cdadf8bedf94b memory
go run cgroup.go c67bbc1b7dcaa8793fedfa55bfc3471b77123090c53ada28142cdadf8bedf94b blkio
go run cgroup.go c67bbc1b7dcaa8793fedfa55bfc3471b77123090c53ada28142cdadf8bedf94b freezer
```