2012-12-19 09:30:18 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* *
|
2015-11-10 05:21:49 +00:00
|
|
|
* Project64 - A Nintendo 64 emulator. *
|
2012-12-19 09:30:18 +00:00
|
|
|
* http://www.pj64-emu.com/ *
|
|
|
|
* Copyright (C) 2012 Project64. All rights reserved. *
|
|
|
|
* *
|
|
|
|
* License: *
|
|
|
|
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
|
|
|
|
* *
|
|
|
|
****************************************************************************/
|
2010-06-07 02:23:58 +00:00
|
|
|
#include "stdafx.h"
|
2016-01-13 15:18:10 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2016-01-05 05:58:31 +00:00
|
|
|
#include <Project64-core/N64System/ProfilingClass.h>
|
2015-12-06 09:59:58 +00:00
|
|
|
#include <Common/LogClass.h>
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
enum { MAX_FRAMES = 13 };
|
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
CProfiling::CProfiling() :
|
|
|
|
m_CurrentTimerAddr(Timer_None),
|
|
|
|
m_CurrentDisplayCount(MAX_FRAMES),
|
|
|
|
m_StartTimeHi(0),
|
|
|
|
m_StartTimeLo(0)
|
2012-10-05 09:18:02 +00:00
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
SPECIAL_TIMERS CProfiling::StartTimer(SPECIAL_TIMERS Address)
|
2012-10-05 09:18:02 +00:00
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
SPECIAL_TIMERS OldTimerAddr = StopTimer();
|
|
|
|
m_CurrentTimerAddr = Address;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-05-14 22:38:52 +00:00
|
|
|
#ifdef _M_IX86
|
2015-12-06 09:59:58 +00:00
|
|
|
uint32_t HiValue, LoValue;
|
|
|
|
_asm
|
|
|
|
{
|
2016-01-18 11:25:10 +00:00
|
|
|
pushad;
|
|
|
|
rdtsc;
|
|
|
|
mov HiValue, edx;
|
|
|
|
mov LoValue, eax;
|
|
|
|
popad;
|
2015-12-23 19:51:37 +00:00
|
|
|
}
|
|
|
|
m_StartTimeHi = HiValue;
|
|
|
|
m_StartTimeLo = LoValue;
|
2015-05-14 22:38:52 +00:00
|
|
|
#else
|
2015-12-23 19:51:37 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2015-05-14 22:38:52 +00:00
|
|
|
#endif
|
2015-12-23 19:51:37 +00:00
|
|
|
return OldTimerAddr;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 11:25:10 +00:00
|
|
|
SPECIAL_TIMERS CProfiling::StopTimer()
|
|
|
|
{
|
2015-12-06 09:59:58 +00:00
|
|
|
uint32_t HiValue, LoValue;
|
2015-12-23 19:51:37 +00:00
|
|
|
|
|
|
|
if (m_CurrentTimerAddr == Timer_None) { return m_CurrentTimerAddr; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-05-14 22:38:52 +00:00
|
|
|
#ifdef _M_IX86
|
2015-12-23 19:51:37 +00:00
|
|
|
_asm {
|
2016-01-18 11:25:10 +00:00
|
|
|
pushad;
|
|
|
|
rdtsc;
|
|
|
|
mov HiValue, edx;
|
|
|
|
mov LoValue, eax;
|
|
|
|
popad;
|
2015-12-23 19:51:37 +00:00
|
|
|
}
|
2015-05-14 22:38:52 +00:00
|
|
|
#else
|
2015-12-23 19:51:37 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__, __LINE__);
|
2015-05-14 22:38:52 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
int64_t StopTime = ((uint64_t)HiValue << 32) + (uint64_t)LoValue;
|
2015-12-21 21:38:56 +00:00
|
|
|
int64_t StartTime = ((uint64_t)m_StartTimeHi << 32) + (uint64_t)m_StartTimeLo;
|
|
|
|
int64_t TimeTaken = StopTime - StartTime;
|
2015-12-23 19:51:37 +00:00
|
|
|
|
|
|
|
PROFILE_ENRTY Entry = m_Entries.find(m_CurrentTimerAddr);
|
|
|
|
if (Entry != m_Entries.end()) {
|
|
|
|
Entry->second += TimeTaken;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_Entries.insert(PROFILE_ENRTIES::value_type(m_CurrentTimerAddr, TimeTaken));
|
|
|
|
}
|
|
|
|
|
|
|
|
SPECIAL_TIMERS OldTimerAddr = m_CurrentTimerAddr;
|
|
|
|
m_CurrentTimerAddr = Timer_None;
|
|
|
|
return OldTimerAddr;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 11:25:10 +00:00
|
|
|
void CProfiling::ShowCPU_Usage()
|
|
|
|
{
|
2015-12-21 21:38:56 +00:00
|
|
|
int64_t TotalTime, CPU = 0, Alist = 0, Dlist = 0, Idle = 0;
|
2015-12-23 19:51:37 +00:00
|
|
|
PROFILE_ENRTY Entry;
|
|
|
|
|
|
|
|
if (m_CurrentDisplayCount > 0) { m_CurrentDisplayCount -= 1; return; }
|
|
|
|
m_CurrentDisplayCount = MAX_FRAMES;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
Entry = m_Entries.find(Timer_R4300);
|
|
|
|
if (Entry != m_Entries.end()) { CPU = Entry->second; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
Entry = m_Entries.find(Timer_RefreshScreen);
|
|
|
|
if (Entry != m_Entries.end()) { CPU += Entry->second; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
Entry = m_Entries.find(Timer_RSP_Dlist);
|
|
|
|
if (Entry != m_Entries.end()) { Dlist = Entry->second; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
Entry = m_Entries.find(Timer_UpdateScreen);
|
|
|
|
if (Entry != m_Entries.end()) { Dlist += Entry->second; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
Entry = m_Entries.find(Timer_RSP_Alist);
|
|
|
|
if (Entry != m_Entries.end()) { Alist = Entry->second; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
Entry = m_Entries.find(Timer_Idel);
|
|
|
|
if (Entry != m_Entries.end()) { Idle = Entry->second; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
TotalTime = CPU + Alist + Dlist + Idle;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
g_Notify->DisplayMessage(0, stdstr_f("r4300i: %0.1f%c GFX: %0.1f%c Alist: %0.1f%c Idle: %0.1f%c",
|
|
|
|
(float)(((double)CPU / (double)TotalTime) * 100), '%',
|
|
|
|
(float)(((double)Dlist / (double)TotalTime) * 100), '%',
|
|
|
|
(float)(((double)Alist / (double)TotalTime) * 100), '%',
|
2015-12-23 20:04:36 +00:00
|
|
|
(float)(((double)Idle / (double)TotalTime) * 100), '%').c_str());
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
ResetCounters();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 11:25:10 +00:00
|
|
|
void CProfiling::ResetCounters()
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
m_Entries.clear();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 11:25:10 +00:00
|
|
|
struct TIMER_NAME
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
SPECIAL_TIMERS Timer;
|
2016-01-18 11:25:10 +00:00
|
|
|
const char * Name;
|
2015-05-04 00:08:53 +00:00
|
|
|
};
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-18 11:25:10 +00:00
|
|
|
void CProfiling::GenerateLog()
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
stdstr LogFileName;
|
|
|
|
{
|
|
|
|
CLog Log;
|
|
|
|
Log.Open("Profiling.txt");
|
|
|
|
LogFileName = Log.FileName();
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-23 19:51:37 +00:00
|
|
|
//Get the total time
|
2015-12-21 21:38:56 +00:00
|
|
|
int64_t TotalTime = 0;
|
2016-01-18 11:25:10 +00:00
|
|
|
for (PROFILE_ENRTY itemTime = m_Entries.begin(); itemTime != m_Entries.end(); itemTime++)
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
TotalTime += itemTime->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create a sortable list of items
|
|
|
|
std::vector<PROFILE_VALUE *> ItemList;
|
2016-01-18 11:25:10 +00:00
|
|
|
for (PROFILE_ENRTY Entry = m_Entries.begin(); Entry != m_Entries.end(); Entry++)
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
ItemList.push_back(&(*Entry));
|
|
|
|
}
|
|
|
|
|
|
|
|
//sort the list with a basic bubble sort
|
2016-01-18 11:25:10 +00:00
|
|
|
for (size_t OuterPass = 0; OuterPass < (ItemList.size() - 1); OuterPass++)
|
|
|
|
{
|
|
|
|
for (size_t InnerPass = 0; InnerPass < (ItemList.size() - 1); InnerPass++)
|
|
|
|
{
|
|
|
|
if (ItemList[InnerPass]->second < ItemList[InnerPass + 1]->second)
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
PROFILE_VALUE * TempPtr = ItemList[InnerPass];
|
|
|
|
ItemList[InnerPass] = ItemList[InnerPass + 1];
|
|
|
|
ItemList[InnerPass + 1] = TempPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-18 11:25:10 +00:00
|
|
|
TIMER_NAME TimerNames[] =
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
{ Timer_R4300, "R4300" },
|
|
|
|
{ Timer_RSP_Dlist, "RSP: Dlist" },
|
|
|
|
{ Timer_RSP_Alist, "RSP: Alist" },
|
|
|
|
{ Timer_RSP_Unknown, "RSP: Unknown" },
|
|
|
|
{ Timer_RefreshScreen, "Refresh Screen" },
|
|
|
|
{ Timer_UpdateScreen, "Update Screen" },
|
|
|
|
{ Timer_UpdateFPS, "Update FPS" },
|
|
|
|
{ Timer_FuncLookup, "Function Lookup" },
|
|
|
|
{ Timer_Done, "Timer_Done" },
|
|
|
|
{ Timer_GetBlockInfo, "Timer_GetBlockInfo" },
|
|
|
|
{ Timer_AnalyseBlock, "Timer_AnalyseBlock" },
|
|
|
|
{ Timer_CompileBlock, "Timer_CompileBlock" },
|
|
|
|
{ Timer_CompileDone, "Timer_CompileDone" },
|
|
|
|
};
|
|
|
|
|
2016-01-18 11:25:10 +00:00
|
|
|
for (size_t count = 0; count < ItemList.size(); count++)
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
char Buffer[255];
|
|
|
|
double CpuUsage = ((double)ItemList[count]->second / (double)TotalTime) * 100;
|
|
|
|
if (CpuUsage <= 0.2) { continue; }
|
|
|
|
sprintf(Buffer, "Func 0x%08X", ItemList[count]->first);
|
2016-01-18 11:25:10 +00:00
|
|
|
for (int NameID = 0; NameID < (sizeof(TimerNames) / sizeof(TIMER_NAME)); NameID++)
|
|
|
|
{
|
|
|
|
if (ItemList[count]->first == TimerNames[NameID].Timer)
|
|
|
|
{
|
2015-12-23 19:51:37 +00:00
|
|
|
strcpy(Buffer, TimerNames[NameID].Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.LogF("%s\t%2.2f", Buffer, CpuUsage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ResetCounters();
|
|
|
|
}
|