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
84f3aa07
Commit
84f3aa07
authored
Dec 30, 2021
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
完善内存统计并在malloc大内存时打印backtrace
parent
7f6be9e1
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
107 行增加
和
51 行删除
+107
-51
src/Common/config.cpp
+107
-51
没有找到文件。
src/Common/config.cpp
查看文件 @
84f3aa07
...
...
@@ -351,20 +351,35 @@ std::vector<size_t> getBlockTypeSize() {
return
ret
;
}
class
MemT
otalInfo
:
public
std
::
enable_shared_from_this
<
MemTotalInfo
>
{
class
MemT
hreadInfo
{
public
:
using
Ptr
=
std
::
shared_ptr
<
MemT
otal
Info
>
;
using
Ptr
=
std
::
shared_ptr
<
MemT
hread
Info
>
;
atomic
<
uint64_t
>
mem_usage
{
0
};
atomic
<
uint64_t
>
mem_block
{
0
};
atomic
<
uint64_t
>
mem_block_map
[
BLOCK_TYPES
];
static
MemTotalInfo
&
Instance
()
{
static
auto
s_instance
=
std
::
make_shared
<
MemTotalInfo
>
();
static
auto
&
s_ref
=
*
s_instance
;
return
s_ref
;
static
MemThreadInfo
*
Instance
(
bool
is_thread_local
)
{
if
(
!
is_thread_local
)
{
static
auto
instance
=
new
MemThreadInfo
(
is_thread_local
);
return
instance
;
}
static
auto
thread_local
instance
=
new
MemThreadInfo
(
is_thread_local
);
return
instance
;
}
~
MemThreadInfo
()
{
//printf("%s %d\r\n", __FUNCTION__, (int) _is_thread_local);
}
MemThreadInfo
(
bool
is_thread_local
)
{
_is_thread_local
=
is_thread_local
;
if
(
_is_thread_local
)
{
//确保所有线程退出后才能释放全局内存统计器
total_mem
=
Instance
(
false
);
}
//printf("%s %d\r\n", __FUNCTION__, (int) _is_thread_local);
}
private
:
void
*
operator
new
(
size_t
sz
)
{
return
__real_malloc
(
sz
);
}
...
...
@@ -372,88 +387,129 @@ private:
void
operator
delete
(
void
*
ptr
)
{
__real_free
(
ptr
);
}
void
addBlock
(
size_t
c
)
{
if
(
total_mem
)
{
total_mem
->
addBlock
(
c
);
}
mem_usage
+=
c
;
++
mem_block_map
[
get_mem_block_type
(
c
)];
++
mem_block
;
}
void
delBlock
(
size_t
c
)
{
if
(
total_mem
)
{
total_mem
->
delBlock
(
c
);
}
mem_usage
-=
c
;
--
mem_block_map
[
get_mem_block_type
(
c
)];
if
(
0
==
--
mem_block
)
{
delete
this
;
}
}
private
:
bool
_is_thread_local
;
MemThreadInfo
*
total_mem
=
nullptr
;
};
class
Mem
AllocInfo
{
class
Mem
ThreadInfoLocal
{
public
:
MemTotalInfo
::
Ptr
total_mem
=
MemTotalInfo
::
Instance
().
shared_from_this
();
atomic
<
uint64_t
>
mem_usage
{
0
};
atomic
<
uint64_t
>
mem_block
{
0
};
atomic
<
uint64_t
>
mem_block_map
[
BLOCK_TYPES
];
MemThreadInfoLocal
()
{
ptr
=
MemThreadInfo
::
Instance
(
true
);
ptr
->
addBlock
(
1
);
}
~
MemThreadInfoLocal
()
{
ptr
->
delBlock
(
1
);
}
MemThreadInfo
*
get
()
const
{
return
ptr
;
}
private
:
MemThreadInfo
*
ptr
;
};
static
thread_local
MemAllocInfo
s_alloc_info
;
//该变量主要确保线程退出后才能释放MemThreadInfo变量
static
thread_local
MemThreadInfoLocal
s_thread_mem_info
;
uint64_t
getTotalMemUsage
()
{
return
s_alloc_info
.
total_mem
->
mem_usage
.
load
();
return
MemThreadInfo
::
Instance
(
false
)
->
mem_usage
.
load
();
}
uint64_t
getTotalMemBlock
()
{
return
s_alloc_info
.
total_mem
->
mem_block
.
load
();
return
MemThreadInfo
::
Instance
(
false
)
->
mem_block
.
load
();
}
uint64_t
getTotalMemBlockByType
(
int
type
)
{
assert
(
type
<
BLOCK_TYPES
);
return
s_alloc_info
.
total_mem
->
mem_block_map
[
type
].
load
();
return
MemThreadInfo
::
Instance
(
false
)
->
mem_block_map
[
type
].
load
();
}
uint64_t
getThisThreadMemUsage
()
{
return
s_alloc_info
.
mem_usage
.
load
();
return
MemThreadInfo
::
Instance
(
true
)
->
mem_usage
.
load
();
}
uint64_t
getThisThreadMemBlock
()
{
return
s_alloc_info
.
mem_block
.
load
();
return
MemThreadInfo
::
Instance
(
true
)
->
mem_block
.
load
();
}
uint64_t
getThisThreadMemBlockByType
(
int
type
)
{
assert
(
type
<
BLOCK_TYPES
);
return
s_alloc_info
.
mem_block_map
[
type
].
load
();
return
MemThreadInfo
::
Instance
(
true
)
->
mem_block_map
[
type
].
load
();
}
#if defined(_WIN32)
#pragma pack(push, 1)
#endif // defined(_WIN32)
class
MemCookie
{
public
:
static
constexpr
uint32_t
kMagic
=
0xFEFDFCFB
;
uint32_t
magic
;
uint32_t
size
;
uint32_t
type
;
MemAllocInfo
*
alloc_info
;
MemThreadInfo
*
alloc_info
;
char
ptr
;
}
PACKED
;
#if defined(_WIN32)
#pragma pack(pop)
#endif // defined(_WIN32)
};
#define MEM_OFFSET (sizeof(MemCookie) - 1)
#define MEM_OFFSET offsetof(MemCookie, ptr)
#if (defined(__linux__) && !defined(ANDROID)) || defined(__MACH__)
#define MAX_STACK_FRAMES 128
#define MEM_WARING
#include <backtrace.h>
#include <limits.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <execinfo.h>
static
void
print_mem_waring
(
size_t
c
)
{
void
*
array
[
MAX_STACK_FRAMES
];
int
size
=
backtrace
(
array
,
MAX_STACK_FRAMES
);
char
**
strings
=
backtrace_symbols
(
array
,
size
);
printf
(
"malloc big memory:%d, back trace:
\r\n
"
,
(
int
)
c
);
for
(
int
i
=
0
;
i
<
size
;
++
i
)
{
printf
(
"[%d]: %s
\r\n
"
,
i
,
strings
[
i
]);
}
__real_free
(
strings
);
}
#endif
void
init_cookie
(
MemCookie
*
cookie
,
size_t
c
)
{
int
type
=
get_mem_block_type
(
c
);
static
void
init_cookie
(
MemCookie
*
cookie
,
size_t
c
)
{
cookie
->
magic
=
MemCookie
::
kMagic
;
cookie
->
size
=
c
;
cookie
->
alloc_info
=
&
s_alloc_info
;
cookie
->
type
=
type
;
cookie
->
alloc_info
->
mem_usage
+=
c
;
++
cookie
->
alloc_info
->
mem_block
;
++
cookie
->
alloc_info
->
mem_block_map
[
type
]
;
cookie
->
alloc_info
->
total_mem
->
mem_usage
+=
c
;
++
cookie
->
alloc_info
->
total_mem
->
mem_block
;
++
cookie
->
alloc_info
->
total_mem
->
mem_block_map
[
type
];
cookie
->
alloc_info
=
s_thread_mem_info
.
get
()
;
cookie
->
alloc_info
->
addBlock
(
c
)
;
#if defined(MEM_WARING)
static
auto
env
=
getenv
(
"MAX_MEM_SIZE"
)
;
static
size_t
kMaxMemSize
=
atoll
(
env
?
env
:
"0"
)
;
if
(
kMaxMemSize
>
1024
&&
c
>=
kMaxMemSize
)
{
print_mem_waring
(
c
)
;
}
#endif
}
void
un_init_cookie
(
MemCookie
*
cookie
)
{
cookie
->
alloc_info
->
mem_usage
-=
cookie
->
size
;
--
cookie
->
alloc_info
->
mem_block
;
--
cookie
->
alloc_info
->
mem_block_map
[
cookie
->
type
];
cookie
->
alloc_info
->
total_mem
->
mem_usage
-=
cookie
->
size
;
--
cookie
->
alloc_info
->
total_mem
->
mem_block
;
--
cookie
->
alloc_info
->
total_mem
->
mem_block_map
[
cookie
->
type
];
static
void
un_init_cookie
(
MemCookie
*
cookie
)
{
cookie
->
alloc_info
->
delBlock
(
cookie
->
size
);
}
void
*
__wrap_malloc
(
size_t
c
)
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论