Skip to content

Commit b8cac55

Browse files
committed
Added get_monotonic_time to support pre-Sierra OS/X
1 parent 478f414 commit b8cac55

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

csdr.c

+27-11
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,26 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5353
#include "fastddc.h"
5454
#include <assert.h>
5555

56-
#ifdef __APPLE__
57-
#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
56+
#ifdef __MACH__
57+
#include <mach/clock.h>
58+
#include <mach/mach.h>
5859
#endif
5960

61+
// Use clock_gettime in linux, clock_get_time in OS X.
62+
void get_monotonic_time(struct timespec *ts){
63+
#ifdef __MACH__
64+
clock_serv_t cclock;
65+
mach_timespec_t mts;
66+
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
67+
clock_get_time(cclock, &mts);
68+
mach_port_deallocate(mach_task_self(), cclock);
69+
ts->tv_sec = mts.tv_sec;
70+
ts->tv_nsec = mts.tv_nsec;
71+
#else
72+
clock_gettime(CLOCK_MONOTONIC_RAW, ts);
73+
#endif
74+
}
75+
6076
char usage[]=
6177
"csdr - a simple commandline tool for Software Defined Radio receiver DSP.\n\n"
6278
"usage: \n\n"
@@ -1803,15 +1819,15 @@ int main(int argc, char *argv[])
18031819
//initialize FFT library, and measure time
18041820
errhead(); fprintf(stderr,"initializing... ");
18051821
struct timespec start_time, end_time;
1806-
clock_gettime(CLOCK_MONOTONIC_RAW, &start_time);
1822+
get_monotonic_time(&start_time);
18071823
FFT_PLAN_T* plan=make_fft_c2c(fft_size,input,output,1,benchmark);
1808-
clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
1824+
get_monotonic_time(&end_time);
18091825
fprintf(stderr,"done in %g seconds.\n",TIME_TAKEN(start_time,end_time));
18101826

18111827
//do the actual measurement about the FFT
1812-
clock_gettime(CLOCK_MONOTONIC_RAW, &start_time);
1828+
get_monotonic_time(&start_time);
18131829
for(int i=0;i<fft_cycles;i++) fft_execute(plan);
1814-
clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
1830+
get_monotonic_time(&end_time);
18151831
float time_taken_fft = TIME_TAKEN(start_time,end_time);
18161832
errhead(); fprintf(stderr,"%d transforms of %d processed in %g seconds, %g seconds each.\n",fft_cycles,fft_size,time_taken_fft,time_taken_fft/fft_cycles);
18171833
return 0;
@@ -2014,11 +2030,11 @@ int main(int argc, char *argv[])
20142030
if(flowcontrol_is_buffering)
20152031
{
20162032
fprintf(stderr, "flowcontrol: buffering, flowcontrol_bufindex = %d\n", flowcontrol_bufindex);
2017-
if(flowcontrol_bufindex==flowcontrol_bufsize) { flowcontrol_is_buffering = 0; clock_gettime(CLOCK_MONOTONIC_RAW, &start_time); }
2033+
if(flowcontrol_bufindex==flowcontrol_bufsize) { flowcontrol_is_buffering = 0; get_monotonic_time(&start_time); }
20182034
else if(read_return<=0) continue;
20192035
}
20202036
else {
2021-
clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
2037+
get_monotonic_time(&end_time);
20222038
int thrust_added=0;
20232039
while( (all_bytes_written+thrust*flowcontrol_readsize) / TIME_TAKEN(start_time,end_time) < data_rate )
20242040
{
@@ -2027,7 +2043,7 @@ int main(int argc, char *argv[])
20272043
//if(!(test++%10)) fprintf(stderr, "abw=%g\n", all_bytes_written / TIME_TAKEN(start_time,end_time));
20282044
/*if(!thrust_added && TIME_TAKEN(start_time,end_time)>50)
20292045
{
2030-
clock_gettime(CLOCK_MONOTONIC_RAW, &start_time);
2046+
get_monotonic_time(&start_time);
20312047
all_bytes_written=0;
20322048
}*/
20332049
while(all_bytes_written>data_rate && TIME_TAKEN(start_time,end_time)>1)
@@ -2073,11 +2089,11 @@ int main(int argc, char *argv[])
20732089
if(!time_now_sec)
20742090
{
20752091
time_now_sec=1;
2076-
clock_gettime(CLOCK_MONOTONIC_RAW, &start_time);
2092+
get_monotonic_time(&start_time);
20772093
}
20782094
else
20792095
{
2080-
clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
2096+
get_monotonic_time(&end_time);
20812097
float timetaken;
20822098
if(time_now_sec<(timetaken=TIME_TAKEN(start_time,end_time)))
20832099
{

0 commit comments

Comments
 (0)