make it possible to clear file caches

This commit is contained in:
Martin Michelsen
2024-10-05 12:43:38 -07:00
parent 40d8227504
commit 5294a53e1b
6 changed files with 40 additions and 28 deletions
+2 -2
View File
@@ -2212,7 +2212,7 @@ Action a_find_rare_enemy_seeds(
} else if (version == Version::PC_V2) { } else if (version == Version::PC_V2) {
s->load_patch_indexes(false); s->load_patch_indexes(false);
} else { } else {
s->clear_map_file_caches(); s->clear_file_caches(false);
} }
shared_ptr<const Map::RareEnemyRates> rare_rates; shared_ptr<const Map::RareEnemyRates> rare_rates;
@@ -2281,7 +2281,7 @@ Action a_load_maps_test(
using SDT = SetDataTable; using SDT = SetDataTable;
auto s = make_shared<ServerState>(get_config_filename(args)); auto s = make_shared<ServerState>(get_config_filename(args));
s->load_config_early(); s->load_config_early();
s->clear_map_file_caches(); s->clear_file_caches(false);
s->load_patch_indexes(false); s->load_patch_indexes(false);
s->load_set_data_tables(false); s->load_set_data_tables(false);
s->load_quest_index(false); s->load_quest_index(false);
+2 -2
View File
@@ -3027,8 +3027,8 @@ static void on_D7_GC(shared_ptr<Client> c, uint16_t, uint32_t, string& data) {
send_command(c, 0xD7, 0x00); send_command(c, 0xD7, 0x00);
} else { } else {
try { try {
static FileContentsCache gba_file_cache(300 * 1000 * 1000); auto s = c->require_server_state();
auto f = gba_file_cache.get_or_load("system/gba/" + filename).file; auto f = s->gba_files_cache->get_or_load("system/gba/" + filename).file;
send_open_quest_file(c, "", filename, "", 0, QuestFileType::GBA_DEMO, f->data); send_open_quest_file(c, "", filename, "", 0, QuestFileType::GBA_DEMO, f->data);
} catch (const out_of_range&) { } catch (const out_of_range&) {
send_command(c, 0xD7, 0x00); send_command(c, 0xD7, 0x00);
+10 -15
View File
@@ -670,22 +670,15 @@ static const vector<string> stream_file_entries = {
"BattleParamEntry_ep4_on.dat", "BattleParamEntry_ep4_on.dat",
"PlyLevelTbl.prs", "PlyLevelTbl.prs",
}; };
static FileContentsCache bb_stream_files_cache(3600000000ULL);
void send_stream_file_index_bb(shared_ptr<Client> c) { void send_stream_file_index_bb(shared_ptr<Client> c) {
auto s = c->require_server_state();
struct S_StreamFileIndexEntry_BB_01EB {
le_uint32_t size;
le_uint32_t checksum; // crc32 of file data
le_uint32_t offset; // offset in stream (== sum of all previous files' sizes)
pstring<TextEncoding::ASCII, 0x40> filename;
};
vector<S_StreamFileIndexEntry_BB_01EB> entries; vector<S_StreamFileIndexEntry_BB_01EB> entries;
size_t offset = 0; size_t offset = 0;
for (const string& filename : stream_file_entries) { for (const string& filename : stream_file_entries) {
string key = "system/blueburst/" + filename; string key = "system/blueburst/" + filename;
auto cache_res = bb_stream_files_cache.get_or_load(key); auto cache_res = s->bb_stream_files_cache->get_or_load(key);
auto& e = entries.emplace_back(); auto& e = entries.emplace_back();
e.size = cache_res.file->data->size(); e.size = cache_res.file->data->size();
// Computing the checksum can be slow, so we cache it along with the file // Computing the checksum can be slow, so we cache it along with the file
@@ -693,12 +686,12 @@ void send_stream_file_index_bb(shared_ptr<Client> c) {
// so we always recompute the checksum in that case. // so we always recompute the checksum in that case.
if (cache_res.generate_called) { if (cache_res.generate_called) {
e.checksum = crc32(cache_res.file->data->data(), e.size); e.checksum = crc32(cache_res.file->data->data(), e.size);
bb_stream_files_cache.replace_obj<uint32_t>(key + ".crc32", e.checksum); s->bb_stream_files_cache->replace_obj<uint32_t>(key + ".crc32", e.checksum);
} else { } else {
auto compute_checksum = [&](const string&) -> uint32_t { auto compute_checksum = [&](const string&) -> uint32_t {
return crc32(cache_res.file->data->data(), e.size); return crc32(cache_res.file->data->data(), e.size);
}; };
e.checksum = bb_stream_files_cache.get_obj<uint32_t>(key + ".crc32", compute_checksum).obj; e.checksum = s->bb_stream_files_cache->get_obj<uint32_t>(key + ".crc32", compute_checksum).obj;
} }
e.offset = offset; e.offset = offset;
e.filename.encode(filename); e.filename.encode(filename);
@@ -708,17 +701,19 @@ void send_stream_file_index_bb(shared_ptr<Client> c) {
} }
void send_stream_file_chunk_bb(shared_ptr<Client> c, uint32_t chunk_index) { void send_stream_file_chunk_bb(shared_ptr<Client> c, uint32_t chunk_index) {
auto cache_result = bb_stream_files_cache.get( auto s = c->require_server_state();
"<BB stream file>", +[](const string&) -> string {
auto cache_result = s->bb_stream_files_cache->get(
"<BB stream file>", [&](const string&) -> string {
size_t bytes = 0; size_t bytes = 0;
for (const auto& name : stream_file_entries) { for (const auto& name : stream_file_entries) {
bytes += bb_stream_files_cache.get_or_load("system/blueburst/" + name).file->data->size(); bytes += s->bb_stream_files_cache->get_or_load("system/blueburst/" + name).file->data->size();
} }
string ret; string ret;
ret.reserve(bytes); ret.reserve(bytes);
for (const auto& name : stream_file_entries) { for (const auto& name : stream_file_entries) {
ret += *bb_stream_files_cache.get_or_load("system/blueburst/" + name).file->data; ret += *s->bb_stream_files_cache->get_or_load("system/blueburst/" + name).file->data;
} }
return ret; return ret;
}); });
+3
View File
@@ -214,6 +214,7 @@ CommandDefinition c_reload(
accounts - reindex user accounts\n\ accounts - reindex user accounts\n\
battle-params - reload the BB enemy stats files\n\ battle-params - reload the BB enemy stats files\n\
bb-keys - reload BB private keys\n\ bb-keys - reload BB private keys\n\
caches - clear all cached files\n\
config - reload most fields from config.json\n\ config - reload most fields from config.json\n\
dol-files - reindex all DOL files\n\ dol-files - reindex all DOL files\n\
drop-tables - reload drop tables\n\ drop-tables - reload drop tables\n\
@@ -244,6 +245,8 @@ CommandDefinition c_reload(
args.s->load_bb_private_keys(true); args.s->load_bb_private_keys(true);
} else if (type == "accounts") { } else if (type == "accounts") {
args.s->load_accounts(true); args.s->load_accounts(true);
} else if (type == "caches") {
args.s->clear_file_caches(true);
} else if (type == "patch-files") { } else if (type == "patch-files") {
args.s->load_patch_indexes(true); args.s->load_patch_indexes(true);
} else if (type == "ep3-cards") { } else if (type == "ep3-cards") {
+16 -5
View File
@@ -44,6 +44,9 @@ ServerState::ServerState(shared_ptr<struct event_base> base, const string& confi
base(base), base(base),
config_filename(config_filename), config_filename(config_filename),
is_replay(is_replay), is_replay(is_replay),
bb_stream_files_cache(new FileContentsCache(3600000000ULL)),
bb_system_cache(new FileContentsCache(3600000000ULL)),
gba_files_cache(new FileContentsCache(3600000000ULL)),
player_files_manager(this->base ? make_shared<PlayerFilesManager>(base) : nullptr), player_files_manager(this->base ? make_shared<PlayerFilesManager>(base) : nullptr),
destroy_lobbies_event(this->base ? event_new(base.get(), -1, EV_TIMEOUT, &ServerState::dispatch_destroy_lobbies, this) : nullptr, event_free) {} destroy_lobbies_event(this->base ? event_new(base.get(), -1, EV_TIMEOUT, &ServerState::dispatch_destroy_lobbies, this) : nullptr, event_free) {}
@@ -523,9 +526,8 @@ shared_ptr<const string> ServerState::load_bb_file(
// Finally, look in system/blueburst // Finally, look in system/blueburst
const string& effective_bb_directory_filename = bb_directory_filename.empty() ? patch_index_filename : bb_directory_filename; const string& effective_bb_directory_filename = bb_directory_filename.empty() ? patch_index_filename : bb_directory_filename;
static FileContentsCache cache(10 * 60 * 1000 * 1000); // 10 minutes
try { try {
auto ret = cache.get_or_load("system/blueburst/" + effective_bb_directory_filename); auto ret = this->bb_system_cache->get_or_load("system/blueburst/" + effective_bb_directory_filename);
return ret.file->data; return ret.file->data;
} catch (const exception& e) { } catch (const exception& e) {
throw phosg::cannot_open_file(patch_index_filename); throw phosg::cannot_open_file(patch_index_filename);
@@ -1465,11 +1467,20 @@ void ServerState::load_patch_indexes(bool from_non_event_thread) {
this->forward_or_call(from_non_event_thread, std::move(set)); this->forward_or_call(from_non_event_thread, std::move(set));
} }
void ServerState::clear_map_file_caches() { void ServerState::clear_file_caches(bool from_non_event_thread) {
auto set = [s = this->shared_from_this()]() {
config_log.info("Clearing map file caches"); config_log.info("Clearing map file caches");
for (auto& cache : this->map_file_caches) { for (auto& cache : s->map_file_caches) {
cache = make_shared<ThreadSafeFileCache>(); cache = make_shared<ThreadSafeFileCache>();
} }
config_log.info("Clearing BB stream file cache");
s->bb_stream_files_cache.reset(new FileContentsCache(3600000000ULL));
config_log.info("Clearing BB system cache");
s->bb_system_cache.reset(new FileContentsCache(3600000000ULL));
config_log.info("Clearing GBA file cache");
s->gba_files_cache.reset(new FileContentsCache(300 * 1000 * 1000));
};
this->forward_or_call(from_non_event_thread, std::move(set));
} }
void ServerState::load_set_data_tables(bool from_non_event_thread) { void ServerState::load_set_data_tables(bool from_non_event_thread) {
@@ -1956,7 +1967,7 @@ void ServerState::load_all() {
this->load_bb_private_keys(false); this->load_bb_private_keys(false);
this->load_bb_system_defaults(false); this->load_bb_system_defaults(false);
this->load_accounts(false); this->load_accounts(false);
this->clear_map_file_caches(); this->clear_file_caches(false);
this->load_patch_indexes(false); this->load_patch_indexes(false);
this->load_ep3_cards(false); this->load_ep3_cards(false);
this->load_ep3_maps(false); this->load_ep3_maps(false);
+4 -1
View File
@@ -151,6 +151,9 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
std::shared_ptr<const PatchFileIndex> pc_patch_file_index; std::shared_ptr<const PatchFileIndex> pc_patch_file_index;
std::shared_ptr<const PatchFileIndex> bb_patch_file_index; std::shared_ptr<const PatchFileIndex> bb_patch_file_index;
std::array<std::shared_ptr<ThreadSafeFileCache>, NUM_VERSIONS> map_file_caches; std::array<std::shared_ptr<ThreadSafeFileCache>, NUM_VERSIONS> map_file_caches;
std::shared_ptr<FileContentsCache> bb_stream_files_cache;
std::shared_ptr<FileContentsCache> bb_system_cache;
std::shared_ptr<FileContentsCache> gba_files_cache;
std::shared_ptr<const DOLFileIndex> dol_file_index; std::shared_ptr<const DOLFileIndex> dol_file_index;
std::shared_ptr<const Episode3::CardIndex> ep3_card_index; std::shared_ptr<const Episode3::CardIndex> ep3_card_index;
std::shared_ptr<const Episode3::CardIndex> ep3_card_index_trial; std::shared_ptr<const Episode3::CardIndex> ep3_card_index_trial;
@@ -386,7 +389,7 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
void load_accounts(bool from_non_event_thread); void load_accounts(bool from_non_event_thread);
void load_teams(bool from_non_event_thread); void load_teams(bool from_non_event_thread);
void load_patch_indexes(bool from_non_event_thread); void load_patch_indexes(bool from_non_event_thread);
void clear_map_file_caches(); void clear_file_caches(bool from_non_event_thread);
void load_battle_params(bool from_non_event_thread); void load_battle_params(bool from_non_event_thread);
void load_level_tables(bool from_non_event_thread); void load_level_tables(bool from_non_event_thread);
void load_text_index(bool from_non_event_thread); void load_text_index(bool from_non_event_thread);