Skip to content

Commit

Permalink
Change how max_lag is calculated and reported (#14378)
Browse files Browse the repository at this point in the history
-Change how max_lag is calculated and reported

- Cap singleplayer step at 60Hz

- Clarify dedicated_server_step
  • Loading branch information
sfan5 committed Feb 26, 2024
1 parent 63a9853 commit 5d8a220
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 25 deletions.
7 changes: 4 additions & 3 deletions builtin/settingtypes.txt
Expand Up @@ -2019,9 +2019,10 @@ ask_reconnect_on_crash (Ask to reconnect after crash) bool false

[**Server/Env Performance]

# Length of a server tick and the interval at which objects are generally updated over
# network, stated in seconds.
dedicated_server_step (Dedicated server step) float 0.09 0.0
# Length of a server tick (the interval at which everything is generally updated),
# stated in seconds.
# Does not apply to sessions hosted from the client menu.
dedicated_server_step (Dedicated server step) float 0.09 0.0 1.0

# Whether players are shown to clients without any range limit.
# Deprecated, use the setting player_transfer_distance instead.
Expand Down
10 changes: 8 additions & 2 deletions src/client/game.cpp
Expand Up @@ -2751,10 +2751,16 @@ inline void Game::step(f32 dtime)
g_settings->getFloat("fps_max_unfocused") :
g_settings->getFloat("fps_max");
fps_max = std::max(fps_max, 1.0f);
float steplen = 1.0f / fps_max;
/*
* Unless you have a barebones game, running the server at more than 60Hz
* is hardly realistic and you're at the point of diminishing returns.
* fps_max is also not necessarily anywhere near the FPS actually achieved
* (also due to vsync).
*/
fps_max = std::min(fps_max, 60.0f);

server->setStepSettings(Server::StepSettings{
steplen,
1.0f / fps_max,
m_is_paused
});

Expand Down
28 changes: 21 additions & 7 deletions src/server.cpp
Expand Up @@ -646,15 +646,29 @@ void Server::AsyncRunStep(float dtime, bool initial_step)

{
MutexAutoLock lock(m_env_mutex);
// Figure out and report maximum lag to environment
float max_lag = m_env->getMaxLagEstimate();
max_lag *= 0.9998; // Decrease slowly (about half per 5 minutes)
if(dtime > max_lag){
if(dtime > 0.1 && dtime > max_lag * 2.0)
infostream<<"Server: Maximum lag peaked to "<<dtime
<<" s"<<std::endl;
max_lag = dtime;
constexpr float lag_warn_threshold = 2.0f;

// Decrease value gradually, halve it every minute.
if (m_max_lag_decrease.step(dtime, 0.5f)) {
// To reproduce this number enter "solve (x)**(60/0.5) = 0.5"
// into Wolfram Alpha.
max_lag *= 0.99425f;
}

// Report a 20% change to the log but only if we're >10% off target
// also report if we crossed into the warning boundary
if (dtime >= max_lag * 1.2f ||
(max_lag < lag_warn_threshold && dtime >= lag_warn_threshold)) {
const float steplen = getStepSettings().steplen;
if (dtime > steplen * 1.1f) {
auto &to = dtime >= lag_warn_threshold ? warningstream : infostream;
to << "Server: Maximum lag peaked at " << dtime
<< " (steplen=" << steplen << ")" << std::endl;
}
}
max_lag = std::max(max_lag, dtime),

m_env->reportMaxLagEstimate(max_lag);

// Step environment
Expand Down
8 changes: 2 additions & 6 deletions src/server.h
Expand Up @@ -609,12 +609,14 @@ class Server : public con::PeerHandler, public MapEventReceiver,
MutexedVariable<std::string> m_async_fatal_error;

// Some timers
float m_time_of_day_send_timer = 0.0f;
float m_liquid_transform_timer = 0.0f;
float m_liquid_transform_every = 1.0f;
float m_masterserver_timer = 0.0f;
float m_emergethread_trigger_timer = 0.0f;
float m_savemap_timer = 0.0f;
IntervalLimiter m_map_timer_and_unload_interval;
IntervalLimiter m_max_lag_decrease;

// Environment
ServerEnvironment *m_env = nullptr;
Expand Down Expand Up @@ -662,12 +664,6 @@ class Server : public con::PeerHandler, public MapEventReceiver,
// The server mainly operates in this thread
ServerThread *m_thread = nullptr;

/*
Time related stuff
*/
// Timer for sending time of day over network
float m_time_of_day_send_timer = 0.0f;

/*
Client interface
*/
Expand Down
2 changes: 1 addition & 1 deletion src/serverenvironment.h
Expand Up @@ -367,7 +367,7 @@ class ServerEnvironment final : public Environment
u32 getGameTime() const { return m_game_time; }

void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
float getMaxLagEstimate() { return m_max_lag_estimate; }
float getMaxLagEstimate() const { return m_max_lag_estimate; }

std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; }

Expand Down
10 changes: 4 additions & 6 deletions src/util/numeric.h
Expand Up @@ -350,12 +350,10 @@ class IntervalLimiter
public:
IntervalLimiter() = default;

/*
dtime: time from last call to this method
wanted_interval: interval wanted
return value:
true: action should be skipped
false: action should be done
/**
@param dtime time from last call to this method
@param wanted_interval interval wanted
@return true if action should be done
*/
bool step(float dtime, float wanted_interval)
{
Expand Down

0 comments on commit 5d8a220

Please sign in to comment.