Physics, Lua, world, mechanics and the navigator reported their per-frame numbers straight into osg::Stats and timed themselves with osg::Timer, so every one of them carried OSG in its headers only to fill the profiler. They now report through Misc::FrameStats, a two-method interface (collectStats, setAttribute) that engine.cpp adapts to the viewer's osg::Stats, and they time with std::chrono::steady_clock; the engine expresses the viewer start tick on that clock, so the reported times stay on the timeline the OSG profiler draws and the stats file records.
151 lines
4.2 KiB
C++
151 lines
4.2 KiB
C++
#ifndef OPENMW_PROFILE_H
|
|
#define OPENMW_PROFILE_H
|
|
|
|
#include <components/misc/framestats.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <string>
|
|
|
|
namespace OMW
|
|
{
|
|
struct UserStats
|
|
{
|
|
const std::string mLabel;
|
|
const std::string mBegin;
|
|
const std::string mEnd;
|
|
const std::string mTaken;
|
|
|
|
explicit UserStats(const std::string& label, const std::string& prefix)
|
|
: mLabel(label)
|
|
, mBegin(prefix + "_time_begin")
|
|
, mEnd(prefix + "_time_end")
|
|
, mTaken(prefix + "_time_taken")
|
|
{
|
|
}
|
|
};
|
|
|
|
enum class UserStatsType : std::size_t
|
|
{
|
|
Input,
|
|
Sound,
|
|
LuaSyncUpdate,
|
|
State,
|
|
Script,
|
|
Mechanics,
|
|
Physics,
|
|
PhysicsWorker,
|
|
World,
|
|
Gui,
|
|
Focus,
|
|
Lua,
|
|
Number,
|
|
};
|
|
|
|
template <UserStatsType type>
|
|
struct UserStatsValue
|
|
{
|
|
static const UserStats sValue;
|
|
};
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::Input>::sValue{ "Input", "input" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::Sound>::sValue{ "Sound", "sound" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::State>::sValue{ "State", "state" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::Script>::sValue{ "Script", "script" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::Mechanics>::sValue{ "Mech", "mechanics" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::Physics>::sValue{ "Phys", "physics" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::PhysicsWorker>::sValue{ " -Async", "physicsworker" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::World>::sValue{ "World", "world" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::Gui>::sValue{ "GUI", "gui" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::Lua>::sValue{ "Lua", "lua" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::LuaSyncUpdate>::sValue{ "LuaSync", "luasyncupdate" };
|
|
|
|
template <>
|
|
inline const UserStats UserStatsValue<UserStatsType::Focus>::sValue{ "Focus", "focusobject" };
|
|
|
|
template <UserStatsType type>
|
|
struct ForEachUserStatsValue
|
|
{
|
|
template <class F>
|
|
static void apply(F&& f)
|
|
{
|
|
f(UserStatsValue<type>::sValue);
|
|
using Next = ForEachUserStatsValue<static_cast<UserStatsType>(static_cast<std::size_t>(type) + 1)>;
|
|
Next::apply(std::forward<F>(f));
|
|
}
|
|
};
|
|
|
|
template <>
|
|
struct ForEachUserStatsValue<UserStatsType::Number>
|
|
{
|
|
template <class F>
|
|
static void apply(F&&)
|
|
{
|
|
}
|
|
};
|
|
|
|
template <class F>
|
|
void forEachUserStatsValue(F&& f)
|
|
{
|
|
ForEachUserStatsValue<static_cast<UserStatsType>(0)>::apply(std::forward<F>(f));
|
|
}
|
|
|
|
template <UserStatsType type>
|
|
class ScopedProfile
|
|
{
|
|
public:
|
|
explicit ScopedProfile(
|
|
Misc::FrameClock::time_point statsEpoch, unsigned int frameNumber, Misc::FrameStats& stats)
|
|
: mScopeStart(Misc::FrameClock::now())
|
|
, mStatsEpoch(statsEpoch)
|
|
, mFrameNumber(frameNumber)
|
|
, mStats(stats)
|
|
{
|
|
}
|
|
|
|
ScopedProfile(const ScopedProfile&) = delete;
|
|
ScopedProfile& operator=(const ScopedProfile&) = delete;
|
|
|
|
~ScopedProfile()
|
|
{
|
|
if (!mStats.collectStats("engine"))
|
|
return;
|
|
|
|
const Misc::FrameClock::time_point end = Misc::FrameClock::now();
|
|
const UserStats& stats = UserStatsValue<type>::sValue;
|
|
|
|
mStats.setAttribute(mFrameNumber, stats.mBegin, Misc::secondsBetween(mStatsEpoch, mScopeStart));
|
|
mStats.setAttribute(mFrameNumber, stats.mTaken, Misc::secondsBetween(mScopeStart, end));
|
|
mStats.setAttribute(mFrameNumber, stats.mEnd, Misc::secondsBetween(mStatsEpoch, end));
|
|
}
|
|
|
|
private:
|
|
const Misc::FrameClock::time_point mScopeStart;
|
|
const Misc::FrameClock::time_point mStatsEpoch;
|
|
const unsigned int mFrameNumber;
|
|
Misc::FrameStats& mStats;
|
|
};
|
|
}
|
|
|
|
#endif
|