mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
add srs memory watcher.
This commit is contained in:
parent
811ef4bcf6
commit
bb4db61192
11 changed files with 178 additions and 3 deletions
|
@ -47,6 +47,7 @@ using namespace std;
|
|||
#include <srs_app_rtsp.hpp>
|
||||
#include <srs_app_statistic.hpp>
|
||||
#include <srs_app_caster_flv.hpp>
|
||||
#include <srs_core_mem_watch.hpp>
|
||||
|
||||
// signal defines.
|
||||
#define SIGNAL_RELOAD SIGHUP
|
||||
|
@ -556,6 +557,10 @@ void SrsServer::destroy()
|
|||
// @remark never destroy the source,
|
||||
// when we free all sources, the fmle publish may retry
|
||||
// and segment fault.
|
||||
|
||||
#ifdef SRS_MEM_WATCH
|
||||
srs_memory_report();
|
||||
#endif
|
||||
}
|
||||
|
||||
void SrsServer::dispose()
|
||||
|
@ -571,6 +576,10 @@ void SrsServer::dispose()
|
|||
srs_trace("gracefully dispose sources");
|
||||
|
||||
srs_trace("terminate server");
|
||||
|
||||
#ifdef SRS_MEM_WATCH
|
||||
srs_memory_report();
|
||||
#endif
|
||||
}
|
||||
|
||||
int SrsServer::initialize(ISrsServerCycle* cycle_handler)
|
||||
|
@ -891,6 +900,9 @@ void SrsServer::on_signal(int signo)
|
|||
signal_gmc_stop = true;
|
||||
#else
|
||||
srs_trace("user terminate program");
|
||||
#ifdef SRS_MEM_WATCH
|
||||
srs_memory_report();
|
||||
#endif
|
||||
exit(0);
|
||||
#endif
|
||||
return;
|
||||
|
|
85
trunk/src/core/srs_core_mem_watch.cpp
Normal file
85
trunk/src/core/srs_core_mem_watch.cpp
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <srs_core_mem_watch.hpp>
|
||||
|
||||
#ifdef SRS_MEM_WATCH
|
||||
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
using namespace std;
|
||||
|
||||
struct SrsMemoryObject
|
||||
{
|
||||
void* ptr;
|
||||
std::string category;
|
||||
int size;
|
||||
};
|
||||
|
||||
std::map<void*, SrsMemoryObject*> _srs_ptrs;
|
||||
|
||||
void srs_memory_watch(void* ptr, string category, int size)
|
||||
{
|
||||
SrsMemoryObject* obj = NULL;
|
||||
|
||||
std::map<void*, SrsMemoryObject*>::iterator it;
|
||||
if ((it = _srs_ptrs.find(ptr)) != _srs_ptrs.end()) {
|
||||
obj = it->second;
|
||||
} else {
|
||||
obj = new SrsMemoryObject();
|
||||
_srs_ptrs[ptr] = obj;
|
||||
}
|
||||
|
||||
obj->ptr = ptr;
|
||||
obj->category = category;
|
||||
obj->size = size;
|
||||
}
|
||||
|
||||
void srs_memory_unwatch(void* ptr)
|
||||
{
|
||||
std::map<void*, SrsMemoryObject*>::iterator it;
|
||||
if ((it = _srs_ptrs.find(ptr)) != _srs_ptrs.end()) {
|
||||
SrsMemoryObject* obj = it->second;
|
||||
srs_freep(obj);
|
||||
|
||||
_srs_ptrs.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void srs_memory_report()
|
||||
{
|
||||
printf("srs memory leak report:\n");
|
||||
|
||||
int total = 0;
|
||||
std::map<void*, SrsMemoryObject*>::iterator it;
|
||||
for (it = _srs_ptrs.begin(); it != _srs_ptrs.end(); ++it) {
|
||||
SrsMemoryObject* obj = it->second;
|
||||
printf(" %s: %#"PRIx64", %dKB\n", obj->category.c_str(), (int64_t)obj->ptr, obj->size / 1000);
|
||||
total += obj->size;
|
||||
}
|
||||
|
||||
printf("%d objects leak %dKB.\n", (int)_srs_ptrs.size(), total / 1024);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
49
trunk/src/core/srs_core_mem_watch.hpp
Normal file
49
trunk/src/core/srs_core_mem_watch.hpp
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRS_CORE_MEM_WATCH_HPP
|
||||
#define SRS_CORE_MEM_WATCH_HPP
|
||||
|
||||
/*
|
||||
#include <srs_core_mem_watch.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#ifdef SRS_MEM_WATCH
|
||||
|
||||
#include <string>
|
||||
|
||||
// watch the specified memory.
|
||||
extern void srs_memory_watch(void* ptr, std::string category, int size);
|
||||
|
||||
// unwatch the specified memory.
|
||||
extern void srs_memory_unwatch(void* ptr);
|
||||
|
||||
// report the memory watch.
|
||||
extern void srs_memory_report();
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
@ -188,5 +188,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#undef SRS_PERF_FAST_FLV_ENCODER
|
||||
#define SRS_PERF_FAST_FLV_ENCODER
|
||||
|
||||
/**
|
||||
* whether enable the special memory watcher.
|
||||
* which used for memory leak debug and hurts performance.
|
||||
*/
|
||||
#undef SRS_MEM_WATCH
|
||||
#define SRS_MEM_WATCH
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ using namespace std;
|
|||
#include <srs_kernel_file.hpp>
|
||||
#include <srs_kernel_codec.hpp>
|
||||
#include <srs_kernel_utility.hpp>
|
||||
#include <srs_core_mem_watch.hpp>
|
||||
|
||||
SrsMessageHeader::SrsMessageHeader()
|
||||
{
|
||||
|
@ -159,6 +160,9 @@ SrsCommonMessage::SrsCommonMessage()
|
|||
|
||||
SrsCommonMessage::~SrsCommonMessage()
|
||||
{
|
||||
#ifdef SRS_MEM_WATCH
|
||||
srs_memory_unwatch(payload);
|
||||
#endif
|
||||
srs_freep(payload);
|
||||
}
|
||||
|
||||
|
@ -171,6 +175,9 @@ SrsSharedPtrMessage::SrsSharedPtrPayload::SrsSharedPtrPayload()
|
|||
|
||||
SrsSharedPtrMessage::SrsSharedPtrPayload::~SrsSharedPtrPayload()
|
||||
{
|
||||
#ifdef SRS_MEM_WATCH
|
||||
srs_memory_unwatch(payload);
|
||||
#endif
|
||||
srs_freep(payload);
|
||||
}
|
||||
|
||||
|
|
|
@ -72,7 +72,7 @@ SrsFastBuffer::~SrsFastBuffer()
|
|||
|
||||
int SrsFastBuffer::size()
|
||||
{
|
||||
return end - p;
|
||||
return (int)(end - p);
|
||||
}
|
||||
|
||||
char* SrsFastBuffer::bytes()
|
||||
|
|
|
@ -30,6 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
#include <srs_kernel_utility.hpp>
|
||||
#include <srs_protocol_buffer.hpp>
|
||||
#include <srs_rtmp_utility.hpp>
|
||||
#include <srs_core_mem_watch.hpp>
|
||||
|
||||
// for srs-librtmp, @see https://github.com/simple-rtmp-server/srs/issues/213
|
||||
#ifndef _WIN32
|
||||
|
@ -364,6 +365,7 @@ int SrsProtocol::recv_message(SrsCommonMessage** pmsg)
|
|||
srs_verbose("entire msg received");
|
||||
|
||||
if (!msg) {
|
||||
srs_info("got empty message without error.");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -467,7 +469,7 @@ int SrsProtocol::do_send_messages(SrsSharedPtrMessage** msgs, int nb_msgs)
|
|||
iovs[0].iov_len = nbh;
|
||||
|
||||
// payload iov
|
||||
int payload_size = srs_min(out_chunk_size, pend - p);
|
||||
int payload_size = srs_min(out_chunk_size, (int)(pend - p));
|
||||
iovs[1].iov_base = p;
|
||||
iovs[1].iov_len = payload_size;
|
||||
|
||||
|
@ -1411,6 +1413,9 @@ int SrsProtocol::read_message_payload(SrsChunkStream* chunk, SrsCommonMessage**
|
|||
if (!chunk->msg->payload) {
|
||||
chunk->msg->payload = new char[chunk->header.payload_length];
|
||||
srs_verbose("create payload for RTMP message. size=%d", chunk->header.payload_length);
|
||||
#ifdef SRS_MEM_WATCH
|
||||
srs_memory_watch(chunk->msg->payload, "msg.payload", chunk->header.payload_length);
|
||||
#endif
|
||||
}
|
||||
|
||||
// read payload to buffer
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue