Add count based unload limit for mapblocks

This commit is contained in:
est31 2015-08-10 22:24:47 +02:00
parent 2b04ab874d
commit a8e238ed06
8 changed files with 154 additions and 65 deletions

@ -94,6 +94,9 @@
#random_input = false #random_input = false
# Timeout for client to remove unused map data from memory # Timeout for client to remove unused map data from memory
#client_unload_unused_data_timeout = 600 #client_unload_unused_data_timeout = 600
# Maximum number of mapblocks for client to be kept in memory
# Set to -1 for unlimited amount
#client_mapblock_limit = 1000
# Whether to fog out the end of the visible area # Whether to fog out the end of the visible area
#enable_fog = true #enable_fog = true
# Whether to show the client debug info (has the same effect as hitting F5) # Whether to show the client debug info (has the same effect as hitting F5)

@ -422,6 +422,7 @@ void Client::step(float dtime)
std::vector<v3s16> deleted_blocks; std::vector<v3s16> deleted_blocks;
m_env.getMap().timerUpdate(map_timer_and_unload_dtime, m_env.getMap().timerUpdate(map_timer_and_unload_dtime,
g_settings->getFloat("client_unload_unused_data_timeout"), g_settings->getFloat("client_unload_unused_data_timeout"),
g_settings->getS32("client_mapblock_limit"),
&deleted_blocks); &deleted_blocks);
/* /*

@ -104,6 +104,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("address", ""); settings->setDefault("address", "");
settings->setDefault("random_input", "false"); settings->setDefault("random_input", "false");
settings->setDefault("client_unload_unused_data_timeout", "600"); settings->setDefault("client_unload_unused_data_timeout", "600");
settings->setDefault("client_mapblock_limit", "1000");
settings->setDefault("enable_fog", "true"); settings->setDefault("enable_fog", "true");
settings->setDefault("fov", "72"); settings->setDefault("fov", "72");
settings->setDefault("view_bobbing", "true"); settings->setDefault("view_bobbing", "true");

@ -43,6 +43,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "database-dummy.h" #include "database-dummy.h"
#include "database-sqlite3.h" #include "database-sqlite3.h"
#include <deque> #include <deque>
#include <queue>
#if USE_LEVELDB #if USE_LEVELDB
#include "database-leveldb.h" #include "database-leveldb.h"
#endif #endif
@ -1399,10 +1400,25 @@ bool Map::getDayNightDiff(v3s16 blockpos)
return false; return false;
} }
struct TimeOrderedMapBlock {
MapSector *sect;
MapBlock *block;
TimeOrderedMapBlock(MapSector *sect, MapBlock *block) :
sect(sect),
block(block)
{}
bool operator<(const TimeOrderedMapBlock &b) const
{
return block->getUsageTimer() < b.block->getUsageTimer();
};
};
/* /*
Updates usage timers Updates usage timers
*/ */
void Map::timerUpdate(float dtime, float unload_timeout, void Map::timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
std::vector<v3s16> *unloaded_blocks) std::vector<v3s16> *unloaded_blocks)
{ {
bool save_before_unloading = (mapType() == MAPTYPE_SERVER); bool save_before_unloading = (mapType() == MAPTYPE_SERVER);
@ -1416,6 +1432,9 @@ void Map::timerUpdate(float dtime, float unload_timeout,
u32 block_count_all = 0; u32 block_count_all = 0;
beginSave(); beginSave();
// If there is no practical limit, we spare creation of mapblock_queue
if (max_loaded_blocks == (u32)-1) {
for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin(); for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
si != m_sectors.end(); ++si) { si != m_sectors.end(); ++si) {
MapSector *sector = si->second; MapSector *sector = si->second;
@ -1431,11 +1450,13 @@ void Map::timerUpdate(float dtime, float unload_timeout,
block->incrementUsageTimer(dtime); block->incrementUsageTimer(dtime);
if(block->refGet() == 0 && block->getUsageTimer() > unload_timeout) { if (block->refGet() == 0
&& block->getUsageTimer() > unload_timeout) {
v3s16 p = block->getPos(); v3s16 p = block->getPos();
// Save if modified // Save if modified
if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) { if (block->getModified() != MOD_STATE_CLEAN
&& save_before_unloading) {
modprofiler.add(block->getModifiedReasonString(), 1); modprofiler.add(block->getModifiedReasonString(), 1);
if (!saveBlock(block)) if (!saveBlock(block))
continue; continue;
@ -1449,8 +1470,7 @@ void Map::timerUpdate(float dtime, float unload_timeout,
unloaded_blocks->push_back(p); unloaded_blocks->push_back(p);
deleted_blocks_count++; deleted_blocks_count++;
} } else {
else {
all_blocks_deleted = false; all_blocks_deleted = false;
block_count_all++; block_count_all++;
} }
@ -1460,6 +1480,62 @@ void Map::timerUpdate(float dtime, float unload_timeout,
sector_deletion_queue.push_back(si->first); sector_deletion_queue.push_back(si->first);
} }
} }
} else {
std::priority_queue<TimeOrderedMapBlock> mapblock_queue;
for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
si != m_sectors.end(); ++si) {
MapSector *sector = si->second;
MapBlockVect blocks;
sector->getBlocks(blocks);
for(MapBlockVect::iterator i = blocks.begin();
i != blocks.end(); ++i) {
MapBlock *block = (*i);
block->incrementUsageTimer(dtime);
mapblock_queue.push(TimeOrderedMapBlock(sector, block));
}
}
block_count_all = mapblock_queue.size();
// Delete old blocks, and blocks over the limit from the memory
while (mapblock_queue.size() > max_loaded_blocks
|| mapblock_queue.top().block->getUsageTimer() > unload_timeout) {
TimeOrderedMapBlock b = mapblock_queue.top();
mapblock_queue.pop();
MapBlock *block = b.block;
if (block->refGet() != 0)
continue;
v3s16 p = block->getPos();
// Save if modified
if (block->getModified() != MOD_STATE_CLEAN && save_before_unloading) {
modprofiler.add(block->getModifiedReasonString(), 1);
if (!saveBlock(block))
continue;
saved_blocks_count++;
}
// Delete from memory
b.sect->deleteBlock(block);
if (unloaded_blocks)
unloaded_blocks->push_back(p);
deleted_blocks_count++;
block_count_all--;
}
// Delete empty sectors
for (std::map<v2s16, MapSector*>::iterator si = m_sectors.begin();
si != m_sectors.end(); ++si) {
if (si->second->empty()) {
sector_deletion_queue.push_back(si->first);
}
}
}
endSave(); endSave();
// Finally delete the empty sectors // Finally delete the empty sectors
@ -1484,7 +1560,7 @@ void Map::timerUpdate(float dtime, float unload_timeout,
void Map::unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks) void Map::unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks)
{ {
timerUpdate(0.0, -1.0, unloaded_blocks); timerUpdate(0.0, -1.0, 0, unloaded_blocks);
} }
void Map::deleteSectors(std::vector<v2s16> &sectorList) void Map::deleteSectors(std::vector<v2s16> &sectorList)

@ -277,7 +277,7 @@ public:
Updates usage timers and unloads unused blocks and sectors. Updates usage timers and unloads unused blocks and sectors.
Saves modified blocks before unloading on MAPTYPE_SERVER. Saves modified blocks before unloading on MAPTYPE_SERVER.
*/ */
void timerUpdate(float dtime, float unload_timeout, void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
std::vector<v3s16> *unloaded_blocks=NULL); std::vector<v3s16> *unloaded_blocks=NULL);
/* /*

@ -142,6 +142,11 @@ void MapSector::getBlocks(MapBlockVect &dest)
} }
} }
bool MapSector::empty()
{
return m_blocks.empty();
}
/* /*
ServerMapSector ServerMapSector
*/ */

@ -63,6 +63,8 @@ public:
void getBlocks(MapBlockVect &dest); void getBlocks(MapBlockVect &dest);
bool empty();
// Always false at the moment, because sector contains no metadata. // Always false at the moment, because sector contains no metadata.
bool differs_from_disk; bool differs_from_disk;

@ -594,7 +594,8 @@ void Server::AsyncRunStep(bool initial_step)
// Run Map's timers and unload unused data // Run Map's timers and unload unused data
ScopeProfiler sp(g_profiler, "Server: map timer and unload"); ScopeProfiler sp(g_profiler, "Server: map timer and unload");
m_env->getMap().timerUpdate(map_timer_and_unload_dtime, m_env->getMap().timerUpdate(map_timer_and_unload_dtime,
g_settings->getFloat("server_unload_unused_data_timeout")); g_settings->getFloat("server_unload_unused_data_timeout"),
(u32)-1);
} }
/* /*