2

I been trying to read cpu usage in my application using the @szcoder answer to How to get Memory usage and CPU usage in android? here at SOF.

His solution used to work on my Samsung Galaxy S2 (dual-core cpu), but its not working on my HTC One M7 which is quad core.

Most of the readings (Value from @szcoder's method*100 i.e percentage cpu usage) are either negative, some less than 10% and few are more than 100%.

In a comment to this answer Dave suggests that we should use multiple readings for accurate value.

Can some one please suggest a possible solution to this problem?

EDIT :

I also tried to use Fabian Knapp's solution which uses top -n 1 command to determine the CPU usage but in my testing parsing /proc/stat is much faster than top -n 1 (0.3xx seconds comapred to 3.xx seconds) so in case some one suggests it, I won't be using the top command because of time consumption.

Community
  • 1
  • 1
Khurram Majeed
  • 2,291
  • 8
  • 37
  • 59

1 Answers1

1

If the top command gives you the correct value, then I suggest you look at Android's source code for top and pull out the bit you need for CPU, which takes less time than a second. I can say this works because I did it once.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
  • Thanks for the information but can you share java code? because `top.c` is written in c and I am not very good with `c language` – Khurram Majeed Jul 18 '13 at 18:19
  • Sorry, I wrote the code working for the Man, so I can't share. But look at the read_stat() function in that link, which does what you want; it's short and not incomprehensible. – Yusuf X Jul 18 '13 at 23:04