<br><br><div class="gmail_quote">On Tue, Sep 20, 2011 at 5:16 PM, Lal <span dir="ltr">&lt;<a href="mailto:learner.kernel@gmail.com">learner.kernel@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
I am having trouble with do_gettimeofday() function. I have following<br>
source code:<br>
<br>
int my_module_init(void)<br>
{<br>
    struct timeval tv = {0};<br>
    do_gettimeofday(&amp;tv);<br>
    printk(&quot;tv.tv_sec = %lu\n&quot;, tv.tv_sec);<br>
    return 0;<br>
}<br>
module_init(my_module_init);<br>
<br>
This code is built-in kernel, and therefore my_module_init is called<br>
during device_initcall.<br>
<br>
However the tv_sec value seems to be wrong (I was expecting this value<br>
to be absolute number of seconds since 1970), but it prints 11 or 12<br>
(which seems to be number of seconds since last boot).<br>
<br>
I checked do_gettimeofday function source which gets the value from<br>
xtime variable. I could not find who is responsible for initializing<br>
xtime.<br>
<br>
I tried to change module_init by late_initcall_sync. Still it&#39;s same.<br>
<br>
Any help is appreciated.<br>
<br>
Regards<br>
Lal<br>
<br>
_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
</blockquote></div><br>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.<br><br>For eg. this is how its done for x86 arch : -<br>
<br>void read_persistent_clock(struct timespec *ts)<br>{<br>        unsigned long retval, flags;<br><br>        spin_lock_irqsave(&amp;rtc_lock, flags);<br>        retval = x86_platform.get_wallclock();<br>        spin_unlock_irqrestore(&amp;rtc_lock, flags);<br>
<br>        ts-&gt;tv_sec = retval;<br>        ts-&gt;tv_nsec = 0;<br>}<br><br>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.<br>
<br>xtime is updated by the function xtime_update() ----&gt; do_timer() -----&gt; update_wall_time() (This function sets the xtime variables time).<br><br><br>This code path is trigger on every timer interrupt.<br><br>Also, this time is measured from system boot as you correctly said.<br>
<br>Regards,<br>Rohan Puri.<br>