System Up Time

Windows specific forum
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

What makes you so sure this gives the correct results ? I cannot find any documentation specifying that the performance counter is linked to the uptime. It could be anything.
quidquid Latine dictum sit altum videtur
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

@freak, yes it does actually work. I'll look for docs on it. He's just using it wrong.

@PB: You're using it wrong. You can't use FormatDate on it. Here's an example of correct (if clumsy) usage:

Code: Select all

QueryPerformanceFrequency_(@perSec.q) 

QueryPerformanceCounter_(@currTime.q) 

seconds = currtime/persec
days = seconds/86400
seconds - days*86400
hours = seconds/3600
seconds-hours*3600
minutes = seconds/60
seconds-minutes*60

thisresult$ = "QPF System Up Time:                         "+Str(days)+" days, "+Str(hours)+" hours, "+Str(minutes)+" minutes, "+Str(seconds)+" seconds"



program = RunProgram("systeminfo.exe","","",#PB_Program_Open|#PB_Program_Read|#PB_Program_Hide) 
While ProgramRunning(program) 
  If AvailableProgramOutput(program) 
    a$=ReadProgramString(program) 
    If FindString(a$, "System Up Time", 1) 
      uptime$ = a$ 
      KillProgram(program) 
    EndIf 
  EndIf 
  Delay(1) 
Wend 
MessageRequester("","OS returned "+uptime$+#CRLF$+thisresult$) 
 
Here the results are equal within 2 seconds, which accounts for processing time.
BERESHEIT
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5502
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

netmaestro wrote:@freak, yes it does actually work. I'll look for docs on it. He's just using it wrong.

@PB: You're using it wrong. You can't use FormatDate on it. Here's an example of correct (if clumsy) usage:
For info the code of FREAK and the code of NETAMESTRO works perfectly 8)
He give the same result, i have try the two in the same time, but the code of netmaestro stop the code of freak when it run, it's surely normal 8)

Thanks at the GREAT FREAK and NETMAESTRO for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Ok nice to hear, but I don't think freak posted any code...

btw, all my code contains a procedure which stops any code freak is running. I'm sure he wonders why :D
BERESHEIT
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5502
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

PB wrote:Thanks to Freak I was able to come up with this, which works perfectly:

Code: Select all

QueryPerformanceFrequency_(@perSec.q)

For a=1 To 10
  QueryPerformanceCounter_(@currTime.q)
  Debug FormatDate("%hh:%ii:%ss",currTime/perSec)
  Sleep_(1000)
Next
No need for program outputs, Registry, COMate, etc. ;) Many thanks must
go to netmaestro and Sparkie for taking the time to post their solutions.

(BTW: I know this only works for 24 hours, but should be easy to enhance).
It's not me who have say that MASTER :oops:
It's PB the culpable :lol:
ImageThe happiness is a road...
Not a destination
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

> @freak, yes it does actually work. I'll look for docs on it. He's just using it wrong.

I know it works, thats not the point. The point is that no documentation guarantees it to work this way (that i am aware of). So this is just a hack that relies on undocumented behavior, and not a solid way of doing things. The actual source for the performance counter is determined by the hardware abstraction layer, so a different cpu, a different bios or a different motherboard could easily change this behavior and you're screwed.

Why take that risk with a method like that when there are probably more solid (read: documented) ways to get this information ?
quidquid Latine dictum sit altum videtur
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Code: Select all

Import ""
  GetTickCount64()
EndImport
Debug FormatDate("%hh:%ii:%ss", GetTickCount64() / 1000)
Requires Vista or Server 2008
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

@freak: Your point is a good one, and well taken. I will try to find docs on it. In the meantime, here are two theories I have that I'm setting out to prove/disprove:

1) The performance counter is started when the OS starts and it keeps incrementing at its frequency intervals as long as the OS is up.

2) The Windows APIs GetSystemTime and GetSystemTimes actually read the performance counter to come up with their values. This is how they achieve hi-res accuracy unavailable in timeGetTime, etc.

If these theories are true, then this should be as safe a method as any. I'm not saying that they are, just that I think they are. I will search..
BERESHEIT
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Post by dhouston »

http://msdn.microsoft.com/en-us/library ... S.85).aspx would appear to support Freak.
On a multiprocessor computer, it should not matter which processor is called. However, you can get different results on different processors due to bugs in the basic input/output system (BIOS) or the hardware abstraction layer (HAL). To specify processor affinity for a thread, use the SetThreadAffinityMask function.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> What makes you so sure this gives the correct results ?

Direct comparison with other UpTime apps on the net. Their results match
the results of my code snippet, to the second. So, it seems reliable to me.

> Why take that risk with a method like that when there are probably
> more solid (read: documented) ways to get this information ?

Well, I can probably use Sparkie's COMate code then, if that's a better way.
But that's a hell of a lot of includes and extra code, compared to a few lines.
And you know how much I hate bloatware! ;) But, if all that extra code gives
correct results on all PCs, then that's the most important thing to consider.

I will test my few lines on my work PC, my brother-in-law's, my library's,
and my Eee PC. If they all give correct results, then I will be very reluctant
to use COMate or any other method. (Well, until I get a bug report from a
user, and then I will release a bug-fixed version that uses COMate). :lol:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply