}
}
-func FindStat(cgroup_path string, statgroup string, stat string) string {
- path := fmt.Sprintf("%s/%s.%s", cgroup_path, statgroup, stat)
+func FindStat(cgroup_root string, cgroup_parent string, container_id string, statgroup string, stat string) string {
+ var path string
+ path = fmt.Sprintf("%s/%s/%s/%s/%s.%s", cgroup_root, statgroup, cgroup_parent, container_id, statgroup, stat)
if _, err := os.Stat(path); err == nil {
return path
}
- path = fmt.Sprintf("%s/%s/%s.%s", cgroup_path, statgroup, statgroup, stat)
+ path = fmt.Sprintf("%s/%s/%s/%s.%s", cgroup_root, cgroup_parent, container_id, statgroup, stat)
+ if _, err := os.Stat(path); err == nil {
+ return path
+ }
+ path = fmt.Sprintf("%s/%s/%s.%s", cgroup_root, statgroup, statgroup, stat)
+ if _, err := os.Stat(path); err == nil {
+ return path
+ }
+ path = fmt.Sprintf("%s/%s.%s", cgroup_root, statgroup, stat)
if _, err := os.Stat(path); err == nil {
return path
}
return ""
}
-func PollCgroupStats(cgroup_path string, stderr chan string, poll int64) {
+func PollCgroupStats(cgroup_root string, cgroup_parent string, container_id string, stderr chan string, poll int64) {
//var last_usage int64 = 0
var last_user int64 = 0
var last_sys int64 = 0
disk := make(map[string]*Disk)
//cpuacct_usage := FindStat(cgroup_path, "cpuacct", "usage")
- cpuacct_stat := FindStat(cgroup_path, "cpuacct", "stat")
- blkio_io_service_bytes := FindStat(cgroup_path, "blkio", "io_service_bytes")
- cpuset_cpus := FindStat(cgroup_path, "cpuset", "cpus")
- memory_stat := FindStat(cgroup_path, "memory", "stat")
+ cpuacct_stat := FindStat(cgroup_root, cgroup_parent, container_id, "cpuacct", "stat")
+ blkio_io_service_bytes := FindStat(cgroup_root, cgroup_parent, container_id, "blkio", "io_service_bytes")
+ cpuset_cpus := FindStat(cgroup_root, cgroup_parent, container_id, "cpuset", "cpus")
+ memory_stat := FindStat(cgroup_root, cgroup_parent, container_id, "memory", "stat")
if cpuacct_stat != "" {
stderr <- fmt.Sprintf("crunchstat: reading stats from %s", cpuacct_stat)
func main() {
var (
- cgroup_path string
+ cgroup_root string
cgroup_parent string
cgroup_cidfile string
wait int64
poll int64
)
- flag.StringVar(&cgroup_path, "cgroup-path", "", "Direct path to cgroup")
- flag.StringVar(&cgroup_parent, "cgroup-parent", "", "Path to parent cgroup")
+ flag.StringVar(&cgroup_root, "cgroup-root", "", "Root of cgroup tree")
+ flag.StringVar(&cgroup_parent, "cgroup-parent", "", "Name of container parent under cgroup")
flag.StringVar(&cgroup_cidfile, "cgroup-cid", "", "Path to container id file")
flag.Int64Var(&wait, "wait", 5, "Maximum time (in seconds) to wait for cid file to show up")
flag.Int64Var(&poll, "poll", 1000, "Polling frequency, in milliseconds")
logger := log.New(os.Stderr, "crunchstat: ", 0)
- if cgroup_path == "" && cgroup_cidfile == "" {
- logger.Fatal("Must provide either -cgroup-path or -cgroup-cid")
+ if cgroup_root == "" {
+ logger.Fatal("Must provide either -cgroup-root")
}
// Make output channel
}
// Read the cid file
+ var container_id string
if cgroup_cidfile != "" {
// wait up to 'wait' seconds for the cid file to appear
var i time.Duration
if err == nil {
cid, err2 := ioutil.ReadAll(f)
if err2 == nil && len(cid) > 0 {
- cgroup_path = string(cid)
+ container_id = string(cid)
f.Close()
break
}
}
time.Sleep(100 * time.Millisecond)
}
- if cgroup_path == "" {
+ if cgroup_root == "" {
logger.Printf("Could not read cid file %s", cgroup_cidfile)
}
}
- // add the parent prefix
- if cgroup_parent != "" {
- cgroup_path = fmt.Sprintf("%s/%s", cgroup_parent, cgroup_path)
- }
-
- logger.Print("Using cgroup ", cgroup_path)
-
- go PollCgroupStats(cgroup_path, stderr_chan, poll)
+ go PollCgroupStats(cgroup_root, cgroup_parent, container_id, stderr_chan, poll)
// Wait for each of stdout and stderr to drain
<-finish_chan