3

I am trying to write a helper method in iOS to allow me to use mach_absolute_time() into seconds (or nanoseconds), so that I can find the time derivative incoming device sensor data. Here is my attempt:

#include <mach/mach_time.h>

- (double) convertMachAbsoluteTimeIntoSeconds:(uint64_t) mach_time {
    mach_timebase_info_data_t _clock_timebase;
    double nanos = (mach_time * _clock_timebase.numer) / _clock_timebase.denom;
    return nanos / 10e9;
}

The above code is compiling but the current warning I am getting is that the 'right operand of '*' is a garbage value'. Plus, it isn't returning a sensible result in any case :/

From what I've read, mach_absolute_time() has some idiosyncrasies, so I figured that getting this working might be useful to others.

All help graciously received. Thanks

//// EDIT ////

Thanks to a super-fast answer from Rob Napier, here is the method with the initialization included:

#include <mach/mach_time.h>

- (double) convertMachAbsoluteTimeIntoSeconds:(uint64_t) mach_time {
    mach_timebase_info_data_t _clock_timebase;
    mach_timebase_info(&_clock_timebase); // Initialize timebase_info
    double nanos = (mach_time * _clock_timebase.numer) / _clock_timebase.denom;
    return nanos / 10e9;
}
Bart van Kuik
  • 4,704
  • 1
  • 33
  • 57
davwillev
  • 83
  • 10
  • 2
    FWIW, [`CACurrentMediaTime()`](https://developer.apple.com/documentation/quartzcore/1395996-cacurrentmediatime) returns `mach_absolute_time()` converted to seconds. – Rob Mar 23 '20 at 16:10
  • FYI, in the future, rather than editing your question to include the answer, feel free to just [post an answer to your own question](https://stackoverflow.com/help/self-answer). – Rob Mar 23 '20 at 16:13
  • Will do, Rob. I see you spotted my newbieness! – davwillev Mar 23 '20 at 18:36
  • 1
    Tagged it with "Objective-C" for completeness' sake. Interesting question! – Bart van Kuik Dec 22 '20 at 06:55

1 Answers1

5

You've declared _clock_timebase, but you never initialized it so it's just filled with garbage values on the stack. You meant the following:

mach_timebase_info_data_t _clock_timebase;
mach_timebase_info(&_clock_timebase); // Initialize timebase_info
...

You may find it more convenient to use AbsoluteToNanoseconds to do this for you. For full examples, see QA1398: Mach Absolute Time Units.

Note that mach_timebase_info is promised to be stable, so you can initialize this a single time if you like. (If it weren't stable, it'd be impossible to avoid race conditions on checking it with the existing API, so good thing there....)

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Ah, thanks so much for this! I shall look into using `AbsoluteToNanoseconds` too – davwillev Mar 23 '20 at 15:22
  • Just seen that `AbsoluteToNanoseconds` is deprecated! – davwillev Mar 23 '20 at 15:32
  • 1
    I know mach absolute time and use it frequently, to measure absolute-time differences, for performance/timing measurements. However- how to convert to NSDate? for some reason I can't find an answer to that. – Motti Shneor Dec 21 '20 at 18:59
  • @MottiShneor mach_time measures the number of ticks that have occurred while the system is awake. That's not particularly compatible with NSDate, which represents a specific instant in time. That means that the difference between two calls to mach_absolute_time is not directly convertible to a NSTimeInterval (which is the foundation of a NSDate). How to convert the values would depend on what you're actually trying to measure. – Rob Napier Dec 21 '20 at 20:15
  • I receive a series of events from the kernel (EndpointSecurity framework) that are timestamped with mach-absolute-time (and another mach-absolute-time as deadline for answering these events). I'd like to translate somehow to the "wall clock time" of the events... – Motti Shneor Dec 22 '20 at 06:20
  • That's not possible in general. You can try to sync up what mach time "now" is and subtract to find how long ago it might have been, but you have to assume that the processor hasn't slept. mach time isn't a wall time. It a number of ticks on the processor. – Rob Napier Dec 22 '20 at 15:04