1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

RTC: Refine the stat logs, limit the object cache pool

This commit is contained in:
winlin 2021-02-27 07:41:51 +08:00
parent 30809aee60
commit 7b3b7381e2
8 changed files with 202 additions and 44 deletions

View file

@ -361,7 +361,7 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy()
{
srs_assert(ptr);
SrsSharedPtrMessage* copy = _srs_rtp_msg_cache2->allocate();
SrsSharedPtrMessage* copy = _srs_rtp_msg_cache_objs->allocate();
// We got an object from cache, the ptr might exists, so unwrap it.
copy->unwrap();

View file

@ -41,6 +41,7 @@ SrsPps* _srs_pps_objs_rraw = new SrsPps();
SrsPps* _srs_pps_objs_rfua = new SrsPps();
SrsPps* _srs_pps_objs_rbuf = new SrsPps();
SrsPps* _srs_pps_objs_rothers = new SrsPps();
SrsPps* _srs_pps_objs_drop = new SrsPps();
/* @see https://tools.ietf.org/html/rfc1889#section-5.1
0 1 2 3
@ -851,10 +852,10 @@ void SrsRtpPacket2::reuse_shared_msg()
// We only recycle the RTC UDP packet messages.
if (shared_msg->payload && shared_msg->size == kRtpPacketSize && shared_msg->count() == 0) {
_srs_rtp_msg_cache->recycle(shared_msg);
_srs_rtp_msg_cache_buffers->recycle(shared_msg);
} else {
shared_msg->unwrap();
_srs_rtp_msg_cache2->recycle(shared_msg);
_srs_rtp_msg_cache_objs->recycle(shared_msg);
}
shared_msg = NULL;
@ -885,7 +886,7 @@ char* SrsRtpPacket2::wrap(int size)
// Create a large enough message, with under-layer buffer.
while (true) {
srs_freep(shared_msg);
shared_msg = _srs_rtp_msg_cache->allocate();
shared_msg = _srs_rtp_msg_cache_buffers->allocate();
// If got a cached message(which has payload), but it's too small,
// we free it and allocate a larger one.
@ -1050,8 +1051,8 @@ SrsRtpObjectCacheManager<SrsRtpPacket2>* _srs_rtp_cache = new SrsRtpObjectCacheM
SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache = new SrsRtpObjectCacheManager<SrsRtpRawPayload>();
SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache = new SrsRtpObjectCacheManager<SrsRtpFUAPayload2>();
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>();
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache2 = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>();
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>();
SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs = new SrsRtpObjectCacheManager<SrsSharedPtrMessage>();
SrsRtpRawPayload::SrsRtpRawPayload()
{

View file

@ -333,6 +333,10 @@ public:
virtual srs_error_t decode(SrsBuffer* buf);
};
// For object cache manager to stat the object dropped.
#include <srs_kernel_kbps.hpp>
extern SrsPps* _srs_pps_objs_drop;
// The RTP packet or message cache manager.
template<typename T>
class SrsRtpObjectCacheManager
@ -340,9 +344,11 @@ class SrsRtpObjectCacheManager
private:
bool enabled_;
std::list<T*> cache_objs_;
size_t capacity_;
public:
SrsRtpObjectCacheManager() {
enabled_ = false;
capacity_ = 0;
}
virtual ~SrsRtpObjectCacheManager() {
typedef typename std::list<T*>::iterator iterator;
@ -353,8 +359,9 @@ public:
}
public:
// Enable or disable cache.
void set_enabled(bool v) {
void set_enabled(bool v, uint64_t memory) {
enabled_ = v;
capacity_ = (size_t)(memory / sizeof(T));
}
bool enabled() {
return enabled_;
@ -362,6 +369,9 @@ public:
int size() {
return (int)cache_objs_.size();
}
int capacity() {
return (int)capacity_;
}
// Try to allocate from cache, create new object if no cache.
T* allocate() {
while (true) {
@ -389,12 +399,20 @@ public:
return;
}
// TODO: FIXME: Directly free to keep low memory?
// If disabled, drop the object.
if (!enabled_) {
srs_freep(p);
return;
}
// If exceed the capacity, drop the object.
if (cache_objs_.size() > capacity_) {
++_srs_pps_objs_drop->sugar;
srs_freep(p);
return;
}
// Recycle it.
cache_objs_.push_back(p);
}
@ -527,7 +545,7 @@ extern SrsRtpObjectCacheManager<SrsRtpRawPayload>* _srs_rtp_raw_cache;
extern SrsRtpObjectCacheManager<SrsRtpFUAPayload2>* _srs_rtp_fua_cache;
// For RTP packet shared messages cache.
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache;
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache2;
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_buffers;
extern SrsRtpObjectCacheManager<SrsSharedPtrMessage>* _srs_rtp_msg_cache_objs;
#endif