Issues while calling times( ) api

21 views
Skip to first unread message

Raj Shekar

unread,
Jan 10, 2020, 9:22:48 AM1/10/20
to andro...@googlegroups.com
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.

Shahriar Vaghar

unread,
Jan 10, 2020, 11:29:42 AM1/10/20
to andro...@googlegroups.com
Have you considered using /proc/[pid]/stat instead?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAOpHf7MwLbzeopauN6pzigEzXSCNoUQ%3DBJ-QVyFLWuuUSvSq1g%40mail.gmail.com.

Raj Shekar

unread,
Jan 14, 2020, 4:55:23 AM1/14/20
to andro...@googlegroups.com
Thanks for your reply Shahriar, is this a known issue in the ndk ? My device is using the 4.4 kernel but as per the documentation, the issue is reported in kernel version 2.6

Is it recommended to skip using the times() api or are there any fixes. 

Thanks, 
Raj. 

Reply all
Reply to author
Forward
0 new messages