@@ -19,11 +19,11 @@ package perfmonUtil
1919
2020import (
2121 "bufio"
22+ "context"
2223 "errors"
2324 "fmt"
2425 "io/ioutil"
2526 "math"
26- "os"
2727 "regexp"
2828 "strconv"
2929 "strings"
@@ -354,26 +354,46 @@ var preTick = -1.0
354354var IntervalTime = 1.0 // # seconds
355355var HZ = 100.0 //# ticks/second
356356var packageCurrentActivity = ""
357- var appPackageName = ""
358- var appPid = ""
357+ var PackageName = ""
358+ var Pid = ""
359359
360- func GetPIDAndPackageCurrentActivity (client * adb.Device , packageName , pid string , timer <- chan time.Time , sign chan os.Signal ) {
361- appPid = pid
362- appPackageName = packageName
360+ func GetPIDAndPackageCurrentActivity (client * adb.Device , sign context.Context ) {
361+ timer := time .Tick (time .Duration (int (IntervalTime * float64 (time .Second ))))
363362 go func () {
364363 for {
365364 select {
366- case <- sign :
365+ case <- sign . Done () :
367366 return
368367 case <- timer :
369368 go func () {
370- packageCurrentActivity = getPackageCurrentActivity (client , packageName , pid )
369+ packageCurrentActivity = getPackageCurrentActivity (client , PackageName , Pid )
371370 }()
372371 }
373372 }
374373 }()
375374}
376375
376+ func GetNameOnPid (client * adb.Device , pid string ) (string , error ) {
377+ lines , err := client .OpenShell ("ps -A" )
378+ if err != nil {
379+ panic (err )
380+ }
381+ data , err := ioutil .ReadAll (lines )
382+ if err != nil {
383+ panic (err )
384+ }
385+ reg := regexp .MustCompile (fmt .Sprintf (".*\\ s+(%s)(\\ s+\\ d+){5}\\ s\\ S+\\ s\\ S+" , pid ))
386+ regResult := reg .FindString (string (data ))
387+ reg = regexp .MustCompile ("\\ s+" )
388+ regResult = reg .ReplaceAllString (regResult , " " )
389+ dataSplit := strings .Split (regResult , " " )
390+ if len (dataSplit ) < 2 {
391+ return "" , errors .New ("unable to find the pid corresponding to app" )
392+ }
393+ name := dataSplit [len (dataSplit )- 1 ]
394+ return name , nil
395+ }
396+
377397func getPackageCurrentActivity (client * adb.Device , packageName string , pid string ) string {
378398 lines , err := client .OpenShell ("dumpsys activity " + packageName )
379399 if err != nil {
@@ -393,15 +413,19 @@ func getPackageCurrentActivity(client *adb.Device, packageName string, pid strin
393413 if len (dataSplit ) < 2 {
394414 return ""
395415 }
416+ if dataSplit [1 ] == "MANAGER" {
417+ return ""
418+ }
396419 return dataSplit [1 ]
397420}
398421
399- func GetProcThreads (client * adb.Device , perfOptions entity.PerfOption , perfmonDataChan chan * entity.PerfmonData , timer <- chan time. Time , sign chan os. Signal ) {
422+ func GetProcThreads (client * adb.Device , perfOptions entity.PerfOption , perfmonDataChan chan * entity.PerfmonData , sign context. Context ) {
400423 if perfOptions .ProcThreads {
424+ timer := time .Tick (time .Duration (int (IntervalTime * float64 (time .Second ))))
401425 go func () {
402426 for {
403427 select {
404- case <- sign :
428+ case <- sign . Done () :
405429 return
406430 case <- timer :
407431 go func () {
@@ -419,7 +443,7 @@ func GetProcThreads(client *adb.Device, perfOptions entity.PerfOption, perfmonDa
419443
420444func getThreads (client * adb.Device ) * entity.ProcessInfo {
421445 processInfo := & entity.ProcessInfo {}
422- status , err := getStatusOnPid (client , appPid )
446+ status , err := getStatusOnPid (client , Pid )
423447 if err != nil {
424448 processInfo .Error = append (processInfo .Error , err .Error ())
425449 }
@@ -434,18 +458,19 @@ func getThreads(client *adb.Device) *entity.ProcessInfo {
434458 TimeStamp : time .Now ().UnixMilli (),
435459 }
436460
437- processInfo .Pid = appPid
438- processInfo .Name = appPackageName
461+ processInfo .Pid = Pid
462+ processInfo .Name = PackageName
439463 processInfo .Activity = packageCurrentActivity
440464 return processInfo
441465}
442466
443- func GetProcFPS (client * adb.Device , perfOptions entity.PerfOption , perfmonDataChan chan * entity.PerfmonData , timer <- chan time. Time , sign chan os. Signal ) {
467+ func GetProcFPS (client * adb.Device , perfOptions entity.PerfOption , perfmonDataChan chan * entity.PerfmonData , sign context. Context ) {
444468 if perfOptions .ProcFPS {
469+ timer := time .Tick (time .Duration (int (IntervalTime * float64 (time .Second ))))
445470 go func () {
446471 for {
447472 select {
448- case <- sign :
473+ case <- sign . Done () :
449474 return
450475 case <- timer :
451476 go func () {
@@ -463,30 +488,31 @@ func GetProcFPS(client *adb.Device, perfOptions entity.PerfOption, perfmonDataCh
463488
464489func getFPS (client * adb.Device ) * entity.ProcessInfo {
465490 processInfo := & entity.ProcessInfo {}
466- fpsInfo , err := getProcessFPSBySurfaceFlinger (client , appPackageName )
491+ fpsInfo , err := getProcessFPSBySurfaceFlinger (client , PackageName )
467492
468493 if fpsInfo .FPS <= 0 || err != nil {
469- fpsInfo , err = getProcessFPSByGFXInfo (client , appPid )
494+ fpsInfo , err = getProcessFPSByGFXInfo (client , Pid )
470495 }
471496 if err != nil {
472497 processInfo .Error = append (processInfo .Error , err .Error ())
473498 }
474499 processInfo .FPSInfo = & fpsInfo
475500
476- processInfo .Pid = appPid
477- processInfo .Name = appPackageName
501+ processInfo .Pid = Pid
502+ processInfo .Name = PackageName
478503 processInfo .Activity = packageCurrentActivity
479504 return processInfo
480505}
481506
482- func GetProcCpu (client * adb.Device , perfOptions entity.PerfOption , perfmonDataChan chan * entity.PerfmonData , timer <- chan time. Time , sign chan os. Signal ) {
507+ func GetProcCpu (client * adb.Device , perfOptions entity.PerfOption , perfmonDataChan chan * entity.PerfmonData , sign context. Context ) {
483508 if perfOptions .ProcCPU {
484509 getProcCpu (client )
485510 time .Sleep (time .Duration (IntervalTime * float64 (time .Second )))
511+ timer := time .Tick (time .Duration (int (IntervalTime * float64 (time .Second ))))
486512 go func () {
487513 for {
488514 select {
489- case <- sign :
515+ case <- sign . Done () :
490516 return
491517 case <- timer :
492518 go func () {
@@ -504,7 +530,7 @@ func GetProcCpu(client *adb.Device, perfOptions entity.PerfOption, perfmonDataCh
504530
505531func getProcCpu (client * adb.Device ) * entity.ProcessInfo {
506532 processInfo := & entity.ProcessInfo {}
507- stat , err := getStatOnPid (client , appPid )
533+ stat , err := getStatOnPid (client , Pid )
508534 if err != nil {
509535 processInfo .Error = append (processInfo .Error , err .Error ())
510536 }
@@ -513,18 +539,19 @@ func getProcCpu(client *adb.Device) *entity.ProcessInfo {
513539 CpuUtilization : getProcCpuUsage (stat ),
514540 TimeStamp : time .Now ().UnixMilli (),
515541 }
516- processInfo .Pid = appPid
517- processInfo .Name = appPackageName
542+ processInfo .Pid = Pid
543+ processInfo .Name = PackageName
518544 processInfo .Activity = packageCurrentActivity
519545 return processInfo
520546}
521547
522- func GetProcMem (client * adb.Device , perfOptions entity.PerfOption , perfmonDataChan chan * entity.PerfmonData , timer <- chan time. Time , sign chan os. Signal ) {
548+ func GetProcMem (client * adb.Device , perfOptions entity.PerfOption , perfmonDataChan chan * entity.PerfmonData , sign context. Context ) {
523549 if perfOptions .ProcMem {
550+ timer := time .Tick (time .Duration (int (IntervalTime * float64 (time .Second ))))
524551 go func () {
525552 for {
526553 select {
527- case <- sign :
554+ case <- sign . Done () :
528555 return
529556 case <- timer :
530557 go func () {
@@ -543,8 +570,8 @@ func GetProcMem(client *adb.Device, perfOptions entity.PerfOption, perfmonDataCh
543570
544571func getProcMem (client * adb.Device ) * entity.ProcessInfo {
545572 processInfo := & entity.ProcessInfo {}
546- pss , _ := getMemTotalPSS (client , appPid )
547- stat , err := getStatOnPid (client , appPid )
573+ pss , _ := getMemTotalPSS (client , Pid )
574+ stat , err := getStatOnPid (client , Pid )
548575 if err != nil {
549576 processInfo .Error = append (processInfo .Error , err .Error ())
550577 }
@@ -554,8 +581,8 @@ func getProcMem(client *adb.Device) *entity.ProcessInfo {
554581 TotalPSS : pss ,
555582 TimeStamp : time .Now ().UnixMilli (),
556583 }
557- processInfo .Pid = appPid
558- processInfo .Name = appPackageName
584+ processInfo .Pid = Pid
585+ processInfo .Name = PackageName
559586 processInfo .Activity = packageCurrentActivity
560587 return processInfo
561588}
0 commit comments