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