Resolve issue #492 to reduce CPU overhead associated with fillFromStat

This commit is contained in:
Ihde 2018-02-20 13:12:09 -05:00 committed by Michael Ihde
parent 543a05cce0
commit 44ef466a5a
1 changed files with 18 additions and 18 deletions

View File

@ -235,10 +235,15 @@ func (p *Process) Terminal() (string, error) {
} }
func (p *Process) TerminalWithContext(ctx context.Context) (string, error) { func (p *Process) TerminalWithContext(ctx context.Context) (string, error) {
terminal, _, _, _, _, _, err := p.fillFromStat() t, _, _, _, _, _, err := p.fillFromStat()
if err != nil { if err != nil {
return "", err return "", err
} }
termmap, err := getTerminalMap()
if err != nil {
return "", err
}
terminal := termmap[t]
return terminal, nil return terminal, nil
} }
@ -1115,11 +1120,11 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
return nil return nil
} }
func (p *Process) fillFromTIDStat(tid int32) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromTIDStat(tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromTIDStatWithContext(context.Background(), tid) return p.fillFromTIDStatWithContext(context.Background(), tid)
} }
func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, error) {
pid := p.Pid pid := p.Pid
var statPath string var statPath string
@ -1131,7 +1136,7 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (st
contents, err := ioutil.ReadFile(statPath) contents, err := ioutil.ReadFile(statPath)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
fields := strings.Fields(string(contents)) fields := strings.Fields(string(contents))
@ -1140,28 +1145,23 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (st
i++ i++
} }
termmap, err := getTerminalMap() terminal, err := strconv.ParseUint(fields[i+5], 10, 64)
terminal := "" if err != nil {
if err == nil { return 0, 0, nil, 0, 0, 0, err
t, err := strconv.ParseUint(fields[i+5], 10, 64)
if err != nil {
return "", 0, nil, 0, 0, 0, err
}
terminal = termmap[t]
} }
ppid, err := strconv.ParseInt(fields[i+2], 10, 32) ppid, err := strconv.ParseInt(fields[i+2], 10, 32)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
utime, err := strconv.ParseFloat(fields[i+12], 64) utime, err := strconv.ParseFloat(fields[i+12], 64)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
stime, err := strconv.ParseFloat(fields[i+13], 64) stime, err := strconv.ParseFloat(fields[i+13], 64)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
cpuTimes := &cpu.TimesStat{ cpuTimes := &cpu.TimesStat{
@ -1173,7 +1173,7 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (st
bootTime, _ := host.BootTime() bootTime, _ := host.BootTime()
t, err := strconv.ParseUint(fields[i+20], 10, 64) t, err := strconv.ParseUint(fields[i+20], 10, 64)
if err != nil { if err != nil {
return "", 0, nil, 0, 0, 0, err return 0, 0, nil, 0, 0, 0, err
} }
ctime := (t / uint64(ClockTicks)) + uint64(bootTime) ctime := (t / uint64(ClockTicks)) + uint64(bootTime)
createTime := int64(ctime * 1000) createTime := int64(ctime * 1000)
@ -1193,11 +1193,11 @@ func (p *Process) fillFromTIDStatWithContext(ctx context.Context, tid int32) (st
return terminal, int32(ppid), cpuTimes, createTime, uint32(rtpriority), nice, nil return terminal, int32(ppid), cpuTimes, createTime, uint32(rtpriority), nice, nil
} }
func (p *Process) fillFromStat() (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromStat() (uint64, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromStatWithContext(context.Background()) return p.fillFromStatWithContext(context.Background())
} }
func (p *Process) fillFromStatWithContext(ctx context.Context) (string, int32, *cpu.TimesStat, int64, uint32, int32, error) { func (p *Process) fillFromStatWithContext(ctx context.Context) (uint64, int32, *cpu.TimesStat, int64, uint32, int32, error) {
return p.fillFromTIDStat(-1) return p.fillFromTIDStat(-1)
} }