LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2005, 02:02 AM   #1
chanwing
Member
 
Registered: Feb 2005
Posts: 42

Rep: Reputation: 15
Window VS Linux : timer question




Hello,
Can anyone help me to solve this problem?
I wanna convert a timer file from a window program(VC++6.0) into Linux.
There are "GetTickCount(),QueryPerformanceFrequency(),QueryPerformanceCounter()".

GetTickCount() return number of milliseconds that have elapsed since the system was started.
QueryPerformanceFrequency() retrieves the frequency of the high-resolution performance counter.
QueryPerformanceCounter() retrieves the current value of the high-resolution performance counter.

How can I create three functions to replace them ?
I had already change the GetTickCount() but I don't know is it correct or not.

#include <sys/time.h>
unsigned int Timer::GetTickCount()
{
timeval t;
if (gettimeofday(&t,NULL)==0)
return t.tv_usec;
}

I saw the "sys/time.h","sys/times.h","time.h","linux/times.h","linux/time.h" and "linux/timer.h" but I still don't have any idea for replacing QueryPerformanceFrequency() and QueryPerformanceCounter().
I would be gratefully if you can help me to solve the problem
Thanks



 
Old 03-29-2005, 06:11 AM   #2
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
unsigned int Timer::GetTickCount()
{
unsigned int result=0;
timeval gtod_now_time;
gettimeofday(&gtod_now_time,0);
result=(gtod_now_time.tv_usec-gtod_start_time.tv_usec)/1000;
result+=(gtod_now_time.tv_sec-gtod_start_time.tv_sec)*1000;
return result;
}

No idea about the Query... functions, sorry.
 
Old 03-29-2005, 07:06 PM   #3
chanwing
Member
 
Registered: Feb 2005
Posts: 42

Original Poster
Rep: Reputation: 15
Thank you very much "Marius2 " for your help and reply.

But, I am not really understand your program.

the variable "gtod_start_time.tv_usec" is come from where?
is it "gettimeofday(&gtod_start_time,0);" called in other place?

In fact, what is the different between gtod_start_time and gtod_now_time?

GetTickCount return millisecond (in window original function)but tv_usec return microsecond(in linux time.h). micro=1/1000milli. is it "*1000" not "/1000"?

QueryPerformanceFrequency() and QueryPerformanceCounter() are really difficult to migrate to Linux.

Do anyone know any functions or methods that can get the CPU counter values in Linux?
thx
 
Old 03-30-2005, 12:49 AM   #4
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
Quote:
Originally posted by chanwing
Thank you very much "Marius2 " for your help and reply.

But, I am not really understand your program.

the variable "gtod_start_time.tv_usec" is come from where?
is it "gettimeofday(&gtod_start_time,0);" called in other place?

In fact, what is the different between gtod_start_time and gtod_now_time?

Ok sorry, I missed that. gtod_start_time is a member variable of the class and set in the constructor. If it's instantiated at program start time, this method will return milliseconds since the start of the program. If you really need milliseconds since the start of the system, you might need to write a small program which dumps the gettimeofday value to a text file at boot time (called in /etc/init.d/rc3.d, or whatever runlevel), which is then read once by your code, etc. etc. There may be other ways, but that is the only way I can think of now.



Quote:
[i]
GetTickCount return millisecond (in window original function)but tv_usec return microsecond(in linux time.h). micro=1/1000milli. is it "*1000" not "/1000"?
Look at it again, the unit you want is milliseconds.
 
Old 03-30-2005, 07:25 PM   #5
chanwing
Member
 
Registered: Feb 2005
Posts: 42

Original Poster
Rep: Reputation: 15

OH, I see.
Thank you very much.
Are there any log files that the Linux will record the time when the linux system startup? If I am right, I remember that everytime the Linux will record any processes in a log file just like a command of "history".
Do you know where can I find out more information about timer in Linux? I found some Linux books but they talk less about it.
Sorry that I have lots of questions.
 
Old 03-31-2005, 02:22 AM   #6
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
No idea about the logs, but you may want to take a look at /var/log directory. A simple shell script containing something like "echo $(/sbin/hwclock) > /tmp/starttime.txt" and invoked at start time via a symlink in /etc/init.d/rc[n].d may also do the trick.

BTW when I'm looking for *NIX aquivalents of windows interfaces, my first try generally is asking google for something like "<name_of_windows_api> linux"


HTH
 
Old 03-31-2005, 06:20 PM   #7
chanwing
Member
 
Registered: Feb 2005
Posts: 42

Original Poster
Rep: Reputation: 15
oh, thanks.
thank you for your useful information.


I have an other additional question.
do you know Mfc Macros ?
I have a problem about the rewrite of IMPLEMENT_DYNCREATE(), DECLARE_DYCREATE() and DECLARE_DYNAMIC().
Can you give me some ideas about it?
 
Old 03-31-2005, 07:05 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Are there any log files that the Linux will record the time when the linux system startup?
Code:
last -1 reboot
should tell you that.
 
Old 04-01-2005, 02:32 AM   #9
chanwing
Member
 
Registered: Feb 2005
Posts: 42

Original Poster
Rep: Reputation: 15

Thank you jlliagre.
and Thanks to ALL.
 
Old 04-01-2005, 11:52 AM   #10
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
have a look at the shell command `uptime', and the source for it.

hth --Jonas Kölker
 
Old 04-01-2005, 12:38 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Both last and uptime use the /var/run/utmp(x) file.
 
Old 04-02-2005, 06:15 PM   #12
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
Both last and uptime use the /var/run/utmp(x) file.
man last: `last searches back through the file /var/log/wtmp'
man uptime: `FILES: /var/run/utmp [...]'

Code:
$ last -1 reboot

wtmp begins Fri Apr  1 18:35:49 2205
$ uptime
 02:06:53 up 3 days,  7:19,  3 users,  load average: 0.34, 0.24, 0.66
$ date
(apr 3)
you don't have to be an expert in time arithmetic to see that there's a mismatch between `last' and `uptime'.

I trust uptime, since I know that I didn't reboot on April 1st (no joke ).

YMMV(!)

--Jonas Kölker
 
Old 04-03-2005, 04:35 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Well, Linux is (or some Linux distros are) rotating or clearing /var/log/wtmp every month, which is questionable (Unix keep the file intact).
Anyway, you can overcome it by pointing "last" to a more persistent file.
Code:
last -1 reboot -f /var/run/utmp
 
Old 04-05-2005, 04:27 AM   #14
chanwing
Member
 
Registered: Feb 2005
Posts: 42

Original Poster
Rep: Reputation: 15

Oh.......Thank you very much

I have seen a book "Linux Device Drivers, 2nd Edition, Orelly" recently.

#include <linux/timex.h>
cycles_t get_cycles(void);

The function is defined for the platform has cycle-counter register

do any one know about "jiffies"?
it is also for the time.

I still read the book and I will try to write a test program for testing the time.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a countdown timer window in Fedora Core statman Linux - Software 0 06-18-2005 04:42 PM
GNUChess Timer Question W0bbles Linux - Games 1 03-11-2005 06:00 AM
xchat question, timer moger Linux - Software 0 02-19-2004 12:46 PM
Hello All...First timer here....Have a question. N00B 4EVER Linux - Newbie 14 11-15-2003 10:24 AM
First timer question on chroot timna Linux From Scratch 1 07-08-2003 01:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration