Skip to content

Commit

Permalink
修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aoliaoaoaojiao committed May 16, 2024
1 parent ed0ae48 commit dfc1b80
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions android/perf/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package perf

import (
"bufio"
"bytes"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -357,6 +358,7 @@ func getProcCpuUsage(stat *entity.ProcessStat, pcf *procCpuConfig, nowTotalCPUTi
var nowProcCpuTime = float64(stat.Utime) + float64(stat.Stime) + float64(stat.Cutime) + float64(stat.Cstime)
if pcf.preProcCpuTime == -1.0 {
pcf.preProcCpuTime = nowProcCpuTime
pcf.preTotalCpuTime = nowTotalCPUTime
return 0.0
}

Expand All @@ -368,18 +370,30 @@ func getProcCpuUsage(stat *entity.ProcessStat, pcf *procCpuConfig, nowTotalCPUTi
}

func GetTotalCpuTime(device *gadb.Device) (float64, error) {
totalCpuInfo, err := device.RunShellCommand(fmt.Sprintf("cat /proc/stat"))
totalCpuInfo, err := device.RunShellCommandWithBytes(fmt.Sprintf("cat /proc/stat"))
if err != nil {
return 0, err
}
re := regexp.MustCompile(`cpu\s(.*?)\\r`)
totalCpuInfoMatch := re.FindStringSubmatch(totalCpuInfo)
totalCpuTimeList := strings.Fields(totalCpuInfoMatch[1])[:7]
totalCpuTime := 0
for _, val := range totalCpuTimeList {
cpuTime, _ := strconv.Atoi(val)
totalCpuTime += cpuTime

var (
nowCPU entity.SystemCpuRaw
)

//if preCPUMap == nil {
// preCPUMap = make(map[string]entity.SystemCpuRaw)
//}

scanner := bufio.NewScanner(bytes.NewReader(totalCpuInfo))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) > 0 && strings.HasPrefix(fields[0], "cpu") { // changing here if you want to get every cpu-core's stats
parseCPUFields(fields, &nowCPU)
}
}

totalCpuTime := nowCPU.User + nowCPU.Nice + nowCPU.System + nowCPU.Idle + nowCPU.Iowait + nowCPU.Irq + nowCPU.SoftIrq

return float64(totalCpuTime), nil
}

Expand Down
2 changes: 1 addition & 1 deletion android/perf/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestGetProcCpu(t *testing.T) {
if err != nil {
panic(err)
}
pid, err := android_util.GetPidOnPackageName(device, "com.taou.maimai")
pid, err := android_util.GetPidOnPackageName(device, "com.baidu.tieba_mini")
if err != nil {
panic(err)
}
Expand Down

0 comments on commit dfc1b80

Please sign in to comment.