Skip to content

Commit

Permalink
Add MSPT support
Browse files Browse the repository at this point in the history
  • Loading branch information
Trompettesib committed Nov 9, 2023
1 parent c8f1d6b commit f4bf3a7
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 15 deletions.
35 changes: 24 additions & 11 deletions cubic-server/Dimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ Dimension::Dimension(std::shared_ptr<World> world, world_storage::DimensionType
_processingThread(),
_dimensionType(dimensionType),
_circularBufferTps(18000),
_previousTickTime(std::chrono::high_resolution_clock::now())
_previousTickTime(std::chrono::high_resolution_clock::now()),
_circularBufferMSPT(18000)
{
}

void Dimension::tick()
{
auto startTickTime = std::chrono::high_resolution_clock::now();
{
std::lock_guard _(_entitiesMutex);
for (auto ent : _entities) {
Expand Down Expand Up @@ -66,9 +68,12 @@ void Dimension::tick()
}
auto endTime = std::chrono::high_resolution_clock::now();
auto compute_time = endTime - _previousTickTime;
auto msptTime = endTime - startTickTime;
_previousTickTime = endTime;
float compute_time_micro = (float) std::chrono::duration_cast<std::chrono::microseconds>(compute_time).count();
float msptTime_micro = (float) std::chrono::duration_cast<std::chrono::microseconds>(msptTime).count();
_circularBufferTps.push_back(compute_time_micro);
_circularBufferMSPT.push_back(msptTime_micro);
}

void Dimension::stop()
Expand Down Expand Up @@ -350,21 +355,16 @@ void Dimension::sendChunkToPlayers(int x, int z)
this->_loadingChunks.erase({x, z});
}

Tps Dimension::getTps()
Tps Dimension::getTps() const
{
constexpr int TICK_PER_MINUTE = 20 * 60;
constexpr int TICKS_FOR_FIVE_MINUTES = TICK_PER_MINUTE * 5;
constexpr int TICKS_FOR_FIFTEEN_MINUTES = TICK_PER_MINUTE * 15;
constexpr float MICROSECS_IN_ONE_SEC = 1000000.0f;

const auto &buffer_size = _circularBufferTps.size();
const auto &buffer_end = _circularBufferTps.end();
const float tpsOnFullBuffer = 1.0f / ((std::accumulate(buffer_end - buffer_size, buffer_end, 0.0f) / (float) buffer_size) / MICROSECS_IN_ONE_SEC);
Tps tps {0, 0, 0};

auto tpsCalculation = [buffer_end](const int tick_number) {
const auto tpsCalculation = [buffer_end](const int tick_number) {
return 1.0f / ((std::accumulate(buffer_end - tick_number, buffer_end, 0.0f) / (float) (tick_number)) / MICROSECS_IN_ONE_SEC);
};
const float tpsOnFullBuffer = tpsCalculation(buffer_size);

Tps tps {0, 0, 0};

if (buffer_size < TICK_PER_MINUTE - 1) {
tps.oneMinTps = tps.fiveMinTps = tps.fifteenMinTps = tpsOnFullBuffer;
Expand All @@ -383,3 +383,16 @@ Tps Dimension::getTps()
tps.fifteenMinTps = tpsCalculation(TICKS_FOR_FIFTEEN_MINUTES);
return tps;
}

MSPTInfos Dimension::getMSPTInfos() const
{
const auto &buffer_begin = _circularBufferMSPT.begin();
const auto &buffer_end = _circularBufferMSPT.end();
// clang-format off
return {
*std::min_element(buffer_begin, buffer_end) / MILLIS_IN_ONE_SEC,
(std::accumulate(buffer_begin, buffer_end, 0.0f) / float(_circularBufferMSPT.size())) / MILLIS_IN_ONE_SEC,
*std::max_element(buffer_begin, buffer_end) / MILLIS_IN_ONE_SEC
};
// clang-format on
}
16 changes: 14 additions & 2 deletions cubic-server/Dimension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
#include "protocol/ClientPackets.hpp"
#include "world_storage/ChunkColumn.hpp"
#include "world_storage/Level.hpp"
#include <boost/circular_buffer.hpp>

// TODO(huntears): Fix whatever this is
constexpr int SEMAPHORE_MAX = 1000;
constexpr int TICK_PER_MINUTE = 20 * 60;
constexpr int TICKS_FOR_FIVE_MINUTES = TICK_PER_MINUTE * 5;
constexpr int TICKS_FOR_FIFTEEN_MINUTES = TICK_PER_MINUTE * 15;
constexpr float MICROSECS_IN_ONE_SEC = 1000000.0f;
constexpr float MILLIS_IN_ONE_SEC = 1000.0f;

class World;
class Player;
Expand Down Expand Up @@ -150,7 +154,14 @@ class Dimension : public std::enable_shared_from_this<Dimension> {
*
* @return Tps
*/
virtual Tps getTps();
virtual Tps getTps() const;

/**
* @brief Get the MSPTInfos of the dimension
*
* @return MSPTInfos
*/
virtual MSPTInfos getMSPTInfos() const;

protected:
virtual void _run();
Expand All @@ -176,6 +187,7 @@ class Dimension : public std::enable_shared_from_this<Dimension> {
world_storage::DimensionType _dimensionType;
boost::circular_buffer_space_optimized<float> _circularBufferTps;
std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<long, std::ratio<1, 1000000000>>> _previousTickTime;
boost::circular_buffer_space_optimized<float> _circularBufferMSPT;
};

template<isBaseOf<Entity> T, typename... Args>
Expand Down
1 change: 1 addition & 0 deletions cubic-server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Server::Server():
_commands.emplace_back(std::make_unique<command_parser::Tp>());
_commands.emplace_back(std::make_unique<command_parser::Teleport>());
_commands.emplace_back(std::make_unique<command_parser::Tps>());
_commands.emplace_back(std::make_unique<command_parser::MSPT>());
}

Server::~Server() { }
Expand Down
9 changes: 9 additions & 0 deletions cubic-server/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Server.hpp"
#include "WorldGroup.hpp"
#include "logging/logging.hpp"
#include "types.hpp"
#include <cstdint>
#include <vector>

Expand Down Expand Up @@ -195,3 +196,11 @@ std::vector<std::pair<std::string, Tps>> World::getTps() const
tps.emplace_back(name, dim->getTps());
return tps;
}

std::vector<std::pair<std::string, MSPTInfos>> World::getMSPTInfos() const
{
std::vector<std::pair<std::string, MSPTInfos>> msptInfos;
for (const auto &[name, dim] : _dimensions)
msptInfos.emplace_back(name, dim->getMSPTInfos());
return msptInfos;
}
6 changes: 6 additions & 0 deletions cubic-server/World.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class World : public std::enable_shared_from_this<World> {
*/
virtual std::vector<std::pair<std::string, Tps>> getTps() const;

/**
* @brief Get the mspt of all dimensions
* @return a vector of pairs of dimension name and their mspt
*/
virtual std::vector<std::pair<std::string, MSPTInfos>> getMSPTInfos() const;

protected:
std::shared_ptr<Chat> _chat;
std::shared_ptr<WorldGroup> _worldGroup;
Expand Down
1 change: 1 addition & 0 deletions cubic-server/allCommands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
#include "command_parser/commands/Teleport.hpp"
#include "command_parser/commands/Time.hpp"
#include "command_parser/commands/Tps.hpp"
#include "command_parser/commands/MSPT.hpp"
2 changes: 2 additions & 0 deletions cubic-server/command_parser/commands/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ target_sources (${CMAKE_PROJECT_NAME} PRIVATE
Teleport.hpp
Tps.cpp
Tps.hpp
MSPT.cpp
MSPT.hpp
)
66 changes: 66 additions & 0 deletions cubic-server/command_parser/commands/MSPT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "MSPT.hpp"

#include "Chat.hpp"
#include "Dimension.hpp"
#include "Player.hpp"
#include "Server.hpp"
#include "World.hpp"
#include "logging/logging.hpp"
#include <vector>

using namespace command_parser;

void MSPT::autocomplete(UNUSED std::vector<std::string> &args, UNUSED Player *invoker) const { }

void MSPT::execute(std::vector<std::string> &args, Player *invoker) const
{
switch (args.size()) {
case 0: {
if (invoker) {
auto msg = chat::Message("Dimension MSPT: min avg max");
msg.style().italic = true;
invoker->getDimension()->getWorld()->getChat()->sendSystemMessage(msg, *invoker);
for (const auto &each : invoker->getDimension()->getWorld()->getMSPTInfos()) {
auto msg = chat::Message(each.first + " " + each.second.toString());
msg.style().italic = true;
invoker->getDimension()->getWorld()->getChat()->sendSystemMessage(msg, *invoker);
}
} else {
LINFO("Dimension MSPT: min avg max");
for (const auto &each : Server::getInstance()->getWorldGroup("default")->getWorld("default")->getMSPTInfos())
LINFO(each.first + " " + each.second.toString());
}
break;
}
case 1:
if (auto search = Server::getInstance()->getWorldGroup("default")->getWorld("default")->getDimensions().find(args[0]);
search != Server::getInstance()->getWorldGroup("default")->getWorld("default")->getDimensions().end()) {
if (invoker) {
invoker->getDimension()->getWorld()->getChat()->sendSystemMessage(chat::Message("Dimension MSPT: min avg max", {false, true}), *invoker);
invoker->getDimension()->getWorld()->getChat()->sendSystemMessage(
chat::Message(args[0] + " " + search->second->getMSPTInfos().toString(), {false, true}), *invoker
);
} else {
LINFO("Dimension MSPT: min avg max");
LINFO(args[0] + " " + search->second->getMSPTInfos().toString());
}
} else {
if (invoker)
invoker->getDimension()->getWorld()->getChat()->sendSystemMessage("Dimension " + args[0] + " not found", *invoker);
else
LINFO("Dimension {} not found", args[0]);
}
break;
default:
this->help(args, invoker);
break;
}
}

void MSPT::help(UNUSED std::vector<std::string> &args, Player *invoker) const
{
if (invoker)
invoker->getDimension()->getWorld()->getChat()->sendSystemMessage(_help, *invoker);
else
LINFO(_help);
}
20 changes: 20 additions & 0 deletions cubic-server/command_parser/commands/MSPT.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CUBICSERVER_COMMANDPARSER_COMMANDS_MSPT_HPP
#define CUBICSERVER_COMMANDPARSER_COMMANDS_MSPT_HPP

#include "CommandBase.hpp"

namespace command_parser {
struct MSPT : public CommandBase {
MSPT():
CommandBase("mspt", "/mspt (dimension)", false)
{
}
~MSPT() override = default;

void autocomplete(std::vector<std::string> &args, Player *invoker) const override;
void execute(std::vector<std::string> &args, Player *invoker) const override;
void help(std::vector<std::string> &args, Player *invoker) const override;
};
}

#endif // CUBICSERVER_COMMANDPARSER_COMMANDS_MSPT_HPP
2 changes: 1 addition & 1 deletion cubic-server/command_parser/commands/Tps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void Tps::autocomplete(UNUSED std::vector<std::string> &args, Player *invoker) c
LINFO("autocomplete time");
}

void Tps::execute(UNUSED std::vector<std::string> &args, Player *invoker) const
void Tps::execute(std::vector<std::string> &args, Player *invoker) const
{
switch (args.size()) {
case 0: {
Expand Down
2 changes: 1 addition & 1 deletion cubic-server/command_parser/commands/Tps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
namespace command_parser {
struct Tps : public CommandBase {
Tps():
CommandBase("tps", "/tps [<dimension name>]", false)
CommandBase("tps", "/tps (dimension name)", false)
{
}

Expand Down
7 changes: 7 additions & 0 deletions cubic-server/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ std::string Tps::toString() const
sstr << "Tps: " << this->oneMinTps << " " << this->fiveMinTps << " " << this->fifteenMinTps;
return sstr.str();
}

std::string MSPTInfos::toString() const
{
std::stringstream sstr;
sstr << "MSPTInfos: " << this->min << " " << this->mean << " " << this->max;
return sstr.str();
}
7 changes: 7 additions & 0 deletions cubic-server/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,11 @@ struct Tps {
std::string toString() const;
};

struct MSPTInfos {
float mean;
float min;
float max;
std::string toString() const;
};

#endif // CUBICSERVER_TYPES_HPP

0 comments on commit f4bf3a7

Please sign in to comment.