Forum > macOS / Mac OS X

High resolution timing - Apple Silicon

(1/3) > >>

AlanTheBeast:
I continue to be frustrated by the coarse timing generally available from the fpc sysutils GetTickCount64 function in MacOS.  (1ms).

I stumbled upon this article (link at bottom) and it suggests that nanosecond level timing can be achieved using a C++ package (that I have not delved into, C being a minor ability and C++ being only of flirting acquaintance or maybe a glance across a crowded room).

1) Anyone care to try to link or adapt for FPC?

2) Is this something that could be implemented in a simpler fashion at the compiler level?  Seems very useful to me - recognizing that it would not be generally portable - though the article suggests adaptability to Linux systems and possibly Windows/x86_64.

    a) A compiler directive to test for compatibility.
    b) a function to get the tick resolution (How many ticks per second)
    c) a function similar to GetTickCount64 ... call it GetTickCountHighResolution;

Unlike GetTickCount64 which is ms since Unix 0 date in the past (1 Jan, 1970 ish), this new function would be ticks since the computer started.


https://lemire.me/blog/2023/03/21/counting-cycles-and-instructions-on-arm-based-apple-systems/

gues1:
For Intel processor I had done that (in assembler) and use it in same cases, for ARM I don't know.
But I think that MacOS with arm still have the QPC (I mean high precision timer) that one can use.

It has to resolution of 0,1 microseconds and works in Hardware, not in software or inside the CPU.

This is a link where I attached a Unit in Pascal that works with Windows and Linux. Someone can adapt it for MacOs.

https://forum.lazarus.freepascal.org/index.php/topic,71540.msg558558.html#msg558558

Thaddy:
Modern Apple hardware does have hardware timers, but abstracted away in the OS with:
mach_absolute_time()  gives access to a continuously incrementing counter with nanosecond-level resolution.

clock_gettime()  POSIX-compliant and useful for portable high-resolution timing.

dispatch_time()  part of Grand Central Dispatch for scheduling.
In MacOS these are backed by the hardware timers.
I think you can not get at the hardware timers directly.

gues1:

--- Quote from: Thaddy on June 24, 2025, 05:51:38 pm ---Modern Apple hardware does have hardware timers, but abstracted away in the OS with:

--- End quote ---

Someone with MacOs hw can adapt the Diagnostics unit so it works with MacOs too, I think is a good thing.

I don't think that is necessary a lot of works, but would need a MacOs PC.

AlanTheBeast:

--- Quote from: Thaddy on June 24, 2025, 05:51:38 pm ---Modern Apple hardware does have hardware timers, but abstracted away in the OS with:
mach_absolute_time()  gives access to a continuously incrementing counter with nanosecond-level resolution.

clock_gettime()  POSIX-compliant and useful for portable high-resolution timing.

dispatch_time()  part of Grand Central Dispatch for scheduling.
In MacOS these are backed by the hardware timers.
I think you can not get at the hardware timers directly.

--- End quote ---

I don't mind much that it's "abstracted away" in the OS, it would still be far better than my current use case.

Is it possible to link to the kernel/mach API with a command line C compile?  Or does one have to slog into XCode?
(Yes - I'm completely lost in the woods here).

Is there a simple C example that I could attempt to link to? 
I'd create a useful unit from that if so (after stumbling around figuring out calls to C from Pascal - it's been a stretch).

Navigation

[0] Message Index

[#] Next page

Go to full version