Merge branch 'master' into feature/add_context_support

pull/480/head
shirou 7 years ago committed by GitHub
commit cd61c36c4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,7 @@ import (
"context"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"
@ -289,6 +290,11 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
ret := make(map[string]IOCountersStat, 0)
empty := IOCountersStat{}
// use only basename such as "/dev/sda1" to "sda1"
for i, name := range names {
names[i] = filepath.Base(name)
}
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 14 {

@ -25,6 +25,8 @@ func TestDisk_partitions(t *testing.T) {
if err != nil || len(ret) == 0 {
t.Errorf("error %v", err)
}
t.Log(ret)
empty := PartitionStat{}
if len(ret) == 0 {
t.Errorf("ret is empty")
@ -46,6 +48,7 @@ func TestDisk_io_counters(t *testing.T) {
}
empty := IOCountersStat{}
for part, io := range ret {
t.Log(part, io)
if io == empty {
t.Errorf("io_counter error %v, %v", part, io)
}

@ -1,6 +1,7 @@
package docker
import (
"encoding/json"
"errors"
"github.com/shirou/gopsutil/internal/common"
@ -50,6 +51,11 @@ type CgroupMemStat struct {
MemFailCnt uint64 `json:"memoryFailcnt"`
}
func (m CgroupMemStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}
type CgroupDockerStat struct {
ContainerID string `json:"containerID"`
Name string `json:"name"`
@ -57,3 +63,8 @@ type CgroupDockerStat struct {
Status string `json:"status"`
Running bool `json:"running"`
}
func (c CgroupDockerStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}

@ -57,11 +57,6 @@ func GetDockerStatWithContext(ctx context.Context) ([]CgroupDockerStat, error) {
return ret, nil
}
func (c CgroupDockerStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}
// GetDockerIDList returnes a list of DockerID.
// This requires certain permission.
func GetDockerIDList() ([]string, error) {
@ -245,11 +240,6 @@ func CgroupMemDockerWithContext(ctx context.Context, containerID string) (*Cgrou
return CgroupMem(containerID, common.HostSys("fs/cgroup/memory/docker"))
}
func (m CgroupMemStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}
// getCgroupFilePath constructs file path to get targetted stats file.
func getCgroupFilePath(containerID, base, target, file string) string {
if len(base) == 0 {

@ -104,7 +104,18 @@ func BootTimeWithContext(ctx context.Context) (uint64, error) {
if t != 0 {
return t, nil
}
filename := common.HostProc("stat")
system, role, err := Virtualization()
if err != nil {
return 0, err
}
statFile := "stat"
if system == "lxc" && role == "guest" {
// if lxc, /proc/uptime is used.
statFile = "uptime"
}
filename := common.HostProc(statFile)
lines, err := common.ReadLines(filename)
if err != nil {
return 0, err

@ -40,11 +40,13 @@ func TestBoot_time(t *testing.T) {
if v < 946652400 {
t.Errorf("Invalid Boottime, older than 2000-01-01")
}
t.Logf("first boot time: %d", v)
v2, err := BootTime()
if v != v2 {
t.Errorf("cached boot time is different")
}
t.Logf("second boot time: %d", v2)
}
func TestUsers(t *testing.T) {

@ -31,6 +31,8 @@ type Process struct {
lastCPUTimes *cpu.TimesStat
lastCPUTime time.Time
tgid int32
}
type OpenFilesStat struct {

@ -96,6 +96,9 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return common.IntToString(k.Proc.P_comm[:]), nil
}
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}

@ -54,6 +54,9 @@ func (p *Process) Name() (string, error) {
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}

@ -63,6 +63,9 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return common.IntToString(k.Comm[:]), nil
}
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}

@ -109,6 +109,16 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return p.name, nil
}
// Tgid returns tgid, a Linux-synonym for user-space Pid
func (p *Process) Tgid() (int32, error) {
if p.tgid == 0 {
if err := p.fillFromStatus(); err != nil {
return 0, err
}
}
return p.tgid, nil
}
// Exe returns executable path of the process.
func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
@ -985,6 +995,12 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
return err
}
p.parent = int32(pval)
case "Tgid":
pval, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return err
}
p.tgid = int32(pval)
case "Uid":
p.uids = make([]int32, 0, 4)
for _, i := range strings.Split(value, "\t") {

@ -66,6 +66,9 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return common.IntToString(k.Comm[:]), nil
}
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}

@ -170,6 +170,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return name, nil
}
func (p *Process) Tgid() (int32, error) {
return 0, common.ErrNotImplementedError
}
func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
}

Loading…
Cancel
Save