How to get uptime in miliseconds ?


 
Thread Tools Search this Thread
Top Forums Programming How to get uptime in miliseconds ?
# 1  
Old 12-05-2011
How to get uptime in miliseconds ?

I need to get, in my application, in different methods, the uptime of the system in milliseconds.

time() - returns only seconds.
/proc/uptime - returns the seconds + a truncated milliseconds value, but it need to be parsed to extract data and convert it to milliseconds

Any other suggestions ?
# 2  
Old 12-05-2011
Since you are describing /proc/uptime which is Linux:
Code:
$ awk '{print $1*1000}' /proc/uptime

or C:

Code:
#include <stdio.h>
#include <stdlib.h>
// return 0 on error, else ms uptime
double ms_uptime(void)
{
    FILE *in=fopen("/proc/uptime", "r");
    double retval=0;
    char tmp[256]={0x0};
    if(in!=NULL)
    {
          fgets(tmp, sizeof(tmp), in);
          retval=atof(tmp);
          fclose(in);
    }
    return retval*1000;
    
}

# 3  
Old 12-05-2011
Quote:
Originally Posted by Pufo
I need to get, in my application, in different methods, the uptime of the system in milliseconds.

time() - returns only seconds.
Note that time(2) returns seconds since the epoch, not since system boot (Linux sysinfo(2) call does return uptime, but only in seconds).

Not terribly helpful, I know...
# 4  
Old 12-05-2011
Which do you think is faster:

jim mcnamara version, or mine ?

Code:
unsigned long ms_uptime()
{
   std::string script_loc, line;
   script_loc = "/proc/uptime";
   m_file.open(script_loc.c_str());

   if(m_file){

       getline(m_file, line);
       double param = atof(line.c_str());
       m_file.close();
 
       return param*1000;  
   } 
   return -1;
}

# 5  
Old 12-05-2011
MySQL

Please post exactly which Operating System and version you have and what programming language or Shell you are using for you "application".

In many unix systems the first entry in the file /etc/utmp (/etc/utmpx in Solaris) is the "system boot" time in seconds since the epoch. See "man utmp". It is the same file which is read by the unix commands "uptime" and "who -b" (to name but a few).

gettimeofday(2) has more precision than time(2) .
# 6  
Old 12-07-2011
OK, why?

Systems take much longer than a millisecond to boot up. Measuring uptime to millisecond precision is like measuring the depth of the Earth's atmosphere to meter precision - the edge isn't well-enough defined to make measuring to that accuracy meaningful.
# 7  
Old 12-07-2011
I sort of agree with "achenle" mainly because unix systems which need accurate time will have additional hardware to keep time and a suite of program routines to access the alternative clock - or even accurately timestamp external events (like someone crossing the finishing line).

Systems with an ordinary "PC style" real time clock will normally be set to use NTP to keep their clocks in sync with the rest of the world. Without adjustment the clock drift can be as much as a minute-a-month.

Sometimes you do need accurate time if you are indexing real time events in a database or trying to generate a decent spread of unique filenames.

Before servers came with real time clocks the Operating System would keep track of time by counting clock ticks since boot.
Real-time clock - Wikipedia, the free encyclopedia
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. War Stories

Once upon an uptime.

Hi All, Having recently started a new job, a Data Center Migration in fact I have been tasked with looking at some of the older Solaris boxes when I came across this little gem. nismas# uname -a SunOS nismas 5.5.1 Generic_103640-27 sun4u sparc SUNW,Ultra-1 nismas# uptime 10:37am up 2900... (2 Replies)
Discussion started by: gull04
2 Replies

2. Shell Programming and Scripting

Extract the uptime from the output of the uptime command

Hi! I want to extract the uptime from the output of the uptime command. The output: 11:53 up 3:02, 2 users, load averages: 0,32 0,34 0,43 I just need the "3:02" part. How can I do this? Dirk (6 Replies)
Discussion started by: Dirk Einecke
6 Replies

3. Linux

uptime options

Hi All is there a way that i can return uptime if the machine has been on for longer than 4 days thanks ab (3 Replies)
Discussion started by: ab52
3 Replies

4. Shell Programming and Scripting

Process Uptime

Hi, I need some help about a script i need to write. I want to check , if some specific process, are running since 2 hours. I tried to use a loop , grep my pid and use find -ctime on /proc directory, to list what i need. for i in `ps -ef |grep process |grep -v grep|awk '{print $2}'`... (2 Replies)
Discussion started by: rokerij
2 Replies

5. AIX

Hmc uptime

HELLO ALL HOW CAN I CHECK HMC UPTIME :confused: :confused: VERSION 6 :D (7 Replies)
Discussion started by: kalaso
7 Replies

6. Solaris

having problem with uptime.

HI All, I have problem with "uptime" on one of the sun server.(SunOS 5.9 Generic_118558-11 sun4u sparc SUNW,Sun-Fire-V240).when i am issuing uptime command its not showing uptime.even its not showing output for who -b. $ uptime 11:01am 1 user, load average: 0.06, 0.04, 0.03 $ who -b $... (3 Replies)
Discussion started by: jeevanbv
3 Replies

7. UNIX for Dummies Questions & Answers

Uptime

hello folks! how can I display just the uptime without the current time, the word "up", and the load averages using the uptime command or some other command I do not know about? (13 Replies)
Discussion started by: MastaFue
13 Replies

8. UNIX for Dummies Questions & Answers

uptime

On HP-UX, the 13th argument of uptime is sometime the load and sometime the word AVERAGE:??? 14 Jun 06 5:00pm up 44 days, 54 mins, 0 users, load average: 0.00, 0.02, 0.03 14 Jun 06 5:15pm up 44 days, 1:09, 0 users, load average: 0.00, 0.01, 0.01 When the time is in minutes, then the load... (1 Reply)
Discussion started by: qfwfq
1 Replies

9. UNIX for Dummies Questions & Answers

Getting uptime

I'm trying to get the uptime of my computer (Mac OS X) and I can go into the terminal and type "uptime" OK, and that gives me a string with the uptime in it. The problem is that the string changes a lot, and its very difficult to get the data I'm trying to extract out cleanly. Now I have 3... (2 Replies)
Discussion started by: Freefall
2 Replies

10. UNIX for Dummies Questions & Answers

uptime

Hi Folks uptime 12:24pm up 2 days, 3:12, 4 users, load average: 0.00, 0.00, 0.00 what does the load average figure mean.. regards Hrishy (2 Replies)
Discussion started by: xiamin
2 Replies
Login or Register to Ask a Question