Shinmera / precise-time

Precise time measurements
https://shinmera.github.io/precise-time/
zlib License
3 stars 0 forks source link

GET-MONOTONIC-TIME fails #1

Open jasondemps1 opened 3 months ago

jasondemps1 commented 3 months ago

While working with Trial, I discovered this issue with get-monotonic-time failing each call and not exactly sure why as it's just calling out via cffi. What's odd is get-precise-time seems to work just fine. I'm currently running on a MacBook Air M1 with SBCL 2.4.5.

The full output is below:

The time query for GET-MONOTONIC-TIME failed
   [Condition of type ORG.SHIRAKUMO.PRECISE-TIME:QUERY-FAILED]

Restarts:
 0: [RETRY] Retry SLY interactive evaluation request.
 1: [*PROCESS-INPUT] Continue reading input.
 2: [ABORT] Return to SLY's top level.
 3: [CLOSE-CONNECTION] Close SLY connection.
 4: [ABORT] Exit debugger, returning to top level.

Backtrace:
 0: (ORG.SHIRAKUMO.PRECISE-TIME:GET-MONOTONIC-TIME)
 1: (SB-INT:SIMPLE-EVAL-IN-LEXENV (ORG.SHIRAKUMO.PRECISE-TIME:GET-MONOTONIC-TIME) #<NULL-LEXENV>)
 2: (EVAL (ORG.SHIRAKUMO.PRECISE-TIME:GET-MONOTONIC-TIME))
 3: ((LAMBDA NIL :IN SLYNK:INTERACTIVE-EVAL))
Shinmera commented 3 months ago

Please compile and post the output of:

#include<stdio.h>
#include<time.h>
#define member_size(type, member) (sizeof( ((type *)0)->member))
int main(){
  printf("timespec: %lu\n", sizeof(struct timespec));
  printf("timespec.secs: %lu\n", member_size(struct timespec, tv_sec));
  printf("timespec.nsecs: %lu\n", member_size(struct timespec, tv_nsec));
  return 0;
}
Shinmera commented 3 months ago

nvm that, seems macos doesn't support monotonic clock_gettime anymore... sigh.

Shinmera commented 3 months ago

Please try again with the latest patch and run the tests if you can.

fiddlerwoaroof commented 3 months ago

That fix produces a fraction locally, I modified MONOTONIC-TIME-UNITS-PER-SECOND like this and it worked, but I'm not sure if the patch is safe:

(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 (floor
                                 (/ (cffi:mem-aref tb :uint32 0)
                                    (cffi:mem-aref tb :uint32 1)))))
            (* 1000000000 ticks-to-nanos))
          (error "Failed to get time scale for monotonic time."))))
Shinmera commented 3 months ago

better to floor after the mul