支持线程内存malloc统计 (#1317)

This commit is contained in:
夏楚
2021-12-27 17:40:15 +08:00
committed by GitHub
parent 7f649b94e5
commit 878ce87329
5 changed files with 132 additions and 55 deletions

View File

@@ -212,6 +212,7 @@ static ApiArgsType getAllArgs(const Parser &parser) {
}
extern uint64_t getTotalMemUsage();
extern uint64_t getThisThreadMemUsage();
static inline void addHttpListener(){
GET_CONFIG(bool, api_debug, API::kApiDebug);
@@ -356,8 +357,9 @@ Value makeMediaSourceJson(MediaSource &media){
return item;
}
Value getStatisticJson() {
Value val(objectValue);
void getStatisticJson(const function<void(Value &val)> &cb) {
auto obj = std::make_shared<Value>(objectValue);
auto &val = *obj;
val["MediaSource"] = (Json::UInt64)(ObjectStatistic<MediaSource>::count());
val["MultiMediaSourceMuxer"] = (Json::UInt64)(ObjectStatistic<MultiMediaSourceMuxer>::count());
@@ -382,8 +384,46 @@ Value getStatisticJson() {
auto bytes = getTotalMemUsage();
val["totalMemUsage"] = (Json::UInt64)bytes;
val["totalMemUsageMB"] = (int)(bytes / 1024 / 1024);
auto thread_size = EventPollerPool::Instance().getExecutorSize() + WorkThreadPool::Instance().getExecutorSize();
std::shared_ptr<vector<Value> > thread_mem_info = std::make_shared<vector<Value> >(thread_size);
std::shared_ptr<atomic<uint64_t> > thread_mem_total = std::make_shared<atomic<uint64_t> >(0);
shared_ptr<void> finished(nullptr, [thread_mem_info, cb, obj, thread_mem_total](void *) {
//poller和work线程开辟的内存
for (auto &val : *thread_mem_info) {
(*obj)["threadMem"].append(val);
}
//其他线程申请的内存为总内存减去poller和work线程开辟的内存
auto bytes = getTotalMemUsage() - *thread_mem_total;
Value val;
val["threadName"] = "other threads";
val["threadMemUsage"] = (Json::UInt64) bytes;
val["threadMemUsageMB"] = (int) (bytes / 1024 / 1024);
(*obj)["threadMem"].append(val);
//触发回调
cb(*obj);
});
auto pos = 0;
auto lam = [&](const TaskExecutor::Ptr &executor) {
auto &val = (*thread_mem_info)[pos++];
executor->async([finished, thread_mem_total, &val]() {
auto bytes = getThisThreadMemUsage();
*thread_mem_total += bytes;
val["threadName"] = getThreadName();
val["threadMemUsage"] = (Json::UInt64) bytes;
val["threadMemUsageMB"] = (int) (bytes / 1024 / 1024);
});
};
EventPollerPool::Instance().for_each(lam);
WorkThreadPool::Instance().for_each(lam);
#else
cb(*obj);
#endif
return val;
}
/**
@@ -1278,9 +1318,12 @@ void installWebApi() {
});
});
api_regist("/index/api/getStatistic",[](API_ARGS_MAP){
api_regist("/index/api/getStatistic",[](API_ARGS_MAP_ASYNC){
CHECK_SECRET();
val["data"] = getStatisticJson();
getStatisticJson([headerOut, val, invoker](const Value &data) mutable{
val["data"] = data;
invoker(200, headerOut, val.toStyledString());
});
});
#ifdef ENABLE_WEBRTC

View File

@@ -235,5 +235,5 @@ bool checkArgs(Args &args, const First &first, const KeyTypes &...keys) {
void installWebApi();
void unInstallWebApi();
Value makeMediaSourceJson(MediaSource &media);
Value getStatisticJson();
void getStatisticJson(const function<void(Value &val)> &cb);
#endif //ZLMEDIAKIT_WEBAPI_H

View File

@@ -202,12 +202,12 @@ static void reportServerKeepalive() {
GET_CONFIG(float, alive_interval, Hook::kAliveInterval);
g_keepalive_timer = std::make_shared<Timer>(alive_interval, []() {
ArgsType body;
body["data"] = getStatisticJson();
//执行hook
do_http_hook(hook_server_keepalive, body, nullptr);
getStatisticJson([](const Value &data) mutable {
ArgsType body;
body["data"] = data;
//执行hook
do_http_hook(hook_server_keepalive, body, nullptr);
});
return true;
}, nullptr);
}