Skip to content

Commit b0469a4

Browse files
authored
Merge branch 'master' into master
2 parents 8ba220d + e0ec1b9 commit b0469a4

38 files changed

+330
-277
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,3 @@ jobs:
2929
- name: Test
3030
run: |
3131
go test -coverprofile='coverage.out' -covermode=atomic ./...
32-
- name: Upload Code Coverage
33-
uses: codecov/codecov-action@v2
34-
with:
35-
fail_ci_if_error: true
36-
files: coverage.out
37-
flags: ${{ runner.os }},go-${{ matrix.go-version }}
38-
token: ${{ secrets.CODECOV_TOKEN }}

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

2929

3030
-------
31-
internal/common/binary.go in the gopsutil is copied and modifid from golang/encoding/binary.go.
31+
internal/common/binary.go in the gopsutil is copied and modified from golang/encoding/binary.go.
3232

3333

3434

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ challenge is porting all psutil functions on some architectures.
77

88
## v3 migration
99

10-
from v3.20.10, gopsutil becomes v3 which breaks backwards compatiblity.
10+
from v3.20.10, gopsutil becomes v3 which breaks backwards compatibility.
1111
See [v3Changes.md](_tools/v3migration/v3Changes.md) more detail changes.
1212

1313
## Tag semantics
@@ -21,7 +21,7 @@ for example, v2.17.04 means
2121
- 17: release year, 2017
2222
- 04: release month
2323

24-
gopsutil aims to keep backwards-compatiblity until major version change.
24+
gopsutil aims to keep backwards compatibility until major version change.
2525

2626
Tagged at every end of month, but if there are only a few commits, it
2727
can be skipped.
@@ -130,7 +130,7 @@ will provide useful information.
130130
- CacheSize
131131
- Flags (ex: "fpu vme de pse tsc msr pae mce cx8 ...")
132132
- Microcode
133-
- load/LoadAvg() (linux, freebsd)
133+
- load/Avg() (linux, freebsd, solaris)
134134
- Load1
135135
- Load5
136136
- Load15
@@ -229,7 +229,7 @@ Some code is ported from Ohai. many thanks.
229229
|**HostInfo** | | | | | | |
230230
|hostname |x |x |x |x |x |x |
231231
|uptime |x |x |x |x | |x |
232-
|proces |x |x |x | | |x |
232+
|process |x |x |x | | |x |
233233
|os |x |x |x |x |x |x |
234234
|platform |x |x |x |x | |x |
235235
|platformfamily |x |x |x |x | |x |

disk/disk_freebsd.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
package disk
55

66
import (
7+
"bufio"
78
"bytes"
89
"context"
910
"encoding/binary"
11+
"fmt"
12+
"os/exec"
1013
"strconv"
14+
"strings"
1115

1216
"golang.org/x/sys/unix"
1317

@@ -163,7 +167,29 @@ func getFsType(stat unix.Statfs_t) string {
163167
}
164168

165169
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
166-
return "", common.ErrNotImplementedError
170+
geom, err := exec.LookPath("geom")
171+
if err != nil {
172+
return "", fmt.Errorf("find geom: %w", err)
173+
}
174+
geomOut, err := invoke.CommandWithContext(ctx, geom, "disk", "list", name)
175+
if err != nil {
176+
return "", fmt.Errorf("exec geom: %w", err)
177+
}
178+
s := bufio.NewScanner(bytes.NewReader(geomOut))
179+
serial := ""
180+
for s.Scan() {
181+
flds := strings.Fields(s.Text())
182+
if len(flds) == 2 && flds[0] == "ident:" {
183+
if flds[1] != "(null)" {
184+
serial = flds[1]
185+
}
186+
break
187+
}
188+
}
189+
if err = s.Err(); err != nil {
190+
return "", err
191+
}
192+
return serial, nil
167193
}
168194

169195
func LabelWithContext(ctx context.Context, name string) (string, error) {

docker/docker_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) {
5757
return ret, nil
5858
}
5959

60-
// GetDockerIDList returnes a list of DockerID.
60+
// GetDockerIDList returns a list of DockerID.
6161
// This requires certain permission.
6262
func GetDockerIDList() ([]string, error) {
6363
return GetDockerIDListWithContext(context.Background())
@@ -86,15 +86,15 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
8686
return ret, nil
8787
}
8888

89-
// CgroupCPU returnes specified cgroup id CPU status.
89+
// CgroupCPU returns specified cgroup id CPU status.
9090
// containerID is same as docker id if you use docker.
9191
// If you use container via systemd.slice, you could use
9292
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
9393
func CgroupCPU(containerID string, base string) (*CgroupCPUStat, error) {
9494
return CgroupCPUWithContext(context.Background(), containerID, base)
9595
}
9696

97-
// CgroupCPUUsage returnes specified cgroup id CPU usage.
97+
// CgroupCPUUsage returns specified cgroup id CPU usage.
9898
// containerID is same as docker id if you use docker.
9999
// If you use container via systemd.slice, you could use
100100
// containerID = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/

docker/docker_notlinux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) {
1919
return nil, ErrDockerNotAvailable
2020
}
2121

22-
// GetDockerIDList returnes a list of DockerID.
22+
// GetDockerIDList returns a list of DockerID.
2323
// This requires certain permission.
2424
func GetDockerIDList() ([]string, error) {
2525
return GetDockerIDListWithContext(context.Background())
@@ -29,7 +29,7 @@ func GetDockerIDListWithContext(ctx context.Context) ([]string, error) {
2929
return nil, ErrDockerNotAvailable
3030
}
3131

32-
// CgroupCPU returnes specified cgroup id CPU status.
32+
// CgroupCPU returns specified cgroup id CPU status.
3333
// containerid is same as docker id if you use docker.
3434
// If you use container via systemd.slice, you could use
3535
// containerid = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ module github.com/shirou/gopsutil/v3
33
go 1.15
44

55
require (
6-
github.com/google/go-cmp v0.5.6
6+
github.com/google/go-cmp v0.5.7
77
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0
88
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c
99
github.com/stretchr/testify v1.7.0
1010
github.com/tklauser/go-sysconf v0.3.9
1111
github.com/yusufpapurcu/wmi v1.2.2
12-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c
12+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320
1313
)

go.sum

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
44
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
5-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
6-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
7-
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
85
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
96
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
107
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
8+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
9+
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
1110
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
1211
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
1312
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -26,8 +25,8 @@ github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQ
2625
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2726
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2827
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c h1:taxlMj0D/1sOAuv/CbSD+MMDof2vbyPTqz5FNYKpXt8=
30-
golang.org/x/sys v0.0.0-20211013075003-97ac67df715c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
29+
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3130
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
3231
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3332
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

host/host_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil
283283
family = "debian"
284284
case "fedora":
285285
family = "fedora"
286-
case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm", "rocky":
286+
case "oracle", "centos", "redhat", "scientific", "enterpriseenterprise", "amazon", "xenserver", "cloudlinux", "ibm_powerkvm", "rocky", "almalinux":
287287
family = "rhel"
288288
case "suse", "opensuse", "opensuse-leap", "opensuse-tumbleweed", "opensuse-tumbleweed-kubic", "sles", "sled", "caasp":
289289
family = "suse"

internal/common/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (i Invoke) CommandWithContext(ctx context.Context, name string, arg ...stri
6464

6565
type FakeInvoke struct {
6666
Suffix string // Suffix species expected file name suffix such as "fail"
67-
Error error // If Error specfied, return the error.
67+
Error error // If Error specified, return the error.
6868
}
6969

7070
// Command in FakeInvoke returns from expected file if exists.

internal/common/common_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
100100
if err != nil {
101101
return 0, err
102102
}
103-
t := uint64(time.Now().Unix()) - uint64(b)
104-
return t, nil
103+
currentTime := float64(time.Now().UnixNano()) / float64(time.Second)
104+
t := currentTime - b
105+
return uint64(t), nil
105106
}
106107

107108
return 0, fmt.Errorf("could not find btime")

load/load_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func AvgWithContext(ctx context.Context) (*AvgStat, error) {
3939
return ret, nil
4040
}
4141

42-
// Misc returnes miscellaneous host-wide statistics.
42+
// Misc returns miscellaneous host-wide statistics.
4343
// darwin use ps command to get process running/blocked count.
4444
// Almost same as FreeBSD implementation, but state is different.
4545
// U means 'Uninterruptible Sleep'.

load/load_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func fileAvgWithContext() (*AvgStat, error) {
6868
return ret, nil
6969
}
7070

71-
// Misc returnes miscellaneous host-wide statistics.
71+
// Misc returns miscellaneous host-wide statistics.
7272
// Note: the name should be changed near future.
7373
func Misc() (*MiscStat, error) {
7474
return MiscWithContext(context.Background())

load/load_solaris.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,58 @@
44
package load
55

66
import (
7+
"bufio"
8+
"bytes"
79
"context"
810
"os/exec"
11+
"strconv"
912
"strings"
10-
11-
"github.com/shirou/gopsutil/v3/internal/common"
1213
)
1314

1415
func Avg() (*AvgStat, error) {
1516
return AvgWithContext(context.Background())
1617
}
1718

1819
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
19-
return nil, common.ErrNotImplementedError
20+
kstat, err := exec.LookPath("kstat")
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
out, err := invoke.CommandWithContext(ctx, kstat, "-p", "unix:0:system_misc:avenrun_*")
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
avg := &AvgStat{}
31+
scanner := bufio.NewScanner(bytes.NewReader(out))
32+
for scanner.Scan() {
33+
flds := strings.Fields(scanner.Text())
34+
if len(flds) < 2 {
35+
continue
36+
}
37+
var tgt *float64
38+
switch {
39+
case strings.HasSuffix(flds[0], ":avenrun_1min"):
40+
tgt = &avg.Load1
41+
case strings.HasSuffix(flds[0], ":avenrun_5min"):
42+
tgt = &avg.Load5
43+
case strings.HasSuffix(flds[0], ":avenrun_15min"):
44+
tgt = &avg.Load15
45+
default:
46+
continue
47+
}
48+
v, err := strconv.ParseInt(flds[1], 10, 64)
49+
if err != nil {
50+
return nil, err
51+
}
52+
*tgt = float64(v) / (1 << 8)
53+
}
54+
if err = scanner.Err(); err != nil {
55+
return nil, err
56+
}
57+
58+
return avg, nil
2059
}
2160

2261
func Misc() (*MiscStat, error) {

mem/mem_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func fillFromMeminfoWithContext() (*VirtualMemoryStat, *VirtualMemoryExStat, err
305305

306306
if !memavail {
307307
if activeFile && inactiveFile && sReclaimable {
308-
ret.Available = calcuateAvailVmem(ret, retEx)
308+
ret.Available = calculateAvailVmem(ret, retEx)
309309
} else {
310310
ret.Available = ret.Cached + ret.Free
311311
}
@@ -387,10 +387,10 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
387387
return ret, nil
388388
}
389389

390-
// calcuateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide
390+
// calculateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide
391391
// "MemAvailable:" column. It reimplements an algorithm from the link below
392392
// https://github.com/giampaolo/psutil/pull/890
393-
func calcuateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint64 {
393+
func calculateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint64 {
394394
var watermarkLow uint64
395395

396396
fn := common.HostProc("zoneinfo")

net/net_aix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
105105
return iocounters, nil
106106
}
107107

108-
// NetIOCountersByFile is an method which is added just a compatibility for linux.
108+
// IOCountersByFile exists just for compatibility with Linux.
109109
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
110110
return IOCountersByFileWithContext(context.Background(), pernic, filename)
111111
}

net/net_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
257257
return ret, nil
258258
}
259259

260-
// NetIOCountersByFile is an method which is added just a compatibility for linux.
260+
// IOCountersByFile exists just for compatibility with Linux.
261261
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
262262
return IOCountersByFileWithContext(context.Background(), pernic, filename)
263263
}

net/net_freebsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
9595
return ret, nil
9696
}
9797

98-
// NetIOCountersByFile is an method which is added just a compatibility for linux.
98+
// IOCountersByFile exists just for compatibility with Linux.
9999
func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
100100
return IOCountersByFileWithContext(context.Background(), pernic, filename)
101101
}

net/net_linux.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const ( // Conntrack Column numbers
4040
ctSEARCH_RESTART
4141
)
4242

43-
// NetIOCounters returnes network I/O statistics for every network
43+
// NetIOCounters returns network I/O statistics for every network
4444
// interface installed on the system. If pernic argument is false,
4545
// return only sum of all information (which name is 'all'). If true,
4646
// every network interface installed on the system is returned
@@ -188,7 +188,7 @@ func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoC
188188
line := lines[i]
189189
r := strings.IndexRune(line, ':')
190190
if r == -1 {
191-
return nil, errors.New(filename + " is not fomatted correctly, expected ':'.")
191+
return nil, errors.New(filename + " is not formatted correctly, expected ':'.")
192192
}
193193
proto := strings.ToLower(line[:r])
194194
if !protos[proto] {
@@ -204,7 +204,7 @@ func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoC
204204
i++
205205
statValues := strings.Split(lines[i][r+2:], " ")
206206
if len(statNames) != len(statValues) {
207-
return nil, errors.New(filename + " is not fomatted correctly, expected same number of columns.")
207+
return nil, errors.New(filename + " is not formatted correctly, expected same number of columns.")
208208
}
209209
stat := ProtoCountersStat{
210210
Protocol: proto,
@@ -543,7 +543,7 @@ func statsFromInodesWithContext(ctx context.Context, root string, pid int32, tma
543543
return ret, nil
544544
}
545545

546-
// getProcInodes returnes fd of the pid.
546+
// getProcInodes returns fd of the pid.
547547
func getProcInodes(root string, pid int32, max int) (map[string][]inodeMap, error) {
548548
ret := make(map[string][]inodeMap)
549549

@@ -553,7 +553,7 @@ func getProcInodes(root string, pid int32, max int) (map[string][]inodeMap, erro
553553
return ret, err
554554
}
555555
defer f.Close()
556-
dirEntries, err := f.ReadDir(max)
556+
dirEntries, err := readDir(f, max)
557557
if err != nil {
558558
return ret, err
559559
}

0 commit comments

Comments
 (0)