Hi All,
we are using the times() api for some time related calculations in our multi-threaded application. There will be around 8-10 threads (created by pthread_create() calls) in our native code. We are facing a strange random issue with the times() api call, it will start to return -1 for around 40 seconds.
The 'times.h' header from Android SDK is as below:
/**
* [times(2)](
http://man7.org/linux/man-pages/man2/times.2.html) fills a buffer with the
* calling process' CPU usage.
*
* Returns a (possibly overflowed) absolute time on success,
* and returns -1 and sets `errno` on failure.
*/
clock_t times(struct tms* __buf);
In the manual page (
http://man7.org/linux/man-pages/man2/times.2.html) there's a BUGS section, which states as below:
"BUGS
A limitation of the Linux system call conventions on some
architectures (notably i386) means that on Linux 2.6 there is a small
time window (41 seconds) soon after boot when times() can return -1,
falsely indicating that an error occurred. The same problem can
occur when the return value wraps past the maximum value that can be
stored in clock_t."
So, this implies that there can be a window of 41 seconds when times() can return -1, but it should be after boot, but in our case its random (not very frequent though) and not just after device boot up but during application run-time too. And due to this delay, some of our timers go for a toss and the application state too.
Below is a snippet from our api where times() fails. And the errno set by times start with 4095 and keep decrementing till 1 during the 40 seconds time. Our application is running on Android 8.1 (API 27) & NDK Revision = 20.1.5948944. Can anyone help me out on this, thanks for reading.
Snippet
-----------
{
clock_t temp;
temp = times( NULL );
if( temp == (clock_t) -1 )
{
log( "errno %d calling times api", errno );
return( 0 );
}
}
Logs:
-------
14:57:44 errno 4095 calling times api
14:57:44 errno 4095 calling times api
14:57:44 errno 4095 calling times api
14:57:44 errno 4094 calling times api
14:57:44 errno 4094 calling times api
14:57:44 errno 4094 calling times api
14:57:44 errno 4094 calling times api
14:57:44 errno 4094 calling times api
.
.
14:58:25 errno 11 calling times api
14:58:25 errno 11 calling times api
14:58:25 errno 11 calling times api
14:58:25 errno 11 calling times api
14:58:25 errno 1 calling times api
14:58:25 errno 1 calling times api
14:58:25 errno 1 calling times api
Thanks,
Raj.