Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyiZzz committed Apr 18, 2024
1 parent 496dc3e commit b5973a5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
16 changes: 10 additions & 6 deletions be/src/common/stack_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,8 @@ static StackTraceCache& cacheInstance() {

static std::mutex stacktrace_cache_mutex;

std::string toStringCached(const StackTrace::FramePointers& pointers, size_t offset, size_t size) {
std::string toStringCached(const StackTrace::FramePointers& pointers, size_t offset, size_t size,
const std::string& dwarf_location_info_mode) {
/// Calculation of stack trace text is extremely slow.
/// We use simple cache because otherwise the server could be overloaded by trash queries.
/// Note that this cache can grow unconditionally, but practically it should be small.
Expand All @@ -452,29 +453,32 @@ std::string toStringCached(const StackTrace::FramePointers& pointers, size_t off
return it->second;
} else {
std::stringstream out;
toStringEveryLineImpl(doris::config::dwarf_location_info_mode, key,
toStringEveryLineImpl(dwarf_location_info_mode, key,
[&](std::string_view str) { out << str << '\n'; });

return cache.emplace(StackTraceTriple {pointers, offset, size}, out.str()).first->second;
}
}

std::string StackTrace::toString(int start_pointers_index) const {
std::string StackTrace::toString(int start_pointers_index,
const std::string& dwarf_location_info_mode) const {
// Default delete the first three frame pointers, which are inside the stack_trace.cpp.
start_pointers_index += 3;
StackTrace::FramePointers frame_pointers_raw {};
std::copy(frame_pointers.begin() + start_pointers_index, frame_pointers.end(),
frame_pointers_raw.begin());
return toStringCached(frame_pointers_raw, offset, size - start_pointers_index);
return toStringCached(frame_pointers_raw, offset, size - start_pointers_index,
dwarf_location_info_mode);
}

std::string StackTrace::toString(void** frame_pointers_raw, size_t offset, size_t size) {
std::string StackTrace::toString(void** frame_pointers_raw, size_t offset, size_t size,
const std::string& dwarf_location_info_mode) {
__msan_unpoison(frame_pointers_raw, size * sizeof(*frame_pointers_raw));

StackTrace::FramePointers frame_pointers {};
std::copy_n(frame_pointers_raw, size, frame_pointers.begin());

return toStringCached(frame_pointers, offset, size);
return toStringCached(frame_pointers, offset, size, dwarf_location_info_mode);
}

void StackTrace::createCache() {
Expand Down
6 changes: 4 additions & 2 deletions be/src/common/stack_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ class StackTrace {
[[nodiscard]] constexpr size_t getSize() const { return size; }
[[nodiscard]] constexpr size_t getOffset() const { return offset; }
[[nodiscard]] const FramePointers& getFramePointers() const { return frame_pointers; }
[[nodiscard]] std::string toString(int start_pointers_index = 0) const;
[[nodiscard]] std::string toString(int start_pointers_index = 0,
const std::string& dwarf_location_info_mode = "FAST") const;

static std::string toString(void** frame_pointers, size_t offset, size_t size);
static std::string toString(void** frame_pointers, size_t offset, size_t size,
const std::string& dwarf_location_info_mode = "FAST");
static void createCache();
static void dropCache();
static void symbolize(const FramePointers& frame_pointers, size_t offset, size_t size,
Expand Down
12 changes: 8 additions & 4 deletions be/src/util/stack_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ void DumpStackTraceToString(std::string* stacktrace);

namespace doris {

std::string get_stack_trace(int start_pointers_index) {
std::string get_stack_trace(int start_pointers_index, std::string dwarf_location_info_mode) {
#ifdef ENABLE_STACKTRACE
if (dwarf_location_info_mode.empty()) {
dwarf_location_info_mode = config::dwarf_location_info_mode;
}
auto tool = config::get_stack_trace_tool;
if (tool == "glog") {
return get_stack_trace_by_glog();
Expand All @@ -48,7 +51,7 @@ std::string get_stack_trace(int start_pointers_index) {
#if defined(__APPLE__) // TODO
return get_stack_trace_by_glog();
#endif
return get_stack_trace_by_libunwind(start_pointers_index);
return get_stack_trace_by_libunwind(start_pointers_index, dwarf_location_info_mode);
} else {
return "no stack";
}
Expand Down Expand Up @@ -80,8 +83,9 @@ std::string get_stack_trace_by_glibc() {
return out.str();
}

std::string get_stack_trace_by_libunwind(int start_pointers_index) {
return "\n" + StackTrace().toString(start_pointers_index);
std::string get_stack_trace_by_libunwind(int start_pointers_index,
const std::string& dwarf_location_info_mode) {
return "\n" + StackTrace().toString(start_pointers_index, dwarf_location_info_mode);
}

} // namespace doris
6 changes: 4 additions & 2 deletions be/src/util/stack_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace doris {
// boost: 1000 times cost 1min, has line numbers, but has memory leak.
// glibc: 1000 times cost 1min, no line numbers, unresolved backtrace symbol.
// libunwind: cost is negligible, has line numbers.
std::string get_stack_trace(int start_pointers_index = 0);
std::string get_stack_trace(int start_pointers_index = 0,
std::string dwarf_location_info_mode = "");

// Note: there is a libc bug that causes this not to work on 64 bit machines
// for recursive calls.
Expand All @@ -53,6 +54,7 @@ std::string get_stack_trace_by_glibc();
// 2. Support signal handle
// 3. libunwid support unw_backtrace for jemalloc
// 4. Use of undefined compile option USE_MUSL for later
std::string get_stack_trace_by_libunwind(int start_pointers_index);
std::string get_stack_trace_by_libunwind(int start_pointers_index,
const std::string& dwarf_location_info_mode);

} // namespace doris

0 comments on commit b5973a5

Please sign in to comment.