1 | // Copyright (C) 2011-2014 ChaosForge Ltd
|
---|
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.
|
---|
6 |
|
---|
7 | #include "nv/core/time.hh"
|
---|
8 |
|
---|
9 | #include "nv/core/logging.hh"
|
---|
10 |
|
---|
11 | #include <ctime>
|
---|
12 |
|
---|
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
|
---|
19 | #if 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>
|
---|
29 | #include <sys/time.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
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 |
|
---|
57 | nv::uint64 nv::get_ticks()
|
---|
58 | {
|
---|
59 | #if NV_COMPILER == NV_MSVC
|
---|
60 | return __rdtsc();
|
---|
61 | #else
|
---|
62 | register long long ticks asm("eax") = 0;
|
---|
63 | asm volatile (".byte 15, 49" : : : "eax", "edx");
|
---|
64 | return static_cast<nv::uint64>( ticks );
|
---|
65 | #endif
|
---|
66 | }
|
---|
67 |
|
---|
68 | void nv::sleep( uint32 ms )
|
---|
69 | {
|
---|
70 | #if NV_COMPILER == NV_MSVC
|
---|
71 | Sleep( ms );
|
---|
72 | #else
|
---|
73 | #if NV_PLATFORM == NV_WINDOWS
|
---|
74 | Sleep( ms );
|
---|
75 | #else
|
---|
76 | struct timespec ts;
|
---|
77 | ts.tv_sec = 0;
|
---|
78 | ts.tv_nsec = (long)ms * 1000000;
|
---|
79 | nanosleep(&ts, NULL);
|
---|
80 | // usleep( ms * 1000 );
|
---|
81 | #endif
|
---|
82 | #endif
|
---|
83 | }
|
---|
84 |
|
---|
85 | nv::uint32 nv::get_cpu_ms()
|
---|
86 | {
|
---|
87 | return static_cast<uint32>( static_cast<f32>( clock() - zero_timer.clock_zero ) / ( static_cast<f32>(CLOCKS_PER_SEC) / 1000.0f ) ) ;
|
---|
88 | }
|
---|
89 |
|
---|
90 | nv::uint64 nv::get_cpu_us()
|
---|
91 | {
|
---|
92 | return static_cast<uint64>( static_cast<f32>( clock() - zero_timer.clock_zero ) / ( static_cast<f32>(CLOCKS_PER_SEC) / 1000000.0f ) ) ;
|
---|
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;
|
---|
101 | return static_cast<uint32>(1000.0 * result / static_cast<f64>( zero_timer.frequency.QuadPart ) );
|
---|
102 | #else
|
---|
103 | struct timeval now;
|
---|
104 | gettimeofday(&now, NULL);
|
---|
105 | return static_cast<uint32>( (now.tv_sec - zero_timer.timeval_zero.tv_sec)*1000+(now.tv_usec-zero_timer.timeval_zero.tv_usec)/1000 );
|
---|
106 | #endif
|
---|
107 | }
|
---|
108 |
|
---|
109 | nv::uint64 nv::get_system_us()
|
---|
110 | {
|
---|
111 | #if NV_COMPILER == NV_MSVC
|
---|
112 | LARGE_INTEGER now;
|
---|
113 | QueryPerformanceCounter(&now);
|
---|
114 | LONGLONG result = now.QuadPart - zero_timer.query_zero.QuadPart;
|
---|
115 | return static_cast<uint64>(1000000.0 * result / static_cast<f64>( zero_timer.frequency.QuadPart ) );
|
---|
116 | #else
|
---|
117 | struct timeval now;
|
---|
118 | gettimeofday(&now, NULL);
|
---|
119 | return static_cast<uint64>( (now.tv_sec - zero_timer.timeval_zero.tv_sec)*1000000+(now.tv_usec - zero_timer.timeval_zero.tv_usec) );
|
---|
120 | #endif
|
---|
121 | }
|
---|