-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
- var last_cpucount int64 = 0
-
- type Disk struct {
- last_read int64
- next_read int64
- last_write int64
- next_write int64
- }
-
- disk := make(map[string]*Disk)
-
- //cpuacct_usage := FindStat(cgroup_path, "cpuacct", "usage")
- 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)
- }
- if blkio_io_service_bytes != "" {
- stderr <- fmt.Sprintf("crunchstat: reading stats from %s", blkio_io_service_bytes)
- }
- if cpuset_cpus != "" {
- stderr <- fmt.Sprintf("crunchstat: reading stats from %s", cpuset_cpus)
- }
- if memory_stat != "" {
- stderr <- fmt.Sprintf("crunchstat: reading stats from %s", memory_stat)
- }
-
- var elapsed int64 = poll
-
- for {
- /*{
- c, _ := os.Open(cpuacct_usage)
- b, _ := ioutil.ReadAll(c)
- var next int64
- fmt.Sscanf(string(b), "%d", &next)
- if last_usage != 0 {
- stderr <- fmt.Sprintf("crunchstat: cpuacct.usage %v", (next-last_usage)/10000000)
- }
- //fmt.Printf("usage %d %d %d %d%%\n", last_usage, next, next-last_usage, (next-last_usage)/10000000)
- last_usage = next
- c.Close()
- }*/
- var cpus int64 = 0
- if cpuset_cpus != "" {
- c, _ := os.Open(cpuset_cpus)
- b, _ := ioutil.ReadAll(c)
- sp := strings.Split(string(b), ",")
- for _, v := range sp {
- var min, max int64
- n, _ := fmt.Sscanf(v, "%d-%d", &min, &max)
- if n == 2 {
- cpus += (max - min) + 1
- } else {
- cpus += 1
- }
- }
-
- if cpus != last_cpucount {
- stderr <- fmt.Sprintf("crunchstat: cpuset.cpus %v", cpus)
- }
- last_cpucount = cpus
-
- c.Close()
- }
- if cpus == 0 {
- cpus = 1
- }
- if cpuacct_stat != "" {
- c, _ := os.Open(cpuacct_stat)
- b, _ := ioutil.ReadAll(c)
- var next_user int64
- var next_sys int64
- fmt.Sscanf(string(b), "user %d\nsystem %d", &next_user, &next_sys)
- c.Close()
-
- if last_user != 0 {
- user_diff := next_user - last_user
- sys_diff := next_sys - last_sys
- // Assume we're reading stats based on 100
- // jiffies per second. Because the elapsed
- // time is in milliseconds, we need to boost
- // that to 1000 jiffies per second, then boost
- // it by another 100x to get a percentage, then
- // finally divide by the actual elapsed time
- // and the number of cpus to get average load
- // over the polling period.
- user_pct := (user_diff * 10 * 100) / (elapsed * cpus)
- sys_pct := (sys_diff * 10 * 100) / (elapsed * cpus)
-
- stderr <- fmt.Sprintf("crunchstat: cpuacct.stat user %v", user_pct)
- stderr <- fmt.Sprintf("crunchstat: cpuacct.stat sys %v", sys_pct)
- }
-
- /*fmt.Printf("user %d %d %d%%\n", last_user, next_user, next_user-last_user)
- fmt.Printf("sys %d %d %d%%\n", last_sys, next_sys, next_sys-last_sys)
- fmt.Printf("sum %d%%\n", (next_user-last_user)+(next_sys-last_sys))*/
- last_user = next_user
- last_sys = next_sys
- }
- if blkio_io_service_bytes != "" {
- c, _ := os.Open(blkio_io_service_bytes)
- b := bufio.NewScanner(c)
- var device, op string
- var next int64
- for b.Scan() {
- if _, err := fmt.Sscanf(string(b.Text()), "%s %s %d", &device, &op, &next); err == nil {
- if disk[device] == nil {
- disk[device] = new(Disk)
- }
- if op == "Read" {
- disk[device].last_read = disk[device].next_read
- disk[device].next_read = next
- if disk[device].last_read > 0 && (disk[device].next_read != disk[device].last_read) {
- stderr <- fmt.Sprintf("crunchstat: blkio.io_service_bytes %s read %v", device, disk[device].next_read-disk[device].last_read)
- }
- }
- if op == "Write" {
- disk[device].last_write = disk[device].next_write
- disk[device].next_write = next
- if disk[device].last_write > 0 && (disk[device].next_write != disk[device].last_write) {
- stderr <- fmt.Sprintf("crunchstat: blkio.io_service_bytes %s write %v", device, disk[device].next_write-disk[device].last_write)
- }
- }
- }
- }
- c.Close()
- }
-
- if memory_stat != "" {
- c, _ := os.Open(memory_stat)
- b := bufio.NewScanner(c)
- var stat string
- var val int64
- for b.Scan() {
- if _, err := fmt.Sscanf(string(b.Text()), "%s %d", &stat, &val); err == nil {
- if stat == "rss" {
- stderr <- fmt.Sprintf("crunchstat: memory.stat rss %v", val)
- }
- }
- }
- c.Close()
+func main() {
+ reporter := crunchstat.Reporter{
+ Logger: log.New(os.Stderr, "crunchstat: ", 0),
+ }
+
+ flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
+ flags.StringVar(&reporter.CgroupRoot, "cgroup-root", "", "Root of cgroup tree")
+ flags.StringVar(&reporter.CgroupParent, "cgroup-parent", "", "Name of container parent under cgroup")
+ flags.StringVar(&reporter.CIDFile, "cgroup-cid", "", "Path to container id file")
+ flags.IntVar(&signalOnDeadPPID, "signal-on-dead-ppid", signalOnDeadPPID, "Signal to send child if crunchstat's parent process disappears (0 to disable)")
+ flags.DurationVar(&ppidCheckInterval, "ppid-check-interval", ppidCheckInterval, "Time between checks for parent process disappearance")
+ pollMsec := flags.Int64("poll", 1000, "Reporting interval, in milliseconds")
+ getVersion := flags.Bool("version", false, "Print version information and exit.")
+
+ if ok, code := cmd.ParseFlags(flags, os.Args[0], os.Args[1:], "program [args ...]", os.Stderr); !ok {
+ os.Exit(code)
+ } else if *getVersion {
+ fmt.Printf("crunchstat %s\n", version)
+ return
+ } else if flags.NArg() == 0 {
+ fmt.Fprintf(os.Stderr, "missing required argument: program (try -help)\n")
+ os.Exit(2)
+ }
+
+ reporter.Logger.Printf("crunchstat %s started", version)
+
+ if reporter.CgroupRoot == "" {
+ reporter.Logger.Printf("error: must provide -cgroup-root")
+ os.Exit(2)
+ } else if signalOnDeadPPID < 0 {
+ reporter.Logger.Printf("-signal-on-dead-ppid=%d is invalid (use a positive signal number, or 0 to disable)", signalOnDeadPPID)
+ os.Exit(2)
+ }
+ reporter.PollPeriod = time.Duration(*pollMsec) * time.Millisecond
+
+ reporter.Start()
+ err := runCommand(flags.Args(), reporter.Logger)
+ reporter.Stop()
+
+ if err, ok := err.(*exec.ExitError); ok {
+ // The program has exited with an exit code != 0
+
+ // This works on both Unix and Windows. Although
+ // package syscall is generally platform dependent,
+ // WaitStatus is defined for both Unix and Windows and
+ // in both cases has an ExitStatus() method with the
+ // same signature.
+ if status, ok := err.Sys().(syscall.WaitStatus); ok {
+ os.Exit(status.ExitStatus())
+ } else {
+ reporter.Logger.Printf("ExitError without WaitStatus: %v", err)
+ os.Exit(1)