2008-11-14 20:47:53 +00:00
|
|
|
/*
|
2015-11-10 05:21:49 +00:00
|
|
|
* RSP Compiler plug in for Project64 (A Nintendo 64 emulator).
|
2008-11-14 20:47:53 +00:00
|
|
|
*
|
|
|
|
* (c) Copyright 2001 jabo (jabo@emulation64.com) and
|
|
|
|
* zilmar (zilmar@emulation64.com)
|
|
|
|
*
|
|
|
|
* pj64 homepage: www.pj64.net
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify and distribute Project64 in both binary and
|
|
|
|
* source form, for non-commercial purposes, is hereby granted without fee,
|
|
|
|
* providing that this license information and copyright notice appear with
|
|
|
|
* all copies and any derived work.
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event shall the authors be held liable for any damages
|
|
|
|
* arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Project64 is freeware for PERSONAL USE only. Commercial users should
|
|
|
|
* seek permission of the copyright holders first. Commercial use includes
|
|
|
|
* charging money for Project64 or software derived from Project64.
|
|
|
|
*
|
|
|
|
* The copyright holders request that bug fixes and improvements to the code
|
|
|
|
* should be forwarded to them so if they want them.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-10-29 23:42:39 +00:00
|
|
|
#include <stdio.h>
|
2008-11-14 20:47:53 +00:00
|
|
|
#include <windows.h>
|
2012-09-25 22:45:39 +00:00
|
|
|
#include <shellapi.h>
|
2008-11-14 20:47:53 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "profiling.h"
|
|
|
|
}
|
|
|
|
#pragma warning(disable:4786)
|
2016-01-20 06:09:05 +00:00
|
|
|
#include <Common/StdString.h>
|
|
|
|
#include <Common/FileClass.h>
|
|
|
|
#include <Common/LogClass.h>
|
2008-11-14 20:47:53 +00:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
class CProfiling
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
typedef std::map<DWORD, __int64 > PROFILE_ENRTIES;
|
|
|
|
typedef PROFILE_ENRTIES::iterator PROFILE_ENRTY;
|
|
|
|
typedef PROFILE_ENRTIES::value_type PROFILE_VALUE;
|
|
|
|
typedef struct { SPECIAL_TIMERS Timer; char * Name; } TIMER_NAME;
|
|
|
|
|
|
|
|
DWORD m_CurrentTimerAddr, CurrentDisplayCount;
|
|
|
|
DWORD m_StartTimeHi, m_StartTimeLo; //The Current Timer start time
|
|
|
|
PROFILE_ENRTIES m_Entries;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CProfiling ()
|
|
|
|
{
|
|
|
|
m_CurrentTimerAddr = Timer_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
//recording timing against current timer, returns the address of the timer stoped
|
|
|
|
DWORD StartTimer ( DWORD Address )
|
|
|
|
{
|
|
|
|
DWORD OldTimerAddr = StopTimer();
|
|
|
|
m_CurrentTimerAddr = Address;
|
|
|
|
|
2015-10-30 00:23:36 +00:00
|
|
|
#if defined(_M_IX86) && defined(_MSC_VER)
|
2008-11-14 20:47:53 +00:00
|
|
|
DWORD HiValue, LoValue;
|
|
|
|
_asm {
|
|
|
|
pushad
|
|
|
|
rdtsc
|
|
|
|
mov HiValue, edx
|
|
|
|
mov LoValue, eax
|
|
|
|
popad
|
|
|
|
}
|
|
|
|
m_StartTimeHi = HiValue;
|
|
|
|
m_StartTimeLo = LoValue;
|
2015-05-16 00:55:23 +00:00
|
|
|
#else
|
|
|
|
DebugBreak();
|
|
|
|
#endif
|
2008-11-14 20:47:53 +00:00
|
|
|
return OldTimerAddr;
|
|
|
|
}
|
|
|
|
DWORD StopTimer ( void )
|
2015-05-16 00:55:23 +00:00
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
if (m_CurrentTimerAddr == Timer_None) { return m_CurrentTimerAddr; }
|
|
|
|
|
2015-10-30 00:23:36 +00:00
|
|
|
#if defined(_M_IX86) && defined(_MSC_VER)
|
2015-05-16 00:55:23 +00:00
|
|
|
DWORD HiValue, LoValue;
|
2008-11-14 20:47:53 +00:00
|
|
|
_asm {
|
|
|
|
pushad
|
|
|
|
rdtsc
|
|
|
|
mov HiValue, edx
|
|
|
|
mov LoValue, eax
|
|
|
|
popad
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:55:23 +00:00
|
|
|
__int64 StopTime = ((unsigned __int64)HiValue << 32) + (unsigned __int64)LoValue;
|
2008-11-14 20:47:53 +00:00
|
|
|
__int64 StartTime = ((unsigned __int64)m_StartTimeHi << 32) + (unsigned __int64)m_StartTimeLo;
|
|
|
|
__int64 TimeTaken = StopTime - StartTime;
|
2015-05-16 00:55:23 +00:00
|
|
|
|
2008-11-14 20:47:53 +00:00
|
|
|
PROFILE_ENRTY Entry = m_Entries.find(m_CurrentTimerAddr);
|
2015-03-18 04:50:48 +00:00
|
|
|
if (Entry != m_Entries.end())
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
Entry->second += TimeTaken;
|
2015-03-18 04:50:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-16 00:55:23 +00:00
|
|
|
m_Entries.insert(PROFILE_ENRTIES::value_type(m_CurrentTimerAddr, TimeTaken));
|
2008-11-14 20:47:53 +00:00
|
|
|
}
|
2015-05-16 00:55:23 +00:00
|
|
|
#else
|
|
|
|
DebugBreak();
|
|
|
|
#endif
|
2008-11-14 20:47:53 +00:00
|
|
|
|
|
|
|
DWORD OldTimerAddr = m_CurrentTimerAddr;
|
|
|
|
m_CurrentTimerAddr = Timer_None;
|
|
|
|
return OldTimerAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Reset all the counters back to 0
|
|
|
|
void ResetCounters ( void )
|
|
|
|
{
|
|
|
|
m_Entries.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Generate a log file with the current results, this will also reset the counters
|
|
|
|
void GenerateLog ( void )
|
|
|
|
{
|
|
|
|
stdstr LogFileName;
|
|
|
|
{
|
|
|
|
CLog Log;
|
|
|
|
Log.Open("RSP Profiling.txt");
|
|
|
|
LogFileName = Log.FileName();
|
|
|
|
|
|
|
|
//Get the total time
|
|
|
|
__int64 TotalTime = 0;
|
2015-03-18 04:50:48 +00:00
|
|
|
for (PROFILE_ENRTY itemTime = m_Entries.begin(); itemTime != m_Entries.end(); itemTime++ )
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
TotalTime += itemTime->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create a sortable list of items
|
|
|
|
std::vector<PROFILE_VALUE *> ItemList;
|
2015-03-18 04:50:48 +00:00
|
|
|
for (PROFILE_ENRTY Entry = m_Entries.begin(); Entry != m_Entries.end(); Entry++ )
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
ItemList.push_back(&(*Entry));
|
|
|
|
}
|
2015-03-18 04:50:48 +00:00
|
|
|
|
2008-11-14 20:47:53 +00:00
|
|
|
//sort the list with a basic bubble sort
|
|
|
|
if (ItemList.size() > 0)
|
|
|
|
{
|
2015-03-18 04:50:48 +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)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
PROFILE_VALUE * TempPtr = ItemList[InnerPass];
|
|
|
|
ItemList[InnerPass] = ItemList[InnerPass + 1];
|
|
|
|
ItemList[InnerPass + 1] = TempPtr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TIMER_NAME TimerNames[] = {
|
|
|
|
{Timer_Compiling, "RSP: Compiling"},
|
|
|
|
{Timer_RSP_Running, "RSP: Running"},
|
|
|
|
{Timer_R4300_Running, "R4300: Running"},
|
|
|
|
{Timer_RDP_Running, "RDP: Running"},
|
|
|
|
};
|
2015-03-18 04:50:48 +00:00
|
|
|
|
|
|
|
for (size_t count = 0; count < ItemList.size(); count++ )
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
char Buffer[255];
|
2012-09-25 22:45:39 +00:00
|
|
|
float CpuUsage = (float)(((double)ItemList[count]->second / (double)TotalTime) * 100);
|
2015-10-29 23:42:39 +00:00
|
|
|
|
2008-11-14 20:47:53 +00:00
|
|
|
if (CpuUsage <= 0.2) { continue; }
|
2015-10-29 23:42:39 +00:00
|
|
|
sprintf(Buffer, "Func 0x%08X", ItemList[count]->first);
|
2015-03-18 04:50:48 +00:00
|
|
|
for (int NameID = 0; NameID < (sizeof(TimerNames) / sizeof(TIMER_NAME)); NameID++)
|
|
|
|
{
|
|
|
|
if (ItemList[count]->first == (DWORD)TimerNames[NameID].Timer)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
strcpy(Buffer,TimerNames[NameID].Name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.LogF("%s\t%2.2f",Buffer, CpuUsage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ShellExecute(NULL,"open",LogFileName.c_str(),NULL,NULL,SW_SHOW);
|
|
|
|
ResetCounters();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CProfiling& GetProfiler ( void )
|
|
|
|
{
|
|
|
|
static CProfiling Profile;
|
|
|
|
return Profile;
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
void ResetTimerList (void)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
GetProfiler().ResetCounters();
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
DWORD StartTimer (DWORD Address)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
return GetProfiler().StartTimer(Address);
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
void StopTimer (void)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
GetProfiler().StopTimer();
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
void GenerateTimerResults (void)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
GetProfiler().GenerateLog();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef todelete
|
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char Label[100];
|
2012-11-06 08:23:17 +00:00
|
|
|
__int64 TimeTotal;
|
2008-11-14 20:47:53 +00:00
|
|
|
} TIME_STAMP_ENTRY;
|
|
|
|
|
|
|
|
DWORD StartTimeHi, StartTimeLo, StopTimeHi, StopTimeLo, TSE_Count, TSE_Max;
|
|
|
|
TIME_STAMP_ENTRY * TS_Entries = NULL;
|
|
|
|
char LastLabel[100];
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
void ResetTimerList (void)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
if (TS_Entries) { free(TS_Entries); }
|
|
|
|
TS_Entries = NULL;
|
|
|
|
TSE_Count = 0;
|
|
|
|
TSE_Max = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
void StartTimer (char * Label)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
strcpy(LastLabel,Label);
|
|
|
|
_asm {
|
|
|
|
pushad
|
|
|
|
rdtsc
|
|
|
|
mov StartTimeHi, edx
|
|
|
|
mov StartTimeLo, eax
|
|
|
|
popad
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
void StopTimer (void)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
_asm {
|
|
|
|
pushad
|
|
|
|
rdtsc
|
|
|
|
mov StopTimeHi, edx
|
|
|
|
mov StopTimeLo, eax
|
|
|
|
popad
|
|
|
|
}
|
|
|
|
if (strlen(LastLabel) == 0) { return; }
|
|
|
|
{
|
|
|
|
DWORD count;
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
for (count = 0; count < TSE_Count; count ++)
|
|
|
|
{
|
|
|
|
if (strcmp(LastLabel,TS_Entries[count].Label) == 0)
|
|
|
|
{
|
2012-11-06 08:23:17 +00:00
|
|
|
__int64 Time = ((unsigned __int64)StopTimeHi << 32) + (unsigned __int64)StopTimeLo;
|
|
|
|
Time -= ((unsigned __int64)StartTimeHi << 32) + (unsigned __int64)StartTimeLo;
|
2008-11-14 20:47:53 +00:00
|
|
|
TS_Entries[count].TimeTotal += Time;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 04:50:48 +00:00
|
|
|
if (TSE_Count == 0)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
TS_Entries = (TIME_STAMP_ENTRY *)malloc(sizeof(TIME_STAMP_ENTRY) * 100);
|
2015-03-18 04:50:48 +00:00
|
|
|
if (TS_Entries == NULL)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
MessageBox(NULL,"TIME_STAMP_ENTRY == NULL ??","ERROR",MB_OK|MB_ICONERROR|MB_SETFOREGROUND);
|
|
|
|
}
|
|
|
|
TSE_Max = 100;
|
2015-03-18 04:50:48 +00:00
|
|
|
}
|
|
|
|
else if (TSE_Count == TSE_Max)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
TSE_Max += 100;
|
|
|
|
TS_Entries = (TIME_STAMP_ENTRY *)realloc(TS_Entries,sizeof(TIME_STAMP_ENTRY) * TSE_Max);
|
2015-03-18 04:50:48 +00:00
|
|
|
if (TS_Entries == NULL)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
MessageBox(NULL,"TIME_STAMP_ENTRY == NULL ??","ERROR",MB_OK|MB_ICONERROR|MB_SETFOREGROUND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strcpy(TS_Entries[TSE_Count].Label,LastLabel);
|
2012-11-06 08:23:17 +00:00
|
|
|
TS_Entries[TSE_Count].TimeTotal = ((unsigned __int64)StopTimeHi << 32) + (unsigned __int64)StopTimeLo;
|
|
|
|
TS_Entries[TSE_Count].TimeTotal -= ((unsigned __int64)StartTimeHi << 32) + (unsigned __int64)StartTimeLo;
|
2008-11-14 20:47:53 +00:00
|
|
|
TSE_Count +=1;
|
|
|
|
}
|
|
|
|
|
2015-03-18 04:50:48 +00:00
|
|
|
void GenerateTimerResults (void)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
char buffer[_MAX_PATH], drive[_MAX_DRIVE] ,dir[_MAX_DIR];
|
|
|
|
char fname[_MAX_FNAME],ext[_MAX_EXT], LogFileName[_MAX_PATH];
|
|
|
|
DWORD dwWritten, count, count2;
|
|
|
|
HANDLE hLogFile = NULL;
|
2012-11-06 08:23:17 +00:00
|
|
|
__int64 TotalTime;
|
2008-11-14 20:47:53 +00:00
|
|
|
|
|
|
|
StopTimer();
|
|
|
|
|
|
|
|
GetModuleFileName(NULL,buffer,sizeof(buffer));
|
|
|
|
_splitpath( buffer, drive, dir, fname, ext );
|
|
|
|
_makepath( LogFileName, drive, dir, "RSP Profiling", "log" );
|
|
|
|
|
|
|
|
hLogFile = CreateFile(LogFileName,GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
|
|
|
|
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
|
|
|
|
SetFilePointer(hLogFile,0,NULL,FILE_BEGIN);
|
2015-03-18 04:50:48 +00:00
|
|
|
|
|
|
|
for (count = 0; count < TSE_Count; count ++)
|
|
|
|
{
|
|
|
|
for (count2 = 0; count2 < (TSE_Count - 1); count2 ++)
|
|
|
|
{
|
|
|
|
if (TS_Entries[count2].TimeTotal < TS_Entries[count2 + 1].TimeTotal)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
TIME_STAMP_ENTRY Temp;
|
|
|
|
memcpy(&Temp,&TS_Entries[count2],sizeof(TIME_STAMP_ENTRY));
|
|
|
|
memcpy(&TS_Entries[count2],&TS_Entries[count2 + 1],sizeof(TIME_STAMP_ENTRY));
|
|
|
|
memcpy(&TS_Entries[count2 + 1],&Temp,sizeof(TIME_STAMP_ENTRY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TotalTime = 0;
|
2015-03-18 04:50:48 +00:00
|
|
|
for (count = 0; count < TSE_Count; count ++)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
TotalTime += TS_Entries[count].TimeTotal;
|
|
|
|
}
|
2015-03-18 04:50:48 +00:00
|
|
|
for (count = 0; count < (TSE_Count < 50?TSE_Count:50); count ++)
|
|
|
|
{
|
2008-11-14 20:47:53 +00:00
|
|
|
sprintf(buffer,"%s - %0.2f%c\r\n",
|
|
|
|
TS_Entries[count].Label,
|
|
|
|
(((double)TS_Entries[count].TimeTotal / (double)TotalTime) * 100),'%'
|
|
|
|
);
|
|
|
|
WriteFile( hLogFile,buffer,strlen(buffer),&dwWritten,NULL );
|
|
|
|
}
|
|
|
|
CloseHandle(hLogFile);
|
|
|
|
ResetTimerList();
|
|
|
|
}
|
|
|
|
|
2015-01-31 19:27:27 +00:00
|
|
|
#endif
|