Skip to content

Commit

Permalink
Merge branch 'master' into feature/add_context_support
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou authored Jan 9, 2018
2 parents 4c73494 + 12199ad commit cd61c36
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 11 deletions.
6 changes: 6 additions & 0 deletions disk/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"os/exec"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
Expand Down
11 changes: 11 additions & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package docker

import (
"encoding/json"
"errors"

"github.com/shirou/gopsutil/internal/common"
Expand Down Expand Up @@ -50,10 +51,20 @@ 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"`
Image string `json:"image"`
Status string `json:"status"`
Running bool `json:"running"`
}

func (c CgroupDockerStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}
10 changes: 0 additions & 10 deletions docker/docker_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion host/host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Process struct {

lastCPUTimes *cpu.TimesStat
lastCPUTime time.Time

tgid int32
}

type OpenFilesStat struct {
Expand Down
3 changes: 3 additions & 0 deletions process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
3 changes: 3 additions & 0 deletions process/process_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
3 changes: 3 additions & 0 deletions process/process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
16 changes: 16 additions & 0 deletions process/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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") {
Expand Down
3 changes: 3 additions & 0 deletions process/process_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 4 additions & 0 deletions process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down

0 comments on commit cd61c36

Please sign in to comment.