mirror of https://github.com/PCSX2/pcsx2.git
Common: Add utility classes
This commit is contained in:
parent
35322cf49b
commit
38e288ef6a
|
@ -27,6 +27,7 @@ target_sources(common PRIVATE
|
|||
PathUtils.cpp
|
||||
PrecompiledHeader.cpp
|
||||
Perf.cpp
|
||||
ProgressCallback.cpp
|
||||
pxStreams.cpp
|
||||
pxTranslate.cpp
|
||||
RwMutex.cpp
|
||||
|
@ -34,6 +35,7 @@ target_sources(common PRIVATE
|
|||
SettingsWrapper.cpp
|
||||
StringHelpers.cpp
|
||||
StringUtil.cpp
|
||||
Timer.cpp
|
||||
ThreadTools.cpp
|
||||
WindowInfo.cpp
|
||||
emitter/bmi.cpp
|
||||
|
@ -79,6 +81,7 @@ target_sources(common PRIVATE
|
|||
Path.h
|
||||
PageFaultSource.h
|
||||
PrecompiledHeader.h
|
||||
ProgressCallback.h
|
||||
pxForwardDefs.h
|
||||
pxStreams.h
|
||||
RedtapeWindows.h
|
||||
|
@ -90,6 +93,7 @@ target_sources(common PRIVATE
|
|||
SettingsWrapper.h
|
||||
StringHelpers.h
|
||||
StringUtil.h
|
||||
Timer.h
|
||||
Threading.h
|
||||
TraceLog.h
|
||||
WindowInfo.h
|
||||
|
|
|
@ -0,0 +1,248 @@
|
|||
/* PCSX2 - PS2 Emulator for PCs
|
||||
* Copyright (C) 2002-2021 PCSX2 Dev Team
|
||||
*
|
||||
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU Lesser General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* PCSX2 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with PCSX2.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
|
||||
#include "Assertions.h"
|
||||
#include "ProgressCallback.h"
|
||||
#include "StringUtil.h"
|
||||
#include "Console.h"
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
|
||||
ProgressCallback::~ProgressCallback() {}
|
||||
|
||||
void ProgressCallback::SetFormattedStatusText(const char* Format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, Format);
|
||||
const std::string str(StringUtil::StdStringFromFormatV(Format, ap));
|
||||
va_end(ap);
|
||||
|
||||
SetStatusText(str.c_str());
|
||||
}
|
||||
|
||||
void ProgressCallback::DisplayFormattedError(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
const std::string str(StringUtil::StdStringFromFormatV(format, ap));
|
||||
va_end(ap);
|
||||
|
||||
DisplayError(str.c_str());
|
||||
}
|
||||
|
||||
void ProgressCallback::DisplayFormattedWarning(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
const std::string str(StringUtil::StdStringFromFormatV(format, ap));
|
||||
va_end(ap);
|
||||
|
||||
DisplayWarning(str.c_str());
|
||||
}
|
||||
|
||||
void ProgressCallback::DisplayFormattedInformation(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
const std::string str(StringUtil::StdStringFromFormatV(format, ap));
|
||||
va_end(ap);
|
||||
|
||||
DisplayInformation(str.c_str());
|
||||
}
|
||||
|
||||
void ProgressCallback::DisplayFormattedDebugMessage(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
const std::string str(StringUtil::StdStringFromFormatV(format, ap));
|
||||
va_end(ap);
|
||||
|
||||
DisplayDebugMessage(str.c_str());
|
||||
}
|
||||
|
||||
void ProgressCallback::DisplayFormattedModalError(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
const std::string str(StringUtil::StdStringFromFormatV(format, ap));
|
||||
va_end(ap);
|
||||
|
||||
ModalError(str.c_str());
|
||||
}
|
||||
|
||||
bool ProgressCallback::DisplayFormattedModalConfirmation(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
const std::string str(StringUtil::StdStringFromFormatV(format, ap));
|
||||
va_end(ap);
|
||||
|
||||
return ModalConfirmation(str.c_str());
|
||||
}
|
||||
|
||||
void ProgressCallback::DisplayFormattedModalInformation(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
const std::string str(StringUtil::StdStringFromFormatV(format, ap));
|
||||
va_end(ap);
|
||||
|
||||
ModalInformation(str.c_str());
|
||||
}
|
||||
|
||||
class NullProgressCallbacks final : public ProgressCallback
|
||||
{
|
||||
public:
|
||||
void PushState() override {}
|
||||
void PopState() override {}
|
||||
|
||||
bool IsCancelled() const override { return false; }
|
||||
bool IsCancellable() const override { return false; }
|
||||
|
||||
void SetCancellable(bool cancellable) override {}
|
||||
void SetTitle(const char* title) override {}
|
||||
void SetStatusText(const char* statusText) override {}
|
||||
void SetProgressRange(u32 range) override {}
|
||||
void SetProgressValue(u32 value) override {}
|
||||
void IncrementProgressValue() override {}
|
||||
|
||||
void DisplayError(const char* message) override { Console.Error("%s", message); }
|
||||
void DisplayWarning(const char* message) override { Console.Warning("%s", message); }
|
||||
void DisplayInformation(const char* message) override { Console.WriteLn("%s", message); }
|
||||
void DisplayDebugMessage(const char* message) override { DevCon.WriteLn("%s", message); }
|
||||
|
||||
void ModalError(const char* message) override { Console.Error(message); }
|
||||
bool ModalConfirmation(const char* message) override
|
||||
{
|
||||
Console.WriteLn("%s", message);
|
||||
return false;
|
||||
}
|
||||
void ModalInformation(const char* message) override { Console.WriteLn("%s", message); }
|
||||
};
|
||||
|
||||
static NullProgressCallbacks s_nullProgressCallbacks;
|
||||
ProgressCallback* ProgressCallback::NullProgressCallback = &s_nullProgressCallbacks;
|
||||
|
||||
BaseProgressCallback::BaseProgressCallback()
|
||||
: m_cancellable(false)
|
||||
, m_cancelled(false)
|
||||
, m_progress_range(1)
|
||||
, m_progress_value(0)
|
||||
, m_base_progress_value(0)
|
||||
, m_saved_state(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
BaseProgressCallback::~BaseProgressCallback()
|
||||
{
|
||||
State* pNextState = m_saved_state;
|
||||
while (pNextState != NULL)
|
||||
{
|
||||
State* pCurrentState = pNextState;
|
||||
pNextState = pCurrentState->next_saved_state;
|
||||
delete pCurrentState;
|
||||
}
|
||||
}
|
||||
|
||||
void BaseProgressCallback::PushState()
|
||||
{
|
||||
State* pNewState = new State;
|
||||
pNewState->cancellable = m_cancellable;
|
||||
pNewState->status_text = m_status_text;
|
||||
pNewState->progress_range = m_progress_range;
|
||||
pNewState->progress_value = m_progress_value;
|
||||
pNewState->base_progress_value = m_base_progress_value;
|
||||
pNewState->next_saved_state = m_saved_state;
|
||||
m_saved_state = pNewState;
|
||||
}
|
||||
|
||||
void BaseProgressCallback::PopState()
|
||||
{
|
||||
pxAssert(m_saved_state);
|
||||
State* state = m_saved_state;
|
||||
m_saved_state = nullptr;
|
||||
|
||||
// impose the current position into the previous range
|
||||
const u32 new_progress_value =
|
||||
(m_progress_range != 0) ?
|
||||
static_cast<u32>(((float)m_progress_value / (float)m_progress_range) * (float)state->progress_range) :
|
||||
state->progress_value;
|
||||
|
||||
m_cancellable = state->cancellable;
|
||||
m_status_text = std::move(state->status_text);
|
||||
m_progress_range = state->progress_range;
|
||||
m_progress_value = new_progress_value;
|
||||
|
||||
m_base_progress_value = state->base_progress_value;
|
||||
m_saved_state = state->next_saved_state;
|
||||
delete state;
|
||||
}
|
||||
|
||||
bool BaseProgressCallback::IsCancelled() const
|
||||
{
|
||||
return m_cancelled;
|
||||
}
|
||||
|
||||
bool BaseProgressCallback::IsCancellable() const
|
||||
{
|
||||
return m_cancellable;
|
||||
}
|
||||
|
||||
void BaseProgressCallback::SetCancellable(bool cancellable)
|
||||
{
|
||||
m_cancellable = cancellable;
|
||||
}
|
||||
|
||||
void BaseProgressCallback::SetStatusText(const char* text)
|
||||
{
|
||||
m_status_text = text;
|
||||
}
|
||||
|
||||
void BaseProgressCallback::SetProgressRange(u32 range)
|
||||
{
|
||||
if (m_saved_state)
|
||||
{
|
||||
// impose the previous range on this range
|
||||
m_progress_range = m_saved_state->progress_range * range;
|
||||
m_base_progress_value = m_progress_value = m_saved_state->progress_value * range;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_progress_range = range;
|
||||
m_progress_value = 0;
|
||||
m_base_progress_value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void BaseProgressCallback::SetProgressValue(u32 value)
|
||||
{
|
||||
m_progress_value = m_base_progress_value + value;
|
||||
}
|
||||
|
||||
void BaseProgressCallback::IncrementProgressValue()
|
||||
{
|
||||
SetProgressValue((m_progress_value - m_base_progress_value) + 1);
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
/* PCSX2 - PS2 Emulator for PCs
|
||||
* Copyright (C) 2002-2021 PCSX2 Dev Team
|
||||
*
|
||||
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU Lesser General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* PCSX2 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with PCSX2.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "Pcsx2Defs.h"
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* Progress callbacks, abstracts a blocking operation and allows it to report progress
|
||||
* without having any dependency on the UI.
|
||||
*/
|
||||
|
||||
class ProgressCallback
|
||||
{
|
||||
public:
|
||||
virtual ~ProgressCallback();
|
||||
|
||||
virtual void PushState() = 0;
|
||||
virtual void PopState() = 0;
|
||||
|
||||
virtual bool IsCancelled() const = 0;
|
||||
virtual bool IsCancellable() const = 0;
|
||||
|
||||
virtual void SetCancellable(bool cancellable) = 0;
|
||||
|
||||
virtual void SetTitle(const char* title) = 0;
|
||||
virtual void SetStatusText(const char* text) = 0;
|
||||
virtual void SetProgressRange(u32 range) = 0;
|
||||
virtual void SetProgressValue(u32 value) = 0;
|
||||
virtual void IncrementProgressValue() = 0;
|
||||
|
||||
void SetFormattedStatusText(const char* Format, ...);
|
||||
|
||||
virtual void DisplayError(const char* message) = 0;
|
||||
virtual void DisplayWarning(const char* message) = 0;
|
||||
virtual void DisplayInformation(const char* message) = 0;
|
||||
virtual void DisplayDebugMessage(const char* message) = 0;
|
||||
|
||||
virtual void ModalError(const char* message) = 0;
|
||||
virtual bool ModalConfirmation(const char* message) = 0;
|
||||
virtual void ModalInformation(const char* message) = 0;
|
||||
|
||||
void DisplayFormattedError(const char* format, ...);
|
||||
void DisplayFormattedWarning(const char* format, ...);
|
||||
void DisplayFormattedInformation(const char* format, ...);
|
||||
void DisplayFormattedDebugMessage(const char* format, ...);
|
||||
void DisplayFormattedModalError(const char* format, ...);
|
||||
bool DisplayFormattedModalConfirmation(const char* format, ...);
|
||||
void DisplayFormattedModalInformation(const char* format, ...);
|
||||
|
||||
public:
|
||||
static ProgressCallback* NullProgressCallback;
|
||||
};
|
||||
|
||||
class BaseProgressCallback : public ProgressCallback
|
||||
{
|
||||
public:
|
||||
BaseProgressCallback();
|
||||
virtual ~BaseProgressCallback();
|
||||
|
||||
virtual void PushState() override;
|
||||
virtual void PopState() override;
|
||||
|
||||
virtual bool IsCancelled() const override;
|
||||
virtual bool IsCancellable() const override;
|
||||
|
||||
virtual void SetCancellable(bool cancellable) override;
|
||||
virtual void SetStatusText(const char* text) override;
|
||||
virtual void SetProgressRange(u32 range) override;
|
||||
virtual void SetProgressValue(u32 value) override;
|
||||
virtual void IncrementProgressValue() override;
|
||||
|
||||
protected:
|
||||
struct State
|
||||
{
|
||||
State* next_saved_state;
|
||||
std::string status_text;
|
||||
u32 progress_range;
|
||||
u32 progress_value;
|
||||
u32 base_progress_value;
|
||||
bool cancellable;
|
||||
};
|
||||
|
||||
bool m_cancellable;
|
||||
bool m_cancelled;
|
||||
std::string m_status_text;
|
||||
u32 m_progress_range;
|
||||
u32 m_progress_value;
|
||||
|
||||
u32 m_base_progress_value;
|
||||
|
||||
State* m_saved_state;
|
||||
};
|
|
@ -0,0 +1,347 @@
|
|||
/* PCSX2 - PS2 Emulator for PCs
|
||||
* Copyright (C) 2002-2021 PCSX2 Dev Team
|
||||
*
|
||||
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU Lesser General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* PCSX2 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with PCSX2.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "PrecompiledHeader.h"
|
||||
|
||||
#include "Timer.h"
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include "RedtapeWindows.h"
|
||||
#elif defined(__APPLE__)
|
||||
#include <mach/mach_init.h>
|
||||
#include <mach/thread_act.h>
|
||||
#include <mach/mach_port.h>
|
||||
#else
|
||||
#include <pthread.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace Common
|
||||
{
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
static double s_counter_frequency;
|
||||
static bool s_counter_initialized = false;
|
||||
|
||||
Timer::Value Timer::GetCurrentValue()
|
||||
{
|
||||
// even if this races, it should still result in the same value..
|
||||
if (!s_counter_initialized)
|
||||
{
|
||||
LARGE_INTEGER Freq;
|
||||
QueryPerformanceFrequency(&Freq);
|
||||
s_counter_frequency = static_cast<double>(Freq.QuadPart) / 1000000000.0;
|
||||
s_counter_initialized = true;
|
||||
}
|
||||
|
||||
Timer::Value ReturnValue;
|
||||
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&ReturnValue));
|
||||
return ReturnValue;
|
||||
}
|
||||
|
||||
double Timer::ConvertValueToNanoseconds(Timer::Value value)
|
||||
{
|
||||
return (static_cast<double>(value) / s_counter_frequency);
|
||||
}
|
||||
|
||||
double Timer::ConvertValueToMilliseconds(Timer::Value value)
|
||||
{
|
||||
return ((static_cast<double>(value) / s_counter_frequency) / 1000000.0);
|
||||
}
|
||||
|
||||
double Timer::ConvertValueToSeconds(Timer::Value value)
|
||||
{
|
||||
return ((static_cast<double>(value) / s_counter_frequency) / 1000000000.0);
|
||||
}
|
||||
|
||||
Timer::Value Timer::ConvertSecondsToValue(double s)
|
||||
{
|
||||
return static_cast<Value>((s * 1000000000.0) * s_counter_frequency);
|
||||
}
|
||||
|
||||
Timer::Value Timer::ConvertMillisecondsToValue(double ms)
|
||||
{
|
||||
return static_cast<Value>((ms * 1000000.0) * s_counter_frequency);
|
||||
}
|
||||
|
||||
Timer::Value Timer::ConvertNanosecondsToValue(double ns)
|
||||
{
|
||||
return static_cast<Value>(ns * s_counter_frequency);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
Timer::Value Timer::GetCurrentValue()
|
||||
{
|
||||
struct timespec tv;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tv);
|
||||
return ((Value)tv.tv_nsec + (Value)tv.tv_sec * 1000000000);
|
||||
}
|
||||
|
||||
double Timer::ConvertValueToNanoseconds(Timer::Value value)
|
||||
{
|
||||
return static_cast<double>(value);
|
||||
}
|
||||
|
||||
double Timer::ConvertValueToMilliseconds(Timer::Value value)
|
||||
{
|
||||
return (static_cast<double>(value) / 1000000.0);
|
||||
}
|
||||
|
||||
double Timer::ConvertValueToSeconds(Timer::Value value)
|
||||
{
|
||||
return (static_cast<double>(value) / 1000000000.0);
|
||||
}
|
||||
|
||||
Timer::Value Timer::ConvertSecondsToValue(double s)
|
||||
{
|
||||
return static_cast<Value>(s * 1000000000.0);
|
||||
}
|
||||
|
||||
Timer::Value Timer::ConvertMillisecondsToValue(double ms)
|
||||
{
|
||||
return static_cast<Value>(ms * 1000000.0);
|
||||
}
|
||||
|
||||
Timer::Value Timer::ConvertNanosecondsToValue(double ns)
|
||||
{
|
||||
return static_cast<Value>(ns);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Timer::Timer()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
void Timer::Reset()
|
||||
{
|
||||
m_tvStartValue = GetCurrentValue();
|
||||
}
|
||||
|
||||
double Timer::GetTimeSeconds() const
|
||||
{
|
||||
return ConvertValueToSeconds(GetCurrentValue() - m_tvStartValue);
|
||||
}
|
||||
|
||||
double Timer::GetTimeMilliseconds() const
|
||||
{
|
||||
return ConvertValueToMilliseconds(GetCurrentValue() - m_tvStartValue);
|
||||
}
|
||||
|
||||
double Timer::GetTimeNanoseconds() const
|
||||
{
|
||||
return ConvertValueToNanoseconds(GetCurrentValue() - m_tvStartValue);
|
||||
}
|
||||
|
||||
double Timer::GetTimeSecondsAndReset()
|
||||
{
|
||||
const Value value = GetCurrentValue();
|
||||
const double ret = ConvertValueToSeconds(value - m_tvStartValue);
|
||||
m_tvStartValue = value;
|
||||
return ret;
|
||||
}
|
||||
|
||||
double Timer::GetTimeMillisecondsAndReset()
|
||||
{
|
||||
const Value value = GetCurrentValue();
|
||||
const double ret = ConvertValueToMilliseconds(value - m_tvStartValue);
|
||||
m_tvStartValue = value;
|
||||
return ret;
|
||||
}
|
||||
|
||||
double Timer::GetTimeNanosecondsAndReset()
|
||||
{
|
||||
const Value value = GetCurrentValue();
|
||||
const double ret = ConvertValueToNanoseconds(value - m_tvStartValue);
|
||||
m_tvStartValue = value;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ThreadCPUTimer::ThreadCPUTimer() = default;
|
||||
|
||||
ThreadCPUTimer::ThreadCPUTimer(ThreadCPUTimer&& move)
|
||||
: m_thread_handle(move.m_thread_handle)
|
||||
{
|
||||
move.m_thread_handle = nullptr;
|
||||
}
|
||||
|
||||
ThreadCPUTimer::~ThreadCPUTimer()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CloseHandle(reinterpret_cast<HANDLE>(m_thread_handle));
|
||||
#endif
|
||||
}
|
||||
|
||||
ThreadCPUTimer& ThreadCPUTimer::operator=(ThreadCPUTimer&& move)
|
||||
{
|
||||
std::swap(m_thread_handle, move.m_thread_handle);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void ThreadCPUTimer::Reset()
|
||||
{
|
||||
m_start_value = GetCurrentValue();
|
||||
}
|
||||
|
||||
ThreadCPUTimer::Value ThreadCPUTimer::GetCurrentValue() const
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
FILETIME create, exit, user, kernel;
|
||||
if (!m_thread_handle || !GetThreadTimes((HANDLE)m_thread_handle, &create, &exit, &user, &kernel))
|
||||
return 0;
|
||||
|
||||
Value value = (static_cast<Value>(user.dwHighDateTime) << 32) | (static_cast<Value>(user.dwLowDateTime));
|
||||
value += (static_cast<Value>(kernel.dwHighDateTime) << 32) | (static_cast<Value>(kernel.dwLowDateTime));
|
||||
return value;
|
||||
#elif defined(__APPLE__)
|
||||
thread_basic_info_data_t info;
|
||||
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
|
||||
|
||||
const kern_return_t kr = thread_info((mach_port_t) reinterpret_cast<uintptr_t>(m_thread_handle), THREAD_BASIC_INFO, (thread_info_t)&info, &count);
|
||||
if (kr != KERN_SUCCESS)
|
||||
return 0;
|
||||
|
||||
Value value = (static_cast<Value>(info.user_time.seconds) * 1000000) + (static_cast<Value>(info.user_time.microseconds));
|
||||
value += (static_cast<Value>(info.system_time.seconds) * 1000000) + (static_cast<Value>(info.system_time.microseconds));
|
||||
return value;
|
||||
#else
|
||||
clockid_t cid;
|
||||
if (!m_thread_handle || pthread_getcpuclockid((pthread_t)m_thread_handle, &cid) != 0)
|
||||
return 0;
|
||||
|
||||
struct timespec ts;
|
||||
if (clock_gettime(cid, &ts) != 0)
|
||||
return 0;
|
||||
|
||||
return (static_cast<Value>(ts.tv_nsec) + static_cast<Value>(ts.tv_sec) * 1000000000LL);
|
||||
#endif
|
||||
}
|
||||
|
||||
double ThreadCPUTimer::GetTimeSeconds() const
|
||||
{
|
||||
return ConvertValueToSeconds(GetCurrentValue() - m_start_value);
|
||||
}
|
||||
|
||||
double ThreadCPUTimer::GetTimeMilliseconds() const
|
||||
{
|
||||
return ConvertValueToMilliseconds(GetCurrentValue() - m_start_value);
|
||||
}
|
||||
|
||||
double ThreadCPUTimer::GetTimeNanoseconds() const
|
||||
{
|
||||
return ConvertValueToNanoseconds(GetCurrentValue() - m_start_value);
|
||||
}
|
||||
|
||||
void ThreadCPUTimer::GetUsageInSecondsAndReset(Value time_diff, double* usage_time, double* usage_percent)
|
||||
{
|
||||
const Value new_value = GetCurrentValue();
|
||||
const Value diff = new_value - m_start_value;
|
||||
m_start_value = new_value;
|
||||
|
||||
*usage_time = ConvertValueToSeconds(diff);
|
||||
*usage_percent = GetUtilizationPercentage(time_diff, diff);
|
||||
}
|
||||
|
||||
void ThreadCPUTimer::GetUsageInMillisecondsAndReset(Value time_diff, double* usage_time, double* usage_percent)
|
||||
{
|
||||
const Value new_value = GetCurrentValue();
|
||||
const Value diff = new_value - m_start_value;
|
||||
m_start_value = new_value;
|
||||
|
||||
*usage_time = ConvertValueToMilliseconds(diff);
|
||||
*usage_percent = GetUtilizationPercentage(time_diff, diff);
|
||||
}
|
||||
|
||||
void ThreadCPUTimer::GetUsageInNanosecondsAndReset(Value time_diff, double* usage_time, double* usage_percent)
|
||||
{
|
||||
const Value new_value = GetCurrentValue();
|
||||
const Value diff = new_value - m_start_value;
|
||||
m_start_value = new_value;
|
||||
|
||||
*usage_time = ConvertValueToNanoseconds(diff);
|
||||
*usage_percent = GetUtilizationPercentage(time_diff, diff);
|
||||
}
|
||||
|
||||
double ThreadCPUTimer::GetUtilizationPercentage(Timer::Value time_diff, Value cpu_time_diff)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
return ((static_cast<double>(cpu_time_diff) * 10000.0) / (static_cast<double>(time_diff) / s_counter_frequency));
|
||||
#elif defined(__APPLE__)
|
||||
// microseconds, but time_tiff is in nanoseconds, so multiply by 1000 * 100
|
||||
return (static_cast<double>(cpu_time_diff) * 100000.0) / static_cast<double>(time_diff);
|
||||
#else
|
||||
// nanoseconds
|
||||
return (static_cast<double>(cpu_time_diff) * 100.0) / static_cast<double>(time_diff);
|
||||
#endif
|
||||
}
|
||||
|
||||
double ThreadCPUTimer::ConvertValueToSeconds(Value value)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
// 100ns units
|
||||
return (static_cast<double>(value) / 10000000.0);
|
||||
#elif defined(__APPLE__)
|
||||
// microseconds
|
||||
return (static_cast<double>(value) / 1000000.0);
|
||||
#else
|
||||
// nanoseconds
|
||||
return (static_cast<double>(value) / 1000000000.0);
|
||||
#endif
|
||||
}
|
||||
|
||||
double ThreadCPUTimer::ConvertValueToMilliseconds(Value value)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
return (static_cast<double>(value) / 10000.0);
|
||||
#elif defined(__APPLE__)
|
||||
return (static_cast<double>(value) / 1000.0);
|
||||
#else
|
||||
return (static_cast<double>(value) / 1000000.0);
|
||||
#endif
|
||||
}
|
||||
|
||||
double ThreadCPUTimer::ConvertValueToNanoseconds(Value value)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
return (static_cast<double>(value) * 100.0);
|
||||
#elif defined(__APPLE__)
|
||||
return (static_cast<double>(value) * 1000.0);
|
||||
#else
|
||||
return static_cast<double>(value);
|
||||
#endif
|
||||
}
|
||||
|
||||
ThreadCPUTimer ThreadCPUTimer::GetForCallingThread()
|
||||
{
|
||||
ThreadCPUTimer ret;
|
||||
#if defined(_WIN32)
|
||||
ret.m_thread_handle = (void*)OpenThread(THREAD_QUERY_INFORMATION, FALSE, GetCurrentThreadId());
|
||||
#elif defined(__APPLE__)
|
||||
ret.m_thread_handle = reinterpret_cast<void*>((uintptr_t)mach_thread_self());
|
||||
#else
|
||||
ret.m_thread_handle = (void*)pthread_self();
|
||||
#endif
|
||||
ret.Reset();
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace Common
|
|
@ -0,0 +1,94 @@
|
|||
/* PCSX2 - PS2 Emulator for PCs
|
||||
* Copyright (C) 2002-2021 PCSX2 Dev Team
|
||||
*
|
||||
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU Lesser General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* PCSX2 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with PCSX2.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
namespace Common
|
||||
{
|
||||
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
using Value = std::uint64_t;
|
||||
|
||||
Timer();
|
||||
|
||||
static Value GetCurrentValue();
|
||||
static double ConvertValueToSeconds(Value value);
|
||||
static double ConvertValueToMilliseconds(Value value);
|
||||
static double ConvertValueToNanoseconds(Value value);
|
||||
static Value ConvertSecondsToValue(double s);
|
||||
static Value ConvertMillisecondsToValue(double s);
|
||||
static Value ConvertNanosecondsToValue(double ns);
|
||||
|
||||
void Reset();
|
||||
void ResetTo(Value value) { m_tvStartValue = value; }
|
||||
|
||||
Value GetStartValue() const { return m_tvStartValue; }
|
||||
|
||||
double GetTimeSeconds() const;
|
||||
double GetTimeMilliseconds() const;
|
||||
double GetTimeNanoseconds() const;
|
||||
|
||||
double GetTimeSecondsAndReset();
|
||||
double GetTimeMillisecondsAndReset();
|
||||
double GetTimeNanosecondsAndReset();
|
||||
|
||||
private:
|
||||
Value m_tvStartValue;
|
||||
};
|
||||
|
||||
class ThreadCPUTimer
|
||||
{
|
||||
public:
|
||||
using Value = std::uint64_t;
|
||||
|
||||
ThreadCPUTimer();
|
||||
ThreadCPUTimer(ThreadCPUTimer&& move);
|
||||
ThreadCPUTimer(const ThreadCPUTimer&) = delete;
|
||||
~ThreadCPUTimer();
|
||||
|
||||
void Reset();
|
||||
void ResetTo(Value value) { m_start_value = value; }
|
||||
|
||||
Value GetStartValue() const { return m_start_value; }
|
||||
Value GetCurrentValue() const;
|
||||
|
||||
double GetTimeSeconds() const;
|
||||
double GetTimeMilliseconds() const;
|
||||
double GetTimeNanoseconds() const;
|
||||
|
||||
void GetUsageInSecondsAndReset(Value time_diff, double* usage_time, double* usage_percent);
|
||||
void GetUsageInMillisecondsAndReset(Value time_diff, double* usage_time, double* usage_percent);
|
||||
void GetUsageInNanosecondsAndReset(Value time_diff, double* usage_time, double* usage_percent);
|
||||
|
||||
static double GetUtilizationPercentage(Timer::Value time_diff, Value cpu_time_diff);
|
||||
|
||||
static double ConvertValueToSeconds(Value value);
|
||||
static double ConvertValueToMilliseconds(Value value);
|
||||
static double ConvertValueToNanoseconds(Value value);
|
||||
|
||||
static ThreadCPUTimer GetForCallingThread();
|
||||
|
||||
ThreadCPUTimer& operator=(const ThreadCPUTimer&) = delete;
|
||||
ThreadCPUTimer& operator=(ThreadCPUTimer&& move);
|
||||
|
||||
private:
|
||||
void* m_thread_handle = nullptr;
|
||||
Value m_start_value = 0;
|
||||
};
|
||||
|
||||
} // namespace Common
|
|
@ -54,10 +54,12 @@
|
|||
<ClCompile Include="GL\StreamBuffer.cpp" />
|
||||
<ClCompile Include="FileSystem.cpp" />
|
||||
<ClCompile Include="IniInterface.cpp" />
|
||||
<ClCompile Include="ProgressCallback.cpp" />
|
||||
<ClCompile Include="pxStreams.cpp" />
|
||||
<ClCompile Include="pxTranslate.cpp" />
|
||||
<ClCompile Include="StringUtil.cpp" />
|
||||
<ClCompile Include="SettingsWrapper.cpp" />
|
||||
<ClCompile Include="Timer.cpp" />
|
||||
<ClCompile Include="VirtualMemory.cpp" />
|
||||
<ClCompile Include="WindowInfo.cpp" />
|
||||
<ClCompile Include="x86\MemcpyFast.cpp" />
|
||||
|
@ -110,6 +112,7 @@
|
|||
<ClInclude Include="GL\ContextWGL.h" />
|
||||
<ClInclude Include="GL\StreamBuffer.h" />
|
||||
<ClInclude Include="FileSystem.h" />
|
||||
<ClInclude Include="ProgressCallback.h" />
|
||||
<ClInclude Include="ScopedGuard.h" />
|
||||
<ClInclude Include="StringUtil.h" />
|
||||
<ClInclude Include="SettingsInterface.h" />
|
||||
|
@ -128,6 +131,7 @@
|
|||
<ClInclude Include="RedtapeWindows.h" />
|
||||
<ClInclude Include="SafeArray.h" />
|
||||
<ClInclude Include="StringHelpers.h" />
|
||||
<ClInclude Include="Timer.h" />
|
||||
<ClInclude Include="WindowInfo.h" />
|
||||
<ClInclude Include="wxBaseTools.h" />
|
||||
<ClInclude Include="Threading.h" />
|
||||
|
|
|
@ -131,6 +131,15 @@
|
|||
<Filter>Source Files\GL</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="FileSystem.cpp">
|
||||
<Filter>Source Files\GL</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="StringUtil.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Timer.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ProgressCallback.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
|
@ -285,9 +294,6 @@
|
|||
<ClInclude Include="SettingsWrapper.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ScopedGuard.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Align.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
|
@ -307,6 +313,21 @@
|
|||
<Filter>Header Files\GL</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="FileSystem.h">
|
||||
<Filter>Header Files\GL</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="StringUtil.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ScopedGuard.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Timer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ProgressCallback.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ScopedGuard.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
@ -334,4 +355,4 @@
|
|||
<Filter>Source Files</Filter>
|
||||
</MASM>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
Loading…
Reference in New Issue