WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

Commit 8a62c87a authored by John Zhang's avatar John Zhang
Browse files

changed implementation of clock callback: use mach_absolute_time on macOS and...

changed implementation of clock callback: use mach_absolute_time on macOS and clock_gettime on Linux
parent c8367bdc
......@@ -16,17 +16,48 @@ limitations under the License.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include "callback.h"
#define DEFAULT_FLOAT_FORMAT_PREC 6
#define DEFAULT_FLOAT_FORMAT_PREC 6 // 1 microsec
#include <time.h>
#ifdef __MACH__
// Use mach_absolute_time on macOS
typedef uint64_t timestamp_t;
#include <mach/mach_time.h>
inline void get_nano_timestamp(timestamp_t* tstamp) {
*tstamp = mach_absolute_time();
}
double get_elapsed_time(timestamp_t t0, timestamp_t t1) {
mach_timebase_info_data_t tb;
timestamp_t elapsed;
uint64_t elapsed_nano;
mach_timebase_info(&tb);
elapsed = t1 - t0;
elapsed_nano = elapsed * tb.numer / tb.denom;
return ((double)elapsed_nano) * 1e-9;
}
#else
// Use clock_gettime on Linux
typedef struct timespec timestamp_t;
inline void get_nano_timestamp(timestamp_t* tstamp) {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, tstamp);
}
double get_elapsed_time(timestamp_t t0, timestamp_t t1) {
return ((double)(t1.tv_sec - t0.tv_sec)) +
((double)(t1.tv_nsec - t0.tv_nsec)) * 1e-9;
}
#endif
// records a series of readings
struct Callback {
int fltfmtprec; // float format precision
clock_t t; // begin time / time diff
int fltfmtprec; // float format precision
timestamp_t t0; // begin time
timestamp_t t1; // end time
};
typedef struct Callback Callback;
......@@ -43,17 +74,17 @@ struct Callback *cb_init(const char *param_s) {
// NOTE: assuming current strategy of only one data point per run.
void cb_begin(struct Callback *cb, void *data) {
// call time measurement function
cb->t = clock();
get_nano_timestamp(&cb->t0);
}
void cb_end(struct Callback *cb, void *data) {
cb->t = clock() - cb->t;
get_nano_timestamp(&cb->t1);
}
void cb_report(struct Callback *cb) {
char fmtstr [6]; // NOTE: assume precision number is at most two digit
sprintf(fmtstr, "%%.%dlf", cb->fltfmtprec);
printf(fmtstr, ((double)cb->t) / ((double)CLOCKS_PER_SEC));
printf(fmtstr, get_elapsed_time(cb->t0, cb->t1));
printf("\n");
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment