Merge branch '14998-hoststat-total'
[arvados.git] / lib / crunchstat / crunchstat.go
index 714e7929ab0a81a88dd94100abe9120f9b559b8c..028083fa0d1a23442f527b24f8ce95aacff660f4 100644 (file)
@@ -53,6 +53,9 @@ type Reporter struct {
        // Interval between samples. Must be positive.
        PollPeriod time.Duration
 
+       // Temporary directory, will be monitored for available, used & total space.
+       TempDir string
+
        // Where to write statistics. Must not be nil.
        Logger *log.Logger
 
@@ -253,8 +256,13 @@ func (r *Reporter) doMemoryStats() {
        }
        var outstat bytes.Buffer
        for _, key := range wantStats {
-               if val, ok := thisSample.memStat[key]; ok {
-                       outstat.WriteString(fmt.Sprintf(" %d %s", val, key))
+               // Use "total_X" stats (entire hierarchy) if enabled,
+               // otherwise just the single cgroup -- see
+               // https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
+               if val, ok := thisSample.memStat["total_"+key]; ok {
+                       fmt.Fprintf(&outstat, " %d %s", val, key)
+               } else if val, ok := thisSample.memStat[key]; ok {
+                       fmt.Fprintf(&outstat, " %d %s", val, key)
                }
        }
        r.Logger.Printf("mem%s\n", outstat.String())
@@ -314,7 +322,7 @@ type diskSpaceSample struct {
 
 func (r *Reporter) doDiskSpaceStats() {
        s := syscall.Statfs_t{}
-       err := syscall.Statfs("/tmp", &s)
+       err := syscall.Statfs(r.TempDir, &s)
        if err != nil {
                return
        }
@@ -323,7 +331,7 @@ func (r *Reporter) doDiskSpaceStats() {
                hasData:    true,
                sampleTime: time.Now(),
                total:      s.Blocks * bs,
-               used:       (s.Blocks - s.Bavail) * bs,
+               used:       (s.Blocks - s.Bfree) * bs,
                available:  s.Bavail * bs,
        }
 
@@ -335,7 +343,7 @@ func (r *Reporter) doDiskSpaceStats() {
                        interval,
                        int64(nextSample.used-prev.used))
        }
-       r.Logger.Printf("tmpdir available:%d used:%d total:%d%s\n",
+       r.Logger.Printf("statfs %d available %d used %d total%s\n",
                nextSample.available, nextSample.used, nextSample.total, delta)
        r.lastDiskSpaceSample = nextSample
 }
@@ -422,6 +430,14 @@ func (r *Reporter) run() {
        r.lastNetSample = make(map[string]ioSample)
        r.lastDiskIOSample = make(map[string]ioSample)
 
+       if len(r.TempDir) == 0 {
+               // Temporary dir not provided, try to get it from the environment.
+               r.TempDir = os.Getenv("TMPDIR")
+       }
+       if len(r.TempDir) > 0 {
+               r.Logger.Printf("notice: monitoring temp dir %s\n", r.TempDir)
+       }
+
        ticker := time.NewTicker(r.PollPeriod)
        for {
                r.doMemoryStats()