- morning := time.Now()
- elapsed := morning.Sub(bedtime).Seconds()
- if cpuset_cpus != "" {
- b, err := OpenAndReadAll(cpuset_cpus, stderr)
- if err != nil {
- // cgroup probably gone -- skip other stats too.
- continue
- }
- sp := strings.Split(string(b), ",")
- cpus := int64(0)
- 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
- }
- }
- last_cpucount = cpus
- }
- if cpuacct_stat != "" {
- b, err := OpenAndReadAll(cpuacct_stat, stderr)
- if err != nil {
- // Next time around, last_user would
- // be >1 interval old, so stats will
- // be incorrect. Start over instead.
- last_user = -1
-
- // cgroup probably gone -- skip other stats too.
- continue
- }
- var next_user int64
- var next_sys int64
- fmt.Sscanf(string(b), "user %d\nsystem %d", &next_user, &next_sys)
-
- delta := ""
- if elapsed > 0 && last_user != -1 {
- delta = fmt.Sprintf(" -- interval %.4f seconds %.4f user %.4f sys",
- elapsed,
- float64(next_user - last_user) / user_hz,
- float64(next_sys - last_sys) / user_hz)
- }
- stderr <- fmt.Sprintf("crunchstat: cpu %.4f user %.4f sys %d cpus%s",
- float64(next_user) / user_hz,
- float64(next_sys) / user_hz,
- last_cpucount,
- delta)
- last_user = next_user
- last_sys = next_sys
- }
- if blkio_io_service_bytes != "" {
- c, err := os.Open(blkio_io_service_bytes)
- if err != nil {
- stderr <- fmt.Sprintf("open %s: %s", blkio_io_service_bytes, err)
- // cgroup probably gone -- skip other stats too.
- continue
- }
- defer c.Close()
- 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 {
- continue
- }
- 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)
- }
- }
- }
- }
-
- if memory_stat != "" {
- c, err := os.Open(memory_stat)
- if err != nil {
- stderr <- fmt.Sprintf("open %s: %s", memory_stat, err)
- // cgroup probably gone -- skip other stats too.
- continue
- }
- 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()
- }
-
- lastNetStat = DoNetworkStats(stderr, cgroup, lastNetStat)