2016-03-01 15:47:10 +00:00
|
|
|
/*
|
|
|
|
** stats.h
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 1998-2006 Randy Heit
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __STATS_H__
|
|
|
|
#define __STATS_H__
|
|
|
|
|
|
|
|
#include "zstring.h"
|
|
|
|
|
2017-01-31 13:56:58 +00:00
|
|
|
#if !defined _WIN32 && !defined __APPLE__
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
#ifdef NO_CLOCK_GETTIME
|
|
|
|
class cycle_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cycle_t &operator= (const cycle_t &o) { return *this; }
|
|
|
|
void Reset() {}
|
|
|
|
void Clock() {}
|
|
|
|
void Unclock() {}
|
|
|
|
double Time() { return 0; }
|
|
|
|
double TimeMS() { return 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
class cycle_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cycle_t &operator= (const cycle_t &o)
|
|
|
|
{
|
|
|
|
Sec = o.Sec;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
Sec = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clock()
|
|
|
|
{
|
|
|
|
timespec ts;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
Sec -= ts.tv_sec + ts.tv_nsec * 1e-9;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unclock()
|
|
|
|
{
|
|
|
|
timespec ts;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
Sec += ts.tv_sec + ts.tv_nsec * 1e-9;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Time()
|
|
|
|
{
|
|
|
|
return Sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
double TimeMS()
|
|
|
|
{
|
|
|
|
return Sec * 1e3;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
double Sec;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-01-31 15:51:12 +00:00
|
|
|
// Windows and macOS
|
2016-03-01 15:47:10 +00:00
|
|
|
#include "x86.h"
|
|
|
|
|
|
|
|
extern double PerfToSec, PerfToMillisec;
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Trying to include intrin.h here results in some bizarre errors, so I'm just
|
|
|
|
// going to duplicate the function prototype instead.
|
|
|
|
//#include <intrin.h>
|
|
|
|
extern "C" unsigned __int64 __rdtsc(void);
|
|
|
|
#pragma intrinsic(__rdtsc)
|
|
|
|
inline unsigned __int64 rdtsc()
|
|
|
|
{
|
2017-04-23 09:52:32 +00:00
|
|
|
return __rdtsc();
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
#else
|
2017-03-09 19:05:22 +00:00
|
|
|
inline uint64_t rdtsc()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
2017-01-31 15:51:12 +00:00
|
|
|
#ifdef __amd64__
|
2017-03-09 19:05:22 +00:00
|
|
|
uint64_t tsc;
|
2017-01-31 15:51:12 +00:00
|
|
|
asm volatile ("rdtsc; shlq $32, %%rdx; orq %%rdx, %%rax" : "=a" (tsc) :: "%rdx");
|
|
|
|
return tsc;
|
2017-03-12 11:10:15 +00:00
|
|
|
#elif defined __ppc__
|
|
|
|
unsigned int lower, upper, temp;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
asm volatile ("mftbu %0 \n mftb %1 \n mftbu %2 \n"
|
|
|
|
: "=r"(upper), "=r"(lower), "=r"(temp));
|
|
|
|
}
|
|
|
|
while (upper != temp);
|
|
|
|
return (static_cast<unsigned long long>(upper) << 32) | lower;
|
2017-01-31 15:51:12 +00:00
|
|
|
#else // i386
|
2016-03-01 15:47:10 +00:00
|
|
|
if (CPU.bRDTSC)
|
|
|
|
{
|
2017-03-09 19:05:22 +00:00
|
|
|
uint64_t tsc;
|
2016-03-01 15:47:10 +00:00
|
|
|
asm volatile ("\trdtsc\n" : "=A" (tsc));
|
|
|
|
return tsc;
|
|
|
|
}
|
|
|
|
return 0;
|
2017-01-31 15:51:12 +00:00
|
|
|
#endif // __amd64__
|
2016-03-01 15:47:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class cycle_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cycle_t &operator= (const cycle_t &o)
|
|
|
|
{
|
|
|
|
Counter = o.Counter;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset()
|
|
|
|
{
|
|
|
|
Counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clock()
|
|
|
|
{
|
2017-03-09 19:05:22 +00:00
|
|
|
int64_t time = rdtsc();
|
2016-03-01 15:47:10 +00:00
|
|
|
Counter -= time;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unclock()
|
|
|
|
{
|
2017-03-09 19:05:22 +00:00
|
|
|
int64_t time = rdtsc();
|
2016-03-01 15:47:10 +00:00
|
|
|
Counter += time;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Time()
|
|
|
|
{
|
|
|
|
return Counter * PerfToSec;
|
|
|
|
}
|
|
|
|
|
|
|
|
double TimeMS()
|
|
|
|
{
|
|
|
|
return Counter * PerfToMillisec;
|
|
|
|
}
|
|
|
|
|
2017-03-09 19:05:22 +00:00
|
|
|
int64_t GetRawCounter()
|
2016-03-01 15:47:10 +00:00
|
|
|
{
|
|
|
|
return Counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-03-09 19:05:22 +00:00
|
|
|
int64_t Counter;
|
2016-03-01 15:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class FStat
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FStat (const char *name);
|
|
|
|
virtual ~FStat ();
|
|
|
|
|
|
|
|
virtual FString GetStats () = 0;
|
|
|
|
|
|
|
|
void ToggleStat ();
|
2016-03-01 17:50:45 +00:00
|
|
|
bool isActive() const
|
|
|
|
{
|
|
|
|
return m_Active;
|
|
|
|
}
|
2016-03-01 15:47:10 +00:00
|
|
|
|
|
|
|
static void PrintStat ();
|
|
|
|
static FStat *FindStat (const char *name);
|
|
|
|
static void ToggleStat (const char *name);
|
|
|
|
static void DumpRegisteredStats ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
FStat *m_Next;
|
|
|
|
const char *m_Name;
|
|
|
|
bool m_Active;
|
|
|
|
|
|
|
|
static FStat *FirstStat;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ADD_STAT(n) \
|
|
|
|
static class Stat_##n : public FStat { \
|
|
|
|
public: \
|
|
|
|
Stat_##n () : FStat (#n) {} \
|
|
|
|
FString GetStats (); } Istaticstat##n; \
|
|
|
|
FString Stat_##n::GetStats ()
|
|
|
|
|
|
|
|
#endif //__STATS_H__
|