1

I'm trying to get CPU usage in some point of running app. I need something like i used for time measurement.

Before I called the function (witch I want to measure) I used System.currentTimeMillis() to get the start time and the difference with the same value after function ended.

Running time of this function could be from 1 to 1000ms.

Mine solutions:

I can use adb top command triggered every millisecond (but i don't think it is working properly) adb shell top -m 15 -d 0.001 > C:\something\something\results.txt

Or, I was thinking about to call this command from running app in another thread (if the function will end so the thread would). If you think this could be the right way, may I still send results of command to some file in phone?
Or should I use adb shell top -m 15 -d 0.001 -n 1 and call it in while cycle until thread will end?

Amphoru
  • 87
  • 1
  • 11
  • 1
    That downvotes are meaningless. I'm pretty sure my question isn't duplicate and the answer isn't easy to find. Another time, please read questions carefully and if you are still unsatisfied, let comment below why so. Thank you. – Amphoru May 14 '15 at 17:28

2 Answers2

4

If by function you mean literally java function then why dont you measure CPU time of its execution (difference of end and start measurements)? You can use System.currentTimeMillis() but this will measure also time of other threads that got CPU quantum. So I believe you are after Debug.threadCpuTimeNanos() which will measure only time CPU was executing your function code, you can investigate how it works by looking into sources:

http://androidxref.com/5.1.0_r1/xref/art/runtime/utils.cc#177

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I'm not quite sure if you didn't understand me, or vice versa. But I need to get CPU usage of java function. Not the time of its execution. I have time measurement. Now i need CPU usage and then, I'll find the way how to measure battery consumption of this one java function. Then test this function on 14 test files. It enrypts from 1000 characters to 11 milion characters. So the result will be something like benchmark for java encrypt function. – Amphoru May 14 '15 at 15:38
  • 1
    @Amphoru you are right, I didnt quite understood, so I can suggest to use both `System.currentTimeMillis()` which will measure total function time (aka wall time), and `Debug.threadCpuTimeNanos() ` to only measure time your function was executed on CPU. So suppose CTS is timing for currentTimeMillis and TCTN is timing for threadCpuTimeNanos (you need to convert it to ms). TCTN should always be less than CTS. To calculate CPU usage you would compute TCTN/CTS * 100%. Of course that is the aproach if you want to do it yourselft and not with the use of some external tool. – marcinj May 14 '15 at 15:45
  • @marcinj - Is it the right way of doing TCTN/CTS *100% ? I tried this and my results sometimes exceeds 100% –  Oct 23 '15 at 11:45
1

I'm not sure if this is what you're looking for, I've been looking into Android debugging recently, but I haven't tried this myself.

Here's the link: Traceview War Story, from the Android Developer's blog.

It describes using the Traceview tool to analyze functions and how much time the system is devoting to each process within that function.

Joseph Roque
  • 5,066
  • 3
  • 16
  • 22
  • Thank you a lot, but I'm goint to use marcinj's solution cause I want it to be inside app solution. – Amphoru May 15 '15 at 09:19