Update top output parse

This commit is contained in:
Ivan Danyliuk 2017-12-16 19:19:27 +01:00
parent eff38714c9
commit 28ed76f428
No known key found for this signature in database
GPG Key ID: 97ED33CE024E1DBF
2 changed files with 21 additions and 29 deletions

View File

@ -13,22 +13,6 @@ var (
ErrParse = errors.New("parse failed")
)
// adbCPU returns CPU value for the given PID via `adb shell` command.
func adbCPU(pid int64) (float64, error) {
cmd := fmt.Sprintf("top -p %d -n 1 -q", pid)
out, err := adbShell(cmd)
if err != nil {
return 0, err
}
top, err := NewTopOutput(out)
if err != nil {
return 0, err
}
return top.CPU, nil
}
// adbShell calls custom command via 'adb shell` and returns it's stdout output.
func adbShell(command string) (string, error) {
cmdWords := strings.Fields(command)

34
top.go
View File

@ -6,6 +6,22 @@ import (
"strings"
)
// adbCPU returns CPU value for the given PID via `adb shell` command.
func adbCPU(pid int64) (float64, error) {
cmd := fmt.Sprintf("top -p %d -n 1 -q -b -o %%CPU", pid)
out, err := adbShell(cmd)
if err != nil {
return 0, err
}
top, err := NewTopOutput(out)
if err != nil {
return 0, err
}
return top.CPU, nil
}
// TopOutput represents data from 'top' command output
// for single process.
type TopOutput struct {
@ -14,19 +30,11 @@ type TopOutput struct {
// NewTopOutput creates new TopOutput from raw stdout data.
func NewTopOutput(data string) (*TopOutput, error) {
fields := parseTopOutput(data)
if len(fields) < 11 {
fmt.Println("[ERROR]: wrong top output", fields)
return nil, ErrParse
}
line := cleanTopOutput(data)
// eithh field is supposed to be CPU value
cpu, err := strconv.ParseFloat(fields[8], 64)
cpu, err := strconv.ParseFloat(line, 64)
if err != nil {
// top output might be different from system to system, so log
// this verbosely
fmt.Println("[ERROR] Parse CPU value:", err)
fmt.Println("Output:", fields)
return nil, ErrParse
}
@ -35,10 +43,10 @@ func NewTopOutput(data string) (*TopOutput, error) {
}, nil
}
func parseTopOutput(data string) []string {
func cleanTopOutput(data string) string {
lines := strings.Split(data, "\r")
line := strings.Replace(lines[0], "\r", "", -1)
line = strings.Replace(lines[0], "\b", "", -1)
line = strings.TrimSpace(line)
fields := strings.Fields(line)
return fields
return line
}