Skip to content

Commit

Permalink
time.c: added missing CLOCK_*
Browse files Browse the repository at this point in the history
  • Loading branch information
catap authored and mascguy committed Aug 22, 2023
1 parent bb525c1 commit 2d25480
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
35 changes: 28 additions & 7 deletions include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,35 @@ __MP__END_DECLS
#if __MP_LEGACY_SUPPORT_GETTIME__

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

#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 6
#define CLOCK_MONOTONIC_RAW 4
#if !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC)
typedef int clockid_t;
#endif /* !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC) */

#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME 0
#endif
#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC 6
#endif

#ifndef CLOCK_MONOTONIC_RAW
#define CLOCK_MONOTONIC_RAW 4
#endif
#ifndef CLOCK_MONOTONIC_RAW_APPROX
#define CLOCK_MONOTONIC_RAW_APPROX 5
#endif

#ifndef CLOCK_UPTIME_RAW
#define CLOCK_UPTIME_RAW 8
#endif

#ifndef CLOCK_UPTIME_RAW_APPROX
#define CLOCK_UPTIME_RAW_APPROX 8
#endif

#ifndef CLOCK_PROCESS_CPUTIME_ID
#define CLOCK_PROCESS_CPUTIME_ID 12
#endif

__MP__BEGIN_DECLS

Expand All @@ -58,8 +81,6 @@ extern int clock_getres ( clockid_t clk_id, struct timespec *ts );

__MP__END_DECLS

#endif /* !defined(CLOCK_REALTIME) && !defined(CLOCK_MONOTONIC) */

#endif /* _MP_LEGACY_SUPPORT_GETTIME__ */

/* Legacy implementation of timespec */
Expand Down
29 changes: 21 additions & 8 deletions src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <sys/time.h>
#include <sys/sysctl.h>
#include <sys/resource.h>
#include <mach/mach_time.h>

#define BILLION 1000000000L
Expand All @@ -44,19 +45,26 @@ int clock_gettime( clockid_t clk_id, struct timespec *ts )
struct timeval boottime;
size_t boottime_len = sizeof(boottime);
ret = sysctlbyname("kern.boottime", &boottime, &boottime_len, NULL, 0);
if (ret == -1)
{
boottime.tv_sec = 0;
boottime.tv_usec = 0;
}
if (ret != KERN_SUCCESS) { return ret; }
struct timeval tv;
ret = gettimeofday(&tv, NULL);
timersub(&tv, &boottime, &tv);
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
ret = 0;
}
else if ( CLOCK_MONOTONIC_RAW == clk_id )
else if ( CLOCK_PROCESS_CPUTIME_ID == clk_id )
{
struct rusage ru;
ret = getrusage(RUSAGE_SELF, &ru);
timeradd(&ru.ru_utime, &ru.ru_stime, &ru.ru_utime);
ts->tv_sec = ru.ru_utime.tv_sec;
ts->tv_nsec = ru.ru_utime.tv_usec * 1000;
}
else if ( CLOCK_MONOTONIC_RAW == clk_id ||
CLOCK_MONOTONIC_RAW_APPROX == clk_id ||
CLOCK_UPTIME_RAW == clk_id ||
CLOCK_UPTIME_RAW_APPROX == clk_id )
{
static mach_timebase_info_data_t timebase;
if ( 0 == timebase.numer || 0 == timebase.denom ) {
Expand All @@ -78,14 +86,19 @@ int clock_getres( clockid_t clk_id, struct timespec *ts )
if ( ts )
{
if ( CLOCK_REALTIME == clk_id ||
CLOCK_MONOTONIC == clk_id )
CLOCK_MONOTONIC == clk_id ||
CLOCK_PROCESS_CPUTIME_ID == clk_id )
{
// return 1us precision
ts->tv_sec = 0;
ts->tv_nsec = THOUSAND;
ret = 0;
}
else if ( CLOCK_MONOTONIC_RAW == clk_id )
else if ( CLOCK_MONOTONIC_RAW == clk_id ||
CLOCK_MONOTONIC_RAW_APPROX == clk_id ||
CLOCK_PROCESS_CPUTIME_ID == clk_id ||
CLOCK_UPTIME_RAW == clk_id ||
CLOCK_UPTIME_RAW_APPROX == clk_id )
{
// return 1ns precision
ts->tv_sec = 0;
Expand Down
34 changes: 33 additions & 1 deletion test/test_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,45 @@ int main()
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;
}
}
{
int c = 0;
res(CLOCK_MONOTONIC_RAW_APPROX);
while ( ++c < 10 )
{
std::cout << "CLOCK_MONOTONIC_RAW_APPROX ("<< CLOCK_MONOTONIC_RAW_APPROX << ") " << time(CLOCK_MONOTONIC_RAW_APPROX) << std::endl;
}
}
{
int c = 0;
res(CLOCK_UPTIME_RAW);
while ( ++c < 10 )
{
std::cout << "CLOCK_UPTIME_RAW ("<< CLOCK_UPTIME_RAW << ") " << time(CLOCK_UPTIME_RAW) << std::endl;
}
}
{
int c = 0;
res(CLOCK_UPTIME_RAW_APPROX);
while ( ++c < 10 )
{
std::cout << "CLOCK_UPTIME_RAW_APPROX ("<< CLOCK_UPTIME_RAW_APPROX << ") " << time(CLOCK_UPTIME_RAW_APPROX) << std::endl;
}
}
{
int c = 0;
res(CLOCK_PROCESS_CPUTIME_ID);
while ( ++c < 10 )
{
std::cout << "CLOCK_PROCESS_CPUTIME_ID ("<< CLOCK_PROCESS_CPUTIME_ID << ") " << time(CLOCK_PROCESS_CPUTIME_ID) << std::endl;
}
}
return 0;
}

0 comments on commit 2d25480

Please sign in to comment.