How to get higher precision of “CPU%” than that from TOP command?

前端 未结 4 637
走了就别回头了
走了就别回头了 2021-02-02 02:08

When I use TOP command, I could get the following info:

shell@android:/ $ top -n 1                                                     

User 31%, System 10%, IO         


        
相关标签:
4条回答
  • 2021-02-02 02:20

    You didn't mention it in your post, but in the comment you said that you really need CPU utilization per thread, not per process.

    If you can't find a tool that's accurate enough, you can look directly in /proc/[pid]/task/[ThreadName] as described in the man page for /proc. This gives total CPU time consumed in "clock ticks" since execution began. Getting better resolution than this is probably difficult or impossible.

    Edit

    From the OP's comment, a command that lists the relevant information is:

    adb shell cat /proc/${pid}/task/*/stat | awk -F\ '{print $1, $14}' 
    

    This just cats the correct /proc files to the debugging host, which runs a tiny awk program to print the columns for pid and user time. You could also easily use cut -d " " -f1,14 or something similar in perl to get the columns if awk isn't available.

    0 讨论(0)
  • 2021-02-02 02:25

    Try this:

    ps -eo pcpu,pid,user,args | sort -r -k1 | less 
    
    %CPU   PID USER     COMMAND
     9.0  2721 user   bash
     1.4   956 root     ...
     0.5  2212 user   ...
    

    EDIT:

    You can use adb shell and busybox (http://www.busybox.net/downloads/BusyBox.html)

    adb shell busybox top

    c:\ adb push busybox /system/bin
    c:\ adb shell
    # busybox top 
    
    CPU:  2.3% usr  3.1% sys  3.9% nic 90.5% idle  0.0% io  0.0% irq  0.0% sirq
    Load average: 1.06 1.66 10.63 1/589 8048
    ←[7m  PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND←[0m
    31619  2180 10112    S     217m 67.0   0  3.8 com.mgeek.android.DolphinBrowser.B
    
     2232  2180 1000     S     551m169.6   0  2.6 system_server
     8038  8037 0        R     2068  0.6   0  0.8 busybox top
     2178     1 0        S    11092  3.3   0  0.6 /system/bin/drexe
     6812  2180 10104    S     199m 61.2   0  0.5 android.tether
     2291  2180 1001     S     324m 99.8   0  0.3 com.android.phone
     2308  2180 10006    S     325m100.0   0  0.1 com.sec.android.app.dialertab
     2177     1 1001     S     9624  2.8   0  0.1 /system/bin/rild
        5     2 0        SW<      0  0.0   0  0.1 [events/0]
    30087  2180 10022    S     358m110.4   0  0.0 com.samsung.vvm
     2304  2180 10006    S     311m 96.0   0  0.0 com.sec.android.app.twlauncher
    16110  2180 10006    S     296m 91.3   0  0.0 android.process.acore
     2445  2180 10006    S     272m 83.8   0  0.0 com.sec.android.provider.logsprovi
    
     8064  2180 10002    S     238m 73.4   0  0.0 com.google.process.gapps
    31537  2180 10037    S     227m 69.9   0  0.0 com.google.android.gm
     2288  2180 10048    S     221m 68.1   0  0.0 com.swype.android.inputmethod
     2285  2180 10013    S     215m 66.3   0  0.0 com.tat.livewallpaper.aurora
    30664  2180 10011    S     213m 65.8   0  0.0 com.android.email
    31191  2180 10099    S     209m 64.4   0  0.0 com.sirma.mobile.bible.android
     2377  2180 10087    S     207m 63.9   0  0.0 android.tts
    

    (Taken from here)

    0 讨论(0)
  • 2021-02-02 02:26

    Use DDMS and method profiling to get a TraceView.

    Basically:

    1. Launch your app in debug mode
    2. In DDMS, in the Devices tab, click "Start method profiling"
    3. Do stuff on your device to recreate the conditions you want to monitor
    4. Click "Stop method profiling"
    5. You'll get a fairly detailed graph with each thread's execution that you can drill down into

    More details here: http://developer.android.com/tools/debugging/debugging-tracing.html

    Disclaimer: I've only done this with a simple test app so I don't know how much mileage you'll get out of it. It does seem to give a bit more precision than what has been described so far, and does not require root.

    enter image description here

    0 讨论(0)
  • 2021-02-02 02:33

    Got this information from another thread:

    3) Getting CPU info

    ~$ adb shell dumpsys cpuinfo

    Output:

    Load: 0.08 / 0.4 / 0.64 CPU usage from 42816ms to 34683ms ago: system_server: 1% = 1% user + 0% kernel / faults: 16 minor kdebuglog.sh: 0% = 0% user + 0% kernel / faults: 160 minor tiwlan_wq: 0% = 0% user + 0% kernel usb_mass_storag: 0% = 0% user + 0% kernel pvr_workqueue: 0% = 0% user + 0% kernel +sleep: 0% = 0% user + 0% kernel +sleep: 0% = 0% user + 0% kernel TOTAL: 6% = 1% user + 3% kernel + 0% irq

    EDIT:

    You can also try this command: echo $(adb shell ps | grep com.android.phone | awk '{ system("adb shell cat /proc/" $2 "/stat");}' | awk '{print $14+$15;}')

    Also:

    using top : This will show you the cpu stats top -b -n 1 |grep ^Cpu

    using ps: This will show you the % cpu usage for each process. ps -eo pcpu,pid,user,args | sort -r -k1 | less

    EDIT2:

    In realtion to your comments and the bounty description (How can I guess which thread eats the battery?) I found an interesting page:

    http://ziyang.eecs.umich.edu/projects/powertutor/

    As stated there:

    You can use PowerTutor to monitor the power consumption of any application.

    Try this for an instance and see if it meets your requirements.

    FINAL EDIT:

    Check out the Systrace documentation on the developer.android.com site:

    http://developer.android.com/tools/debugging/systrace.html http://developer.android.com/tools/help/systrace.html

    I'm sorry if you already tried that, but that's one concrete method to measure the performance.

    0 讨论(0)
提交回复
热议问题