Recent

Author Topic: [SOLVED] Getting the current time in form of nanoseconds count as Int64  (Read 9339 times)

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #15 on: April 17, 2019, 11:17:54 pm »
Yep, it is. Is this means, that it can not be imported and used in sofware layer, e.g. in a regular application?
« Last Edit: April 17, 2019, 11:23:04 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
« Last Edit: April 18, 2019, 12:34:18 am by sstvmaster »
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #17 on: April 18, 2019, 12:57:43 am »
@sstvmaster: thank you, I have improved the source of the test project. Now it should compile and run, so I added the complete sources to the attachment. If someone has time, I would ask for a check.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #18 on: April 18, 2019, 01:10:42 am »
What I get

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #19 on: April 18, 2019, 01:22:40 am »
Is it possible to use this function at all? Even indirectly?
« Last Edit: April 18, 2019, 01:27:31 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #20 on: April 18, 2019, 04:41:39 am »
Is it possible to use this function at all? Even indirectly?
Example stackoverflow go language wrapper. Is no system function.

Can see is wrapper darwin http://newosxbook.com/src.jl?tree=xnu-1228.15.4&file=/osfmk/ppc/rtclock.c#absolutetime_to_nanotime
Code: C  [Select][+][-]
  1. void
  2. clock_get_system_nanotime(
  3.         uint32_t                        *secs,
  4.         uint32_t                        *nanosecs)
  5. {
  6.         uint64_t        now, t64;
  7.         uint32_t        divisor;
  8.  
  9.         now = mach_absolute_time();
  10.  
  11.         *secs = t64 = now / (divisor = rtclock_sec_divisor);
  12.         now -= (t64 * divisor);
  13.         *nanosecs = (now * NSEC_PER_SEC) / divisor;
  14. }
  15.  

Important read "mach_absolute_time()"

Stackoverflow writes go use import https://github.com/golang/go/blob/fe68ab3bcde97e3a325e4aa3c70f5f9172540453/src/runtime/sys_darwin.go#L351
Code: Go  [Select][+][-]
  1. 351 go:cgo_import_dynamic libc_mach_absolute_time mach_absolute_time "/usr/lib/libSystem.B.dylib"
  2.  

I not know /usr/lib/libSystem.B.dylib ... or is simple libc

If is dylib then no static and is dynamic load.

I not sure help you furious programmer... i sorry if not help.


Have more read topic time measure:
http://nadeausoftware.com/articles/2012/04/c_c_tip_how_measure_elapsed_real_time_benchmarking
https://developer.apple.com/library/archive/qa/qa1398/_index.html
https://opensource.apple.com/source/xnu/xnu-3789.41.3/libsyscall/wrappers/mach_absolute_time.s.auto.html
« Last Edit: April 18, 2019, 05:44:04 am by Thausand »

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #21 on: April 18, 2019, 02:37:34 pm »
Yesterday I was tired of thinking about the solution and writing stupid. Today I have a fresh mind and I can look at the problem again. it should be better. 8)

I not sure help you furious programmer... i sorry if not help.

You probably solved the riddle.

The first thing is mach_absolute_time, which is the right time retrieval function. It can be used to get the current time and then, calculate the nanoseconds count.

But the second thing is this article:

Quote
http://nadeausoftware.com/articles/2012/04/c_c_tip_how_measure_elapsed_real_time_benchmarking

It turns out that there is a better solution than mach_absolute_time, that is, the gettimeofday function. Although the result has microsecond precision, but it is available on many POSIX systems, including Linux, BSD, OSX, Solaris and so on. And it is declared in Unix unit with the name fpgettimeofday.

Thanks to this, I can simplify the project code and use QPC and QPF for Windows, and fpgettimeofday on other systems (with predefined frequency value). Get the time, calculate nanoseconds count and use the result for fpnanosleep.


@Thausand: huge thanks! It should do the job.
« Last Edit: April 18, 2019, 02:41:18 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

guest58172

  • Guest
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #22 on: April 18, 2019, 03:13:44 pm »
If the problem is still not solved You can try to see how this is done in D stand library since its StopWatch is nano-second accurate on all platforms.

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #23 on: April 18, 2019, 07:17:10 pm »
I hope that the problem is solved due to the "discovery" of the FPGetTimeOfDay function. :D

But I need confirmation, that's why I prepared a new test application that checks the correctness of the simple function that takes time and returns it in the form of a number of nanoseconds. This simple function looks like this:

Code: Pascal  [Select][+][-]
  1. function TimeInNanoseconds(): Int64;
  2. var
  3.   Time: TTimeVal;
  4. begin
  5.   FPGetTimeOfDay(@Time, nil);
  6.   Result := Int64(Time.tv_sec) * 1000000000 + Int64(Time.tv_usec) * 1000;
  7. end;

Should work on different non-Windows systems, including macOS, Linux, BSD and Solaris. Test application is in the attachment, so please check if it compiles and works properly. The output of the console will be appreciated.
« Last Edit: April 18, 2019, 07:20:06 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #24 on: April 21, 2019, 08:59:53 pm »
No one has the opportunity to check this test program on desktop macOS (or on any other Unix-like system) and write whether it works and what is the output of the console?

Source code is in the attachment in the previous post. It will help me a lot.
« Last Edit: April 21, 2019, 09:01:42 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #25 on: April 22, 2019, 03:23:57 am »
Attached console. MacOS.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #26 on: April 22, 2019, 04:15:23 am »
attach raspbian

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #27 on: April 22, 2019, 01:38:51 pm »
@Lainz and @Thausand: thank you for tests.

It looks like the calculations on both macOS and Linux are correct, so the problem is solved.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Getting the current time in form of nanoseconds count as Int64
« Reply #28 on: April 22, 2019, 09:55:13 pm »
I hope that the problem is solved due to the "discovery" of the FPGetTimeOfDay function.

I would love to see how you call the QPC on Windows in your routine.
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: [SOLVED] Getting the current time in form of nanoseconds count as Int64
« Reply #29 on: April 22, 2019, 10:00:37 pm »
Sure, here you have:

Code: Pascal  [Select][+][-]
  1. function TClock.GetHardwareCounterValue(): Int64;
  2. {$IFDEF UNIX}
  3. var
  4.   Counter: TTimeVal;
  5. {$ENDIF}
  6. begin
  7.   {$IFDEF WINDOWS}
  8.   Result := 0;
  9.   QueryPerformanceCounter(Result);
  10.   {$ELSE}
  11.   FPGetTimeOfDay(@Counter, nil);
  12.   Result := Int64(Counter.tv_sec) * 1000000000 + Int64(Counter.tv_usec) * 1000;
  13.   {$ENDIF}
  14. end;
  15.  
  16. procedure TClock.WaitForNMI();
  17. var
  18.   {$IFDEF WINDOWS}
  19.   NextFrameCounts, CurrentCounts: Int64;
  20.   {$ELSE}
  21.   WaitTime: Int64;
  22.   RequestedTime, RemainingTime: TTimeSpec;
  23.   {$ENDIF}
  24. begin
  25.   {$IFDEF WINDOWS}
  26.   NextFrameCounts := FFrameCountsBegin + FCountsPerFrame;
  27.  
  28.   repeat
  29.     CurrentCounts := GetHardwareCounterValue();
  30.   until CurrentCounts >= NextFrameCounts;
  31.   {$ELSE}
  32.   WaitTime := FFrameCountsBegin + FCountsPerFrame - FFrameCountsEnd;
  33.  
  34.   if WaitTime > 0 then
  35.   begin
  36.     RemainingTime.tv_sec := WaitTime div 1000000000;
  37.     RemainingTime.tv_nsec := WaitTime mod 1000000000;
  38.  
  39.     repeat
  40.       RequestedTime := RemainingTime;
  41.  
  42.       if FPNanoSleep(@RequestedTime, @RemainingTime) = 0 then Exit;
  43.       if FPGetErrNo() <> ESysEINTR then Exit;
  44.     until False;
  45.   end;
  46.   {$ENDIF}
  47. end;
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018