avoid potentially wrong measurement when clock modified (#16)

This commit is contained in:
Danny Tsai 2023-09-26 20:16:27 +08:00 committed by GitHub
parent f91ce7ae6b
commit 0294bb4de8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 7 deletions

14
utils.c
View File

@ -1,3 +1,7 @@
#ifndef _WIN32
#define _POSIX_C_SOURCE 199309L
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -79,14 +83,14 @@ long long hp_time_diff(LARGE_INTEGER *pt0, LARGE_INTEGER *pt1) {
#else
void get_hp_time(struct timeval *pt) {
gettimeofday(pt, NULL);
void get_hp_time(struct timespec *pt) {
clock_gettime(CLOCK_MONOTONIC, pt);
}
long long hp_time_diff(struct timeval *pt0, struct timeval *pt1) {
long long hp_time_diff(struct timespec *pt0, struct timespec *pt1) {
long long diff = pt1->tv_sec - pt0->tv_sec;
diff *= 1000000;
diff += pt1->tv_usec - pt0->tv_usec;
diff += (pt1->tv_nsec - pt0->tv_nsec) / 1000;
return diff;
}
@ -195,4 +199,4 @@ void intHandler() {
// Not really needed for now.
stop_bfcl = 1;
exit(0);
}
}

View File

@ -18,8 +18,8 @@ typedef LARGE_INTEGER TimeHP;
#else
#include <sys/time.h>
typedef struct timeval TimeHP;
#include <time.h>
typedef struct timespec TimeHP;
void get_hp_time(TimeHP *pt);
#define LL "ll"