Skip to content

Commit e597f6a

Browse files
committed
Moved time stamp functions to its own file. Use new common time stamp functions in Qt GUI timing.
1 parent a20f6f5 commit e597f6a

File tree

8 files changed

+360
-276
lines changed

8 files changed

+360
-276
lines changed

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ set(SRC_CORE
527527
${CMAKE_CURRENT_SOURCE_DIR}/utils/md5.cpp
528528
${CMAKE_CURRENT_SOURCE_DIR}/utils/memory.cpp
529529
${CMAKE_CURRENT_SOURCE_DIR}/utils/mutex.cpp
530+
${CMAKE_CURRENT_SOURCE_DIR}/utils/timeStamp.cpp
530531
)
531532

532533

src/drivers/Qt/ConsoleWindow.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "../../profiler.h"
5959
#include "../../version.h"
6060
#include "common/os_utils.h"
61+
#include "utils/timeStamp.h"
6162

6263
#ifdef _S9XLUA_H
6364
#include "../../fceulua.h"

src/drivers/Qt/sdl-throttle.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "Qt/sdl.h"
2424
#include "Qt/throttle.h"
25+
#include "utils/timeStamp.h"
2526

2627
#if defined(__linux__) || defined(__APPLE__) || defined(__unix__)
2728
#include <time.h>
@@ -60,21 +61,22 @@ extern bool turbo;
6061

6162
double getHighPrecTimeStamp(void)
6263
{
63-
#if defined(__linux__) || defined(__APPLE__) || defined(__unix__)
64-
struct timespec ts;
6564
double t;
6665

67-
clock_gettime( CLOCK_REALTIME, &ts );
68-
69-
t = (double)ts.tv_sec + (double)(ts.tv_nsec * 1.0e-9);
70-
#else
71-
double t;
66+
if (FCEU::timeStampModuleInitialized())
67+
{
68+
FCEU::timeStampRecord ts;
7269

73-
t = (double)SDL_GetTicks();
70+
ts.readNew();
7471

75-
t = t * 1e-3;
76-
#endif
72+
t = ts.toSeconds();
73+
}
74+
else
75+
{
76+
t = (double)SDL_GetTicks();
7777

78+
t = t * 1e-3;
79+
}
7880
return t;
7981
}
8082

src/profiler.cpp

-75
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,10 @@
2727
#include <QThread>
2828
#endif
2929

30-
#if defined(__linux__) || defined(__APPLE__) || defined(__unix__)
31-
#include <unistd.h>
32-
#endif
33-
3430
#include "utils/mutex.h"
3531
#include "fceu.h"
3632
#include "profiler.h"
3733

38-
#if defined(WIN32)
39-
#include <windows.h>
40-
#endif
41-
4234
namespace FCEU
4335
{
4436
static thread_local profileExecVector execList;
@@ -48,71 +40,6 @@ FILE *profilerManager::pLog = nullptr;
4840

4941
static profilerManager pMgr;
5042

51-
//-------------------------------------------------------------------------
52-
//---- Time Stamp Record
53-
//-------------------------------------------------------------------------
54-
#if defined(WIN32)
55-
uint64_t timeStampRecord::qpcFreq = 0;
56-
#include <intrin.h>
57-
#pragma intrinsic(__rdtsc)
58-
#else
59-
#include <x86intrin.h>
60-
#endif
61-
uint64_t timeStampRecord::tscFreq = 0;
62-
63-
static uint64_t rdtsc()
64-
{
65-
return __rdtsc();
66-
}
67-
68-
void timeStampRecord::readNew(void)
69-
{
70-
#if defined(__linux__) || defined(__APPLE__) || defined(__unix__)
71-
clock_gettime( CLOCK_REALTIME, &ts );
72-
#else
73-
QueryPerformanceCounter((LARGE_INTEGER*)&ts);
74-
#endif
75-
tsc = rdtsc();
76-
}
77-
78-
static void calibrateTSC(void)
79-
{
80-
constexpr int numSamples = 1;
81-
timeStampRecord t1, t2, td;
82-
uint64_t td_sum = 0;
83-
double td_avg;
84-
85-
#if defined(WIN32)
86-
if (QueryPerformanceFrequency((LARGE_INTEGER*)&timeStampRecord::qpcFreq) == 0)
87-
{
88-
printf("QueryPerformanceFrequency FAILED!\n");
89-
}
90-
#endif
91-
FCEU_printf("Running TSC Calibration: %i sec...\n", numSamples);
92-
93-
for (int i=0; i<numSamples; i++)
94-
{
95-
t1.readNew();
96-
#if defined(WIN32)
97-
Sleep(1000);
98-
#else
99-
sleep(1);
100-
#endif
101-
t2.readNew();
102-
103-
td += t2 - t1;
104-
105-
td_sum = td.tsc;
106-
107-
td_avg = static_cast<double>(td_sum);
108-
109-
timeStampRecord::tscFreq = static_cast<uint64_t>( td_avg / td.toSeconds() );
110-
111-
FCEU_printf("%i Calibration: %f sec TSC:%llu TSC Freq: %f MHz\n", i, td.toSeconds(),
112-
static_cast<unsigned long long>(td.tsc), static_cast<double>(timeStampRecord::tscFreq) * 1.0e-6 );
113-
}
114-
}
115-
11643
//-------------------------------------------------------------------------
11744
//---- Function Profile Record
11845
//-------------------------------------------------------------------------
@@ -371,8 +298,6 @@ profilerManager* profilerManager::getInstance(void)
371298
//-------------------------------------------------------------------------
372299
profilerManager::profilerManager(void)
373300
{
374-
calibrateTSC();
375-
376301
//printf("profilerManager Constructor\n");
377302
if (pLog == nullptr)
378303
{

src/profiler.h

+1-191
Original file line numberDiff line numberDiff line change
@@ -45,200 +45,10 @@
4545
#endif
4646

4747
#include "utils/mutex.h"
48+
#include "utils/timeStamp.h"
4849

4950
namespace FCEU
5051
{
51-
struct timeStampRecord
52-
{
53-
#if defined(__linux__) || defined(__APPLE__) || defined(__unix__)
54-
struct timespec ts;
55-
uint64_t tsc;
56-
57-
timeStampRecord(void)
58-
{
59-
ts.tv_sec = 0;
60-
ts.tv_nsec = 0;
61-
tsc = 0;
62-
}
63-
64-
timeStampRecord& operator = (const timeStampRecord& in)
65-
{
66-
ts = in.ts;
67-
tsc = in.tsc;
68-
return *this;
69-
}
70-
71-
timeStampRecord& operator += (const timeStampRecord& op)
72-
{
73-
ts.tv_sec += op.ts.tv_sec;
74-
ts.tv_nsec += op.ts.tv_nsec;
75-
76-
if (ts.tv_nsec >= 1000000000)
77-
{
78-
ts.tv_nsec -= 1000000000;
79-
ts.tv_sec++;
80-
}
81-
tsc += op.tsc;
82-
return *this;
83-
}
84-
85-
timeStampRecord operator + (const timeStampRecord& op)
86-
{
87-
timeStampRecord res;
88-
89-
res.ts.tv_sec = ts.tv_sec + op.ts.tv_sec;
90-
res.ts.tv_nsec = ts.tv_nsec + op.ts.tv_nsec;
91-
92-
if (res.ts.tv_nsec >= 1000000000)
93-
{
94-
res.ts.tv_nsec -= 1000000000;
95-
res.ts.tv_sec++;
96-
}
97-
res.tsc = tsc + op.tsc;
98-
return res;
99-
}
100-
101-
timeStampRecord operator - (const timeStampRecord& op)
102-
{
103-
timeStampRecord res;
104-
105-
res.ts.tv_sec = ts.tv_sec - op.ts.tv_sec;
106-
res.ts.tv_nsec = ts.tv_nsec - op.ts.tv_nsec;
107-
108-
if (res.ts.tv_nsec < 0)
109-
{
110-
res.ts.tv_nsec += 1000000000;
111-
res.ts.tv_sec--;
112-
}
113-
res.tsc = tsc - op.tsc;
114-
115-
return res;
116-
}
117-
118-
bool operator > (const timeStampRecord& op)
119-
{
120-
bool res = false;
121-
if (ts.tv_sec == op.ts.tv_sec)
122-
{
123-
res = (ts.tv_nsec > op.ts.tv_nsec);
124-
}
125-
else if (ts.tv_sec > op.ts.tv_sec)
126-
{
127-
res = true;
128-
}
129-
return res;
130-
}
131-
132-
bool operator < (const timeStampRecord& op)
133-
{
134-
bool res = false;
135-
if (ts.tv_sec == op.ts.tv_sec)
136-
{
137-
res = (ts.tv_nsec < op.ts.tv_nsec);
138-
}
139-
else if (ts.tv_sec < op.ts.tv_sec)
140-
{
141-
res = true;
142-
}
143-
return res;
144-
}
145-
146-
void zero(void)
147-
{
148-
ts.tv_sec = 0;
149-
ts.tv_nsec = 0;
150-
tsc = 0;
151-
}
152-
153-
void fromSeconds(unsigned int sec)
154-
{
155-
ts.tv_sec = sec;
156-
ts.tv_nsec = 0;
157-
tsc = 0;
158-
}
159-
160-
double toSeconds(void)
161-
{
162-
double sec = static_cast<double>(ts.tv_sec) + ( static_cast<double>(ts.tv_nsec) * 1.0e-9 );
163-
return sec;
164-
}
165-
#else // WIN32
166-
uint64_t ts;
167-
uint64_t tsc;
168-
169-
timeStampRecord(void)
170-
{
171-
ts = 0;
172-
tsc = 0;
173-
}
174-
175-
timeStampRecord& operator = (const timeStampRecord& in)
176-
{
177-
ts = in.ts;
178-
tsc = in.tsc;
179-
return *this;
180-
}
181-
182-
timeStampRecord& operator += (const timeStampRecord& op)
183-
{
184-
ts += op.ts;
185-
tsc += op.tsc;
186-
return *this;
187-
}
188-
189-
timeStampRecord operator + (const timeStampRecord& op)
190-
{
191-
timeStampRecord res;
192-
193-
res.ts = ts + op.ts;
194-
res.tsc = tsc + op.tsc;
195-
return res;
196-
}
197-
198-
timeStampRecord operator - (const timeStampRecord& op)
199-
{
200-
timeStampRecord res;
201-
202-
res.ts = ts - op.ts;
203-
res.tsc = tsc - op.tsc;
204-
205-
return res;
206-
}
207-
208-
bool operator > (const timeStampRecord& op)
209-
{
210-
return ts > op.ts;
211-
}
212-
213-
bool operator < (const timeStampRecord& op)
214-
{
215-
return ts < op.ts;
216-
}
217-
218-
void zero(void)
219-
{
220-
ts = 0;
221-
tsc = 0;
222-
}
223-
224-
void fromSeconds(unsigned int sec)
225-
{
226-
ts = sec * qpcFreq;
227-
tsc = 0;
228-
}
229-
230-
double toSeconds(void)
231-
{
232-
double sec = static_cast<double>(ts) / static_cast<double>(qpcFreq);
233-
return sec;
234-
}
235-
static uint64_t qpcFreq;
236-
#endif
237-
static uint64_t tscFreq;
238-
239-
void readNew(void);
240-
};
241-
24252
struct funcProfileRecord
24353
{
24454
const int fileLineNum;

0 commit comments

Comments
 (0)