Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nautaa committed May 13, 2024
1 parent 0d52c14 commit e8b6347
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 20 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
shell: bash
run: bash build.sh debug -DCONCURRENCY=ON -DENABLE_COVERAGE=ON --make -j4

# `memtracer_test` unittest runs in `memtracer-test` action.
- name: Test
shell: bash
run: |
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,9 @@ jobs:
cd test/sysbench
sysbench --mysql-socket=/tmp/miniob.sock --mysql-ignore-errors=41 --threads=10 miniob_insert prepare
sysbench --mysql-socket=/tmp/miniob.sock --mysql-ignore-errors=41 --threads=10 miniob_insert run
killall observer
nohup ./build_release/bin/observer -T one-thread-per-connection -s /tmp/miniob.sock -f etc/observer.ini -P mysql -t mvcc -d disk &
sleep 10 && echo "wake up"
mysql -S /tmp/miniob.sock -e "show tables"
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OPTION(ENABLE_TSAN "Build with thread sanitizer" OFF)
OPTION(ENABLE_UBSAN "Build with undefined behavior sanitizer" OFF)
OPTION(WITH_UNIT_TESTS "Compile miniob with unit tests" ON)
OPTION(WITH_BENCHMARK "Compile benchmark" OFF)
# MemTracer doesn't work with sanitizers.
# TODO: support MemTracer with sanitizers, currently MemTracer doesn't work with sanitizers.
OPTION(WITH_MEMTRACER "Compile memtracer" ON)
OPTION(ENABLE_COVERAGE "Enable unittest coverage" OFF)
OPTION(ENABLE_NOPIE "Enable no pie" OFF)
Expand Down
15 changes: 13 additions & 2 deletions benchmark/memtracer_performance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

#include <benchmark/benchmark.h>
#include <cstdlib>

static void BM_MallocFree(benchmark::State &state)
{
Expand All @@ -22,6 +21,18 @@ static void BM_MallocFree(benchmark::State &state)
state.SetBytesProcessed(static_cast<int64_t>(state.iterations() * size));
}

BENCHMARK(BM_MallocFree)->Arg(8)->Arg(64)->Arg(512)->Arg(1 << 10)->Arg(1 << 20)->Arg(8 << 20)->Arg(1 << 30);
static void BM_NewDelete(benchmark::State &state)
{
size_t size = state.range(0);
for (auto _ : state) {
char *ptr = new char[size];
benchmark::DoNotOptimize(ptr);
delete[] ptr;
}
state.SetBytesProcessed(static_cast<int64_t>(state.iterations() * size));
}

BENCHMARK(BM_MallocFree)->Arg(8)->Arg(64-8)->Arg(512-8)->Arg(1 << 10-8)->Arg(1 << 20-8)->Arg(8 << 20-8)->Arg(1 << 30-8);
BENCHMARK(BM_NewDelete)->Arg(8)->Arg(64)->Arg(512)->Arg(1 << 10)->Arg(1 << 20)->Arg(8 << 20)->Arg(1 << 30);

BENCHMARK_MAIN();
1 change: 0 additions & 1 deletion deps/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR})
MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR})

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
include(CheckCXXCompilerFlag)

FILE(GLOB_RECURSE ALL_SRC *.cpp)

Expand Down
2 changes: 2 additions & 0 deletions deps/memtracer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
include(CheckCXXCompilerFlag)

file(GLOB MEMTRACER_SOURCES "*.cpp")

SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
Expand Down
1 change: 0 additions & 1 deletion deps/memtracer/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

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

// `dlsym` calls `calloc` internally, so here use a dummy buffer
Expand Down
23 changes: 12 additions & 11 deletions deps/memtracer/memtracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ See the Mulan PSL v2 for more details. */
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(); \
int64_t cur_time = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count(); \
if (int64_t(i + last_time) < cur_time) [[unlikely]] { \
last_time = cur_time; \
bret = true; \
Expand Down Expand Up @@ -71,20 +71,21 @@ void MemTracer::init()
}

// init print_interval
const char *print_interval_str = std::getenv("MT_PRINT_INTERVAL");
if (print_interval_str != nullptr) {
const char *print_interval_ms_str = std::getenv("MT_PRINT_INTERVAL_MS");
if (print_interval_ms_str != nullptr) {
char * end;
unsigned long long value = std::strtoull(print_interval_str, &end, 10);
if (end != print_interval_str && *end == '\0') {
unsigned long long value = std::strtoull(print_interval_ms_str, &end, 10);
if (end != print_interval_ms_str && *end == '\0') {
MT.set_print_interval(static_cast<size_t>(value));
} else {
fprintf(stderr, "Invalid environment variable value for MT_MEMORY_LIMIT: %s\n", print_interval_str);
fprintf(stderr, "Invalid environment variable value for MT_MEMORY_LIMIT: %s\n", print_interval_ms_str);
}
} else {
MT.set_print_interval(1000 * 1000 * 5); // 5s
MT.set_print_interval(1000 * 5); // 5s
}

// init `text` memory usage
// TODO: support static variables statistic.
size_t text_size = 0;
std::ifstream file("/proc/self/status");
if (file.is_open()) {
Expand Down Expand Up @@ -147,14 +148,14 @@ void MemTracer::init_hook_funcs_impl()

void MemTracer::stat()
{
const size_t print_interval = MT.print_interval();
const size_t sleep_interval = 100 * 1000; // 100 ms
const size_t print_interval_ms = MT.print_interval();
const size_t sleep_interval = 100; // 100 ms
while (!MT.is_stop()) {
if (REACH_TIME_INTERVAL(print_interval)) {
if (REACH_TIME_INTERVAL(print_interval_ms)) {
// TODO: optimize the output format
MEMTRACER_LOG("allocated memory: %lu, metadata memory: %lu\n", MT.allocated_memory(), MT.meta_memory());
}
std::this_thread::sleep_for(std::chrono::microseconds(sleep_interval));
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_interval));
}
}
} // namespace memtracer
6 changes: 3 additions & 3 deletions deps/memtracer/memtracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class MemTracer

size_t meta_memory() const { return ((alloc_cnt_.load() - free_cnt_.load()) * sizeof(size_t)); }

size_t print_interval() const { return print_interval_; }
size_t print_interval() const { return print_interval_ms_; }

size_t memory_limit() const { return memory_limit_; }

bool is_stop() const { return is_stop_; }

void set_print_interval(size_t print_interval) { print_interval_ = print_interval; }
void set_print_interval(size_t print_interval_ms) { print_interval_ms_ = print_interval_ms; }

inline void add_allocated_memory(size_t size) { allocated_memory_.fetch_add(size); }

Expand Down Expand Up @@ -79,7 +79,7 @@ class MemTracer
std::once_flag init_hook_funcs_once_;
std::once_flag memory_limit_once_;
size_t memory_limit_ = UINT64_MAX;
size_t print_interval_ = 0;
size_t print_interval_ms_ = 0;
std::thread t_;
};
} // namespace memtracer
2 changes: 1 addition & 1 deletion docs/src/game/miniob-memtracer.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ MT_MEMORY_LIMIT=100000000 LD_PRELOAD=./lib/libmemtracer.so ./bin/observer
Aborted
```
开发者可根据堆栈地址
1. 通过链接 MemTracer,可以在 MiniOB 代码任意位置获取当前内存使用情况。示例可参考:`unittest/memtracer_test.cpp`.
1. TODO: 通过链接 MemTracer,可以在 MiniOB 代码任意位置获取当前内存使用情况。示例可参考:`unittest/memtracer_test.cpp`.

### 注意
1. MemTracer 会记录 `mmap` 映射的整个虚拟内存占用, 因此不建议使用 `mmap` 管理内存。
Expand Down

0 comments on commit e8b6347

Please sign in to comment.