diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index 13fceb302..6b216bb8e 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -11,6 +11,6 @@ jobs: with: # sources 和 excludes 最终生成的find查找文件的命令大概是这样的 # find . -type f ! -wholename "./src/observer/sql/parser/lex_sql.*" \( -wholename "./**/*.h" -o -wholename "./**/*.cpp" \) - sources: "**/*.h,**/*.cpp,src/**/*.h,src/**/*.cpp,deps/common/**/*.h,deps/common/**/*.cpp" - excludes: "src/observer/sql/parser/lex_sql.*,src/observer/sql/parser/yacc_sql.*" + sources: "**/*.h,**/*.cpp,src/**/*.h,src/**/*.cpp" + excludes: "src/observer/sql/parser/lex_sql.*,src/observer/sql/parser/yacc_sql.*,deps/3rd/**/*.cpp,deps/3rd/**/*.h" style: "file" \ No newline at end of file diff --git a/deps/memtracer/CMakeLists.txt b/deps/memtracer/CMakeLists.txt index 76d58a408..1d825567f 100644 --- a/deps/memtracer/CMakeLists.txt +++ b/deps/memtracer/CMakeLists.txt @@ -1,7 +1,6 @@ file(GLOB MEMTRACER_SOURCES "*.cpp") - -set(CMAKE_CXX_VISIBILITY_PRESET hide) +SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) add_library(memtracer SHARED ${MEMTRACER_SOURCES}) #ignore missing attributes #ref jemalloc: https://github.com/jemalloc/jemalloc/blob/master/configure.ac @@ -9,8 +8,10 @@ CHECK_CXX_COMPILER_FLAG("-Wno-missing-attributes" COMPILER_SUPPORTS_NO_MISSING_A if(COMPILER_SUPPORTS_NO_MISSING_ATTRIBUTES) target_compile_options(memtracer PRIVATE -Wno-missing-attributes) endif() + +# hidden memtracer internal interfaces set_target_properties(memtracer PROPERTIES - CXX_VISIBILITY_PRESET hidden # 默认隐藏所有符号 - VISIBILITY_INLINES_HIDDEN YES # 内联函数也隐藏 + CXX_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN YES ) target_link_libraries(memtracer pthread) \ No newline at end of file diff --git a/deps/memtracer/allocator.cpp b/deps/memtracer/allocator.cpp index 34f4723fc..bc8c6d347 100644 --- a/deps/memtracer/allocator.cpp +++ b/deps/memtracer/allocator.cpp @@ -10,11 +10,12 @@ See the Mulan PSL v2 for more details. */ #include "memtracer/allocator.h" #include -#include +#include // `dlsym` calls `calloc` internally, so here use a dummy buffer // to avoid infinite loop when hook functions initialized. -// ref: https://stackoverflow.com/questions/7910666/problems-with-ld-preload-and-calloc-interposition-for-certain-executables +// ref: +// https://stackoverflow.com/questions/7910666/problems-with-ld-preload-and-calloc-interposition-for-certain-executables static unsigned char calloc_buffer[8192]; // only used internally @@ -44,7 +45,7 @@ mt_visible void *calloc(size_t nelem, size_t size) return calloc_buffer; } size_t alloc_size = nelem * size; - void *ptr = malloc(alloc_size); + void * ptr = malloc(alloc_size); if (ptr == NULL) [[unlikely]] { return NULL; } @@ -82,10 +83,7 @@ mt_visible void free(void *ptr) orig_free((size_t *)ptr - 1); } -mt_visible void cfree(void *ptr) -{ - free(ptr); -} +mt_visible void cfree(void *ptr) { free(ptr); } mt_visible void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { @@ -107,10 +105,10 @@ mt_visible int munmap(void *addr, size_t length) return res; } -mt_visible char *strdup(const char *s) +mt_visible char *strdup(const char *s) throw() { size_t len = strlen(s); - char *p = (char *)malloc(len + 1); + char * p = (char *)malloc(len + 1); if (p == NULL) { return NULL; } @@ -119,18 +117,19 @@ mt_visible char *strdup(const char *s) return p; } -mt_visible char *strndup(const char *s, size_t n) +mt_visible char *strndup(const char *s, size_t n) throw() { - const char* end = (const char*)memchr(s, 0, n); - const size_t m = (end != NULL ? (size_t)(end - s) : n); - char* t = (char*)malloc(m + 1); - if (t == NULL) return NULL; + const char * end = (const char *)memchr(s, 0, n); + const size_t m = (end != NULL ? (size_t)(end - s) : n); + char * t = (char *)malloc(m + 1); + if (t == NULL) + return NULL; memcpy(t, s, m); t[m] = 0; return t; } -mt_visible char* realpath(const char* fname, char* resolved_name) +mt_visible char *realpath(const char *fname, char *resolved_name) { MEMTRACER_LOG("realpath not supported\n"); exit(-1); diff --git a/deps/memtracer/allocator.h b/deps/memtracer/allocator.h index 50fc15322..ad23e0884 100644 --- a/deps/memtracer/allocator.h +++ b/deps/memtracer/allocator.h @@ -24,32 +24,32 @@ munmap_func_t orig_munmap = nullptr; extern "C" mt_visible void *malloc(size_t size); extern "C" mt_visible void *calloc(size_t nelem, size_t size); extern "C" mt_visible void *realloc(void *ptr, size_t size); -extern "C" mt_visible void free(void *ptr); -extern "C" mt_visible void cfree(void *ptr); +extern "C" mt_visible void free(void *ptr); +extern "C" mt_visible void cfree(void *ptr); extern "C" mt_visible void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); -extern "C" mt_visible int munmap(void *addr, size_t length); -extern "C" mt_visible char *strdup(const char *s) __THROW; -extern "C" mt_visible char *strndup(const char *s, size_t n) __THROW; +extern "C" mt_visible int munmap(void *addr, size_t length); +extern "C" mt_visible char *strdup(const char *s) throw(); +extern "C" mt_visible char *strndup(const char *s, size_t n) throw(); mt_visible void *operator new(std::size_t size); mt_visible void *operator new[](std::size_t size); mt_visible void *operator new(std::size_t size, const std::nothrow_t &) noexcept; mt_visible void *operator new[](std::size_t size, const std::nothrow_t &) noexcept; -mt_visible void operator delete(void *ptr) noexcept; -mt_visible void operator delete[](void *ptr) noexcept; -mt_visible void operator delete(void *ptr, const std::nothrow_t &) noexcept; -mt_visible void operator delete[](void *ptr, const std::nothrow_t &) noexcept; -mt_visible void operator delete(void *ptr, std::size_t size) noexcept; -mt_visible void operator delete[](void *ptr, std::size_t size) noexcept; +mt_visible void operator delete(void *ptr) noexcept; +mt_visible void operator delete[](void *ptr) noexcept; +mt_visible void operator delete(void *ptr, const std::nothrow_t &) noexcept; +mt_visible void operator delete[](void *ptr, const std::nothrow_t &) noexcept; +mt_visible void operator delete(void *ptr, std::size_t size) noexcept; +mt_visible void operator delete[](void *ptr, std::size_t size) noexcept; // unsupported libc functions, for simpler memory tracking. -extern "C" mt_visible char* realpath(const char* fname, char* resolved_name); -extern "C" mt_visible void *memalign(size_t alignment, size_t size); -extern "C" mt_visible void *valloc(size_t size); -extern "C" mt_visible void *pvalloc(size_t size); -extern "C" mt_visible int posix_memalign(void **memptr, size_t alignment, size_t size); -extern "C" mt_visible int brk(void *addr); -extern "C" mt_visible void *sbrk(intptr_t increment); +extern "C" mt_visible char * realpath(const char *fname, char *resolved_name); +extern "C" mt_visible void * memalign(size_t alignment, size_t size); +extern "C" mt_visible void * valloc(size_t size); +extern "C" mt_visible void * pvalloc(size_t size); +extern "C" mt_visible int posix_memalign(void **memptr, size_t alignment, size_t size); +extern "C" mt_visible int brk(void *addr); +extern "C" mt_visible void * sbrk(intptr_t increment); extern "C" mt_visible long int syscall(long int __sysno, ...); // forword libc interface @@ -65,8 +65,3 @@ extern "C" mt_visible void *__libc_memalign(size_t alignment, size_t size) __att extern "C" mt_visible int __posix_memalign(void **memptr, size_t alignment, size_t size) __attribute__((alias("posix_memalign"), used)); #endif - - - - - diff --git a/deps/memtracer/common.cpp b/deps/memtracer/common.cpp index a8c103f4e..1dc143d6a 100644 --- a/deps/memtracer/common.cpp +++ b/deps/memtracer/common.cpp @@ -12,9 +12,8 @@ See the Mulan PSL v2 for more details. */ #include #include "memtracer/common.h" -namespace memtracer -{ -void log_stderr(const char *format, ...) +namespace memtracer { +void log_stderr(const char *format, ...) { va_list vl; va_start(vl, format); @@ -22,16 +21,4 @@ void log_stderr(const char *format, ...) va_end(vl); } -// used only for getting memory size from `/proc/self/status` -long get_memory_size(const std::string &line) -{ - std::string token; - std::istringstream iss(line); - long size; - // skip token - iss >> token; - iss >> size; - return size; // KB -} - -} \ No newline at end of file +} // namespace memtracer \ No newline at end of file diff --git a/deps/memtracer/common.h b/deps/memtracer/common.h index 6e9d9a405..d6cb3f4ea 100644 --- a/deps/memtracer/common.h +++ b/deps/memtracer/common.h @@ -11,10 +11,8 @@ See the Mulan PSL v2 for more details. */ #pragma once #include -#include -namespace memtracer -{ +namespace memtracer { #define mt_visible __attribute__((visibility("default"))) using malloc_func_t = void *(*)(size_t); @@ -24,13 +22,10 @@ using munmap_func_t = int (*)(void *, size_t); void log_stderr(const char *format, ...); -#define MEMTRACER_LOG(format, ...) \ -do { \ - fprintf(stderr, "[MEMTRACER] ");\ - log_stderr(format, ##__VA_ARGS__);\ -} while (0) +#define MEMTRACER_LOG(format, ...) \ + do { \ + fprintf(stderr, "[MEMTRACER] "); \ + log_stderr(format, ##__VA_ARGS__); \ + } while (0) -// used only for getting memory size from `/proc/self/status` -long get_memory_size(const std::string &line); - -} \ No newline at end of file +} // namespace memtracer \ No newline at end of file diff --git a/deps/memtracer/memtracer.cpp b/deps/memtracer/memtracer.cpp index 63cea79c8..1f0decc14 100644 --- a/deps/memtracer/memtracer.cpp +++ b/deps/memtracer/memtracer.cpp @@ -11,6 +11,7 @@ See the Mulan PSL v2 for more details. */ #include "memtracer.h" #include #include +#include #include #include #include @@ -18,24 +19,34 @@ See the Mulan PSL v2 for more details. */ #include #define REACH_TIME_INTERVAL(i) \ -({ \ - bool bret = false; \ - static volatile int64_t last_time = 0; \ - auto now = std::chrono::system_clock::now(); \ - int64_t cur_time = std::chrono::duration_cast(now.time_since_epoch()).count(); \ - if (int64_t(i + last_time) < cur_time) [[unlikely]] { \ - last_time = cur_time; \ - bret = true; \ - } \ - bret; \ -}) + ({ \ + bool bret = false; \ + static volatile int64_t last_time = 0; \ + auto now = std::chrono::system_clock::now(); \ + int64_t cur_time = std::chrono::duration_cast(now.time_since_epoch()).count(); \ + if (int64_t(i + last_time) < cur_time) [[unlikely]] { \ + last_time = cur_time; \ + bret = true; \ + } \ + bret; \ + }) extern memtracer::malloc_func_t orig_malloc; extern memtracer::free_func_t orig_free; extern memtracer::mmap_func_t orig_mmap; extern memtracer::munmap_func_t orig_munmap; -namespace memtracer +namespace memtracer { +// used only for getting memory size from `/proc/self/status` +long get_memory_size(const std::string &line) { + std::string token; + std::istringstream iss(line); + long size; + // skip token + iss >> token; + iss >> size; + return size; // KB +} MemTracer &MemTracer::get_instance() { @@ -50,7 +61,7 @@ void MemTracer::init() // init memory limit const char *memory_limit_str = std::getenv("MT_MEMORY_LIMIT"); if (memory_limit_str != nullptr) { - char *end; + char * end; unsigned long long value = std::strtoull(memory_limit_str, &end, 10); if (end != memory_limit_str && *end == '\0') { MT.set_memory_limit(static_cast(value)); @@ -62,7 +73,7 @@ void MemTracer::init() // init print_interval const char *print_interval_str = std::getenv("MT_PRINT_INTERVAL"); if (print_interval_str != nullptr) { - char *end; + char * end; unsigned long long value = std::strtoull(print_interval_str, &end, 10); if (end != print_interval_str && *end == '\0') { MT.set_print_interval(static_cast(value)); @@ -74,11 +85,11 @@ void MemTracer::init() } // init `text` memory usage - size_t text_size = 0; + size_t text_size = 0; std::ifstream file("/proc/self/status"); if (file.is_open()) { std::string line; - const int KB = 1024; + const int KB = 1024; while (std::getline(file, line)) { if (line.find("VmExe") != std::string::npos) { text_size = get_memory_size(line) * KB; @@ -103,7 +114,9 @@ void MemTracer::alloc(size_t size) { if (allocated_memory_.load() + size > memory_limit_) [[unlikely]] { MEMTRACER_LOG("alloc memory:%lu, allocated_memory: %lu, memory_limit: %lu, Memory limit exceeded!\n", - size, allocated_memory_.load(), memory_limit_); + size, + allocated_memory_.load(), + memory_limit_); exit(-1); } allocated_memory_.fetch_add(size); @@ -144,4 +157,4 @@ void MemTracer::stat() std::this_thread::sleep_for(std::chrono::microseconds(sleep_interval)); } } -} \ No newline at end of file +} // namespace memtracer \ No newline at end of file diff --git a/deps/memtracer/memtracer.h b/deps/memtracer/memtracer.h index bbdbd0ca8..99d3b6f72 100644 --- a/deps/memtracer/memtracer.h +++ b/deps/memtracer/memtracer.h @@ -16,12 +16,11 @@ See the Mulan PSL v2 for more details. */ #include #include "memtracer/common.h" -namespace memtracer -{ +namespace memtracer { #define MT MemTracer::get_instance() -// MemTracer is used to monitor MiniOB memory usage; +// MemTracer is used to monitor MiniOB memory usage; // it is used to run and debug MiniOB under memory-constrained conditions. // MemTracer statistics memory allocation in MiniOB processes by // hooking memory alloc/free functions. @@ -51,7 +50,10 @@ class MemTracer inline void add_allocated_memory(size_t size) { allocated_memory_.fetch_add(size); } - void set_memory_limit(size_t memory_limit) { std::call_once(memory_limit_once_, [&](){memory_limit_ = memory_limit;}); } + void set_memory_limit(size_t memory_limit) + { + std::call_once(memory_limit_once_, [&]() { memory_limit_ = memory_limit; }); + } void alloc(size_t size); @@ -60,7 +62,6 @@ class MemTracer inline void init_hook_funcs() { std::call_once(init_hook_funcs_once_, init_hook_funcs_impl); } private: - static void init_hook_funcs_impl(); void init_stats_thread(); @@ -71,14 +72,14 @@ class MemTracer private: bool is_inited_ = false; - bool is_stop_ = false; + bool is_stop_ = false; std::atomic allocated_memory_{}; std::atomic alloc_cnt_{}; std::atomic free_cnt_{}; std::once_flag init_hook_funcs_once_; std::once_flag memory_limit_once_; - size_t memory_limit_ = UINT64_MAX; + size_t memory_limit_ = UINT64_MAX; size_t print_interval_ = 0; std::thread t_; }; -} \ No newline at end of file +} // namespace memtracer \ No newline at end of file diff --git a/deps/memtracer/mt_info.cpp b/deps/memtracer/mt_info.cpp index 2c1b3b888..3a77a50f3 100644 --- a/deps/memtracer/mt_info.cpp +++ b/deps/memtracer/mt_info.cpp @@ -9,12 +9,11 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. */ #include "mt_info.h" -namespace memtracer -{ +namespace memtracer { mt_visible size_t allocated_memory() { return MT.allocated_memory(); } mt_visible size_t meta_memory() { return MT.meta_memory(); } mt_visible size_t memory_limit() { return MT.memory_limit(); } -} +} // namespace memtracer diff --git a/deps/memtracer/mt_info.h b/deps/memtracer/mt_info.h index b7f20ffca..592d46413 100644 --- a/deps/memtracer/mt_info.h +++ b/deps/memtracer/mt_info.h @@ -9,11 +9,10 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the Mulan PSL v2 for more details. */ #include "memtracer.h" -namespace memtracer -{ +namespace memtracer { mt_visible size_t allocated_memory(); mt_visible size_t meta_memory(); mt_visible size_t memory_limit(); -} \ No newline at end of file +} // namespace memtracer \ No newline at end of file diff --git a/unittest/memtracer_test.cpp b/unittest/memtracer_test.cpp index 8c612e095..8516036e3 100644 --- a/unittest/memtracer_test.cpp +++ b/unittest/memtracer_test.cpp @@ -146,8 +146,8 @@ TEST(test_mem_tracer, test_mem_tracer_basic) // ASSERT_EQ(allocated_memory(), mem_base); } - // __libc_malloc - #ifdef __linux__ +// __libc_malloc +#ifdef __linux__ { void *ptr = __libc_malloc(1024); memset(ptr, 0, 1024); @@ -155,7 +155,7 @@ TEST(test_mem_tracer, test_mem_tracer_basic) free(ptr); ASSERT_EQ(memtracer::allocated_memory(), mem_base); } - #endif +#endif // __builtin_malloc {