This commit is contained in:
Amos Bird 2021-08-28 11:43:43 +08:00
parent e6a1124ebe
commit 163a416366
No known key found for this signature in database
GPG Key ID: 80D430DCBECFEDB4
5 changed files with 33 additions and 20 deletions

View File

@ -431,16 +431,20 @@ std::string StackTrace::toString(void ** frame_pointers_, size_t offset, size_t
return toStringStatic(frame_pointers_copy, offset, size);
}
std::string StackTrace::toStringStatic(const StackTrace::FramePointers & frame_pointers, size_t offset, size_t size, bool reload)
static SimpleCache<decltype(toStringImpl), &toStringImpl> & cacheInstance()
{
static SimpleCache<decltype(toStringImpl), &toStringImpl> cache;
return cache;
}
std::string StackTrace::toStringStatic(const StackTrace::FramePointers & frame_pointers, size_t offset, size_t size)
{
/// Calculation of stack trace text is extremely slow.
/// We use simple cache because otherwise the server could be overloaded by trash queries.
static SimpleCache<decltype(toStringImpl), &toStringImpl> func_cached;
/// Reload cached stacktrace instead.
if (reload)
{
func_cached.drop();
return "";
}
return func_cached(frame_pointers, offset, size);
return cacheInstance()(frame_pointers, offset, size);
}
void StackTrace::dropCache()
{
cacheInstance().drop();
}

View File

@ -61,7 +61,8 @@ public:
std::string toString() const;
static std::string toString(void ** frame_pointers, size_t offset, size_t size);
static std::string toStringStatic(const FramePointers & frame_pointers, size_t offset, size_t size, bool reload = false);
static std::string toStringStatic(const FramePointers & frame_pointers, size_t offset, size_t size);
static void dropCache();
static void symbolize(const FramePointers & frame_pointers, size_t offset, size_t size, StackTrace::Frames & frames);
void toStringEveryLine(std::function<void(const std::string &)> callback) const;

View File

@ -462,16 +462,22 @@ String SymbolIndex::getBuildIDHex() const
return build_id_hex;
}
MultiVersion<SymbolIndex>::Version SymbolIndex::instance(bool reload)
MultiVersion<SymbolIndex> & SymbolIndex::instanceImpl()
{
static MultiVersion<SymbolIndex> instance(std::unique_ptr<SymbolIndex>(new SymbolIndex));
if (reload)
{
instance.set(std::unique_ptr<SymbolIndex>(new SymbolIndex));
/// Also reload stacktrace cache.
StackTrace::toStringStatic({}, 0, 0, true);
}
return instance.get();
return instance;
}
MultiVersion<SymbolIndex>::Version SymbolIndex::instance()
{
return instanceImpl().get();
}
void SymbolIndex::reload()
{
instanceImpl().set(std::unique_ptr<SymbolIndex>(new SymbolIndex));
/// Also drop stacktrace cache.
StackTrace::dropCache();
}
}

View File

@ -22,7 +22,8 @@ protected:
SymbolIndex() { update(); }
public:
static MultiVersion<SymbolIndex>::Version instance(bool reload = false);
static MultiVersion<SymbolIndex>::Version instance();
static void reload();
struct Symbol
{
@ -60,6 +61,7 @@ private:
Data data;
void update();
static MultiVersion<SymbolIndex> & instanceImpl();
};
}

View File

@ -337,7 +337,7 @@ BlockIO InterpreterSystemQuery::execute()
{
#if defined(__ELF__) && !defined(__FreeBSD__)
getContext()->checkAccess(AccessType::SYSTEM_RELOAD_SYMBOLS);
(void)SymbolIndex::instance(true);
SymbolIndex::reload();
break;
#else
throw Exception("SYSTEM RELOAD SYMBOLS is not supported on current platform", ErrorCodes::NOT_IMPLEMENTED);