do_gettiimeofday query

rohan puri rohan.puri15 at gmail.com
Tue Sep 20 09:18:37 EDT 2011


On Tue, Sep 20, 2011 at 5:16 PM, Lal <learner.kernel at gmail.com> wrote:

> I am having trouble with do_gettimeofday() function. I have following
> source code:
>
> int my_module_init(void)
> {
>     struct timeval tv = {0};
>     do_gettimeofday(&tv);
>     printk("tv.tv_sec = %lu\n", tv.tv_sec);
>     return 0;
> }
> module_init(my_module_init);
>
> This code is built-in kernel, and therefore my_module_init is called
> during device_initcall.
>
> However the tv_sec value seems to be wrong (I was expecting this value
> to be absolute number of seconds since 1970), but it prints 11 or 12
> (which seems to be number of seconds since last boot).
>
> I checked do_gettimeofday function source which gets the value from
> xtime variable. I could not find who is responsible for initializing
> xtime.
>
> I tried to change module_init by late_initcall_sync. Still it's same.
>
> Any help is appreciated.
>
> Regards
> Lal
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>

If you want time since the epoch in seconds then you can call the
architecture specific method get_wallclock(), but make sure u hold the
appropriate lock during this call.

For eg. this is how its done for x86 arch : -

void read_persistent_clock(struct timespec *ts)
{
        unsigned long retval, flags;

        spin_lock_irqsave(&rtc_lock, flags);
        retval = x86_platform.get_wallclock();
        spin_unlock_irqrestore(&rtc_lock, flags);

        ts->tv_sec = retval;
        ts->tv_nsec = 0;
}

NOTE : - this read_persistent_clock() function is not exported so u cannot
make use of this symbol in your code. But x86_platform is exported variable,
so u can make use of that.

xtime is updated by the function xtime_update() ----> do_timer() ----->
update_wall_time() (This function sets the xtime variables time).


This code path is trigger on every timer interrupt.

Also, this time is measured from system boot as you correctly said.

Regards,
Rohan Puri.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110920/6ee5be76/attachment.html 


More information about the Kernelnewbies mailing list