|
1
|
1 #include "pfc-lite.h"
|
|
|
2
|
|
|
3 #include "timers.h"
|
|
|
4 #include "debug.h"
|
|
|
5
|
|
|
6 #if defined(_WIN32) && defined(PFC_HAVE_PROFILER)
|
|
|
7 #include <ShlObj.h>
|
|
|
8 #endif
|
|
|
9
|
|
|
10 #ifndef _WIN32
|
|
|
11 #include "nix-objects.h"
|
|
|
12 #include <time.h>
|
|
|
13 #endif
|
|
|
14
|
|
|
15 namespace pfc {
|
|
|
16
|
|
|
17 #ifdef PFC_HAVE_PROFILER
|
|
|
18
|
|
|
19 profiler_static::profiler_static(const char * p_name)
|
|
|
20 {
|
|
|
21 name = p_name;
|
|
|
22 total_time = 0;
|
|
|
23 num_called = 0;
|
|
|
24 }
|
|
|
25
|
|
|
26 static void profilerMsg(const char* msg) {
|
|
|
27 #ifdef _WIN32
|
|
|
28 if (!IsDebuggerPresent()) {
|
|
|
29 static HANDLE hWriteTo = INVALID_HANDLE_VALUE;
|
|
|
30 static bool initialized = false;
|
|
|
31 if (!initialized) {
|
|
|
32 initialized = true;
|
|
|
33 wchar_t path[1024] = {};
|
|
|
34 if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_DESKTOP, NULL, SHGFP_TYPE_CURRENT, path))) {
|
|
|
35 size_t l = wcslen(path);
|
|
|
36 if (l > 0) {
|
|
|
37 if (path[l - 1] != '\\') {
|
|
|
38 wcscat_s(path, L"\\");
|
|
|
39 }
|
|
|
40 wchar_t fn[256];
|
|
|
41 wsprintf(fn, L"profiler-%u.txt", GetProcessId(GetCurrentProcess()));
|
|
|
42 wcscat_s(path, fn);
|
|
|
43 hWriteTo = CreateFile(path, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
|
|
|
44 }
|
|
|
45 }
|
|
|
46 }
|
|
|
47 if (hWriteTo != INVALID_HANDLE_VALUE) {
|
|
|
48 SetFilePointer(hWriteTo, 0, 0, SEEK_END);
|
|
|
49 pfc::string8 temp = msg;
|
|
|
50 temp += "\r\n";
|
|
|
51 DWORD written = 0;
|
|
|
52 WriteFile(hWriteTo, temp.c_str(), (DWORD) temp.length(), &written, NULL);
|
|
|
53 }
|
|
|
54 }
|
|
|
55 #endif
|
|
|
56 outputDebugLine(msg);
|
|
|
57 }
|
|
|
58
|
|
|
59 profiler_static::~profiler_static()
|
|
|
60 {
|
|
|
61 try {
|
|
|
62 pfc::string8 message;
|
|
|
63 message << "profiler: " << pfc::format_pad_left(48,' ',name) << " - " <<
|
|
|
64 pfc::format_pad_right(16,' ',pfc::format_uint(total_time) ) << " cycles";
|
|
|
65
|
|
|
66 if (num_called > 0) {
|
|
|
67 message << " (executed " << num_called << " times, " << (total_time / num_called) << " average)";
|
|
|
68 }
|
|
|
69 profilerMsg(message);
|
|
|
70 } catch(...) {
|
|
|
71 //should never happen
|
|
|
72 OutputDebugString(_T("unexpected profiler failure\n"));
|
|
|
73 }
|
|
|
74 }
|
|
|
75 #endif
|
|
|
76
|
|
|
77 #ifndef _WIN32
|
|
|
78
|
|
|
79 void hires_timer::start() {
|
|
|
80 m_start = nixGetTime();
|
|
|
81 }
|
|
|
82 double hires_timer::query() const {
|
|
|
83 return nixGetTime() - m_start;
|
|
|
84 }
|
|
|
85 double hires_timer::query_reset() {
|
|
|
86 double t = nixGetTime();
|
|
|
87 double r = t - m_start;
|
|
|
88 m_start = t;
|
|
|
89 return r;
|
|
|
90 }
|
|
|
91 pfc::string8 hires_timer::queryString(unsigned precision) const {
|
|
|
92 return format_time_ex( query(), precision ).get_ptr();
|
|
|
93 }
|
|
|
94 #endif
|
|
|
95
|
|
|
96
|
|
|
97 uint64_t fileTimeWtoU(uint64_t ft) {
|
|
|
98 return (ft - 116444736000000000 + /*rounding*/10000000/2) / 10000000;
|
|
|
99 }
|
|
|
100 uint64_t fileTimeUtoW(uint64_t ft) {
|
|
|
101 return (ft * 10000000) + 116444736000000000;
|
|
|
102 }
|
|
|
103 #ifndef _WIN32
|
|
|
104 uint64_t fileTimeUtoW(const timespec & ts) {
|
|
|
105 uint64_t ft = (uint64_t)ts.tv_sec * 10000000 + (uint64_t)ts.tv_nsec / 100;
|
|
|
106 return ft + 116444736000000000;
|
|
|
107 }
|
|
|
108 #endif
|
|
|
109
|
|
|
110 uint64_t fileTimeNow() {
|
|
|
111 #ifdef _WIN32
|
|
|
112 uint64_t ret;
|
|
|
113 GetSystemTimeAsFileTime((FILETIME*)&ret);
|
|
|
114 return ret;
|
|
|
115 #else // not _WIN32
|
|
|
116 timespec ts = {};
|
|
|
117 auto status = clock_gettime( CLOCK_REALTIME, &ts);
|
|
|
118 PFC_ASSERT( status == 0 ); (void) status;
|
|
|
119 return fileTimeUtoW(ts);
|
|
|
120 #endif // _WIN32 or not
|
|
|
121 }
|
|
|
122
|
|
|
123 }
|