Re: times() question | |
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] | |
hello chris,
On 10/3/07, Christian Boon <c.boon@xxxxxxxxxxxxxxxxxxxx> wrote:
> Hello,
>
> for my application i use times(NULL).
> the manual page says:
>
> The function times returns the number of clock ticks that have elapsed
> since an arbitrary point in the past. For Linux this point is the moment the
> system was booted. This return value may overflow the possible range of
> type clock_t. On error, (clock_t) -1 is returned, and errno is set appro-
> priately.
>
true.
> i use the following program:
>
> #include <stdio.h>
> #include <unistd.h>
> #include <sys/times.h>
>
> int main(void)
> {
> clock_t time;
>
> while (1)
> {
> time = times(NULL);
> printf("times value is: %d\n",time);
>
> if (time == (clock_t)-1)
> perror("error: ");
>
you must "break;" here when the error occurs. see reason below.
> usleep(10000);
> }
> return 0;
> }
>
> the output when the system just booted is:
>
> times value is: -27317
> times value is: -27314
> times value is: -27311
> times value is: -27308
as you know, times() is a call into glibc. glibc traps into the
kernel, and in the kernel, it gets to the system call sys_times(). the
flow of calls is:
sys_times
-> jiffies_64_to_clock_t(get_jiffies_64())
-> get_jiffies_64() returns the jiffies_64 that was
initialized during bootup.
take a look at the definition of INITIAL_JIFFIES. this variable
defines the value that the jiffies_64 is initialized to during bootup.
the jiffies_64 value is set to just 5 minutes before wrap-around
(-5minutes*30*seconds/minute*HZclockticks/seconds) = (-30000 on my
system with HZ=100). and hence these negative values.
> times value is: -27305
> times value is: -27302
> times value is: -27299
> times value is: -27296
> times value is: -27293
> times value is: -27290
> times value is: -27287
> times value is: -27284
> times value is: -27281
> times value is: -27278
> times value is: -27275
> times value is: -27272
> ...
>
> but after a while:
>
> times value is: -4103
> times value is: -4100
> times value is: -4097
I am not sure why it starts returning (-1) values at around 4096. it
happens on my system too. i think the reason for this could be found
into the computations involved in the function:
jiffies_64_to_clock_t().
> times value is: -1
> error: : Unknown error 4094
> times value is: -1
> error: : Unknown error 4091
> times value is: -1
> error: : Unknown error 4088
> times value is: -1
> error: : Unknown error 4085
> times value is: -1
> error: : Unknown error 4082
> times value is: -1
> error: : Unknown error 4079
> times value is: -1
> error: : Unknown error 4076
> times value is: -1
> error: : Unknown error 4073
> times value is: -1
> error: : Unknown error 4070
> times value is: -1
> error: : Unknown error 4067
> times value is: -1
> error: : Unknown error 4064
> times value is: -1
> error: : Unknown error 4061
> times value is: -1
> error: : Unknown error 4058
>
> ......
>
> error: : Unknown error 143
> times value is: -1
> error: : Unknown error 140
> times value is: -1
> error: : Unknown error 137
> times value is: -1
> error: : Unknown error 134
> times value is: -1
if you look at the flow, the next printf should have been:
error: : Unknown error 131
But instead you get:
> error: : State not recoverable
> times value is: -1
if you take a look at what error number 131 corresponds to in
include/asm-generic/errno.h, you will see:
#define ENOTRECOVERABLE 131 /* State not recoverable */
For all other previous errors, since there was no corresponding
errnos, we did not see any info. 131 happens to be the largest errno,
and once you hit that, you see the error info printed, which ofcourse
is incorrect.
> error: : State not recoverable
> times value is: -1
> error: : Key has been revoked
> times value is: -1
> error: : Operation canceled
> times value is: -1
Since all these error information are just direct correspondence to
errnos, these are not correct error info. hence my earlier statement
to break out of your while loop once you encounter the error.
cheers, nitin
-------------------------------------------------------------------
List admin: http://lists.arm.linux.org.uk/mailman/listinfo/linux-arm
FAQ: http://www.arm.linux.org.uk/mailinglists/faq.php
Etiquette: http://www.arm.linux.org.uk/mailinglists/etiquette.php
[Site Home] [Fedora ARM] [IETF Annouce] [Security] [Bugtraq] [Linux] [Linux ARM Kernel] [Linux MIPS] [ECOS] [Tools] [DDR & Rambus] [Monitors]