Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nautaa committed May 9, 2024
1 parent 849c72c commit 1df8c79
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 107 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 5 additions & 4 deletions deps/memtracer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
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
CHECK_CXX_COMPILER_FLAG("-Wno-missing-attributes" COMPILER_SUPPORTS_NO_MISSING_ATTRIBUTES)
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)
29 changes: 14 additions & 15 deletions deps/memtracer/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ See the Mulan PSL v2 for more details. */

#include "memtracer/allocator.h"
#include <stdio.h>
#include <cstring>
#include <string.h>

// `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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
41 changes: 18 additions & 23 deletions deps/memtracer/allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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





19 changes: 3 additions & 16 deletions deps/memtracer/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,13 @@ See the Mulan PSL v2 for more details. */
#include <sstream>
#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);
vfprintf(stderr, format, vl);
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
}

}
} // namespace memtracer
19 changes: 7 additions & 12 deletions deps/memtracer/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ See the Mulan PSL v2 for more details. */
#pragma once

#include <stdio.h>
#include <string>

namespace memtracer
{
namespace memtracer {
#define mt_visible __attribute__((visibility("default")))

using malloc_func_t = void *(*)(size_t);
Expand All @@ -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);

}
} // namespace memtracer
49 changes: 31 additions & 18 deletions deps/memtracer/memtracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,42 @@ See the Mulan PSL v2 for more details. */
#include "memtracer.h"
#include <dlfcn.h>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <chrono>
#include <execinfo.h>
#include <iostream>
#include <cstdio>

#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<std::chrono::microseconds>(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<std::chrono::microseconds>(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()
{
Expand All @@ -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<size_t>(value));
Expand All @@ -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<size_t>(value));
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -144,4 +157,4 @@ void MemTracer::stat()
std::this_thread::sleep_for(std::chrono::microseconds(sleep_interval));
}
}
}
} // namespace memtracer
17 changes: 9 additions & 8 deletions deps/memtracer/memtracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ See the Mulan PSL v2 for more details. */
#include <mutex>
#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.
Expand Down Expand Up @@ -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);

Expand All @@ -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();
Expand All @@ -71,14 +72,14 @@ class MemTracer

private:
bool is_inited_ = false;
bool is_stop_ = false;
bool is_stop_ = false;
std::atomic<size_t> allocated_memory_{};
std::atomic<size_t> alloc_cnt_{};
std::atomic<size_t> 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_;
};
}
} // namespace memtracer
Loading

0 comments on commit 1df8c79

Please sign in to comment.