From a3f57b1314c3dd725f5b4bae74466b696b71a6fe Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Mon, 11 Jul 2016 14:05:30 -0400 Subject: [PATCH] Resolve cyclic import and create a common helper func, NumProcs() --- host/host_linux.go | 6 ++---- internal/common/common_linux.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/host/host_linux.go b/host/host_linux.go index e485ef4..6d5ece4 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -17,7 +17,6 @@ import ( "github.com/shirou/gopsutil/internal/common" common "github.com/shirou/gopsutil/internal/common" - "github.com/shirou/gopsutil/process" ) type LSB struct { @@ -59,9 +58,8 @@ func Info() (*InfoStat, error) { ret.Uptime = uptime(boot) } - procs, err := process.Pids() - if err == nil { - ret.Procs = uint64(len(procs)) + if numProcs, err := common.NumProcs(); err == nil { + ret.Procs = numProcs } return ret, nil diff --git a/internal/common/common_linux.go b/internal/common/common_linux.go index 0a122e9..b3df722 100644 --- a/internal/common/common_linux.go +++ b/internal/common/common_linux.go @@ -1,3 +1,19 @@ // +build linux package common + +import "os" + +func NumProcs() (uint64, error) { + f, err := os.Open(HostProc()) + if err != nil { + return 0, err + } + + list, err := f.Readdir(-1) + defer f.Close() + if err != nil { + return 0, err + } + return uint64(len(list)), error +}