[319] | 1 | // Copyright (C) 2011-2014 ChaosForge Ltd
|
---|
[34] | 2 | // This file is part of NV Libraries.
|
---|
| 3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 4 |
|
---|
[319] | 5 | #include "nv/core/time.hh"
|
---|
[34] | 6 |
|
---|
[319] | 7 | #include "nv/core/logging.hh"
|
---|
[34] | 8 |
|
---|
[376] | 9 | #include <ctime>
|
---|
| 10 |
|
---|
[34] | 11 | #if NV_COMPILER == NV_MSVC
|
---|
| 12 | #define WIN32_LEAN_AND_MEAN
|
---|
| 13 | #include <windows.h>
|
---|
| 14 | #include <intrin.h>
|
---|
| 15 | #pragma intrinsic(__rdtsc)
|
---|
| 16 | #else
|
---|
[376] | 17 | #if NV_COMPILER == NV_GNUC && NV_PLATFORM == NV_WINDOWS
|
---|
| 18 | // mingw doesn't have usleep nor nanosleep...
|
---|
| 19 | #include <windows.h>
|
---|
| 20 | #endif
|
---|
| 21 | // #include <unistd.h>
|
---|
| 22 | // #include <time.h>
|
---|
| 23 | // #include <sys/time.h>
|
---|
| 24 | // #include <sys/types.h>
|
---|
| 25 | #include <stdio.h>
|
---|
| 26 | #include <time.h>
|
---|
[34] | 27 | #include <sys/time.h>
|
---|
| 28 | #endif
|
---|
| 29 |
|
---|
| 30 |
|
---|
[376] | 31 |
|
---|
[34] | 32 | struct timer_impl
|
---|
| 33 | {
|
---|
| 34 | timer_impl()
|
---|
| 35 | {
|
---|
| 36 | clock_zero = clock();
|
---|
| 37 | #if NV_COMPILER == NV_MSVC
|
---|
| 38 | QueryPerformanceFrequency(&frequency);
|
---|
| 39 | QueryPerformanceCounter(&query_zero);
|
---|
| 40 | #else
|
---|
| 41 | gettimeofday(&timeval_zero, NULL);
|
---|
| 42 | #endif
|
---|
| 43 | }
|
---|
| 44 | clock_t clock_zero;
|
---|
| 45 | #if NV_COMPILER == NV_MSVC
|
---|
| 46 | LARGE_INTEGER query_zero;
|
---|
| 47 | LARGE_INTEGER frequency;
|
---|
| 48 | #else
|
---|
| 49 | struct timeval timeval_zero;
|
---|
| 50 | #endif
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | static timer_impl zero_timer;
|
---|
| 54 |
|
---|
[121] | 55 | nv::uint64 nv::get_ticks()
|
---|
[34] | 56 | {
|
---|
| 57 | #if NV_COMPILER == NV_MSVC
|
---|
| 58 | return __rdtsc();
|
---|
[121] | 59 | #else
|
---|
| 60 | register long long ticks asm("eax") = 0;
|
---|
[34] | 61 | asm volatile (".byte 15, 49" : : : "eax", "edx");
|
---|
[121] | 62 | return static_cast<nv::uint64>( ticks );
|
---|
[34] | 63 | #endif
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void nv::sleep( uint32 ms )
|
---|
| 67 | {
|
---|
| 68 | #if NV_COMPILER == NV_MSVC
|
---|
| 69 | Sleep( ms );
|
---|
[121] | 70 | #else
|
---|
[376] | 71 | #if NV_COMPILER == NV_GNUC && NV_PLATFORM == NV_WINDOWS
|
---|
| 72 | Sleep( ms );
|
---|
| 73 | #else
|
---|
| 74 | struct timespec ts;
|
---|
| 75 | ts.tv_sec = 0;
|
---|
[380] | 76 | ts.tv_nsec = (long)ms * 1000000;
|
---|
[376] | 77 | nanosleep(&ts, NULL);
|
---|
| 78 | // usleep( ms * 1000 );
|
---|
[34] | 79 | #endif
|
---|
[376] | 80 | #endif
|
---|
[34] | 81 | }
|
---|
| 82 |
|
---|
| 83 | nv::uint32 nv::get_cpu_ms()
|
---|
| 84 | {
|
---|
| 85 | return (uint32)( (f32)( clock() - zero_timer.clock_zero ) / ( (f32)CLOCKS_PER_SEC / 1000.0 ) ) ;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[149] | 88 | nv::uint64 nv::get_cpu_us()
|
---|
[34] | 89 | {
|
---|
[149] | 90 | return (uint64)( (f32)( clock() - zero_timer.clock_zero ) / ( (f32)CLOCKS_PER_SEC / 1000000.0 ) ) ;
|
---|
[34] | 91 | }
|
---|
| 92 |
|
---|
| 93 | nv::uint32 nv::get_system_ms()
|
---|
| 94 | {
|
---|
| 95 | #if NV_COMPILER == NV_MSVC
|
---|
| 96 | LARGE_INTEGER now;
|
---|
| 97 | QueryPerformanceCounter(&now);
|
---|
| 98 | LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
|
---|
[149] | 99 | return (uint32) (1000.0 * result / (double) zero_timer.frequency.QuadPart);
|
---|
[34] | 100 | #else
|
---|
| 101 | struct timeval now;
|
---|
| 102 | gettimeofday(&now, NULL);
|
---|
[121] | 103 | return (uint32)( (now.tv_sec - zero_timer.timeval_zero.tv_sec)*1000+(now.tv_usec-zero_timer.timeval_zero.tv_usec)/1000 );
|
---|
[34] | 104 | #endif
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[149] | 107 | nv::uint64 nv::get_system_us()
|
---|
[34] | 108 | {
|
---|
| 109 | #if NV_COMPILER == NV_MSVC
|
---|
| 110 | LARGE_INTEGER now;
|
---|
| 111 | QueryPerformanceCounter(&now);
|
---|
| 112 | LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
|
---|
[149] | 113 | return (uint64) (1000000.0 * result / (double) zero_timer.frequency.QuadPart);
|
---|
[34] | 114 | #else
|
---|
| 115 | struct timeval now;
|
---|
| 116 | gettimeofday(&now, NULL);
|
---|
[121] | 117 | return (uint32)( (now.tv_sec - zero_timer.timeval_zero.tv_sec)*1000000+(now.tv_usec - zero_timer.timeval_zero.tv_usec) );
|
---|
[34] | 118 | #endif
|
---|
| 119 | }
|
---|