aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/elastic/gosigar/sigar_unix.go
blob: 3f3a9f7ff2c2c331d63a8d80160d245ff991d6de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2012 VMware, Inc.

// +build darwin freebsd linux

package gosigar

import (
    "syscall"
    "time"

    "golang.org/x/sys/unix"
)

func (self *FileSystemUsage) Get(path string) error {
    stat := syscall.Statfs_t{}
    err := syscall.Statfs(path, &stat)
    if err != nil {
        return err
    }

    self.Total = uint64(stat.Blocks) * uint64(stat.Bsize)
    self.Free = uint64(stat.Bfree) * uint64(stat.Bsize)
    self.Avail = uint64(stat.Bavail) * uint64(stat.Bsize)
    self.Used = self.Total - self.Free
    self.Files = stat.Files
    self.FreeFiles = uint64(stat.Ffree)

    return nil
}

func (r *Rusage) Get(who int) error {
    ru, err := getResourceUsage(who)
    if err != nil {
        return err
    }

    uTime := convertRtimeToDur(ru.Utime)
    sTime := convertRtimeToDur(ru.Stime)

    r.Utime = uTime
    r.Stime = sTime
    r.Maxrss = int64(ru.Maxrss)
    r.Ixrss = int64(ru.Ixrss)
    r.Idrss = int64(ru.Idrss)
    r.Isrss = int64(ru.Isrss)
    r.Minflt = int64(ru.Minflt)
    r.Majflt = int64(ru.Majflt)
    r.Nswap = int64(ru.Nswap)
    r.Inblock = int64(ru.Inblock)
    r.Oublock = int64(ru.Oublock)
    r.Msgsnd = int64(ru.Msgsnd)
    r.Msgrcv = int64(ru.Msgrcv)
    r.Nsignals = int64(ru.Nsignals)
    r.Nvcsw = int64(ru.Nvcsw)
    r.Nivcsw = int64(ru.Nivcsw)

    return nil
}

func getResourceUsage(who int) (unix.Rusage, error) {
    r := unix.Rusage{}
    err := unix.Getrusage(who, &r)

    return r, err
}

func convertRtimeToDur(t unix.Timeval) time.Duration {
    return time.Duration(t.Nano())
}