X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/b211e857d304f7fbe8787d2b65a307da841d047b..535acf5c59158521330ef2ef10f4146c905cf39c:/lib/crunchstat/crunchstat.go diff --git a/lib/crunchstat/crunchstat.go b/lib/crunchstat/crunchstat.go index 714e7929ab..10cd7cfce4 100644 --- a/lib/crunchstat/crunchstat.go +++ b/lib/crunchstat/crunchstat.go @@ -21,16 +21,6 @@ import ( "time" ) -// This magically allows us to look up userHz via _SC_CLK_TCK: - -/* -#include -#include -#include -#include -*/ -import "C" - // A Reporter gathers statistics for a cgroup and writes them to a // log.Logger. type Reporter struct { @@ -53,6 +43,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 +246,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 +312,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 +321,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 +333,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 } @@ -387,7 +385,7 @@ func (r *Reporter) doCPUStats() { var userTicks, sysTicks int64 fmt.Sscanf(string(b), "user %d\nsystem %d", &userTicks, &sysTicks) - userHz := float64(C.sysconf(C._SC_CLK_TCK)) + userHz := float64(100) nextSample := cpuSample{ hasData: true, sampleTime: time.Now(), @@ -422,6 +420,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()