2010-04-25 00:31:27 +00:00
|
|
|
/*
|
2009-02-09 21:15:56 +00:00
|
|
|
* Copyright (C) 2007-2009 Gabest
|
|
|
|
* http://www.gabest.org
|
|
|
|
*
|
|
|
|
* This Program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-02-09 21:15:56 +00:00
|
|
|
* This Program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-02-09 21:15:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GNU Make; see the file COPYING. If not, write to
|
2012-09-09 18:16:11 +00:00
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
2009-02-09 21:15:56 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "GSPerfMon.h"
|
|
|
|
|
|
|
|
GSPerfMon::GSPerfMon()
|
2011-12-22 01:48:16 +00:00
|
|
|
: m_frame(0)
|
2009-02-09 21:15:56 +00:00
|
|
|
, m_lastframe(0)
|
|
|
|
, m_count(0)
|
|
|
|
{
|
|
|
|
memset(m_counters, 0, sizeof(m_counters));
|
|
|
|
memset(m_stats, 0, sizeof(m_stats));
|
2011-12-22 01:48:16 +00:00
|
|
|
memset(m_total, 0, sizeof(m_total));
|
|
|
|
memset(m_begin, 0, sizeof(m_begin));
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSPerfMon::Put(counter_t c, double val)
|
|
|
|
{
|
2015-08-10 06:33:39 +00:00
|
|
|
#ifndef DISABLE_PERF_MON
|
2009-02-09 21:15:56 +00:00
|
|
|
if(c == Frame)
|
|
|
|
{
|
2020-04-09 09:32:07 +00:00
|
|
|
#if defined(__unix__) || defined(__APPLE__)
|
2014-11-24 22:44:32 +00:00
|
|
|
// clock on linux will return CLOCK_PROCESS_CPUTIME_ID.
|
|
|
|
// CLOCK_THREAD_CPUTIME_ID is much more useful to measure the fps
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
|
|
|
|
uint64 now = (uint64) ts.tv_sec * (uint64) 1e6 + (uint64) ts.tv_nsec / (uint64) 1e3;
|
|
|
|
#else
|
2009-02-09 21:15:56 +00:00
|
|
|
clock_t now = clock();
|
2014-11-24 22:44:32 +00:00
|
|
|
#endif
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
if(m_lastframe != 0)
|
|
|
|
{
|
2011-02-24 23:46:26 +00:00
|
|
|
m_counters[c] += (now - m_lastframe) * 1000 / CLOCKS_PER_SEC;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_lastframe = now;
|
|
|
|
m_frame++;
|
|
|
|
m_count++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_counters[c] += val;
|
|
|
|
}
|
2015-08-10 06:33:39 +00:00
|
|
|
#endif
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSPerfMon::Update()
|
|
|
|
{
|
2015-08-10 06:33:39 +00:00
|
|
|
#ifndef DISABLE_PERF_MON
|
2009-02-09 21:15:56 +00:00
|
|
|
if(m_count > 0)
|
|
|
|
{
|
2013-06-29 12:02:03 +00:00
|
|
|
for(size_t i = 0; i < countof(m_counters); i++)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
m_stats[i] = m_counters[i] / m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(m_counters, 0, sizeof(m_counters));
|
2015-08-10 06:33:39 +00:00
|
|
|
#endif
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 01:48:16 +00:00
|
|
|
void GSPerfMon::Start(int timer)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2015-08-10 06:33:39 +00:00
|
|
|
#ifndef DISABLE_PERF_MON
|
2011-12-22 01:48:16 +00:00
|
|
|
m_start[timer] = __rdtsc();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2011-12-22 01:48:16 +00:00
|
|
|
if(m_begin[timer] == 0)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2011-12-22 01:48:16 +00:00
|
|
|
m_begin[timer] = m_start[timer];
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2015-08-10 06:33:39 +00:00
|
|
|
#endif
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 01:48:16 +00:00
|
|
|
void GSPerfMon::Stop(int timer)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2015-08-10 06:33:39 +00:00
|
|
|
#ifndef DISABLE_PERF_MON
|
2011-12-22 01:48:16 +00:00
|
|
|
if(m_start[timer] > 0)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2011-12-22 01:48:16 +00:00
|
|
|
m_total[timer] += __rdtsc() - m_start[timer];
|
|
|
|
m_start[timer] = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2015-08-10 06:33:39 +00:00
|
|
|
#endif
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 01:48:16 +00:00
|
|
|
int GSPerfMon::CPU(int timer, bool reset)
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2011-12-22 14:36:54 +00:00
|
|
|
int percent = (int)(100 * m_total[timer] / (__rdtsc() - m_begin[timer]));
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2011-12-22 01:48:16 +00:00
|
|
|
if(reset)
|
|
|
|
{
|
|
|
|
m_begin[timer] = 0;
|
|
|
|
m_start[timer] = 0;
|
|
|
|
m_total[timer] = 0;
|
|
|
|
}
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
return percent;
|
2010-04-25 00:31:27 +00:00
|
|
|
}
|