2008-03-16 13:34:13     system time

Document created by Aaronwu Employee on Aug 5, 2013
Version 1Show Document
  • View in full screen mode

2008-03-16 13:34:13     system time

Linux Newbie (INDIA)

Message: 52572   

 

Hi,

 

How to get the time of the system i.e.duration from which it is booted.?

 

 

 

gettimeofday() gives with respect to some pre defined default values (jan 2000) but is there any funchtion like VDK_Getuptime();

 

 

 

Thanks in Advance

QuoteReplyEditDelete

 

 

2008-03-16 13:42:13     Re: system time

Praneeth Bodduluri (INDIA)

Message: 52573    See if this helps.

Use the command "uptime" through your code....

 

 

 

 

 

QuoteReplyEditDelete

 

 

2008-03-16 20:31:26     Re: system time

Mike Frysinger (UNITED STATES)

Message: 52574    as Praneeth indicates, the program `uptime` reads /proc/uptime to give you the info

QuoteReplyEditDelete

 

 

2008-03-17 04:50:32     Re: system time

Linux Newbie (INDIA)

Message: 52581   

 

Hi

 

thanks for your reply.

 

but "uptime" is an system call, which prints the data. So we need to write a program to parse it.

 

is there any API similar to getuptime that gives system uptime?

QuoteReplyEditDelete

 

 

2008-03-17 06:51:22     Re: system time

Mike Frysinger (UNITED STATES)

Message: 52582    `uptime` is not a system call, it is a program you run on the system

 

there is no API to get the uptime ... you need to read/parse /proc/uptime directly if you dont want to execute the uptime binary

QuoteReplyEditDelete

 

 

2008-03-18 04:59:45     Re: system time

J.J. Krabbendam (NETHERLANDS)

Message: 52670    I am not sure if it is supported on uclinux, but normaly under linux it should be possible to use sysinfo() to obtain the uptime.

 

#include <sys/sysinfo.h>

long uptime(void)

{

   struct sysinfo info;

 

   if (sysinfo( &info ) != 0)

      return -1;

   else

      return info.uptime;

}

 

 

Artis.

QuoteReplyEditDelete

 

 

2008-03-18 08:01:15     Re: system time

Mike Frysinger (UNITED STATES)

Message: 52693    uClinux is Linux, and since sysinfo isnt related to the MMU, then it should work fine ... thanks for the tip

QuoteReplyEditDelete

 

 

2008-03-26 14:04:49     Re: system time

Linux Newbie (INDIA)

Message: 53070   

 

HI

 

Thanks for the info.

 

But uptime gives in terms of seconds, how to obtain time in terms of milli seconds resolution.

QuoteReplyEditDelete

 

 

2008-03-26 15:15:59     Re: system time

Mike Frysinger (UNITED STATES)

Message: 53072    i doubt that millisecond resolution on uptime is important.

 

you can still parse /proc/uptime as it gives a few fractions of a second, but it isnt milliseconds.

QuoteReplyEditDelete

 

 

2008-03-26 18:43:29     Re: system time

Graham Davies (UNITED STATES)

Message: 53081    If it's of any help, I gave up trying to figure out how to get a free-running counter of millisecond resolution from uClinux in user-space.  I already had a driver that polled a piece of hardware, so I just bolted the necessary functionality onto that.  In other words, I effectively had to write my own millisecond tick counter, which required me to learn how to write Linux drivers and interface between kernel and user space.

 

Graham.

QuoteReplyEditDelete

 

 

2008-03-26 19:12:50     Re: system time

Mike Frysinger (UNITED STATES)

Message: 53083    i dont know what your needs were, but wouldnt gettimeofday() work ?  that has microsecond resolution and for all intents, it is a free running counter.

QuoteReplyEditDelete

 

 

2008-03-27 09:31:35     Re: system time

Graham Davies (UNITED STATES)

Message: 53140    The work I inherited did use gettimeofday() followed by some tortuous arithmetic to compute differences between time snapshots.  You're right, it has microsecond resolution, by the strict definition of "resolution", but I was unsure of the accuracy.  Or, again strictly speaking, the differential accuracy.  It's no help being able to resolve microseconds if the value only increases on a kernel tick.  Anyway, all that to one side, it isn't really a free running counter.  Whenever we used the product user interface to set the date and time, the software would go crazy and apparently random stuff would happen.  Not suspecting that getting access to a true, user-space tick counter would be at all difficult (given that it is very easy in Windoze), the implementation of this function was the last place I looked for the cause of this problem.  Having burned tens of hours getting to the bottom of this, spending a couple of hours to kill the problem dead by implementing a real free-running tick counter seemed like the best course of action.  So, my advice would be to only use gettimeofday() if you never allow the time and date to be changed while your application is running and implementing a real tick counter is more than a couple of hours work for you.

 

Graham.

QuoteReplyEditDelete

 

 

2008-03-27 09:52:07     Re: system time

Linux Newbie (INDIA)

Message: 53145   

 

Hi,

 

I am almost lost in the discussion.

 

Please clarify.

 

In order to get time in terms of tick (similar to vdk_getuptime) we need to write a basic driver. Is my understanding correct?

 

We couldnt use getuptime() because it calculates time from Jan 2000.

 

Thanks in Advance

QuoteReplyEditDelete

 

 

2008-03-27 12:38:44     Re: system time

Mike Frysinger (UNITED STATES)

Message: 53151    the 2008R1 release should have real resolution beyond a jiffie.  the gettimeofday function will look at the jiffies, and then include the time since the last tick so that you get accurate results.

 

but you're right that if you turn around and start changing the time, that will most likely screw up your application if it doesnt take things into account.  that's why ntp daemons gradually adjusts time until things are in sync again.

 

curious though ... what is this "external piece of hardware" you refer to ?  is it an actual time source or what ?

QuoteReplyEditDelete

 

 

2008-03-27 12:39:55     Re: system time

Mike Frysinger (UNITED STATES)

Message: 53152    there is no "getuptime()" function in Linux, so i dont know what problems you're having with it.  the function referred to by J.J. will give you the number of seconds since boot, not since any arbitrary date.

QuoteReplyEditDelete

 

 

2008-03-27 14:05:38     Re: system time

Linux Newbie (INDIA)

Message: 53159   

 

sorry...i meant gettimeofday()!!!!!

 

Is it possible to write a driver which calculates time based on number of ticks?

QuoteReplyEditDelete

 

 

2008-03-27 15:28:48     Re: system time

Mike Frysinger (UNITED STATES)

Message: 53167    i dont know what "ticks" you're referring to, but if you arent messing with the system clock, just use gettimeofday()

QuoteReplyEditDelete

 

 

2008-03-27 15:36:59     Re: system time

Graham Davies (UNITED STATES)

Message: 53168    The "external piece of hardware" I referred to is an FPGA that does stuff that is peculiar to the product that I'd rather not disclose.  If I'd been around when this was designed, it would generate an interrupt when it needs attention.  But I wasn't and it doesn't.  So, I wrote a driver that runs a Blackfin timer at 10 kHz to generate interrupts and I poll the FPGA status registers in the interrupt service routine to find out when it needs attention.  Then, I schedule one of several tasklets to do whatever is needed.

 

When I figured out that the implementation of the millisecond tick count that I inherited was using the wall clock and getting confused when this was changed by the product user, I just bolted a free-running counter at 1 kHz onto the timer-driven interrupt service routine in the obvious way.

 

Graham.

QuoteReplyEditDelete

 

 

2008-03-27 15:41:54     Re: system time

Graham Davies (UNITED STATES)

Message: 53169    Although I am not a Linux expert, I would say yes.  In fact, writing a driver to provide user-space access to the kernel jiffy counter may be one of the simplest meaningful drivers you could write.

 

If you don't want to use gettimeofday(), perhaps because, like me, you need to allow for the date and time to be set by a user, and you've configured the jiffy counter to run fast enough to meet your timing resolution needs, this is probably the way to go.

 

I hope someone will correct me if I'm wrong.

 

Graham.

QuoteReplyEditDelete

 

 

2008-03-27 16:20:36     Re: system time

Mike Frysinger (UNITED STATES)

Message: 53173    if you're merely exposing the jiffie count, isnt that the same thing as getting uptime (which already has a standard user<->kernel interface) ?

QuoteReplyEditDelete

 

 

2008-03-27 16:21:57     Re: system time

Mike Frysinger (UNITED STATES)

Message: 53174    sure, i wouldnt expect intimate details about your hardware ... but what you've posted answered my questions, thanks

QuoteReplyEditDelete

 

 

2008-03-28 09:57:58     Re: system time

Graham Davies (UNITED STATES)

Message: 53249    If uptime exposes the jiffy count to user-space, then it seems to me that's the answer to the OP's question.  I was answering this question from the OP: "Is it possible to write a driver which calculates time based on number of ticks?".  I guess I was right, the answer is yes and it has already been done.

 

But, let's recap.  On Wednesday, the OP wrote "But uptime gives in terms of seconds, how to obtain time in terms of milliseconds resolution."  You (Mike) replied "you can still parse /proc/uptime as it gives a few fractions of a second, but it isnt milliseconds."  So, the OP wants milliseconds and the solution you're offering gives "fractions of a second", so this must be why the thread is still going.

 

I have described my solution, which was easy for me because I already had a driver doing sub-millisecond polling on a piece of custom hardware.  Without that, the OP would have to write a driver from scratch to use a Blackfin timer to increment a free-running software tick counter at 1 kHz.

 

Are we suggesting that uptime's exposure of the jiffy count can, in fact, be used to gain millisecond resolution in user-space (not just "fractions of a second")?  Can the OP configure the jiffy counter rate to 1 kHz and then parse the output of /proc/uptime to get the exact value of the count, thus achieving millisecond resolution?  If this is possible, I think we have to explicitly say so, and then the OP's question will be answered.

 

When I "cat /proc/uptime", I see only two places after the decimal point, which would not allow millisecond resolution.  So, the only solutions discussed so far that meet the OP's needs are 1) a custom driver using a Blackfin timer and 2) configure the jiffy counter rate to 1 kHz and write a custom (but much simpler) driver to expose it to user space.

 

Or, maybe I'm completely wrong.

 

Graham.

 

P.S. I don't have a problem here.  I just don't think the OP is getting a clear answer to his or her question.

QuoteReplyEditDelete

 

 

2008-03-28 10:37:43     Re: system time

Mike Frysinger (UNITED STATES)

Message: 53252    that is an accurate summary, i'm just left wondering what the OP is looking for exactly.  all we get is questions of "how do i do XYZ#" with no direct correlation between the different XYZs.  if all you need is uptime, that's easy to get.  i cant fathom why you would really want it, let alone with a millisecond resolution, unless your goal really wasnt uptime at all, but a free running counter with millisecond resolution.  but that hasnt been really explained.  so it's hard to give a clear answer to a question that is anything but clear.

QuoteReplyEditDelete

 

 

2008-03-30 09:41:24     Re: system time

Linux Newbie (INDIA)

Message: 53336   

 

Hi.

 

Thanks for your answers...

 

sorry for not being more specific to my question...but ur discussion gave the answer i was looking for..

 

once again..thanks

Attachments

    Outcomes