5175: Do not add +sha1 tag unless --sha1-tag flag given.
[arvados.git] / services / crunchstat / crunchstat.go
index 91027d7677b93c04e0efcc7f79dff25d64950d54..e35e98aa59dedba5848d2a6cc1013fd322c15f09 100644 (file)
@@ -34,11 +34,19 @@ type Cgroup struct {
        cid    string
 }
 
-func CopyPipeToChan(in io.Reader, out chan string, done chan<- bool) {
+func CopyPipeToChan(in io.ReadCloser, out chan string, done chan<- bool) {
+       defer in.Close()
+
+       // TODO(twp): handle long input records gracefully, if possible
+       // without killing the child task (#4889)
+       //
        s := bufio.NewScanner(in)
        for s.Scan() {
                out <- s.Text()
        }
+       if s.Err() != nil {
+               out <- fmt.Sprintf("crunchstat: line buffering error: %s", s.Err())
+       }
        done <- true
 }
 
@@ -49,11 +57,12 @@ func CopyChanToPipe(in <-chan string, out io.Writer) {
 }
 
 var logChan chan string
+
 func LogPrintf(format string, args ...interface{}) {
        if logChan == nil {
                return
        }
-       logChan <- fmt.Sprintf("crunchstat: " + format, args...)
+       logChan <- fmt.Sprintf("crunchstat: "+format, args...)
 }
 
 func ReadAllOrWarn(in *os.File) ([]byte, error) {
@@ -192,7 +201,7 @@ func DoMemoryStats(cgroup Cgroup) {
        defer c.Close()
        b := bufio.NewScanner(c)
        thisSample := MemSample{time.Now(), make(map[string]int64)}
-       wantStats := [...]string{"cache", "pgmajfault", "rss"}
+       wantStats := [...]string{"cache", "swap", "pgmajfault", "rss"}
        for b.Scan() {
                var stat string
                var val int64
@@ -242,12 +251,12 @@ func DoNetworkStats(cgroup Cgroup, lastSample map[string]IoSample) {
                nextSample.txBytes = tx
                nextSample.rxBytes = rx
                var delta string
-               if lastSample, ok := lastSample[ifName]; ok {
-                       interval := nextSample.sampleTime.Sub(lastSample.sampleTime).Seconds()
+               if prev, ok := lastSample[ifName]; ok {
+                       interval := nextSample.sampleTime.Sub(prev.sampleTime).Seconds()
                        delta = fmt.Sprintf(" -- interval %.4f seconds %d tx %d rx",
                                interval,
-                               tx-lastSample.txBytes,
-                               rx-lastSample.rxBytes)
+                               tx-prev.txBytes,
+                               rx-prev.rxBytes)
                }
                LogPrintf("net:%s %d tx %d rx%s", ifName, tx, rx, delta)
                lastSample[ifName] = nextSample