Perf: Limit the size of object cache pool

pull/2204/head
winlin 4 years ago
parent 3989f2d553
commit 501104e728

@ -503,11 +503,11 @@ rtc_server {
# default: off
enabled off;
# The cache size for rtp packet in MB, each object is about 300B..
# default: 128
pkt_size 128.0;
# default: 64
pkt_size 64.0;
# The cache size for rtp payload in MB, each object is about 40B.
# default: 32
payload_size 32.0;
# default: 16
payload_size 16.0;
}
# For RTP shared message and the large buffer cache.
rtp_msg_cache {
@ -515,11 +515,11 @@ rtc_server {
# default: off
enabled off;
# The cache size for message object in MB, each object is about 40B.
# default: 32
msg_size 32.0;
# default: 16
msg_size 16.0;
# The cache size for message large buffer in MB, each object is about 1500B.
# default: 1024
buffer_size 1024.0;
# default: 512
buffer_size 512.0;
}
# The black-hole to copy packet to, for debugging.
# For example, when debugging Chrome publish stream, the received packets are encrypted cipher,

@ -4936,7 +4936,7 @@ bool SrsConfig::get_rtc_server_rtp_cache_enabled()
uint64_t SrsConfig::get_rtc_server_rtp_cache_pkt_size()
{
int DEFAULT = 128 * 1024 * 1024;
int DEFAULT = 64 * 1024 * 1024;
SrsConfDirective* conf = get_rtc_server_rtp_cache();
if (!conf) {
@ -4948,12 +4948,12 @@ uint64_t SrsConfig::get_rtc_server_rtp_cache_pkt_size()
return DEFAULT;
}
return (uint64_t)(1024 * ::atof(conf->arg0().c_str()));
return 1024 * (uint64_t)(1024 * ::atof(conf->arg0().c_str()));
}
uint64_t SrsConfig::get_rtc_server_rtp_cache_payload_size()
{
int DEFAULT = 32 * 1024 * 1024;
int DEFAULT = 16 * 1024 * 1024;
SrsConfDirective* conf = get_rtc_server_rtp_cache();
if (!conf) {
@ -4965,7 +4965,7 @@ uint64_t SrsConfig::get_rtc_server_rtp_cache_payload_size()
return DEFAULT;
}
return (uint64_t)(1024 * ::atof(conf->arg0().c_str()));
return 1024 * (uint64_t)(1024 * ::atof(conf->arg0().c_str()));
}
SrsConfDirective* SrsConfig::get_rtc_server_rtp_msg_cache()
@ -5002,7 +5002,7 @@ bool SrsConfig::get_rtc_server_rtp_msg_cache_enabled()
uint64_t SrsConfig::get_rtc_server_rtp_msg_cache_msg_size()
{
int DEFAULT = 32 * 1024 * 1024;
int DEFAULT = 16 * 1024 * 1024;
SrsConfDirective* conf = get_rtc_server_rtp_msg_cache();
if (!conf) {
@ -5014,12 +5014,12 @@ uint64_t SrsConfig::get_rtc_server_rtp_msg_cache_msg_size()
return DEFAULT;
}
return (uint64_t)(1024 * ::atof(conf->arg0().c_str()));
return 1024 * (uint64_t)(1024 * ::atof(conf->arg0().c_str()));
}
uint64_t SrsConfig::get_rtc_server_rtp_msg_cache_buffer_size()
{
int DEFAULT = 1024 * 1024 * 1024;
int DEFAULT = 512 * 1024 * 1024;
SrsConfDirective* conf = get_rtc_server_rtp_msg_cache();
if (!conf) {
@ -5031,7 +5031,7 @@ uint64_t SrsConfig::get_rtc_server_rtp_msg_cache_buffer_size()
return DEFAULT;
}
return (uint64_t)(1024 * ::atof(conf->arg0().c_str()));
return 1024 * (uint64_t)(1024 * ::atof(conf->arg0().c_str()));
}
bool SrsConfig::get_rtc_server_black_hole()

@ -282,17 +282,17 @@ srs_error_t SrsRtcServer::initialize()
bool rtp_cache_enabled = _srs_config->get_rtc_server_rtp_cache_enabled();
uint64_t rtp_cache_pkt_size = _srs_config->get_rtc_server_rtp_cache_pkt_size();
uint64_t rtp_cache_payload_size = _srs_config->get_rtc_server_rtp_cache_payload_size();
_srs_rtp_cache->set_enabled(rtp_cache_enabled, rtp_cache_pkt_size);
_srs_rtp_raw_cache->set_enabled(rtp_cache_enabled, rtp_cache_payload_size);
_srs_rtp_fua_cache->set_enabled(rtp_cache_enabled, rtp_cache_payload_size);
_srs_rtp_cache->setup(rtp_cache_enabled, rtp_cache_pkt_size);
_srs_rtp_raw_cache->setup(rtp_cache_enabled, rtp_cache_payload_size);
_srs_rtp_fua_cache->setup(rtp_cache_enabled, rtp_cache_payload_size);
bool rtp_msg_cache_enabled = _srs_config->get_rtc_server_rtp_msg_cache_enabled();
uint64_t rtp_msg_cache_msg_size = _srs_config->get_rtc_server_rtp_msg_cache_msg_size();
uint64_t rtp_msg_cache_buffer_size = _srs_config->get_rtc_server_rtp_msg_cache_buffer_size();
_srs_rtp_msg_cache_buffers->set_enabled(rtp_msg_cache_enabled, rtp_msg_cache_msg_size);
_srs_rtp_msg_cache_objs->set_enabled(rtp_msg_cache_enabled, rtp_msg_cache_buffer_size);
_srs_rtp_msg_cache_buffers->setup(rtp_msg_cache_enabled, rtp_msg_cache_buffer_size);
_srs_rtp_msg_cache_objs->setup(rtp_msg_cache_enabled, rtp_msg_cache_msg_size);
srs_trace("RTC server init ok, rtp-cache=(enabled:%d,pkt:%dm-%dw,payload:%dm-%dw-%dw), msg-cache=(enabled:%d,obj:%dm-%dw,buf:%dm-%dw)",
srs_trace("RTC: Object cache init, rtp-cache=(enabled:%d,pkt:%dm-%dw,payload:%dm-%dw-%dw), msg-cache=(enabled:%d,obj:%dm-%dw,buf:%dm-%dw)",
rtp_cache_enabled, (int)(rtp_cache_pkt_size/1024/1024), _srs_rtp_cache->capacity()/10000,
(int)(rtp_cache_payload_size/1024/1024), _srs_rtp_raw_cache->capacity()/10000, _srs_rtp_fua_cache->capacity()/10000,
rtp_msg_cache_enabled, (int)(rtp_msg_cache_msg_size/1024/1024), _srs_rtp_msg_cache_objs->capacity()/10000,

@ -97,6 +97,7 @@ public:
virtual ~SrsRtcServer();
public:
virtual srs_error_t initialize();
public:
// Set the handler for server events.
void set_handler(ISrsRtcServerHandler* h);
void set_hijacker(ISrsRtcServerHijacker* h);

@ -826,6 +826,7 @@ SrsRtpPacket2::~SrsRtpPacket2()
void SrsRtpPacket2::reuse()
{
if (_srs_rtp_raw_cache->enabled() || _srs_rtp_fua_cache->enabled()) {
// Only recycle some common payloads.
SrsRtpRawPayload* raw_payload;
SrsRtpFUAPayload2* fua_payload;
@ -839,6 +840,9 @@ void SrsRtpPacket2::reuse()
} else {
srs_freep(payload);
}
} else {
srs_freep(payload);
}
// Recycle the real owner of message, clear the reference.
reuse_shared_msg();
@ -850,6 +854,7 @@ void SrsRtpPacket2::reuse_shared_msg()
return;
}
if (_srs_rtp_msg_cache_buffers->enabled() || _srs_rtp_msg_cache_objs->enabled()) {
// We only recycle the RTC UDP packet messages.
if (shared_msg->payload && shared_msg->size == kRtpPacketSize && shared_msg->count() == 0) {
_srs_rtp_msg_cache_buffers->recycle(shared_msg);
@ -859,6 +864,9 @@ void SrsRtpPacket2::reuse_shared_msg()
}
shared_msg = NULL;
} else {
srs_freep(shared_msg);
}
}
bool SrsRtpPacket2::reset()
@ -1047,12 +1055,12 @@ srs_error_t SrsRtpPacket2::decode(SrsBuffer* buf)
return err;
}
SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache = new SrsRtpObjectCacheManager<SrsRtpPacket2>();
SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache = new SrsRtpObjectCacheManager<SrsRtpRawPayload>();
SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache = new SrsRtpObjectCacheManager<SrsRtpFUAPayload2>();
SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache = new SrsRtpObjectCacheManager<SrsRtpPacket2>(sizeof(SrsRtpPacket2));
SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache = new SrsRtpObjectCacheManager<SrsRtpRawPayload>(sizeof(SrsRtpRawPayload));
SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache = new SrsRtpObjectCacheManager<SrsRtpFUAPayload2>(sizeof(SrsRtpFUAPayload2));
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>();
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>();
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>(sizeof(SrsSharedPtrMessage) + kRtpPacketSize);
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>(sizeof(SrsSharedPtrMessage));
SrsRtpRawPayload::SrsRtpRawPayload()
{

@ -345,10 +345,12 @@ private:
bool enabled_;
std::list<T*> cache_objs_;
size_t capacity_;
size_t object_size_;
public:
SrsRtpObjectCacheManager() {
SrsRtpObjectCacheManager(size_t size_of_object) {
enabled_ = false;
capacity_ = 0;
object_size_ = size_of_object;
}
virtual ~SrsRtpObjectCacheManager() {
typedef typename std::list<T*>::iterator iterator;
@ -358,11 +360,24 @@ public:
}
}
public:
// Enable or disable cache.
void set_enabled(bool v, uint64_t memory) {
// Setup the object cache, shrink if capacity changed.
void setup(bool v, uint64_t memory) {
enabled_ = v;
capacity_ = (size_t)(memory / sizeof(T));
capacity_ = (size_t)(memory / object_size_);
if (!enabled_) {
capacity_ = 0;
}
// Shrink the cache.
while (cache_objs_.size() > capacity_) {
T* obj = cache_objs_.back();
cache_objs_.pop_back();
srs_freep(obj);
}
}
// Get the status of object cache.
bool enabled() {
return enabled_;
}

Loading…
Cancel
Save