Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
fdc00d5a
Unverified
Commit
fdc00d5a
authored
Oct 10, 2023
by
alexliyu7352
Committed by
GitHub
Oct 10, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加jemalloc工具类, 增加jemalloc内存统计分析 (#2885)
增加jemalloc工具类, 增加jemalloc内存统计分析
parent
db3f0147
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
124 行增加
和
2 行删除
+124
-2
server/System.cpp
+19
-2
src/Common/JemallocUtil.cpp
+75
-0
src/Common/JemallocUtil.h
+30
-0
没有找到文件。
server/System.cpp
查看文件 @
fdc00d5a
...
...
@@ -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
)
{
...
...
src/Common/JemallocUtil.cpp
0 → 100644
查看文件 @
fdc00d5a
/*
* 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
\ No newline at end of file
src/Common/JemallocUtil.h
0 → 100644
查看文件 @
fdc00d5a
/*
* 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
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论