How to get a high resolution timer in native Android code?

I am looking for a way to get a high resolution timer (sync source) on Android. This should be in native code and may not be quite portable. I would like to get permission for a microsecond, or better. For my particular application, resolution is more important than accuracy, and a monotonous timer is best. I tested on a specific ARMv7 chipset with Android 4.0.3.

What I have tried so far:

  • clock_gettime(CLOCK_MONOTONIC) : increases in units of 1 ms on the target equipment ( EDIT originally I thought 10 ms due to an error in my code, 1 ms is correct, but still not very good)
  • gettimeofday() : similar to clock_gettime

What I have not tried:

See also (related questions, but no answer):

+7
source share
2 answers

You can use the processor's cycle counter to measure the number of cycles between two moments, and then convert them to seconds. To ensure that the processor operates at its rated frequency, switch its zoom control to β€œperformance” mode (there are many applications on the Play Store for this, but root access is required for this). You also need to cool the device well to make sure that the CPU is not throttling (this is especially important for phones). Finally, you must bind your code to one kernel (preferably kernel 0, because other kernels can be disabled by the system on the system).

There are two ways to access the cycle counter in ARM: using the perf_event subsystem on Linux or directly reading the cycle counter from the CP15 coprocessor. The perf_event method will only work if the Linux kernel is compiled with support for this subsystem (the file / proc / sys / kernel / perf_event_paranoid exists) and reading event counters is unlimited (the file / proc / sys / kernel / perf_event_paranoid contains 0 or - 1, with root access, you can overwrite the value in this file). The CP15 method will work only if the user access to performance counters mode is enabled (by default it is always disabled and you will either have to fix the kernel or use the kernel driver to enable it). Yeppp! the library (I am the author) is able to use any method whenever it is available, and includes a kernel-mode driver that allows the user access to performance counters in user mode. You may find this example helpful.

+6
source
  • Android does not support ARMv7-M, so Systick goes beyond the equation.
  • I am not very experienced with timers in android / linux. If you intend to run this software on your own device, you can enable PMU in linux and use the processor cycle count register for accuracy approaching nanoseconds. This is really not recommended if you intend to publish your software or offer it to the end user, since the Manufacturers have a different kernel configuration on different devices, and therefore the PMU may not be available / available.

EDIT: you can additionally look at https://groups.google.com/forum/?fromgroups=#!topic/android-ndk/dgrqhgHWbHI and clock_getres () to check the resolution of clock_gettime ()

0
source

All Articles