Measuring thread CPU usage

827 views
Skip to first unread message

Olivier Guilyardi

unread,
Sep 12, 2011, 6:38:35 AM9/12/11
to andro...@googlegroups.com
Hi,

I'm trying to optimize a loop, and in order to determine the effects of my
changes, I am logging some timings based on clock_gettime(CLOCK_THREAD_CPUTIME_ID).

But this appears to be pretty unreliable, the timings vary a lot even though the
loop always does the exact same thing.

What I want is to measure the CPU time or count the CPU ticks consumed by a
given thread. What is the recommended way to do so?

Thanks in advance

--
Olivier

Glenn Kasten

unread,
Sep 13, 2011, 2:14:16 PM9/13/11
to android-ndk
It might be related to CPU frequency scaling. As I recall,
CLOCK_THREAD_CPUTIME_ID returns in real time units (i.e. a subdivision
of real seconds that elapsed while the thread was executing) so if the
processor is operating at a lower MHz then the thread will appear to
be consuming more CPU time. I don't know of a portable way to read
cycles. To confirm whether frequency scaling is the issue, try
pinning the CPU frequency. Unfortunately the details vary by Linux
kernel version and processor/board model. But a starting point is the
Documentation/cpu-freq/ subdirectory of the Linux kernel sources.
There are easier how-to's on the web but I've found they're often out-
of-date or apply to a different kernel version :-(

Olivier Guilyardi

unread,
Sep 14, 2011, 6:37:46 AM9/14/11
to andro...@googlegroups.com
Ah, ok thanks, that's good to know. I've had a look at Documentation/cpu-freq/,
but the files in sys/*/cpufreq/ require root access and I find it a bit
cumbersome to use anyway. Maybe that I will try and average the timings on a
wider window. Right now, the measures vary between 5ms and 7ms approximately, it
gives an idea even if it is a little fuzzy. It's sort of usable.

By the way, does CLOCK_THREAD_CPUTIME_ID behave differently according to the SDK
version?

Thanks

Olivier

Glenn Kasten

unread,
Sep 14, 2011, 2:23:36 PM9/14/11
to android-ndk
clock_gettime(CLOCK_THREAD_CPUTIME_ID) is a thin veneer over a Linux
system call, so if there were behavioral differences then they should
depend on the kernel version, not the Android SDK version. And I'm not
aware of any changes in that area of Linux kernel.

Olivier Guilyardi

unread,
Sep 14, 2011, 3:04:43 PM9/14/11
to andro...@googlegroups.com
Ok, thanks. I may try and pin the cpu frequency on one of my rooted devices, as
you suggested. I will report back here if that helps making the timings more
reliable.

Cheers,
Olivier

Reply all
Reply to author
Forward
0 new messages