--- /dev/null
+/*
+Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+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, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef PROFILER_HEADER
+#define PROFILER_HEADER
+
+#include "common_irrlicht.h"
+#include <string>
+#include "utility.h"
+#include <jmutex.h>
+#include <jmutexautolock.h>
+
+/*
+ Time profiler
+*/
+
+class Profiler
+{
+public:
+ Profiler()
+ {
+ m_mutex.Init();
+ }
+
+ void add(const std::string &name, u32 duration)
+ {
+ JMutexAutoLock lock(m_mutex);
+ core::map<std::string, u32>::Node *n = m_data.find(name);
+ if(n == NULL)
+ {
+ m_data[name] = duration;
+ }
+ else
+ {
+ n->setValue(n->getValue()+duration);
+ }
+ }
+
+ void clear()
+ {
+ JMutexAutoLock lock(m_mutex);
+ for(core::map<std::string, u32>::Iterator
+ i = m_data.getIterator();
+ i.atEnd() == false; i++)
+ {
+ i.getNode()->setValue(0);
+ }
+ }
+
+ void print(std::ostream &o)
+ {
+ JMutexAutoLock lock(m_mutex);
+ for(core::map<std::string, u32>::Iterator
+ i = m_data.getIterator();
+ i.atEnd() == false; i++)
+ {
+ std::string name = i.getNode()->getKey();
+ o<<name<<": ";
+ s32 clampsize = 40;
+ s32 space = clampsize-name.size();
+ for(s32 j=0; j<space; j++)
+ {
+ if(j%2 == 0 && j < space - 1)
+ o<<"-";
+ else
+ o<<" ";
+ }
+ o<<i.getNode()->getValue();
+ o<<std::endl;
+ }
+ }
+
+private:
+ JMutex m_mutex;
+ core::map<std::string, u32> m_data;
+};
+
+class ScopeProfiler
+{
+public:
+ ScopeProfiler(Profiler *profiler, const std::string &name):
+ m_profiler(profiler),
+ m_name(name),
+ m_timer(NULL)
+ {
+ if(m_profiler)
+ m_timer = new TimeTaker(m_name.c_str());
+ }
+ // name is copied
+ ScopeProfiler(Profiler *profiler, const char *name):
+ m_profiler(profiler),
+ m_name(name),
+ m_timer(NULL)
+ {
+ if(m_profiler)
+ m_timer = new TimeTaker(m_name.c_str());
+ }
+ ~ScopeProfiler()
+ {
+ if(m_timer)
+ {
+ u32 duration = m_timer->stop(true);
+ if(m_profiler)
+ m_profiler->add(m_name, duration);
+ delete m_timer;
+ }
+ }
+private:
+ Profiler *m_profiler;
+ std::string m_name;
+ TimeTaker *m_timer;
+};
+
+#endif
+
TimeTaker timer("RemoteClient::GetNextBlocks", &timer_result);*/
// Increment timers
- m_nearest_unsent_reset_timer += dtime;
m_nothing_to_send_pause_timer -= dtime;
if(m_nothing_to_send_pause_timer >= 0)
+ {
+ // Keep this reset
+ m_nearest_unsent_reset_timer = 0;
return;
+ }
// Won't send anything if already sending
if(m_blocks_sending.size() >= g_settings.getU16
return;
}
+ //TimeTaker timer("RemoteClient::GetNextBlocks");
+
Player *player = server->m_env.getPlayer(peer_id);
assert(player != NULL);
v3f playerpos = player->getPosition();
v3f playerspeed = player->getSpeed();
+ v3f playerspeeddir(0,0,0);
+ if(playerspeed.getLength() > 1.0*BS)
+ playerspeeddir = playerspeed / playerspeed.getLength();
+ // Predict to next block
+ v3f playerpos_predicted = playerpos + playerspeeddir*MAP_BLOCKSIZE*BS;
- v3s16 center_nodepos = floatToInt(playerpos, BS);
+ v3s16 center_nodepos = floatToInt(playerpos_predicted, BS);
v3s16 center = getNodeBlockPos(center_nodepos);
camera_dir.rotateYZBy(player->getPitch());
camera_dir.rotateXZBy(player->getYaw());
+ /*dstream<<"camera_dir=("<<camera_dir.X<<","<<camera_dir.Y<<","
+ <<camera_dir.Z<<")"<<std::endl;*/
+
/*
Get the starting value of the block finder radius.
*/
/*dstream<<"m_nearest_unsent_reset_timer="
<<m_nearest_unsent_reset_timer<<std::endl;*/
- if(m_nearest_unsent_reset_timer > 5.0)
+
+ // This has to be incremented only when the nothing to send pause
+ // is not active
+ m_nearest_unsent_reset_timer += dtime;
+
+ // Reset periodically to avoid possible bugs or other mishaps
+ if(m_nearest_unsent_reset_timer > 10.0)
{
m_nearest_unsent_reset_timer = 0;
m_nearest_unsent_d = 0;
- //dstream<<"Resetting m_nearest_unsent_d"<<std::endl;
+ dstream<<"Resetting m_nearest_unsent_d for "
+ <<server->getPlayerName(peer_id)<<std::endl;
}
//s16 last_nearest_unsent_d = m_nearest_unsent_d;
s16 d_max = g_settings.getS16("max_block_send_distance");
s16 d_max_gen = g_settings.getS16("max_block_generate_distance");
+ // Don't loop very much at a time
+ if(d_max > d_start+1)
+ d_max = d_start+1;
+ /*if(d_max_gen > d_start+2)
+ d_max_gen = d_start+2;*/
+
//dstream<<"Starting from "<<d_start<<std::endl;
bool sending_something = false;
- for(s16 d = d_start; d <= d_max; d++)
+ bool no_blocks_found_for_sending = true;
+
+ bool queue_is_full = false;
+
+ s16 d;
+ for(d = d_start; d <= d_max; d++)
{
//dstream<<"RemoteClient::SendBlocks(): d="<<d<<std::endl;
// Don't select too many blocks for sending
if(num_blocks_selected >= max_simul_dynamic)
- goto queue_full;
+ {
+ queue_is_full = true;
+ goto queue_full_break;
+ }
// Don't send blocks that are currently being transferred
if(m_blocks_sending.find(p) != NULL)
}
#endif
+ //dstream<<"d="<<d<<std::endl;
+
/*
Don't generate or send if not in sight
*/
*/
{
if(m_blocks_sent.find(p) != NULL)
+ {
continue;
+ }
}
/*
bool block_is_invalid = false;
if(block != NULL)
{
+ // Block is dummy if data doesn't exist.
+ // It means it has been not found from disk and not generated
if(block->isDummy())
{
surely_not_found_on_disk = true;
}
-
+
+ // Block is valid if lighting is up-to-date and data exists
if(block->isValid() == false)
{
block_is_invalid = true;
If block is not close, don't send it unless it is near
ground level.
- Block is not near ground level if night-time mesh
- doesn't differ from day-time mesh.
+ Block is near ground level if night-time mesh
+ differs from day-time mesh.
*/
if(d > 3)
{
}
/*
- Record the lowest d from which a a block has been
+ Record the lowest d from which a block has been
found being not sent and possibly to exist
*/
- if(new_nearest_unsent_d == -1 || d < new_nearest_unsent_d)
+ if(no_blocks_found_for_sending)
{
if(generate == true)
new_nearest_unsent_d = d;
}
+
+ no_blocks_found_for_sending = false;
/*
Add inexistent block to emerge queue.
sending_something = true;
}
}
-queue_full:
+queue_full_break:
- if(new_nearest_unsent_d != -1)
+ //dstream<<"Stopped at "<<d<<std::endl;
+
+ if(no_blocks_found_for_sending)
{
- m_nearest_unsent_d = new_nearest_unsent_d;
+ if(queue_is_full == false)
+ new_nearest_unsent_d = d;
}
+ if(new_nearest_unsent_d != -1)
+ m_nearest_unsent_d = new_nearest_unsent_d;
+
if(sending_something == false)
{
m_nothing_to_send_counter++;
- if(m_nothing_to_send_counter >= 3)
+ if((s16)m_nothing_to_send_counter >=
+ g_settings.getS16("max_block_send_distance"))
{
// Pause time in seconds
- m_nothing_to_send_pause_timer = 2.0;
+ m_nothing_to_send_pause_timer = 1.0;
+ dstream<<"nothing to send to "
+ <<server->getPlayerName(peer_id)
+ <<" (d="<<d<<")"<<std::endl;
}
}
else
dtime = m_step_dtime;
}
- // Send blocks to clients
- SendBlocks(dtime);
+ {
+ ScopeProfiler sp(&g_profiler, "Server: selecting and sending "
+ "blocks to clients");
+ // Send blocks to clients
+ SendBlocks(dtime);
+ }
if(dtime < 0.001)
return;
{
// Process connection's timeouts
JMutexAutoLock lock2(m_con_mutex);
+ ScopeProfiler sp(&g_profiler, "Server: connection timeout processing");
m_con.RunTimeouts(dtime);
}
{
// This has to be called so that the client list gets synced
// with the peer list of the connection
+ ScopeProfiler sp(&g_profiler, "Server: peer change handling");
handlePeerChanges();
}
// Step environment
// This also runs Map's timers
JMutexAutoLock lock(m_env_mutex);
+ ScopeProfiler sp(&g_profiler, "Server: environment step");
m_env.step(dtime);
}
m_liquid_transform_timer -= 1.00;
JMutexAutoLock lock(m_env_mutex);
-
+
+ ScopeProfiler sp(&g_profiler, "Server: liquid transform");
+
core::map<v3s16, MapBlock*> modified_blocks;
m_env.getMap().transformLiquids(modified_blocks);
#if 0
*/
{
//dstream<<"Server: Checking added and deleted active objects"<<std::endl;
-
JMutexAutoLock envlock(m_env_mutex);
JMutexAutoLock conlock(m_con_mutex);
-
+
+ ScopeProfiler sp(&g_profiler, "Server: checking added and deleted objects");
+
// Radius inside which objects are active
s16 radius = 32;
JMutexAutoLock envlock(m_env_mutex);
JMutexAutoLock conlock(m_con_mutex);
+ ScopeProfiler sp(&g_profiler, "Server: sending object messages");
+
// Key = object id
// Value = data sent by object
core::map<u16, core::list<ActiveObjectMessage>* > buffered_messages;
{
JMutexAutoLock lock1(m_env_mutex);
JMutexAutoLock lock2(m_con_mutex);
+
+ ScopeProfiler sp(&g_profiler, "Server: sending mbo positions");
+
SendObjectData(counter);
counter = 0.0;
JMutexAutoLock envlock(m_env_mutex);
JMutexAutoLock conlock(m_con_mutex);
-
+
+ ScopeProfiler sp(&g_profiler, "Server: stepping node metadata");
+
core::map<v3s16, MapBlock*> changed_blocks;
m_env.getMap().nodeMetadataStep(dtime, changed_blocks);
{
counter = 0.0;
+ ScopeProfiler sp(&g_profiler, "Server: saving stuff");
+
// Auth stuff
m_authmanager.save();
core::array<PrioritySortedBlockTransfer> queue;
s32 total_sending = 0;
-
- for(core::map<u16, RemoteClient*>::Iterator
- i = m_clients.getIterator();
- i.atEnd() == false; i++)
+
{
- RemoteClient *client = i.getNode()->getValue();
- assert(client->peer_id == i.getNode()->getKey());
+ ScopeProfiler sp(&g_profiler, "Server: selecting blocks for sending");
- total_sending += client->SendingCount();
-
- if(client->serialization_version == SER_FMT_VER_INVALID)
- continue;
-
- client->GetNextBlocks(this, dtime, queue);
+ for(core::map<u16, RemoteClient*>::Iterator
+ i = m_clients.getIterator();
+ i.atEnd() == false; i++)
+ {
+ RemoteClient *client = i.getNode()->getValue();
+ assert(client->peer_id == i.getNode()->getKey());
+
+ total_sending += client->SendingCount();
+
+ if(client->serialization_version == SER_FMT_VER_INVALID)
+ continue;
+
+ client->GetNextBlocks(this, dtime, queue);
+ }
}
// Sort.
{
DSTACK(__FUNCTION_NAME);
- std::cout<<DTIME<<std::endl;
- std::cout<<"========================"<<std::endl;
- std::cout<<"Running dedicated server"<<std::endl;
- std::cout<<"========================"<<std::endl;
- std::cout<<std::endl;
+ dstream<<DTIME<<std::endl;
+ dstream<<"========================"<<std::endl;
+ dstream<<"Running dedicated server"<<std::endl;
+ dstream<<"========================"<<std::endl;
+ dstream<<std::endl;
+
+ IntervalLimiter m_profiler_interval;
for(;;)
{
// This is kind of a hack but can be done like this
// because server.step() is very light
- sleep_ms(30);
+ {
+ ScopeProfiler sp(&g_profiler, "dedicated server sleep");
+ sleep_ms(30);
+ }
server.step(0.030);
if(server.getShutdownRequested() || kill)
{
- std::cout<<DTIME<<" dedicated_server_loop(): Quitting."<<std::endl;
+ dstream<<DTIME<<" dedicated_server_loop(): Quitting."<<std::endl;
break;
}
+ /*
+ Profiler
+ */
+ float profiler_print_interval =
+ g_settings.getFloat("profiler_print_interval");
+ if(profiler_print_interval != 0)
+ {
+ if(m_profiler_interval.step(0.030, profiler_print_interval))
+ {
+ dstream<<"Profiler:"<<std::endl;
+ g_profiler.print(dstream);
+ g_profiler.clear();
+ }
+ }
+
+ /*
+ Player info
+ */
static int counter = 0;
counter--;
if(counter <= 0)
u32 sum = PIChecksum(list);
if(sum != sum_old)
{
- std::cout<<DTIME<<"Player info:"<<std::endl;
+ dstream<<DTIME<<"Player info:"<<std::endl;
for(i=list.begin(); i!=list.end(); i++)
{
- i->PrintLine(&std::cout);
+ i->PrintLine(&dstream);
}
}
sum_old = sum;