-func DoMemoryStats(stderr chan<- string, cgroup Cgroup) {
- memory_stat := FindStat(stderr, cgroup, "memory", "memory.stat")
- if memory_stat == "" {
- return
- }
- c, err := os.Open(memory_stat)
- if err != nil {
- stderr <- fmt.Sprintf("crunchstat: open %s: %s", memory_stat, err)
- return
- }
- defer c.Close()
- b := bufio.NewScanner(c)
- thisSample := MemSample{time.Now(), make(map[string]int64)}
- wantStats := [...]string{"cache", "pgmajfault", "rss"}
- for b.Scan() {
- var stat string
- var val int64
- if _, err := fmt.Sscanf(string(b.Text()), "%s %d", &stat, &val); err != nil {
- continue
- }
- thisSample.memStat[stat] = val
- }
- var outstat bytes.Buffer
- for _, key := range wantStats {
- if val, ok := thisSample.memStat[key]; ok {
- outstat.WriteString(fmt.Sprintf(" %d %s", val, key))
- }
- }
- stderr <- fmt.Sprintf("crunchstat: mem%s", outstat.String())
-}
-
-func DoNetworkStats(stderr chan<- string, cgroup Cgroup, lastSample map[string]IoSample) (map[string]IoSample) {
- sampleTime := time.Now()
- stats, err := GetContainerNetStats(stderr, cgroup)
- if err != nil { return lastSample }
-
- if lastSample == nil {
- lastSample = make(map[string]IoSample)
- }
- scanner := bufio.NewScanner(stats)
- Iface: for scanner.Scan() {
- var ifName string
- var rx, tx int64
- words := bufio.NewScanner(strings.NewReader(scanner.Text()))
- words.Split(bufio.ScanWords)
- wordIndex := 0
- for words.Scan() {
- word := words.Text()
- switch wordIndex {
- case 0:
- ifName = strings.TrimRight(word, ":")
- case 1:
- if _, err := fmt.Sscanf(word, "%d", &rx); err != nil {
- continue Iface
- }
- case 9:
- if _, err := fmt.Sscanf(word, "%d", &tx); err != nil {
- continue Iface
- }
- }
- wordIndex++
- }
- if ifName == "lo" || ifName == "" || wordIndex != 17 {
- // Skip loopback interface and lines with wrong format
- continue
- }
- nextSample := IoSample{}
- nextSample.sampleTime = sampleTime
- nextSample.txBytes = tx
- nextSample.rxBytes = rx
- var delta string
- if lastSample, ok := lastSample[ifName]; ok {
- interval := nextSample.sampleTime.Sub(lastSample.sampleTime).Seconds()
- delta = fmt.Sprintf(" -- interval %.4f seconds %d tx %d rx",
- interval,
- tx - lastSample.txBytes,
- rx - lastSample.rxBytes)