Skip to content

Commit 7573f1b

Browse files
committed
Added profiler per thread logging.
1 parent 58b8738 commit 7573f1b

File tree

6 files changed

+165
-36
lines changed

6 files changed

+165
-36
lines changed

src/drivers/Qt/AviRecord.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,7 @@ int FCEUD_AviGetFormatOpts( std::vector <std::string> &formatList )
25802580
AviRecordDiskThread_t::AviRecordDiskThread_t( QObject *parent )
25812581
: QThread(parent)
25822582
{
2583+
setObjectName( QString("AviRecordDiskThread") );
25832584
}
25842585
//----------------------------------------------------
25852586
AviRecordDiskThread_t::~AviRecordDiskThread_t(void)

src/drivers/Qt/ConsoleWindow.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ consoleWin_t::consoleWin_t(QWidget *parent)
125125

126126
printf("Running on Platform: %s\n", QGuiApplication::platformName().toStdString().c_str() );
127127

128+
QThread *thread = QThread::currentThread();
129+
130+
if (thread)
131+
{
132+
thread->setObjectName( QString("MainThread") );
133+
}
134+
128135
QApplication::setStyle( new fceuStyle() );
129136

130137
initHotKeys();
@@ -4604,6 +4611,9 @@ void consoleWin_t::updatePeriodic(void)
46044611

46054612
updateCounter++;
46064613

4614+
#ifdef __FCEU_PROFILER_ENABLE__
4615+
FCEU_profiler_log_thread_activity();
4616+
#endif
46074617
return;
46084618
}
46094619

@@ -4614,6 +4624,7 @@ emulatorThread_t::emulatorThread_t( QObject *parent )
46144624
pself = 0;
46154625
#endif
46164626

4627+
setObjectName( QString("EmulationThread") );
46174628
}
46184629

46194630
#if defined(__linux__)

src/drivers/Qt/TraceLogger.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -2507,6 +2507,7 @@ void QTraceLogView::paintEvent(QPaintEvent *event)
25072507
TraceLogDiskThread_t::TraceLogDiskThread_t( QObject *parent )
25082508
: QThread(parent)
25092509
{
2510+
setObjectName( QString("TraceLogDiskThread") );
25102511
}
25112512
//----------------------------------------------------
25122513
TraceLogDiskThread_t::~TraceLogDiskThread_t(void)

src/drivers/Qt/fceuWrapper.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "../../cheat.h"
5454
#include "../../movie.h"
5555
#include "../../state.h"
56+
#include "../../profiler.h"
5657
#include "../../version.h"
5758

5859
#ifdef _S9XLUA_H
@@ -1465,6 +1466,9 @@ int fceuWrapperUpdate( void )
14651466

14661467
emulatorHasMutex = 0;
14671468

1469+
#ifdef __FCEU_PROFILER_ENABLE__
1470+
FCEU_profiler_log_thread_activity();
1471+
#endif
14681472
while ( SpeedThrottle() )
14691473
{
14701474
// Input device processing is in main thread
@@ -1478,6 +1482,9 @@ int fceuWrapperUpdate( void )
14781482

14791483
emulatorHasMutex = 0;
14801484

1485+
#ifdef __FCEU_PROFILER_ENABLE__
1486+
FCEU_profiler_log_thread_activity();
1487+
#endif
14811488
msleep( 100 );
14821489
}
14831490
return 0;

src/profiler.cpp

+102-28
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
#include <stdio.h>
2525

26+
#ifdef __QT_DRIVER__
27+
#include <QThread>
28+
#endif
29+
2630
#if defined(__linux__) || defined(__APPLE__) || defined(__unix__)
2731
#include <unistd.h>
2832
#endif
@@ -37,6 +41,7 @@
3741

3842
namespace FCEU
3943
{
44+
static thread_local profileExecVector execList;
4045
static thread_local profilerFuncMap threadProfileMap;
4146

4247
FILE *profilerManager::pLog = nullptr;
@@ -64,10 +69,8 @@ void timeStampRecord::readNew(void)
6469
{
6570
#if defined(__linux__) || defined(__APPLE__) || defined(__unix__)
6671
clock_gettime( CLOCK_REALTIME, &ts );
67-
#elif defined(WIN32)
68-
QueryPerformanceCounter((LARGE_INTEGER*)&ts);
6972
#else
70-
ts = 0;
73+
QueryPerformanceCounter((LARGE_INTEGER*)&ts);
7174
#endif
7275
tsc = rdtsc();
7376
}
@@ -121,16 +124,19 @@ funcProfileRecord::funcProfileRecord(const char *fileNameStringLiteral,
121124
: fileLineNum(fileLineNumber), fileName(fileNameStringLiteral),
122125
funcName(funcNameStringLiteral), comment(commentStringLiteral)
123126
{
124-
min.zero();
127+
min.fromSeconds(9);
125128
max.zero();
126129
sum.zero();
127130
numCalls = 0;
128131
recursionCount = 0;
132+
133+
threadProfileMap.addRecord( fileNameStringLiteral, fileLineNumber,
134+
funcNameStringLiteral, commentStringLiteral, this);
129135
}
130136
//-------------------------------------------------------------------------
131137
void funcProfileRecord::reset(void)
132138
{
133-
min.zero();
139+
min.fromSeconds(9);
134140
max.zero();
135141
sum.zero();
136142
numCalls = 0;
@@ -149,20 +155,9 @@ double funcProfileRecord::average(void)
149155
//-------------------------------------------------------------------------
150156
//---- Profile Scoped Function Class
151157
//-------------------------------------------------------------------------
152-
profileFuncScoped::profileFuncScoped(const char *fileNameStringLiteral,
153-
const int fileLineNumber,
154-
const char *funcNameStringLiteral,
155-
const char *commentStringLiteral)
158+
profileFuncScoped::profileFuncScoped( funcProfileRecord *recordIn )
156159
{
157-
rec = nullptr;
158-
159-
//if (threadProfileMap == nullptr)
160-
//{
161-
// threadProfileMap = new profilerFuncMap();
162-
//}
163-
164-
rec = threadProfileMap.findRecord( fileNameStringLiteral, fileLineNumber,
165-
funcNameStringLiteral, commentStringLiteral, true);
160+
rec = recordIn;
166161

167162
if (rec)
168163
{
@@ -181,17 +176,70 @@ profileFuncScoped::~profileFuncScoped(void)
181176
ts.readNew();
182177
dt = ts - start;
183178

179+
rec->last = dt;
184180
rec->sum += dt;
185181
if (dt < rec->min) rec->min = dt;
186182
if (dt > rec->max) rec->max = dt;
187183

188184
rec->recursionCount--;
189185

190-
//printf("%s: %u %f %f %f %f\n", rec->funcName, rec->numCalls, dt.toSeconds(), rec->average(), rec->min.toSeconds(), rec->max.toSeconds());
186+
execList._vec.push_back(*rec);
187+
191188
threadProfileMap.popStack(rec);
192189
}
193190
}
194191
//-------------------------------------------------------------------------
192+
//---- Profile Execution Vector
193+
//-------------------------------------------------------------------------
194+
profileExecVector::profileExecVector(void)
195+
{
196+
_vec.reserve( 10000 );
197+
198+
char threadName[128];
199+
char fileName[256];
200+
201+
strcpy( threadName, "MainThread");
202+
203+
#ifdef __QT_DRIVER__
204+
QThread *thread = QThread::currentThread();
205+
206+
if (thread)
207+
{
208+
//printf("Thread: %s\n", thread->objectName().toStdString().c_str());
209+
strcpy( threadName, thread->objectName().toStdString().c_str());
210+
}
211+
#endif
212+
sprintf( fileName, "fceux-profile-%s.log", threadName);
213+
214+
logFp = ::fopen(fileName, "w");
215+
216+
if (logFp == nullptr)
217+
{
218+
printf("Error: Failed to create profiler logfile: %s\n", fileName);
219+
}
220+
}
221+
//-------------------------------------------------------------------------
222+
profileExecVector::~profileExecVector(void)
223+
{
224+
if (logFp)
225+
{
226+
::fclose(logFp);
227+
}
228+
}
229+
//-------------------------------------------------------------------------
230+
void profileExecVector::update(void)
231+
{
232+
size_t n = _vec.size();
233+
234+
for (size_t i=0; i<n; i++)
235+
{
236+
funcProfileRecord &rec = _vec[i];
237+
238+
fprintf( logFp, "%s: %u %f %f %f %f\n", rec.funcName, rec.numCalls, rec.last.toSeconds(), rec.average(), rec.min.toSeconds(), rec.max.toSeconds());
239+
}
240+
_vec.clear();
241+
}
242+
//-------------------------------------------------------------------------
195243
//---- Profile Function Record Map
196244
//-------------------------------------------------------------------------
197245
profilerFuncMap::profilerFuncMap(void)
@@ -207,15 +255,15 @@ profilerFuncMap::~profilerFuncMap(void)
207255
//printf("profilerFuncMap Destructor: %p\n", this);
208256
pMgr.removeThreadProfiler(this);
209257

210-
{
211-
autoScopedLock aLock(_mapMtx);
258+
//{
259+
// autoScopedLock aLock(_mapMtx);
212260

213-
for (auto it = _map.begin(); it != _map.end(); it++)
214-
{
215-
delete it->second;
216-
}
217-
_map.clear();
218-
}
261+
// for (auto it = _map.begin(); it != _map.end(); it++)
262+
// {
263+
// delete it->second;
264+
// }
265+
// _map.clear();
266+
//}
219267
}
220268
//-------------------------------------------------------------------------
221269
void profilerFuncMap::pushStack(funcProfileRecord *rec)
@@ -228,6 +276,26 @@ void profilerFuncMap::popStack(funcProfileRecord *rec)
228276
stack.pop_back();
229277
}
230278
//-------------------------------------------------------------------------
279+
int profilerFuncMap::addRecord(const char *fileNameStringLiteral,
280+
const int fileLineNumber,
281+
const char *funcNameStringLiteral,
282+
const char *commentStringLiteral,
283+
funcProfileRecord *rec )
284+
{
285+
autoScopedLock aLock(_mapMtx);
286+
char lineString[64];
287+
288+
sprintf( lineString, ":%i", fileLineNumber);
289+
290+
std::string fname(fileNameStringLiteral);
291+
292+
fname.append( lineString );
293+
294+
_map[fname] = rec;
295+
296+
return 0;
297+
}
298+
//-------------------------------------------------------------------------
231299
funcProfileRecord *profilerFuncMap::findRecord(const char *fileNameStringLiteral,
232300
const int fileLineNumber,
233301
const char *funcNameStringLiteral,
@@ -363,6 +431,12 @@ int profilerManager::removeThreadProfiler( profilerFuncMap *m, bool shouldDestro
363431
return result;
364432
}
365433
//-------------------------------------------------------------------------
366-
//
434+
} // namespace FCEU
435+
436+
//-------------------------------------------------------------------------
437+
int FCEU_profiler_log_thread_activity(void)
438+
{
439+
FCEU::execList.update();
440+
return 0;
367441
}
368442
#endif // __FCEU_PROFILER_ENABLE__

0 commit comments

Comments
 (0)