2014-11-19 18:57:12 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-01-23 07:08:45 +00:00
|
|
|
#include <algorithm>
|
2014-11-19 18:57:12 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <ios>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "Common/Profiler.h"
|
|
|
|
#include "Common/Timer.h"
|
|
|
|
|
2015-08-20 09:41:49 +00:00
|
|
|
namespace Common
|
|
|
|
{
|
2014-12-04 03:54:54 +00:00
|
|
|
static const u32 PROFILER_FIELD_LENGTH = 8;
|
|
|
|
static const u32 PROFILER_FIELD_LENGTH_FP = PROFILER_FIELD_LENGTH + 3;
|
2016-06-24 08:43:46 +00:00
|
|
|
static const int PROFILER_LAZY_DELAY = 60; // in frames
|
2014-12-04 03:54:54 +00:00
|
|
|
|
2014-11-19 18:57:12 +00:00
|
|
|
std::list<Profiler*> Profiler::s_all_profilers;
|
2015-08-20 09:41:49 +00:00
|
|
|
std::mutex Profiler::s_mutex;
|
2014-11-19 18:57:12 +00:00
|
|
|
u32 Profiler::s_max_length = 0;
|
|
|
|
u64 Profiler::s_frame_time;
|
|
|
|
u64 Profiler::s_usecs_frame;
|
|
|
|
|
2019-03-02 18:42:25 +00:00
|
|
|
std::string Profiler::s_lazy_result;
|
2014-11-19 18:57:12 +00:00
|
|
|
int Profiler::s_lazy_delay = 0;
|
|
|
|
|
|
|
|
Profiler::Profiler(const std::string& name)
|
2017-06-07 11:16:02 +00:00
|
|
|
: m_name(name), m_usecs(0), m_usecs_min(UINT64_MAX), m_usecs_max(0), m_usecs_quad(0),
|
|
|
|
m_calls(0), m_depth(0)
|
2014-11-19 18:57:12 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_time = Common::Timer::GetTimeUs();
|
|
|
|
s_max_length = std::max<u32>(s_max_length, u32(m_name.length()));
|
2014-11-19 18:57:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::lock_guard<std::mutex> lk(s_mutex);
|
|
|
|
s_all_profilers.push_back(this);
|
2014-11-19 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Profiler::~Profiler()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::lock_guard<std::mutex> lk(s_mutex);
|
|
|
|
s_all_profilers.remove(this);
|
2014-11-19 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 09:41:49 +00:00
|
|
|
bool Profiler::operator<(const Profiler& b) const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return m_usecs < b.m_usecs;
|
2015-08-20 09:41:49 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 18:57:12 +00:00
|
|
|
std::string Profiler::ToString()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_lazy_delay > 0)
|
|
|
|
{
|
|
|
|
s_lazy_delay--;
|
|
|
|
return s_lazy_result;
|
|
|
|
}
|
|
|
|
s_lazy_delay = PROFILER_LAZY_DELAY - 1;
|
|
|
|
|
|
|
|
// don't write anything if no profilation is enabled
|
|
|
|
std::lock_guard<std::mutex> lk(s_mutex);
|
|
|
|
if (s_all_profilers.empty())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
u64 end = Common::Timer::GetTimeUs();
|
|
|
|
s_usecs_frame = end - s_frame_time;
|
|
|
|
s_frame_time = end;
|
|
|
|
|
|
|
|
std::ostringstream buffer;
|
|
|
|
buffer << std::setw(s_max_length) << std::left << ""
|
|
|
|
<< " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << "calls"
|
|
|
|
<< " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << "sum"
|
|
|
|
<< " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << "rel"
|
|
|
|
<< " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << "min"
|
|
|
|
<< " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << "avg"
|
|
|
|
<< " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << "stdev"
|
|
|
|
<< " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << "max"
|
|
|
|
<< " ";
|
|
|
|
buffer << "/ usec" << std::endl;
|
|
|
|
|
|
|
|
s_all_profilers.sort([](Profiler* a, Profiler* b) { return *b < *a; });
|
|
|
|
|
|
|
|
for (auto profiler : s_all_profilers)
|
|
|
|
{
|
|
|
|
buffer << profiler->Read() << std::endl;
|
|
|
|
}
|
|
|
|
s_lazy_result = buffer.str();
|
|
|
|
return s_lazy_result;
|
2014-11-19 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Profiler::Start()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_depth++)
|
|
|
|
{
|
|
|
|
m_time = Common::Timer::GetTimeUs();
|
|
|
|
}
|
2014-11-19 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Profiler::Stop()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!--m_depth)
|
|
|
|
{
|
|
|
|
u64 end = Common::Timer::GetTimeUs();
|
|
|
|
|
|
|
|
u64 diff = end - m_time;
|
|
|
|
|
|
|
|
m_usecs += diff;
|
|
|
|
m_usecs_min = std::min(m_usecs_min, diff);
|
|
|
|
m_usecs_max = std::max(m_usecs_max, diff);
|
|
|
|
m_usecs_quad += diff * diff;
|
|
|
|
m_calls++;
|
|
|
|
}
|
2014-11-19 18:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Profiler::Read()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
double avg = 0;
|
|
|
|
double stdev = 0;
|
|
|
|
double time_rel = 0;
|
|
|
|
if (m_calls)
|
|
|
|
{
|
|
|
|
avg = double(m_usecs) / m_calls;
|
|
|
|
stdev = std::sqrt(double(m_usecs_quad) / m_calls - avg * avg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_usecs_min = 0;
|
|
|
|
}
|
|
|
|
if (s_usecs_frame)
|
|
|
|
{
|
|
|
|
time_rel = double(m_usecs) * 100 / s_usecs_frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostringstream buffer;
|
|
|
|
|
|
|
|
buffer << std::setw(s_max_length) << std::left << m_name << " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << m_calls << " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << m_usecs << " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << time_rel << " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << m_usecs_min << " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << std::fixed << std::setprecision(2)
|
|
|
|
<< avg << " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH_FP) << std::right << std::fixed << std::setprecision(2)
|
|
|
|
<< stdev << " ";
|
|
|
|
buffer << std::setw(PROFILER_FIELD_LENGTH) << std::right << m_usecs_max;
|
|
|
|
|
|
|
|
m_usecs = 0;
|
2017-06-07 11:16:02 +00:00
|
|
|
m_usecs_min = UINT64_MAX;
|
2016-06-24 08:43:46 +00:00
|
|
|
m_usecs_max = 0;
|
|
|
|
m_usecs_quad = 0;
|
|
|
|
m_calls = 0;
|
|
|
|
|
|
|
|
return buffer.str();
|
2014-11-19 18:57:12 +00:00
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace Common
|