Issues while calling times( ) api

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Issues while calling times( ) api

Raj Shekar
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAOpHf7MwLbzeopauN6pzigEzXSCNoUQ%3DBJ-QVyFLWuuUSvSq1g%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Issues while calling times( ) api

Shahriar Vaghar
Have you considered using /proc/[pid]/stat instead?

On Fri, Jan 10, 2020 at 6:22 AM Raj Shekar <[hidden email]> wrote:
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAOpHf7MwLbzeopauN6pzigEzXSCNoUQ%3DBJ-QVyFLWuuUSvSq1g%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAPwkQFHqRssLY62_-dBoQjw9Pns4tT-3goXwBu%2B7jzk_4qUqEg%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: Issues while calling times( ) api

Raj Shekar
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. 

On Fri, Jan 10, 2020, 21:59 Shahriar Vaghar <[hidden email]> wrote:
Have you considered using /proc/[pid]/stat instead?

On Fri, Jan 10, 2020 at 6:22 AM Raj Shekar <[hidden email]> wrote:
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.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAOpHf7MwLbzeopauN6pzigEzXSCNoUQ%3DBJ-QVyFLWuuUSvSq1g%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAPwkQFHqRssLY62_-dBoQjw9Pns4tT-3goXwBu%2B7jzk_4qUqEg%40mail.gmail.com.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/CAOpHf7P6PnGPQ3YRUjVrAcYp2eqYpN89NDcuosgRpUefWkkaGw%40mail.gmail.com.