winlin

add srs memory watcher.

... ... @@ -138,7 +138,7 @@ if [ $SRS_MIPS_UBUNTU12 = YES ]; then SrsLinkOptions="${SrsLinkOptions} -lgcc_eh
MODULE_ID="CORE"
MODULE_DEPENDS=()
ModuleLibIncs=(${SRS_OBJS_DIR})
MODULE_FILES=("srs_core" "srs_core_autofree" "srs_core_performance")
MODULE_FILES=("srs_core" "srs_core_autofree" "srs_core_performance" "srs_core_mem_watch")
CORE_INCS="src/core"; MODULE_DIR=${CORE_INCS} . auto/modules.sh
CORE_OBJS="${MODULE_OBJS[@]}"
#
... ...
... ... @@ -16,6 +16,8 @@ file
../../src/core/srs_core.cpp,
../../src/core/srs_core_autofree.hpp,
../../src/core/srs_core_autofree.cpp,
../../src/core/srs_core_mem_watch.hpp,
../../src/core/srs_core_mem_watch.cpp,
../../src/core/srs_core_performance.hpp,
../../src/core/srs_core_performance.cpp,
kernel readonly separator,
... ...
... ... @@ -100,6 +100,7 @@
<ClInclude Include="..\..\src\app\srs_app_utility.hpp" />
<ClInclude Include="..\..\src\core\srs_core.hpp" />
<ClInclude Include="..\..\src\core\srs_core_autofree.hpp" />
<ClInclude Include="..\..\src\core\srs_core_mem_watch.hpp" />
<ClInclude Include="..\..\src\core\srs_core_performance.hpp" />
<ClInclude Include="..\..\src\kernel\srs_kernel_aac.hpp" />
<ClInclude Include="..\..\src\kernel\srs_kernel_avc.hpp" />
... ... @@ -181,6 +182,7 @@
<ClCompile Include="..\..\src\app\srs_app_utility.cpp" />
<ClCompile Include="..\..\src\core\srs_core.cpp" />
<ClCompile Include="..\..\src\core\srs_core_autofree.cpp" />
<ClCompile Include="..\..\src\core\srs_core_mem_watch.cpp" />
<ClCompile Include="..\..\src\core\srs_core_performance.cpp" />
<ClCompile Include="..\..\src\kernel\srs_kernel_aac.cpp" />
<ClCompile Include="..\..\src\kernel\srs_kernel_avc.cpp" />
... ...
... ... @@ -79,6 +79,7 @@
3C36DB5B1ABD1CB90066CCAF /* srs_lib_bandwidth.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3C36DB551ABD1CB90066CCAF /* srs_lib_bandwidth.cpp */; };
3C36DB5C1ABD1CB90066CCAF /* srs_lib_simple_socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3C36DB571ABD1CB90066CCAF /* srs_lib_simple_socket.cpp */; };
3C36DB5D1ABD1CB90066CCAF /* srs_librtmp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3C36DB591ABD1CB90066CCAF /* srs_librtmp.cpp */; };
3C5265B41B241BF0009CA186 /* srs_core_mem_watch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3C5265B21B241BF0009CA186 /* srs_core_mem_watch.cpp */; };
3C663F0F1AB0155100286D8B /* srs_aac_raw_publish.c in Sources */ = {isa = PBXBuildFile; fileRef = 3C663F021AB0155100286D8B /* srs_aac_raw_publish.c */; };
3C663F101AB0155100286D8B /* srs_audio_raw_publish.c in Sources */ = {isa = PBXBuildFile; fileRef = 3C663F031AB0155100286D8B /* srs_audio_raw_publish.c */; };
3C663F111AB0155100286D8B /* srs_bandwidth_check.c in Sources */ = {isa = PBXBuildFile; fileRef = 3C663F041AB0155100286D8B /* srs_bandwidth_check.c */; };
... ... @@ -327,6 +328,8 @@
3C36DB581ABD1CB90066CCAF /* srs_lib_simple_socket.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_lib_simple_socket.hpp; path = ../../../src/libs/srs_lib_simple_socket.hpp; sourceTree = "<group>"; };
3C36DB591ABD1CB90066CCAF /* srs_librtmp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = srs_librtmp.cpp; path = ../../../src/libs/srs_librtmp.cpp; sourceTree = "<group>"; };
3C36DB5A1ABD1CB90066CCAF /* srs_librtmp.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_librtmp.hpp; path = ../../../src/libs/srs_librtmp.hpp; sourceTree = "<group>"; };
3C5265B21B241BF0009CA186 /* srs_core_mem_watch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = srs_core_mem_watch.cpp; path = ../../../src/core/srs_core_mem_watch.cpp; sourceTree = "<group>"; };
3C5265B31B241BF0009CA186 /* srs_core_mem_watch.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = srs_core_mem_watch.hpp; path = ../../../src/core/srs_core_mem_watch.hpp; sourceTree = "<group>"; };
3C663F021AB0155100286D8B /* srs_aac_raw_publish.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = srs_aac_raw_publish.c; path = ../../../research/librtmp/srs_aac_raw_publish.c; sourceTree = "<group>"; };
3C663F031AB0155100286D8B /* srs_audio_raw_publish.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = srs_audio_raw_publish.c; path = ../../../research/librtmp/srs_audio_raw_publish.c; sourceTree = "<group>"; };
3C663F041AB0155100286D8B /* srs_bandwidth_check.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = srs_bandwidth_check.c; path = ../../../research/librtmp/srs_bandwidth_check.c; sourceTree = "<group>"; };
... ... @@ -428,6 +431,8 @@
children = (
3C1231F01AAE652C00CE8F6C /* srs_core_autofree.cpp */,
3C1231F11AAE652C00CE8F6C /* srs_core_autofree.hpp */,
3C5265B21B241BF0009CA186 /* srs_core_mem_watch.cpp */,
3C5265B31B241BF0009CA186 /* srs_core_mem_watch.hpp */,
3C1231F21AAE652C00CE8F6C /* srs_core_performance.cpp */,
3C1231F31AAE652C00CE8F6C /* srs_core_performance.hpp */,
3C1231F41AAE652D00CE8F6C /* srs_core.cpp */,
... ... @@ -882,6 +887,7 @@
3CC52DDC1ACE4023006FEB01 /* srs_utest_protocol.cpp in Sources */,
3C663F151AB0155100286D8B /* srs_h264_raw_publish.c in Sources */,
3C1231F61AAE652D00CE8F6C /* srs_core_autofree.cpp in Sources */,
3C5265B41B241BF0009CA186 /* srs_core_mem_watch.cpp in Sources */,
3C1EE6D71AB1367D00576EE9 /* README.md in Sources */,
3C1232411AAE81A400CE8F6C /* srs_raw_avc.cpp in Sources */,
3C1232491AAE81A400CE8F6C /* srs_rtmp_utility.cpp in Sources */,
... ...
... ... @@ -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;
... ...
/*
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
... ...
/*
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
... ...