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
0c728827
Commit
0c728827
authored
Feb 21, 2021
by
xia-chu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加全局内存统计功能
parent
a19f6767
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
132 行增加
和
4 行删除
+132
-4
CMakeLists.txt
+15
-0
server/WebApi.cpp
+7
-0
src/Common/config.cpp
+110
-4
没有找到文件。
CMakeLists.txt
查看文件 @
0c728827
...
...
@@ -53,6 +53,21 @@ option(ENABLE_API "Enable C API SDK" true)
option
(
ENABLE_CXX_API
"Enable C++ API SDK"
false
)
option
(
ENABLE_TESTS
"Enable Tests"
true
)
option
(
ENABLE_SERVER
"Enable Server"
true
)
option
(
ENABLE_MEM_DEBUG
"Enable Memory Debug"
false
)
option
(
ENABLE_ASAN
"Enable Address Sanitize"
false
)
if
(
ENABLE_MEM_DEBUG
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-Wl,-wrap,free -Wl,-wrap,malloc -Wl,-wrap,realloc -Wl,-wrap,calloc"
)
set
(
CMAKE_C_FLAGS
"
${
CMAKE_C_FLAGS
}
-Wl,-wrap,free -Wl,-wrap,malloc -Wl,-wrap,realloc -Wl,-wrap,calloc"
)
add_definitions
(
-DENABLE_MEM_DEBUG
)
message
(
STATUS
"已启用内存调试功能"
)
endif
()
if
(
ENABLE_ASAN
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-fsanitize=address -fno-omit-frame-pointer"
)
set
(
CMAKE_C_FLAGS
"
${
CMAKE_C_FLAGS
}
-fsanitize=address -fno-omit-frame-pointer"
)
message
(
STATUS
"已启用Address Sanitize"
)
endif
()
set
(
LINK_LIB_LIST zlmediakit zltoolkit
)
...
...
server/WebApi.cpp
查看文件 @
0c728827
...
...
@@ -170,6 +170,8 @@ static ApiArgsType getAllArgs(const Parser &parser) {
return
allArgs
;
}
extern
uint64_t
getTotalMemUsage
();
static
inline
void
addHttpListener
(){
GET_CONFIG
(
bool
,
api_debug
,
API
::
kApiDebug
);
//注册监听kBroadcastHttpRequest事件
...
...
@@ -1046,6 +1048,11 @@ void installWebApi() {
val
[
"data"
][
"RtpPacket"
]
=
(
Json
::
UInt64
)(
ObjectStatistic
<
RtpPacket
>::
count
());
val
[
"data"
][
"RtmpPacket"
]
=
(
Json
::
UInt64
)(
ObjectStatistic
<
RtmpPacket
>::
count
());
#ifdef ENABLE_MEM_DEBUG
auto
bytes
=
getTotalMemUsage
();
val
[
"data"
][
"totalMemUsage"
]
=
(
Json
::
UInt64
)
bytes
;
val
[
"data"
][
"totalMemUsageMB"
]
=
(
int
)(
bytes
/
1024
/
1024
);
#endif
});
////////////以下是注册的Hook API////////////
...
...
src/Common/config.cpp
查看文件 @
0c728827
...
...
@@ -307,11 +307,117 @@ const string kBenchmarkMode = "benchmark_mode";
}
// namespace mediakit
void
Assert_Throw
(
int
failed
,
const
char
*
exp
,
const
char
*
func
,
const
char
*
file
,
int
line
){
if
(
failed
)
{
extern
"C"
{
void
Assert_Throw
(
int
failed
,
const
char
*
exp
,
const
char
*
func
,
const
char
*
file
,
int
line
)
{
if
(
failed
)
{
_StrPrinter
printer
;
printer
<<
"Assertion failed: ("
<<
exp
<<
"), function "
<<
func
<<
", file "
<<
file
<<
", line "
<<
line
<<
"."
;
printer
<<
"Assertion failed: ("
<<
exp
<<
"), function "
<<
func
<<
", file "
<<
file
<<
", line "
<<
line
<<
"."
;
throw
std
::
runtime_error
(
printer
);
}
}
}
#ifdef ENABLE_MEM_DEBUG
static
atomic
<
uint64_t
>
mem_usage
(
0
);
uint64_t
getTotalMemUsage
()
{
return
mem_usage
.
load
();
}
extern
"C"
{
#include <stdio.h>
#define MAGIC_BYTES 0xFEFDFCFB
#define MAGIC_BYTES_SIZE 4
#define MEM_PREFIX_SIZE 8
extern
void
*
__real_malloc
(
size_t
);
extern
void
__real_free
(
void
*
);
extern
void
*
__real_realloc
(
void
*
ptr
,
size_t
c
);
void
*
__wrap_malloc
(
size_t
c
)
{
c
+=
MEM_PREFIX_SIZE
;
char
*
ret
=
(
char
*
)
__real_malloc
(
c
);
if
(
ret
)
{
mem_usage
+=
c
;
*
((
uint32_t
*
)
(
ret
))
=
MAGIC_BYTES
;
*
((
uint32_t
*
)
(
ret
+
MAGIC_BYTES_SIZE
))
=
c
;
return
ret
+
MEM_PREFIX_SIZE
;
}
return
nullptr
;
}
void
__wrap_free
(
void
*
ptr
)
{
if
(
!
ptr
)
{
return
;
}
ptr
=
(
char
*
)
ptr
-
MEM_PREFIX_SIZE
;
uint32_t
magic
=
*
((
uint32_t
*
)
(
ptr
));
if
(
magic
!=
MAGIC_BYTES
)
{
throw
std
::
invalid_argument
(
"attempt to free invalid memory"
);
}
mem_usage
-=
*
((
uint32_t
*
)
((
char
*
)
ptr
+
MAGIC_BYTES_SIZE
));
__real_free
(
ptr
);
}
void
*
__wrap_calloc
(
size_t
__nmemb
,
size_t
__size
)
{
auto
size
=
__nmemb
*
__size
;
auto
ret
=
malloc
(
size
);
if
(
ret
)
{
memset
(
ret
,
0
,
size
);
}
return
ret
;
}
void
*
__wrap_realloc
(
void
*
ptr
,
size_t
c
){
if
(
!
ptr
)
{
return
malloc
(
c
);
}
c
+=
MEM_PREFIX_SIZE
;
ptr
=
(
char
*
)
ptr
-
MEM_PREFIX_SIZE
;
uint32_t
magic
=
*
((
uint32_t
*
)
(
ptr
));
if
(
magic
!=
MAGIC_BYTES
)
{
throw
std
::
invalid_argument
(
"attempt to realloc invalid memory"
);
}
auto
old_size
=
*
((
uint32_t
*
)
((
char
*
)
ptr
+
MAGIC_BYTES_SIZE
));
char
*
ret
=
(
char
*
)
__real_realloc
(
ptr
,
c
);
if
(
ret
)
{
mem_usage
+=
c
-
old_size
;
*
((
uint32_t
*
)
(
ret
))
=
MAGIC_BYTES
;
*
((
uint32_t
*
)
(
ret
+
MAGIC_BYTES_SIZE
))
=
c
;
return
ret
+
MEM_PREFIX_SIZE
;
}
free
(
ptr
);
mem_usage
-=
old_size
;
return
nullptr
;
}
}
void
*
operator
new
(
std
::
size_t
size
)
{
auto
ret
=
malloc
(
size
);
if
(
ret
)
{
return
ret
;
}
throw
std
::
bad_alloc
();
}
void
operator
delete
(
void
*
ptr
)
{
free
(
ptr
);
}
void
*
operator
new
[](
std
::
size_t
size
)
{
auto
ret
=
malloc
(
size
);
if
(
ret
)
{
return
ret
;
}
throw
std
::
bad_alloc
();
}
void
operator
delete
[](
void
*
ptr
)
{
free
(
ptr
);
}
#endif
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论