提交 f7d7a10a 编写于 作者: V Vlad Ilyushchenko

Misc: Os.currentTimeNanos() Windows implementation

上级 1112a83e
......@@ -29,6 +29,7 @@
#include <sspi.h>
#include <issper16.h>
#include <rpc.h>
#include <sys/timeb.h>
#include "../share/os.h"
#include "errno.h"
#include "timer.h"
......@@ -132,6 +133,51 @@ JNIEXPORT jint JNICALL Java_com_questdb_std_Os_setCurrentThreadAffinity0
}
#define exp7 10000000LL //1E+7 //C-file part
#define exp9 1000000000LL //1E+9
#define w2ux 116444736000000000LL //1.jan1601 to 1.jan1970
void unix_time(struct timespec *spec) {
__int64 wintime;
GetSystemTimeAsFileTime((FILETIME *) &wintime);
wintime -= w2ux;
spec->tv_sec = wintime / exp7;
spec->tv_nsec = wintime % exp7 * 100;
}
int clock_gettime(struct timespec *spec) {
static struct timespec startspec;
static double ticks2nano;
static __int64 startticks, tps = 0;
__int64 tmp, curticks;
QueryPerformanceFrequency((LARGE_INTEGER *) &tmp);
if (tps != tmp) {
tps = tmp; //init ~~ONCE
// possibly change freq ?
QueryPerformanceCounter((LARGE_INTEGER *) &startticks);
unix_time(&startspec);
ticks2nano = (double) exp9 / tps;
}
QueryPerformanceCounter((LARGE_INTEGER *) &curticks);
curticks -= startticks;
spec->tv_sec = startspec.tv_sec + (curticks / tps);
spec->tv_nsec = (long) (startspec.tv_nsec + (double) (curticks % tps) * ticks2nano);
if (spec->tv_nsec >= exp9) {
spec->tv_sec++;
spec->tv_nsec -= exp9;
}
return 0;
}
JNIEXPORT jlong JNICALL Java_com_questdb_std_Os_currentTimeNanos
(JNIEnv *e, jclass cl) {
struct timespec spec;
clock_gettime(&spec);
return spec.tv_sec * 1000000000 + spec.tv_nsec;
}
JNIEXPORT void JNICALL Java_com_questdb_std_Os_freeKrbToken
(JNIEnv *e, jclass cl, jlong ptr) {
......
......@@ -61,7 +61,10 @@ public class OsTest {
public void testCurrentTimeNanos() {
long reference = System.currentTimeMillis();
long actual = Os.currentTimeNanos();
Assert.assertTrue(actual > 0);
long delta = actual / 1_000_000 - reference;
Assert.assertTrue(delta < 200);
System.out.println(reference);
System.out.println(actual);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册