Android – How to get per-core CPU usage on Android/Galaxy SIII

How to get per-core CPU usage on Android/Galaxy SIII… here is a solution to the problem.

How to get per-core CPU usage on Android/Galaxy SIII

I have a Samsung Galaxy SIII with Android 4.1.2 “rooted”. I need to measure the CPU usage of some of my multithreaded applications written in C/C++, but I need this information on a per-core basis.

I know (according to Wikipedia ,…) Galaxy has an SoC with 4 ARM Cortex A9s, but when I do cat/proc/cpuinfo it doesn’t show anything about the number of available cores (which is true in any Linux), is this the right behavior?

I

read somewhere that I can use cat/proc/stat to see the average load per corebut, in my device, the content of such “file” only shows “core0”, again, is this correct or do I need to do something to enable all cores?

I also tried top and ps without success.

EDITED:
----------------- cat /proc/cpuinfo
Processor   : ARMv7 Processor rev 0 (v7l)
processor   : 0
BogoMIPS    : 1592.52

Features    : swp half thumb fastmult vfp edsp neon vfpv3 tls 
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x3
CPU part    : 0xc09
CPU revision    : 0

Chip revision   : 0011
Hardware    : SMDK4x12
Revision    : 000c
Serial      : 11e16f694df1267e

----------------- cat /proc/stat

cpu  89515 1686 23283 464122 3835 2 376 0 0 0
cpu0 74214 457 16736 221609 1111 1 347 0 0 0
intr 1860068 0 0 0 0 0 0 0 0 0 0 .... (removed: a serie of numbers)
ctxt 3138146
btime 1371578546
processes 15904
procs_running 1
procs_blocked 0
softirq 1154788 12 403499 864 4501 12 12 444746 67202 576 233364

Solution

You can do this by checking for hot-plug devices /sys/devices/system/cpu/present and /sys/devices/system/cpu/possible. See also the hotplug docs

I’m not sure how much this helps to get CPU usage. You can get a rough idea of how much user and system time a thread is using from adb shell ps -x -t, for example:

USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
system    19598 19574 955672 57324 ffffffff 4012d9b8 S system_server (u:2186, s:1521)
system    19602 19598 955672 57324 c007f840 4012db84 S GC (u:79, s:6)

This means that the main thread of system_server used 2186 user time ticks, while the GC thread used 79 ticks.

If you can instrument your application, you can use POSIX clock_gettime() calls with CLOCK_THREAD_CPUTIME_ID to measure the amount of CPU time used by a given thread. (The Dalvik VM uses it when generating results for the traceview.) )

However, this is all thread-based. Getting usage information for each core is much more difficult, especially on Android:

  • The hot-plug mechanism can add or remove kernels during your testing.
  • Cores can adjust the CPU frequency of each core independently, so it may take different amounts of work to perform X amounts of work on different cores.
  • Threads can be migrated between cores (no core affinity by default).

Related Problems and Solutions