增加jemalloc工具类, 增加jemalloc内存统计分析 (#2885)

增加jemalloc工具类, 增加jemalloc内存统计分析
This commit is contained in:
alexliyu7352 2023-10-10 11:48:56 +08:00 committed by GitHub
parent db3f0147be
commit fdc00d5a02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 123 additions and 2 deletions

View File

@ -22,10 +22,11 @@
#include <map>
#include <iostream>
#include "Common/JemallocUtil.h"
#include "Common/macros.h"
#include "System.h"
#include "Util/logger.h"
#include "Util/uv_errno.h"
#include "System.h"
#include "Common/macros.h"
using namespace std;
using namespace toolkit;
@ -55,6 +56,16 @@ string System::execute(const string &cmd) {
static constexpr int MAX_STACK_FRAMES = 128;
static void save_jemalloc_stats() {
string jemalloc_status = JemallocUtil::get_malloc_stats();
if (jemalloc_status.empty()) {
return;
}
ofstream out(StrPrinter << exeDir() << "/jemalloc.json", ios::out | ios::binary | ios::trunc);
out << jemalloc_status;
out.flush();
}
static void sig_crash(int sig) {
signal(sig, SIG_DFL);
void *array[MAX_STACK_FRAMES];
@ -149,6 +160,12 @@ void System::startDaemon(bool &kill_parent_if_failed) {
}
void System::systemSetup(){
#ifdef ENABLE_JEMALLOC_DUMP
//Save memory report when program exits
atexit(save_jemalloc_stats);
#endif //ENABLE_JEMALLOC_DUMP
#if !defined(_WIN32)
struct rlimit rlim,rlim_new;
if (getrlimit(RLIMIT_CORE, &rlim)==0) {

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include "JemallocUtil.h"
#include "Util/logger.h"
#ifdef USE_JEMALLOC
#include <iostream>
#include <jemalloc/jemalloc.h>
#endif
namespace mediakit {
void set_profile_active(bool active) {
#ifdef USE_JEMALLOC
int err = mallctl("prof.active", nullptr, nullptr, (void *)&active, sizeof(active));
if (err != 0) {
WarnL << "mallctl failed with: " << err;
}
#endif
}
void JemallocUtil::enable_profiling() {
set_profile_active(true);
}
void JemallocUtil::disable_profiling() {
set_profile_active(false);
}
void JemallocUtil::dump(const std::string &file_name) {
#ifdef USE_JEMALLOC
auto *c_str = file_name.c_str();
int err = mallctl("prof.dump", nullptr, nullptr, &c_str, sizeof(const char *));
if (err != 0) {
std::cerr << "mallctl failed with: " << err << std::endl;
}
#endif
}
std::string JemallocUtil::get_malloc_stats() {
#ifdef USE_JEMALLOC
std::string res;
malloc_stats_print([](void *opaque, const char *s) { ((std::string *)opaque)->append(s); }, &res, "J");
return res;
#else
return "";
#endif
}
void JemallocUtil::some_malloc_stats(const std::function<void(const char *, uint64_t)> &fn) {
#ifdef USE_JEMALLOC
constexpr std::array<const char *, 8> STATS = {
"stats.allocated", "stats.active", "stats.metadata", "stats.metadata_thp",
"stats.resident", "stats.mapped", "stats.retained", "stats.zero_reallocs",
};
for (const char *stat : STATS) {
size_t value;
size_t len = sizeof(value);
auto err = mallctl(stat, &value, &len, nullptr, 0);
if (err != 0) {
ErrorL << "Failed reading " << stat << ": " << err;
continue;
}
fn(stat, value);
}
#endif
}
} // namespace mediakit

30
src/Common/JemallocUtil.h Normal file
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef ZLMEDIAKIT_JEMALLOCUTIL_H
#define ZLMEDIAKIT_JEMALLOCUTIL_H
#include <functional>
#include <string>
namespace mediakit {
class JemallocUtil {
public:
JemallocUtil() = default;
~JemallocUtil() = default;
static void enable_profiling();
static void disable_profiling();
static void dump(const std::string &file_name);
static std::string get_malloc_stats();
static void some_malloc_stats(const std::function<void(const char *, uint64_t)> &fn);
};
} // namespace mediakit
#endif // ZLMEDIAKIT_JEMALLOCUTIL_H