Skip to content

Commit

Permalink
Add support for CLOCK_MONOTONIC_RAW to clock_gettime
Browse files Browse the repository at this point in the history
  • Loading branch information
cjones051073 committed Mar 26, 2023
1 parent c39f56b commit 88852aa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
7 changes: 4 additions & 3 deletions include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ __MP__END_DECLS
#if __MP_LEGACY_SUPPORT_GETTIME__

/* One define types and methods if not already defined. */
#if !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC)
#if !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC) && !defined(CLOCK_MONOTONIC_RAW)

#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 6
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 6
#define CLOCK_MONOTONIC_RAW 4
typedef int clockid_t;

__MP__BEGIN_DECLS
Expand Down
51 changes: 23 additions & 28 deletions src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include <sys/time.h>
#include <mach/mach_time.h>

#define BILLION 1000000000L
#define MILLION 1000000L
#define THOUSAND 1000L

int clock_gettime( clockid_t clk_id, struct timespec *ts )
{
int ret = -1;
Expand All @@ -34,21 +38,24 @@ int clock_gettime( clockid_t clk_id, struct timespec *ts )
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
}
else if ( CLOCK_MONOTONIC == clk_id )
else if ( CLOCK_MONOTONIC == clk_id || CLOCK_MONOTONIC_RAW == clk_id )
{
const uint64_t t = mach_absolute_time();
const uint64_t clock = mach_absolute_time();
mach_timebase_info_data_t timebase;
mach_timebase_info(&timebase);
const uint64_t tdiff = t * timebase.numer / timebase.denom;
ts->tv_sec = tdiff / 1000000000;
ts->tv_nsec = tdiff % 1000000000;
uint64_t tdiff = clock * timebase.numer / timebase.denom;
if ( CLOCK_MONOTONIC == clk_id ) {
tdiff = THOUSAND * ( tdiff / THOUSAND );
}
ts->tv_sec = tdiff / BILLION;
ts->tv_nsec = tdiff % BILLION;
ret = 0;
}
}
return ret;
}

int clock_getres ( clockid_t clk_id, struct timespec *ts )
int clock_getres( clockid_t clk_id, struct timespec *ts )
{
int ret = -1;
if ( ts )
Expand All @@ -58,32 +65,20 @@ int clock_getres ( clockid_t clk_id, struct timespec *ts )
{
// return 1us precision
ts->tv_sec = 0;
ts->tv_nsec = 1000;
ts->tv_nsec = THOUSAND;
ret = 0;
}
else if ( CLOCK_MONOTONIC_RAW == clk_id )
{
// return 1ns precision
ts->tv_sec = 0;
ts->tv_nsec = 1;
ret = 0;
}
}
return ret;
}

/*
#include <mach/clock.h>
#include <mach/mach.h>
int clock_gettime( int clk_id, struct timespec *ts )
{
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service( mach_host_self(), clk_id, &cclock );
const int ret = clock_get_time( cclock, &mts );
mach_port_deallocate( mach_task_self(), cclock );
if ( ts )
{
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
}
return ret;
}
*/

#endif

#if __MP_LEGACY_SUPPORT_TIMESPEC_GET__
Expand All @@ -92,9 +87,9 @@ int timespec_get(struct timespec *ts, int base)
{
switch (base) {
case TIME_UTC:
if (clock_gettime(CLOCK_REALTIME, ts) == -1)
if (clock_gettime(CLOCK_REALTIME, ts) == -1) {
return 0;

}
return base;

default:
Expand Down
31 changes: 20 additions & 11 deletions test/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,29 @@ inline void res( CLOCKID clk_id )

int main()
{

int c(0);
res(CLOCK_REALTIME);
while ( ++c < 10 )
{
std::cout << "CLOCK_REALTIME " << time(CLOCK_REALTIME) << std::endl;
int c =0;
res(CLOCK_REALTIME);
while ( ++c < 10 )
{
std::cout << "CLOCK_REALTIME ("<< CLOCK_REALTIME << ") " << time(CLOCK_REALTIME) << std::endl;
}
}
c = 0;
res(CLOCK_MONOTONIC);
while ( ++c < 10 )
{
std::cout << "CLOCK_MONOTONIC " << time(CLOCK_MONOTONIC) << std::endl;
int c = 0;
res(CLOCK_MONOTONIC);
while ( ++c < 10 )
{
std::cout << "CLOCK_MONOTONIC ("<< CLOCK_MONOTONIC << ") " << time(CLOCK_MONOTONIC) << std::endl;
}
}
{
int c = 0;
res(CLOCK_MONOTONIC_RAW);
while ( ++c < 10 )
{
std::cout << "CLOCK_MONOTONIC_RAW ("<< CLOCK_MONOTONIC_RAW << ") " << time(CLOCK_MONOTONIC_RAW) << std::endl;
}
}

return 0;
}

0 comments on commit 88852aa

Please sign in to comment.