Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit f940cb3

Browse files
fix perfdata interval bug
1 parent 8ea94b0 commit f940cb3

3 files changed

Lines changed: 79 additions & 54 deletions

File tree

cmd/perfmon.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package cmd
1919

2020
import (
2121
"fmt"
22+
"os"
23+
"os/signal"
24+
"time"
25+
2226
"github.com/SonicCloudOrg/sonic-android-supply/src/entity"
2327
"github.com/SonicCloudOrg/sonic-android-supply/src/perfmonUtil"
2428
"github.com/SonicCloudOrg/sonic-android-supply/src/util"
2529
"github.com/spf13/cobra"
26-
"os"
27-
"os/signal"
28-
"time"
2930
)
3031

3132
var perfmonCmd = &cobra.Command{
@@ -64,15 +65,14 @@ var perfmonCmd = &cobra.Command{
6465
!perfOptions.SystemNetWorking &&
6566
!perfOptions.SystemGPU &&
6667
!perfOptions.SystemMem {
67-
perfmonUtil.IntervalTime = float64(refreshTime)
6868
//sysAllParamsSet()
6969
perfOptions.ProcMem = true
7070
perfOptions.ProcCPU = true
7171
perfOptions.ProcThreads = true
7272
perfOptions.ProcFPS = true
7373
}
7474
timer := time.Tick(time.Duration(refreshTime * int(time.Millisecond)))
75-
75+
perfmonUtil.IntervalTime = float64(refreshTime*2) / 1000
7676
perfmonUtil.GetPIDAndPackageCurrentActivity(device, packageName, pidStr, timer, sig)
7777

7878
var perfDataChan = make(chan *entity.PerfmonData)
@@ -94,7 +94,6 @@ var perfmonCmd = &cobra.Command{
9494
}
9595
}
9696
}
97-
return nil
9897
},
9998
}
10099

@@ -127,7 +126,7 @@ func init() {
127126
//perfmonCmd.Flags().BoolVar(&, "proc-network", false, "get process network data")
128127
perfmonCmd.Flags().BoolVar(&perfOptions.ProcCPU, "proc-cpu", false, "get process cpu data")
129128
perfmonCmd.Flags().BoolVar(&perfOptions.ProcMem, "proc-mem", false, "get process mem data")
130-
perfmonCmd.Flags().IntVarP(&refreshTime, "refresh", "r", 1000, "data refresh time (millisecond)")
129+
perfmonCmd.Flags().IntVarP(&refreshTime, "interval", "i", 500, "interval refresh time (millisecond)")
131130
perfmonCmd.Flags().BoolVarP(&isFormat, "format", "f", false, "convert to JSON string and format")
132131
perfmonCmd.Flags().BoolVarP(&isJson, "json", "j", false, "convert to JSON string")
133132
}

src/perfmonUtil/process.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,9 @@ func GetPIDAndPackageCurrentActivity(client *adb.Device, packageName, pid string
366366
case <-sign:
367367
return
368368
case <-timer:
369-
packageCurrentActivity = getPackageCurrentActivity(client, packageName, pid)
369+
go func() {
370+
packageCurrentActivity = getPackageCurrentActivity(client, packageName, pid)
371+
}()
370372
}
371373
}
372374
}()
@@ -402,9 +404,12 @@ func GetProcThreads(client *adb.Device, perfOptions entity.PerfOption, perfmonDa
402404
case <-sign:
403405
return
404406
case <-timer:
405-
perfmonDataChan <- &entity.PerfmonData{
406-
Process: getThreads(client),
407-
}
407+
go func() {
408+
perfmonDataChan <- &entity.PerfmonData{
409+
Process: getThreads(client),
410+
}
411+
}()
412+
408413
}
409414
}
410415
}()
@@ -443,9 +448,12 @@ func GetProcFPS(client *adb.Device, perfOptions entity.PerfOption, perfmonDataCh
443448
case <-sign:
444449
return
445450
case <-timer:
446-
perfmonDataChan <- &entity.PerfmonData{
447-
Process: getFPS(client),
448-
}
451+
go func() {
452+
perfmonDataChan <- &entity.PerfmonData{
453+
Process: getFPS(client),
454+
}
455+
}()
456+
449457
}
450458
}
451459
}()
@@ -473,16 +481,20 @@ func getFPS(client *adb.Device) *entity.ProcessInfo {
473481

474482
func GetProcCpu(client *adb.Device, perfOptions entity.PerfOption, perfmonDataChan chan *entity.PerfmonData, timer <-chan time.Time, sign chan os.Signal) {
475483
if perfOptions.ProcCPU {
484+
getProcCpu(client)
485+
time.Sleep(time.Duration(IntervalTime * float64(time.Second)))
476486
go func() {
477487
for {
478488
select {
479489
case <-sign:
480490
return
481491
case <-timer:
492+
go func() {
493+
perfmonDataChan <- &entity.PerfmonData{
494+
Process: getProcCpu(client),
495+
}
496+
}()
482497

483-
perfmonDataChan <- &entity.PerfmonData{
484-
Process: getProcCpu(client),
485-
}
486498
}
487499
}
488500
}()
@@ -515,9 +527,12 @@ func GetProcMem(client *adb.Device, perfOptions entity.PerfOption, perfmonDataCh
515527
case <-sign:
516528
return
517529
case <-timer:
518-
perfmonDataChan <- &entity.PerfmonData{
519-
Process: getProcMem(client),
520-
}
530+
go func() {
531+
perfmonDataChan <- &entity.PerfmonData{
532+
Process: getProcMem(client),
533+
}
534+
}()
535+
521536
}
522537
}
523538
}()

src/perfmonUtil/system.go

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,39 @@ package perfmonUtil
1919

2020
import (
2121
"bufio"
22-
"github.com/SonicCloudOrg/sonic-android-supply/src/adb"
23-
"github.com/SonicCloudOrg/sonic-android-supply/src/entity"
2422
"io"
2523
"os"
2624
"strconv"
2725
"strings"
2826
"time"
27+
28+
"github.com/SonicCloudOrg/sonic-android-supply/src/adb"
29+
"github.com/SonicCloudOrg/sonic-android-supply/src/entity"
2930
)
3031

3132
func GetSystemCPU(client *adb.Device, perfOptions entity.PerfOption, perfmonDataChan chan *entity.PerfmonData, timer <-chan time.Time, sign chan os.Signal) {
3233
if perfOptions.SystemCPU {
3334
_ = getCPU(client, &entity.SystemInfo{})
35+
time.Sleep(time.Duration(IntervalTime * float64(time.Second)))
3436
go func() {
3537
for {
3638
select {
3739
case <-sign:
3840
return
3941
case <-timer:
40-
systemInfo := &entity.SystemInfo{}
41-
err := getCPU(client, systemInfo)
42-
if err != nil {
43-
systemInfo.Error = append(systemInfo.Error, err.Error())
44-
}
45-
if perfmonDataChan != nil {
46-
perfmonDataChan <- &entity.PerfmonData{
47-
System: systemInfo,
42+
go func() {
43+
systemInfo := &entity.SystemInfo{}
44+
err := getCPU(client, systemInfo)
45+
if err != nil {
46+
systemInfo.Error = append(systemInfo.Error, err.Error())
47+
}
48+
if perfmonDataChan != nil {
49+
perfmonDataChan <- &entity.PerfmonData{
50+
System: systemInfo,
51+
}
4852
}
49-
}
53+
}()
54+
5055
}
5156
}
5257
}()
@@ -60,17 +65,20 @@ func GetSystemMem(client *adb.Device, perfOptions entity.PerfOption, perfmonData
6065
for {
6166
select {
6267
case <-timer:
63-
systemInfo := &entity.SystemInfo{}
64-
systemInfo.MemInfo = &entity.SystemMemInfo{}
65-
err := getMemInfo(client, systemInfo)
66-
if err != nil {
67-
systemInfo.Error = append(systemInfo.Error, err.Error())
68-
}
69-
if perfmonDataChan != nil {
70-
perfmonDataChan <- &entity.PerfmonData{
71-
System: systemInfo,
68+
go func() {
69+
systemInfo := &entity.SystemInfo{}
70+
systemInfo.MemInfo = &entity.SystemMemInfo{}
71+
err := getMemInfo(client, systemInfo)
72+
if err != nil {
73+
systemInfo.Error = append(systemInfo.Error, err.Error())
74+
}
75+
if perfmonDataChan != nil {
76+
perfmonDataChan <- &entity.PerfmonData{
77+
System: systemInfo,
78+
}
7279
}
73-
}
80+
}()
81+
7482
}
7583
}
7684
}()
@@ -85,20 +93,23 @@ func GetSystemNetwork(client *adb.Device, perfOptions entity.PerfOption, perfmon
8593
for {
8694
select {
8795
case <-timer:
88-
systemInfo := &entity.SystemInfo{}
89-
err := getInterfaces(client, systemInfo)
90-
if err != nil {
91-
systemInfo.Error = append(systemInfo.Error, err.Error())
92-
}
93-
err = getInterfaceInfo(client, systemInfo)
94-
if err != nil {
95-
systemInfo.Error = append(systemInfo.Error, err.Error())
96-
}
97-
if perfmonDataChan != nil {
98-
perfmonDataChan <- &entity.PerfmonData{
99-
System: systemInfo,
96+
go func() {
97+
systemInfo := &entity.SystemInfo{}
98+
err := getInterfaces(client, systemInfo)
99+
if err != nil {
100+
systemInfo.Error = append(systemInfo.Error, err.Error())
100101
}
101-
}
102+
err = getInterfaceInfo(client, systemInfo)
103+
if err != nil {
104+
systemInfo.Error = append(systemInfo.Error, err.Error())
105+
}
106+
if perfmonDataChan != nil {
107+
perfmonDataChan <- &entity.PerfmonData{
108+
System: systemInfo,
109+
}
110+
}
111+
}()
112+
102113
}
103114
}
104115
}()

0 commit comments

Comments
 (0)