Skip to content
Snippets Groups Projects
Commit 5fd09518 authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Use static for cache dir and cache extension to eliminate mutex lock overhead...

Use static for cache dir and cache extension to eliminate mutex lock overhead to generate cache file path
parent 5eb5ba34
No related branches found
No related tags found
No related merge requests found
...@@ -39,25 +39,27 @@ ...@@ -39,25 +39,27 @@
#include "lldiskcache.h" #include "lldiskcache.h"
std::string LLDiskCache::sCacheDir = "";
std::string LLDiskCache::sCacheFilenameExt = ".sl_cache";
LLDiskCache::LLDiskCache(const std::string cache_dir, LLDiskCache::LLDiskCache(const std::string cache_dir,
const int max_size_bytes, const int max_size_bytes,
const bool enable_cache_debug_info) : const bool enable_cache_debug_info) :
mCacheDir(cache_dir),
mMaxSizeBytes(max_size_bytes), mMaxSizeBytes(max_size_bytes),
mEnableCacheDebugInfo(enable_cache_debug_info) mEnableCacheDebugInfo(enable_cache_debug_info)
{ {
mCacheFilenameExt = ".sl_cache"; sCacheDir = cache_dir;
createCache(); createCache();
} }
void LLDiskCache::createCache() void LLDiskCache::createCache()
{ {
LLFile::mkdir(mCacheDir); LLFile::mkdir(sCacheDir);
std::vector<std::string> uuidprefix = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; std::vector<std::string> uuidprefix = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
for (auto& prefixchar : uuidprefix) for (auto& prefixchar : uuidprefix)
{ {
LLFile::mkdir(absl::StrCat(mCacheDir, gDirUtilp->getDirDelimiter(), prefixchar)); LLFile::mkdir(absl::StrCat(sCacheDir, gDirUtilp->getDirDelimiter(), prefixchar));
} }
} }
...@@ -65,7 +67,7 @@ void LLDiskCache::purge() ...@@ -65,7 +67,7 @@ void LLDiskCache::purge()
{ {
if (mEnableCacheDebugInfo) if (mEnableCacheDebugInfo)
{ {
LL_INFOS() << "Total dir size before purge is " << dirFileSize(mCacheDir) << LL_ENDL; LL_INFOS() << "Total dir size before purge is " << dirFileSize(sCacheDir) << LL_ENDL;
} }
auto start_time = std::chrono::high_resolution_clock::now(); auto start_time = std::chrono::high_resolution_clock::now();
...@@ -74,7 +76,7 @@ void LLDiskCache::purge() ...@@ -74,7 +76,7 @@ void LLDiskCache::purge()
std::vector<file_info_t> file_info; std::vector<file_info_t> file_info;
#if LL_WINDOWS #if LL_WINDOWS
std::wstring cache_path(ll_convert_string_to_wide(mCacheDir)); std::wstring cache_path(ll_convert_string_to_wide(sCacheDir));
#else #else
std::string cache_path(mCacheDir); std::string cache_path(mCacheDir);
#endif #endif
...@@ -84,7 +86,7 @@ void LLDiskCache::purge() ...@@ -84,7 +86,7 @@ void LLDiskCache::purge()
{ {
if (boost::filesystem::is_regular_file(entry)) if (boost::filesystem::is_regular_file(entry))
{ {
if (entry.path().string().rfind(mCacheFilenameExt) != std::string::npos) if (entry.path().string().rfind(sCacheFilenameExt) != std::string::npos)
{ {
uintmax_t file_size = boost::filesystem::file_size(entry); uintmax_t file_size = boost::filesystem::file_size(entry);
const std::string file_path = entry.path().string(); const std::string file_path = entry.path().string();
...@@ -137,7 +139,7 @@ void LLDiskCache::purge() ...@@ -137,7 +139,7 @@ void LLDiskCache::purge()
{ {
auto end_time = std::chrono::high_resolution_clock::now(); auto end_time = std::chrono::high_resolution_clock::now();
auto execute_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count(); auto execute_time = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
LL_INFOS() << "Total dir size after purge is " << dirFileSize(mCacheDir) << LL_ENDL; LL_INFOS() << "Total dir size after purge is " << dirFileSize(sCacheDir) << LL_ENDL;
LL_INFOS() << "Cache purge took " << execute_time << " ms to execute for " << file_info.size() << " files" << LL_ENDL; LL_INFOS() << "Cache purge took " << execute_time << " ms to execute for " << file_info.size() << " files" << LL_ENDL;
} }
} }
...@@ -189,18 +191,19 @@ const std::string LLDiskCache::assetTypeToString(LLAssetType::EType at) ...@@ -189,18 +191,19 @@ const std::string LLDiskCache::assetTypeToString(LLAssetType::EType at)
return std::string("UNKNOWN"); return std::string("UNKNOWN");
} }
// static
const std::string LLDiskCache::metaDataToFilepath(const LLUUID& id, const std::string LLDiskCache::metaDataToFilepath(const LLUUID& id,
LLAssetType::EType at) LLAssetType::EType at)
{ {
std::string uuidstr = id.asString(); std::string uuidstr = id.asString();
const auto& dirdelim = gDirUtilp->getDirDelimiter(); const auto& dirdelim = gDirUtilp->getDirDelimiter();
return absl::StrCat(mCacheDir, dirdelim, absl::string_view(&uuidstr[0], 1), dirdelim, uuidstr, mCacheFilenameExt); return absl::StrCat(sCacheDir, dirdelim, absl::string_view(&uuidstr[0], 1), dirdelim, uuidstr, sCacheFilenameExt);
} }
const std::string LLDiskCache::getCacheInfo() const std::string LLDiskCache::getCacheInfo()
{ {
F32 max_in_mb = (F32)mMaxSizeBytes / (1024.0 * 1024.0); F32 max_in_mb = (F32)mMaxSizeBytes / (1024.0 * 1024.0);
F32 percent_used = ((F32)dirFileSize(mCacheDir) / (F32)mMaxSizeBytes) * 100.0; F32 percent_used = ((F32)dirFileSize(sCacheDir) / (F32)mMaxSizeBytes) * 100.0;
return llformat("Max size %1.f MB (%.1f %% used)", max_in_mb, percent_used); return llformat("Max size %1.f MB (%.1f %% used)", max_in_mb, percent_used);
} }
...@@ -214,7 +217,7 @@ void LLDiskCache::clearCache() ...@@ -214,7 +217,7 @@ void LLDiskCache::clearCache()
* likely just fine * likely just fine
*/ */
#if LL_WINDOWS #if LL_WINDOWS
boost::filesystem::path cache_path(ll_convert_string_to_wide(mCacheDir)); boost::filesystem::path cache_path(ll_convert_string_to_wide(sCacheDir));
#else #else
boost::filesystem::path cache_path(mCacheDir); boost::filesystem::path cache_path(mCacheDir);
#endif #endif
...@@ -250,7 +253,7 @@ uintmax_t LLDiskCache::dirFileSize(const std::string dir) ...@@ -250,7 +253,7 @@ uintmax_t LLDiskCache::dirFileSize(const std::string dir)
{ {
if (boost::filesystem::is_regular_file(entry)) if (boost::filesystem::is_regular_file(entry))
{ {
if (entry.path().string().rfind(mCacheFilenameExt) != std::string::npos) if (entry.path().string().rfind(sCacheFilenameExt) != std::string::npos)
{ {
total_file_size += boost::filesystem::file_size(entry); total_file_size += boost::filesystem::file_size(entry);
} }
......
...@@ -104,7 +104,7 @@ class LLDiskCache : ...@@ -104,7 +104,7 @@ class LLDiskCache :
* so many things had to be pushed back there to accomodate it, that I * so many things had to be pushed back there to accomodate it, that I
* decided to move it here. Still not sure that's completely right. * decided to move it here. Still not sure that's completely right.
*/ */
const std::string metaDataToFilepath(const LLUUID& id, static const std::string metaDataToFilepath(const LLUUID& id,
LLAssetType::EType at); LLAssetType::EType at);
/** /**
...@@ -159,7 +159,7 @@ class LLDiskCache : ...@@ -159,7 +159,7 @@ class LLDiskCache :
* setting could potentially point it at a non-cache directory (for example, * setting could potentially point it at a non-cache directory (for example,
* the Windows System dir) with disastrous results. * the Windows System dir) with disastrous results.
*/ */
std::string mCacheDir; static std::string sCacheDir;
/** /**
* The extension inserted at the end of a cache file filename to * The extension inserted at the end of a cache file filename to
...@@ -169,7 +169,7 @@ class LLDiskCache : ...@@ -169,7 +169,7 @@ class LLDiskCache :
* like the users' OS system dir by mistake or maliciously and * like the users' OS system dir by mistake or maliciously and
* this will help to offset any damage if that happens. * this will help to offset any damage if that happens.
*/ */
std::string mCacheFilenameExt; static std::string sCacheFilenameExt;
/** /**
* When enabled, displays additional debugging information in * When enabled, displays additional debugging information in
......
...@@ -50,7 +50,7 @@ LLFileSystem::LLFileSystem(const LLUUID& file_id, const LLAssetType::EType file_ ...@@ -50,7 +50,7 @@ LLFileSystem::LLFileSystem(const LLUUID& file_id, const LLAssetType::EType file_
// static // static
bool LLFileSystem::getExists(const LLUUID& file_id, const LLAssetType::EType file_type) bool LLFileSystem::getExists(const LLUUID& file_id, const LLAssetType::EType file_type)
{ {
const std::string filename = LLDiskCache::getInstance()->metaDataToFilepath(file_id, file_type); const std::string filename = LLDiskCache::metaDataToFilepath(file_id, file_type);
llstat stat; llstat stat;
if (LLFile::stat(filename, &stat) == 0) if (LLFile::stat(filename, &stat) == 0)
...@@ -63,7 +63,7 @@ bool LLFileSystem::getExists(const LLUUID& file_id, const LLAssetType::EType fil ...@@ -63,7 +63,7 @@ bool LLFileSystem::getExists(const LLUUID& file_id, const LLAssetType::EType fil
// static // static
bool LLFileSystem::removeFile(const LLUUID& file_id, const LLAssetType::EType file_type) bool LLFileSystem::removeFile(const LLUUID& file_id, const LLAssetType::EType file_type)
{ {
const std::string filename = LLDiskCache::getInstance()->metaDataToFilepath(file_id, file_type); const std::string filename = LLDiskCache::metaDataToFilepath(file_id, file_type);
LLFile::remove(filename, ENOENT); LLFile::remove(filename, ENOENT);
...@@ -74,8 +74,8 @@ bool LLFileSystem::removeFile(const LLUUID& file_id, const LLAssetType::EType fi ...@@ -74,8 +74,8 @@ bool LLFileSystem::removeFile(const LLUUID& file_id, const LLAssetType::EType fi
bool LLFileSystem::renameFile(const LLUUID& old_file_id, const LLAssetType::EType old_file_type, bool LLFileSystem::renameFile(const LLUUID& old_file_id, const LLAssetType::EType old_file_type,
const LLUUID& new_file_id, const LLAssetType::EType new_file_type) const LLUUID& new_file_id, const LLAssetType::EType new_file_type)
{ {
const std::string old_filename = LLDiskCache::getInstance()->metaDataToFilepath(old_file_id, old_file_type); const std::string old_filename = LLDiskCache::metaDataToFilepath(old_file_id, old_file_type);
const std::string new_filename = LLDiskCache::getInstance()->metaDataToFilepath(new_file_id, new_file_type); const std::string new_filename = LLDiskCache::metaDataToFilepath(new_file_id, new_file_type);
// Rename needs the new file to not exist. // Rename needs the new file to not exist.
LLFile::remove(new_filename, ENOENT); LLFile::remove(new_filename, ENOENT);
...@@ -95,7 +95,7 @@ bool LLFileSystem::renameFile(const LLUUID& old_file_id, const LLAssetType::ETyp ...@@ -95,7 +95,7 @@ bool LLFileSystem::renameFile(const LLUUID& old_file_id, const LLAssetType::ETyp
// static // static
S32 LLFileSystem::getFileSize(const LLUUID& file_id, const LLAssetType::EType file_type) S32 LLFileSystem::getFileSize(const LLUUID& file_id, const LLAssetType::EType file_type)
{ {
const std::string filename = LLDiskCache::getInstance()->metaDataToFilepath(file_id, file_type); const std::string filename = LLDiskCache::metaDataToFilepath(file_id, file_type);
S32 file_size = 0; S32 file_size = 0;
llstat stat; llstat stat;
...@@ -111,7 +111,7 @@ BOOL LLFileSystem::read(U8* buffer, S32 bytes) ...@@ -111,7 +111,7 @@ BOOL LLFileSystem::read(U8* buffer, S32 bytes)
{ {
BOOL success = TRUE; BOOL success = TRUE;
const std::string filename = LLDiskCache::getInstance()->metaDataToFilepath(mFileID, mFileType); const std::string filename = LLDiskCache::metaDataToFilepath(mFileID, mFileType);
LLUniqueFile filep = LLFile::fopen(filename, "rb"); LLUniqueFile filep = LLFile::fopen(filename, "rb");
if (filep) if (filep)
...@@ -175,7 +175,7 @@ BOOL LLFileSystem::eof() ...@@ -175,7 +175,7 @@ BOOL LLFileSystem::eof()
BOOL LLFileSystem::write(const U8* buffer, S32 bytes) BOOL LLFileSystem::write(const U8* buffer, S32 bytes)
{ {
const std::string filename = LLDiskCache::getInstance()->metaDataToFilepath(mFileID, mFileType); const std::string filename = LLDiskCache::metaDataToFilepath(mFileID, mFileType);
BOOL success = FALSE; BOOL success = FALSE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment