mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-30 09:03:47 +00:00
feat(Core/Time): Implement saparated manager for game time (#8630)
This commit is contained in:
70
src/server/game/Time/GameTime.cpp
Normal file
70
src/server/game/Time/GameTime.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by the
|
||||
* Free Software Foundation; either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "GameTime.h"
|
||||
#include "Timer.h"
|
||||
|
||||
namespace GameTime
|
||||
{
|
||||
using namespace std::chrono;
|
||||
|
||||
Seconds const StartTime = GetEpochTime();
|
||||
|
||||
Seconds GameTime = GetEpochTime();
|
||||
Milliseconds GameMSTime = 0ms;
|
||||
|
||||
SystemTimePoint GameTimeSystemPoint = SystemTimePoint::min();
|
||||
TimePoint GameTimeSteadyPoint = TimePoint::min();
|
||||
|
||||
Seconds GetStartTime()
|
||||
{
|
||||
return StartTime;
|
||||
}
|
||||
|
||||
Seconds GetGameTime()
|
||||
{
|
||||
return GameTime;
|
||||
}
|
||||
|
||||
Milliseconds GetGameTimeMS()
|
||||
{
|
||||
return GameMSTime;
|
||||
}
|
||||
|
||||
SystemTimePoint GetSystemTime()
|
||||
{
|
||||
return GameTimeSystemPoint;
|
||||
}
|
||||
|
||||
TimePoint Now()
|
||||
{
|
||||
return GameTimeSteadyPoint;
|
||||
}
|
||||
|
||||
Seconds GetUptime()
|
||||
{
|
||||
return GameTime - StartTime;
|
||||
}
|
||||
|
||||
void UpdateGameTimers()
|
||||
{
|
||||
GameTime = GetEpochTime();
|
||||
GameMSTime = GetTimeMS();
|
||||
GameTimeSystemPoint = system_clock::now();
|
||||
GameTimeSteadyPoint = steady_clock::now();
|
||||
}
|
||||
}
|
||||
48
src/server/game/Time/GameTime.h
Normal file
48
src/server/game/Time/GameTime.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by the
|
||||
* Free Software Foundation; either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __GAMETIME_H
|
||||
#define __GAMETIME_H
|
||||
|
||||
#include "Define.h"
|
||||
#include "Duration.h"
|
||||
|
||||
namespace GameTime
|
||||
{
|
||||
// Server start time
|
||||
AC_GAME_API Seconds GetStartTime();
|
||||
|
||||
// Current server time (unix)
|
||||
AC_GAME_API Seconds GetGameTime();
|
||||
|
||||
// Milliseconds since server start
|
||||
AC_GAME_API Milliseconds GetGameTimeMS();
|
||||
|
||||
/// Current chrono system_clock time point
|
||||
AC_GAME_API SystemTimePoint GetSystemTime();
|
||||
|
||||
/// Current chrono steady_clock time point
|
||||
AC_GAME_API TimePoint Now();
|
||||
|
||||
/// Uptime
|
||||
AC_GAME_API Seconds GetUptime();
|
||||
|
||||
/// Update all timers
|
||||
void UpdateGameTimers();
|
||||
}
|
||||
|
||||
#endif
|
||||
124
src/server/game/Time/UpdateTime.cpp
Normal file
124
src/server/game/Time/UpdateTime.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by the
|
||||
* Free Software Foundation; either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "UpdateTime.h"
|
||||
#include "Config.h"
|
||||
#include "Log.h"
|
||||
#include "Timer.h"
|
||||
|
||||
// create instance
|
||||
WorldUpdateTime sWorldUpdateTime;
|
||||
|
||||
UpdateTime::UpdateTime()
|
||||
{
|
||||
_averageUpdateTime = 0;
|
||||
_totalUpdateTime = 0;
|
||||
_updateTimeTableIndex = 0;
|
||||
_maxUpdateTime = 0;
|
||||
_maxUpdateTimeOfLastTable = 0;
|
||||
_maxUpdateTimeOfCurrentTable = 0;
|
||||
|
||||
_updateTimeDataTable = { };
|
||||
}
|
||||
|
||||
uint32 UpdateTime::GetAverageUpdateTime() const
|
||||
{
|
||||
return _averageUpdateTime;
|
||||
}
|
||||
|
||||
uint32 UpdateTime::GetTimeWeightedAverageUpdateTime() const
|
||||
{
|
||||
uint32 sum = 0, weightsum = 0;
|
||||
|
||||
for (uint32 diff : _updateTimeDataTable)
|
||||
{
|
||||
sum += diff * diff;
|
||||
weightsum += diff;
|
||||
}
|
||||
|
||||
if (weightsum == 0)
|
||||
return 0;
|
||||
|
||||
return sum / weightsum;
|
||||
}
|
||||
|
||||
uint32 UpdateTime::GetMaxUpdateTime() const
|
||||
{
|
||||
return _maxUpdateTime;
|
||||
}
|
||||
|
||||
uint32 UpdateTime::GetMaxUpdateTimeOfCurrentTable() const
|
||||
{
|
||||
return std::max(_maxUpdateTimeOfCurrentTable, _maxUpdateTimeOfLastTable);
|
||||
}
|
||||
|
||||
uint32 UpdateTime::GetLastUpdateTime() const
|
||||
{
|
||||
return _updateTimeDataTable[_updateTimeTableIndex != 0 ? _updateTimeTableIndex - 1 : _updateTimeDataTable.size() - 1];
|
||||
}
|
||||
|
||||
void UpdateTime::UpdateWithDiff(uint32 diff)
|
||||
{
|
||||
_totalUpdateTime = _totalUpdateTime - _updateTimeDataTable[_updateTimeTableIndex] + diff;
|
||||
_updateTimeDataTable[_updateTimeTableIndex] = diff;
|
||||
|
||||
if (diff > _maxUpdateTime)
|
||||
_maxUpdateTime = diff;
|
||||
|
||||
if (diff > _maxUpdateTimeOfCurrentTable)
|
||||
_maxUpdateTimeOfCurrentTable = diff;
|
||||
|
||||
if (++_updateTimeTableIndex >= _updateTimeDataTable.size())
|
||||
{
|
||||
_updateTimeTableIndex = 0;
|
||||
_maxUpdateTimeOfLastTable = _maxUpdateTimeOfCurrentTable;
|
||||
_maxUpdateTimeOfCurrentTable = 0;
|
||||
}
|
||||
|
||||
if (_updateTimeDataTable[_updateTimeDataTable.size() - 1])
|
||||
_averageUpdateTime = _totalUpdateTime / _updateTimeDataTable.size();
|
||||
else if (_updateTimeTableIndex)
|
||||
_averageUpdateTime = _totalUpdateTime / _updateTimeTableIndex;
|
||||
}
|
||||
|
||||
void UpdateTime::RecordUpdateTimeReset()
|
||||
{
|
||||
_recordedTime = GetTimeMS();
|
||||
}
|
||||
|
||||
void WorldUpdateTime::LoadFromConfig()
|
||||
{
|
||||
_recordUpdateTimeInverval = Milliseconds(sConfigMgr->GetOption<uint32>("RecordUpdateTimeDiffInterval", 60000));
|
||||
_recordUpdateTimeMin = Milliseconds(sConfigMgr->GetOption<uint32>("MinRecordUpdateTimeDiff", 100));
|
||||
}
|
||||
|
||||
void WorldUpdateTime::SetRecordUpdateTimeInterval(Milliseconds t)
|
||||
{
|
||||
_recordUpdateTimeInverval = t;
|
||||
}
|
||||
|
||||
void WorldUpdateTime::RecordUpdateTime(Milliseconds gameTimeMs, uint32 diff, uint32 sessionCount)
|
||||
{
|
||||
if (_recordUpdateTimeInverval > 0s && diff > _recordUpdateTimeMin.count())
|
||||
{
|
||||
if (GetMSTimeDiff(_lastRecordTime, gameTimeMs) > _recordUpdateTimeInverval)
|
||||
{
|
||||
FMT_LOG_INFO("time.update", "Update time diff: {}. Players online: {}.", GetAverageUpdateTime(), sessionCount);
|
||||
_lastRecordTime = gameTimeMs;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
src/server/game/Time/UpdateTime.h
Normal file
75
src/server/game/Time/UpdateTime.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by the
|
||||
* Free Software Foundation; either version 3 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* 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 Affero General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __UPDATETIME_H
|
||||
#define __UPDATETIME_H
|
||||
|
||||
#include "Define.h"
|
||||
#include "Duration.h"
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
constexpr auto AVG_DIFF_COUNT = 500;
|
||||
|
||||
class AC_GAME_API UpdateTime
|
||||
{
|
||||
using DiffTableArray = std::array<uint32, AVG_DIFF_COUNT>;
|
||||
|
||||
public:
|
||||
uint32 GetAverageUpdateTime() const;
|
||||
uint32 GetTimeWeightedAverageUpdateTime() const;
|
||||
uint32 GetMaxUpdateTime() const;
|
||||
uint32 GetMaxUpdateTimeOfCurrentTable() const;
|
||||
uint32 GetLastUpdateTime() const;
|
||||
|
||||
void UpdateWithDiff(uint32 diff);
|
||||
|
||||
void RecordUpdateTimeReset();
|
||||
|
||||
protected:
|
||||
UpdateTime();
|
||||
|
||||
private:
|
||||
DiffTableArray _updateTimeDataTable;
|
||||
uint32 _averageUpdateTime;
|
||||
uint32 _totalUpdateTime;
|
||||
uint32 _updateTimeTableIndex;
|
||||
uint32 _maxUpdateTime;
|
||||
uint32 _maxUpdateTimeOfLastTable;
|
||||
uint32 _maxUpdateTimeOfCurrentTable;
|
||||
|
||||
Milliseconds _recordedTime;
|
||||
};
|
||||
|
||||
class AC_GAME_API WorldUpdateTime : public UpdateTime
|
||||
{
|
||||
public:
|
||||
WorldUpdateTime() : UpdateTime(), _recordUpdateTimeInverval(0), _recordUpdateTimeMin(0), _lastRecordTime(0) { }
|
||||
void LoadFromConfig();
|
||||
void SetRecordUpdateTimeInterval(Milliseconds t);
|
||||
void RecordUpdateTime(Milliseconds gameTimeMs, uint32 diff, uint32 sessionCount);
|
||||
void RecordUpdateTimeDuration(std::string const& text);
|
||||
|
||||
private:
|
||||
Milliseconds _recordUpdateTimeInverval;
|
||||
Milliseconds _recordUpdateTimeMin;
|
||||
Milliseconds _lastRecordTime;
|
||||
};
|
||||
|
||||
AC_GAME_API extern WorldUpdateTime sWorldUpdateTime;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user