When I use TOP command, I could get the following info:
shell#android:/ $ top -n 1
User 31%, System 10%, IOW 0%, IRQ 0%
User 346 + Nice 10 + Sys 120 + Idle 637 + IOW 6 + IRQ 0 + SIRQ 2 = 1121
PID PR CPU% S #THR VSS RSS PCY UID Name
481 1 26% S 89 762832K 81688K fg system system_server
1699 0 5% S 27 676472K 39092K fg u0_a72 wm.cs.systemmonitor
11243 0 3% S 28 673140K 29796K bg u0_a111 com.weather.Weather
13327 2 1% S 23 680472K 35844K bg u0_a83 com.rhmsoft.fm
659 0 1% S 17 663044K 33136K bg u0_a13 android.process.media
20260 1 0% R 1 1208K 508K shell top
We can see the CPU% is round to integer, is there any way I could get a process's CPU% with higher precision?
-- Clarifications on the bounty -- Alex
The question refers to Android system, and preferably to a non-rooted device. While Android provides advanced profiling techniques for Java applications, tools for native code (C++) are limited. top command on Android allows to show the statistics for all threads running in the system, both Java threads and C++ threads. I am looking for an answer that will help with the following quest:
My app uses 2% CPU when it is inactive in background, while it should be below 0.1%. If I run top -t, I get 0% for all 15 threads that belong to my process (some threads are Java threads, e.g. the Main, UI thread; others are pthreads that never attach to JVM). How can I guess which thread eats the battery?
I would be glad to get even more details about this unexpected activity, and Android provides great helpers like TraceView for Java threads. Any insight regarding tools for native code will be highly appreciated.
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.
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)
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.
Use DDMS and method profiling to get a TraceView.
Basically:
Launch your app in debug mode
In DDMS, in the Devices tab, click "Start method profiling"
Do stuff on your device to recreate the conditions you want to monitor
Click "Stop method profiling"
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.
Related
Is there any substitute for pidstat command between the supported commands in busybox? I need to run pidstat on my Android device. I have busybox but pidstat is not supported.
Busybox has no such command. It does however provide the top command which can be used in a similar fashion.
top
top [-b] [-nCOUNT] [-dSECONDS] [-m]
Provide a view of process activity in real time. Read the status of all processes from /proc each SECONDS and display a screenful of them.
Where:
$ top -bn10 -p1
-b - shows top in batch mode
n10 - shows 10 iterations and then stops
-p1 - shows PID 1
Example
$ top -bn10 -p1
top - 23:19:27 up 5:30, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1015500 total, 414600 free, 88176 used, 512724 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 737992 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 125776 4240 2616 S 0.0 0.4 0:04.59 systemd
You can then use grep to filter this output like so:
$ top -bn10 -p1 | grep -A1 "PID"
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 125776 4240 2616 S 0.0 0.4 0:04.61 systemd
--
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 125776 4240 2616 S 0.0 0.4 0:04.61 systemd
--
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 125776 4240 2616 S 0.0 0.4 0:04.61 systemd
--
You can of course manipulate top so that it shows a similar set of columns to what pidstat shows.
I have the output result from "adb shell top" in a .txt file. I want to get only the CPU usage for my testing app and display it as a table:
Time cpu %
1 18
2 20
3 15
4 19
I have tried cat top.txt | grep Cpu | nl > cpu.txt (followed the instructions from here link), but the file is empty.
Do you have any idea how can I start? I have never used regex and not sure will it help. Please, looking forward hearing any tips :)
User 23%, System 5%, IOW 0%, IRQ 0%
User 434 + Nice 0 + Sys 106 + Idle 1273 + IOW 10 + IRQ 0 + SIRQ 3 = 1826
PID PR CPU% S #THR VSS RSS PCY UID Name
1868 1 20% S 50 1394620K 175176K fg u0_a174 com.test.app
1841 6 2% S 11 1897824K 48632K fg shell uiautomator
4794 1 1% S 148 2334708K 130924K fg system system_server
447 1 1% S 14 422152K 17476K fg system /system/bin/surfaceflinger
29195 1 1% S 21 369676K 20040K fg media
Given your top.txt sample, I would do the following, step by step:
First, select only the lines for your app com.test.app with grep:
grep com.test.app top.txt
Take the third field (delimited by spaces, so this is the CPU usage) by piping into awk:
grep com.test.app top.txt | awk '{ print $3; }'
Add line numbering by piping into nl (the -nln is for left justification):
grep com.test.app top.txt | awk '{ print $3; }' | nl -nln
Finally, just prepend your header with echo (-e and double quotes are needed for newline \n):
echo -e "Time cpu %\n" && grep com.test.app top.txt | awk '{ print $3; }' | nl -nln
This will give you for the sample top.txt:
Time cpu %
1 20%
I use an EC2 instance (Ubuntu), and I want launch gradlew in my Android project, through the Terminal.
But I have this error:
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':app'.
> Could not download artifact 'maven-ant-tasks.jar (org.apache.maven:maven-ant-tasks:2.1.3)'
> Failed to download resource 'https://jcenter.bintray.com/org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.jar'.
> No space left on device
I checked the space, and Inodes and all is ok:
root#ip-xx-xx-xx-xx:/home/mytest/sandbox/MyApplication# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 99G 12G 83G 13% /
none 4.0K 0 4.0K 0% /sys/fs/cgroup
udev 492M 12K 492M 1% /dev
tmpfs 100M 324K 99M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 497M 0 497M 0% /run/shm
none 100M 0 100M 0% /run/user
overflow 1.0M 0 1.0M 0% /tmp
root#ip-xx-xx-xx-xx:/home/mytest/sandbox/MyApplication# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/xvda1 6553600 241915 6311685 4% /
none 127032 2 127030 1% /sys/fs/cgroup
udev 125790 403 125387 1% /dev
tmpfs 127032 307 126725 1% /run
none 127032 1 127031 1% /run/lock
none 127032 1 127031 1% /run/shm
none 127032 2 127030 1% /run/user
overflow 127032 4 127028 1% /tmp
If I download manually this jar (maven-ant-tasks-2.1.3.jar) with wget command, the download is successful.
I tried with sudo privileges, and the same error appears (No space left on device).
Previously, I followed these explanations, to be sure about the space : EC2 instance on Amazon and I am greeted with "No space left on the disk". Again, the same error appears (No space left on device).
Thank you for your help guys!
I need to monitor the behaviour of my app and collect statistics about how threads are created/destroyed. I know DDMS has a thread view which shows this information live, but could I get the same information through the command line? I want to create my own tool which will log this information and the process it.
Clarification:
What I am looking for is a command that I can pass via ADB, which will list the threads running under a process. This way, I can run the command at different points of time to get the status of all threads (number of threads & their names) over a period of time.
There are a two ways you can do this.
Tried this on a Motorola Moto G on Ubuntu 12.10
You can list all the thread running on the device by using top (under ADB Shell).
$ top -t
PID TID PR CPU% S VSS RSS PCY UID Thread Proc
271 895 1 0% S 11120K 1892K root netd /system/bin/netd
272 272 0 0% S 1040K 200K root debuggerd /system/bin/debuggerd
274 274 2 0% S 63256K 7008K fg system surfaceflinger /system/bin/surfaceflinger
274 451 0 0% S 63256K 7008K fg system Binder_1 /system/bin/surfaceflinger
So to get details for any particular process you can use grep
$ top -t | grep com.whatsapp
PID TID PR CPU% S VSS RSS PCY UID Thread Proc
15210 15210 0 0% S 550076K 51180K bg u0_a96 com.whatsapp com.whatsapp
15210 15214 0 0% S 550076K 51180K bg u0_a96 GC com.whatsapp
15210 15215 0 0% S 550076K 51180K bg u0_a96 Signal Catcher com.whatsapp
15210 15216 0 0% S 550076K 51180K bg u0_a96 Compiler com.whatsapp
To run this on your host Machine just use
$ adb shell top -t | grep com.whatsapp
If grep is not supported, use Busybox.
If you are looking for Static view. You can also use ps.
$ ps -p 15210 -t
USER PID PPID VSIZE RSS PRIO NICE RTPRI SCHED WCHAN PC NAME
u0_a96 15210 275 549036 52136 20 0 0 0 ffffffff 00000000 S com.whatsapp
u0_a96 15214 15210 549036 52136 20 0 0 0 ffffffff 00000000 S GC
u0_a96 15215 15210 549036 52136 20 0 0 0 ffffffff 00000000 S Signal Catcher
u0_a96 15216 15210 549036 52136 20 0 0 0 ffffffff 00000000 S Compiler
Where 15210 is the PID for your process com.whatsapp
Hope this solves your issues, let me know if it works.
In my limited knowledge, and with the chances that I may be totally wrong, have a look at:
public static Map<Thread,StackTraceElement[]> getAllStackTraces()
Docs: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getAllStackTraces()
Returns a map of stack traces for all live threads. The map keys are
threads and each map value is an array of StackTraceElement that
represents the stack dump of the corresponding Thread. The returned
stack traces are in the format specified for the getStackTrace method.
The threads may be executing while this method is called. The stack
trace of each thread only represents a snapshot and each stack trace
may be obtained at different time. A zero-length array will be
returned in the map value if the virtual machine has no stack trace
information about a thread.
I hope that helped.
I have a multi-threaded application(C++,C and pthread library) and I will like to know how much resources(CPU and memory) each thread uses. Is there a way to find out these details on Android?
I have tried
$ adb shell ps -p -t
But, this gives information in the below format,
USER PID PPID VSIZE RSS PRIO NICE RTPRI SCHED WCHAN PC NAME
root 1 0 476 312 20 0 0 0 c037c0e0 000087bc S /init
root 2 0 0 0 20 0 0 0 c031f114 00000000 S kthreadd
root 3 2 0 0 20 0 0 0 c030e5d0 00000000 S ksoftirqd/0
root 4 2 0 0 -100 0 99 1 c033fc50 00000000 S watchdog/0
root 5 2 0 0 20 0 0 0 c031b4b4 00000000 S events/0
root 6 2 0 0 20 0 0 0 c031b4b4 00000000 S khelper
Any way to know which threads are running along with their parent's id will be really helpful.
using
$ adb logcat -v threadtime
prints info about the running process (PID) and the thread (TID).
But, its not enough for my purpose. I want to know how much resources is a particular thread consuming. Any pointers.
ps command in android seems to be very limited compared to linux.
You could try:
ps -t -c APP_NAME
This will show you threads(-t) and the CPU assigned to the thread (-c)
Example:
ps -t -c -p com.whatsapp
-p is thread priority(linux nice value)