diff options
| author | xinef1 <w.szyszko2@gmail.com> | 2017-03-02 02:19:25 +0100 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2019-08-17 20:04:14 +0200 |
| commit | 60663d1374beef3103f4787152654034fa4a8897 (patch) | |
| tree | 38e07d44442ad903a9729536942e8e253a072274 /src/server/game/Time | |
| parent | 98180ecdc179386270e93b80c0db8344b659557f (diff) | |
Ensure that all actions are compared to fixed point in time (ie. world update start) (#18910)
- Actions will not be dependent on processing moment
- Increased GameObjects cooldown resolution to milliseconds, fixes arming time of traps to be exactly one second and not something from range (1000, 1999)
- Created GameTime namespace and UpdateTime class and moved there some code out of world
(cherrypicked from 7567cafec84080d26ea513242a1f540a823b8f9d)
Diffstat (limited to 'src/server/game/Time')
| -rw-r--r-- | src/server/game/Time/GameTime.cpp | 68 | ||||
| -rw-r--r-- | src/server/game/Time/GameTime.h | 48 | ||||
| -rw-r--r-- | src/server/game/Time/UpdateTime.cpp | 135 | ||||
| -rw-r--r-- | src/server/game/Time/UpdateTime.h | 74 |
4 files changed, 325 insertions, 0 deletions
diff --git a/src/server/game/Time/GameTime.cpp b/src/server/game/Time/GameTime.cpp new file mode 100644 index 00000000000..8884d4612be --- /dev/null +++ b/src/server/game/Time/GameTime.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.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 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 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 +{ + time_t const StartTime = time(nullptr); + + time_t GameTime = 0; + uint32 GameMSTime = 0; + + std::chrono::system_clock::time_point GameTimeSystemPoint = std::chrono::system_clock::time_point::min(); + std::chrono::steady_clock::time_point GameTimeSteadyPoint = std::chrono::steady_clock::time_point::min(); + + time_t GetStartTime() + { + return StartTime; + } + + time_t GetGameTime() + { + return GameTime; + } + + uint32 GetGameTimeMS() + { + return GameMSTime; + } + + std::chrono::system_clock::time_point GetGameTimeSystemPoint() + { + return GameTimeSystemPoint; + } + + std::chrono::steady_clock::time_point GetGameTimeSteadyPoint() + { + return GameTimeSteadyPoint; + } + + uint32 GetUptime() + { + return uint32(GameTime - StartTime); + } + + void UpdateGameTimers() + { + GameTime = time(nullptr); + GameMSTime = getMSTime(); + GameTimeSystemPoint = std::chrono::system_clock::now(); + GameTimeSteadyPoint = std::chrono::steady_clock::now(); + } +} diff --git a/src/server/game/Time/GameTime.h b/src/server/game/Time/GameTime.h new file mode 100644 index 00000000000..2b130962a02 --- /dev/null +++ b/src/server/game/Time/GameTime.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.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 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 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 <chrono> + +namespace GameTime +{ + // Server start time + time_t GetStartTime(); + + // Current server time (unix) in seconds + time_t GetGameTime(); + + // Milliseconds since server start + uint32 GetGameTimeMS(); + + /// Current chrono system_clock time point + std::chrono::system_clock::time_point GetGameTimeSystemPoint(); + + /// Current chrono steady_clock time point + std::chrono::steady_clock::time_point GetGameTimeSteadyPoint(); + + /// Uptime (in secs) + uint32 GetUptime(); + + void UpdateGameTimers(); +}; + +#endif diff --git a/src/server/game/Time/UpdateTime.cpp b/src/server/game/Time/UpdateTime.cpp new file mode 100644 index 00000000000..1cc9fd1c507 --- /dev/null +++ b/src/server/game/Time/UpdateTime.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.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 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 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 "Timer.h" +#include "Config.h" +#include "Log.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; + } + 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 = getMSTime(); +} + +void UpdateTime::_RecordUpdateTimeDuration(std::string const& text, uint32 minUpdateTime) +{ + uint32 thisTime = getMSTime(); + uint32 diff = getMSTimeDiff(_recordedTime, thisTime); + + if (diff > minUpdateTime) + TC_LOG_INFO("misc", "Recored Update Time of %s: %u.", text.c_str(), diff); + + _recordedTime = thisTime; +} + +void WorldUpdateTime::LoadFromConfig() +{ + _recordUpdateTimeInverval = sConfigMgr->GetIntDefault("RecordUpdateTimeDiffInterval", 60000); + _recordUpdateTimeMin = sConfigMgr->GetIntDefault("MinRecordUpdateTimeDiff", 100); +} + +void WorldUpdateTime::SetRecordUpdateTimeInterval(uint32 t) +{ + _recordUpdateTimeInverval = t; +} + +void WorldUpdateTime::RecordUpdateTime(uint32 gameTimeMs, uint32 diff, uint32 sessionCount) +{ + if (_recordUpdateTimeInverval > 0 && diff > _recordUpdateTimeMin) + { + if (getMSTimeDiff(_lastRecordTime, gameTimeMs) > _recordUpdateTimeInverval) + { + TC_LOG_DEBUG("misc", "Update time diff: %u. Players online: %u.", GetAverageUpdateTime(), sessionCount); + _lastRecordTime = gameTimeMs; + } + } +} + +void WorldUpdateTime::RecordUpdateTimeDuration(std::string const& text) +{ + _RecordUpdateTimeDuration(text, _recordUpdateTimeMin); +} diff --git a/src/server/game/Time/UpdateTime.h b/src/server/game/Time/UpdateTime.h new file mode 100644 index 00000000000..32ddf49c8a4 --- /dev/null +++ b/src/server/game/Time/UpdateTime.h @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.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 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 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" + +#define AVG_DIFF_COUNT 500 + +class TC_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(); + + void _RecordUpdateTimeDuration(std::string const& text, uint32 minUpdateTime); + + private: + DiffTableArray _updateTimeDataTable; + uint32 _averageUpdateTime; + uint32 _totalUpdateTime; + uint32 _updateTimeTableIndex; + uint32 _maxUpdateTime; + uint32 _maxUpdateTimeOfLastTable; + uint32 _maxUpdateTimeOfCurrentTable; + + uint32 _recordedTime; +}; + +class WorldUpdateTime : public UpdateTime +{ + public: + WorldUpdateTime() : UpdateTime(), _recordUpdateTimeInverval(0), _recordUpdateTimeMin(0), _lastRecordTime(0) { } + void LoadFromConfig(); + void SetRecordUpdateTimeInterval(uint32 t); + void RecordUpdateTime(uint32 gameTimeMs, uint32 diff, uint32 sessionCount); + void RecordUpdateTimeDuration(std::string const& text); + + private: + uint32 _recordUpdateTimeInverval; + uint32 _recordUpdateTimeMin; + uint32 _lastRecordTime; +}; + +TC_GAME_API extern WorldUpdateTime sWorldUpdateTime; + +#endif |
