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