Re: Monotonic clock

"David McClain (as dbm at refined-audiometrics dot com)" <[email protected]>
Newsgroups gmane.lisp.lispworks.general
Message-ID <[email protected]>
On my M1 iMac, mach_timebase_info returns the ratio 125/3. Multiply ticks by this ratio to get nanosec. Ticks are returned from mach_continuous_time().

And common sense would dictate that, indeed, ticks are arriving at a rate of 1 per ≈42 ns, or about 24 MHz rate. To be the inverse would demand that the realtime clock receives ticks at a rate of 42 GHz, which seems wholly unreasonable, given that the CPU clocks at around 4 GHz or lower.


> On Oct 9, 2025, at 08:14, David McClain (as dbm at refined-audiometrics dot com) <[email protected]> wrote:
> 
> Better yet, here is the direct code in Mac header file IOLib.h:
> 
> /*!
>  * @group Raw Mach Time API
>  * In general prefer to use the <time.h> API clock_gettime_nsec_np(3), which
>  * deals in the same clocks (and more) in ns units.
>  * Conversion of ns to (resp. from) tick units as returned by the mach time APIs
>  * is performed by division (resp. multiplication) with the fraction returned by
>  * mach_timebase_info().
>  */
> 
> struct mach_timebase_info {
> 	uint32_t numer;
> 	uint32_t denom;
> };
> 
> typedef struct mach_timebase_info *mach_timebase_info_t;
> typedef struct mach_timebase_info  mach_timebase_info_data_t;
> 
> /*!
>  * @function mach_timebase_info
>  * Returns fraction to multiply a value in mach tick units with to convert it
>  * to nanoseconds.
>  *
>  * @param info	Timebase fraction out parameter.
>  * @result		KERN_SUCCESS if `info` was filled in.
>  */
> kern_return_t
> mach_timebase_info(mach_timebase_info_t info);
> 
> 
> 
>> On Oct 9, 2025, at 08:08, David McClain (as dbm at refined-audiometrics dot com) <[email protected]> wrote:
>> 
>> I could be mistaken, but from my reading of the Apple docs, it appears that he has the ratio upside down:
>> 
>> https://github.com/Shinmera/precise-time/blob/main/darwin.lisp
>> 
>> 
>> (define-constant MONOTONIC-TIME-UNITS-PER-SECOND
>>     (cffi:with-foreign-objects ((tb :uint32 2))
>>       (if (= 0 (cffi:foreign-funcall "mach_timebase_info" :pointer tb :int))
>>           (let ((ticks-to-nanos (/ (cffi:mem-aref tb :uint32 1) (cffi:mem-aref tb :uint32 0))))
>>             (floor (* 1000000000 ticks-to-nanos)))
>>           (error "Failed to get time scale for monotonic time."))))
>> 
>> On my M1 iMac, mach_timebase_info returns the ratio 125/3. info numerator is at index 0, and denominator is at index 1.
>> 
>> 
>> 
>>> On Oct 9, 2025, at 07:39, Yacin Nadji (as yacin at defmacro dot cc) <[email protected]> wrote:
>>> 
>>>> On Oct 9, 2025, at 5:11 AM, Eugene Zaikonnikov (as ez at norphonic dot com) <[email protected]> wrote:
>>>> 
>>>> Hello,
>>>> 
>>>> Just wonder before I do it with FFI, is there already any support for
>>>> reading monotonic clock? Or perhaps a library that covers LispWorks too?
>>> 
>>> isn’t that what get-internal-real-time does or am i mistaken? if i am or you want a library and more precision maybe check out shinmera’s precise time <https://shinmera.github.io/precise-time/> library. 
>>> 
>>> cheers
>>> 
>>> yacin
>> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.