Recent

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

furious programming

  • Sr. Member
  • ****
  • Posts: 312
  • I click a little.
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 2.0.2 with FPC 3.0.4, Windows XP (all 32-bit)

sstvmaster

  • Jr. Member
  • **
  • Posts: 95
« Last Edit: April 18, 2019, 12:34:18 am by sstvmaster »
Lazarus 2.0.2 x32
Lazarus 2.1.0 Trunk x32
OS Win 7 32bit

furious programming

  • Sr. Member
  • ****
  • Posts: 312
  • I click a little.
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 2.0.2 with FPC 3.0.4, Windows XP (all 32-bit)

Lainz

  • Hero Member
  • *****
  • Posts: 3085
    • 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

  • Sr. Member
  • ****
  • Posts: 312
  • I click a little.
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 2.0.2 with FPC 3.0.4, Windows XP (all 32-bit)

Thausand

  • Full Member
  • ***
  • Posts: 227
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

  • Sr. Member
  • ****
  • Posts: 312
  • I click a little.
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 2.0.2 with FPC 3.0.4, Windows XP (all 32-bit)

Basile-B

  • Sr. Member
  • ****
  • Posts: 457
    • GH...
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

  • Sr. Member
  • ****
  • Posts: 312
  • I click a little.
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 2.0.2 with FPC 3.0.4, Windows XP (all 32-bit)

furious programming

  • Sr. Member
  • ****
  • Posts: 312
  • I click a little.
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 2.0.2 with FPC 3.0.4, Windows XP (all 32-bit)

Lainz

  • Hero Member
  • *****
  • Posts: 3085
    • 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

  • Full Member
  • ***
  • Posts: 227
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

  • Sr. Member
  • ****
  • Posts: 312
  • I click a little.
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 2.0.2 with FPC 3.0.4, Windows XP (all 32-bit)

ASBzone

  • Full Member
  • ***
  • Posts: 225
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
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.0.3 r61159 / FPC v3.2.0-beta-r42011 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1809 (Build 17763.379)
Other Systems: Windows 10 Pro x64, Version 1803 or greater

furious programming

  • Sr. Member
  • ****
  • Posts: 312
  • I click a little.
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 2.0.2 with FPC 3.0.4, Windows XP (all 32-bit)