From e8fa92e3d1409c799d8a10de9f9c3a275938c21a Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 21 Dec 2020 11:59:35 +0800 Subject: [PATCH] Refine logs for resource manager --- trunk/src/app/srs_app_conn.cpp | 65 ++++++++++++++++++++-------------- trunk/src/app/srs_app_conn.hpp | 1 + 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index ca963358c..e3ae754d1 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -95,7 +95,7 @@ srs_error_t SrsResourceManager::cycle() { srs_error_t err = srs_success; - srs_trace("%s: connection manager run", label_.c_str()); + srs_trace("%s: connection manager run, conns=%d", label_.c_str(), (int)conns_.size()); while (true) { if ((err = trd->pull()) != srs_success) { @@ -178,6 +178,8 @@ void SrsResourceManager::unsubscribe(ISrsDisposingHandler* h) void SrsResourceManager::remove(ISrsResource* c) { + SrsContextRestore(_srs_context->get_id()); + removing_ = true; do_remove(c); removing_ = false; @@ -185,27 +187,21 @@ void SrsResourceManager::remove(ISrsResource* c) void SrsResourceManager::do_remove(ISrsResource* c) { - SrsContextRestore(_srs_context->get_id()); + bool in_zombie = false; + bool in_disposing = false; + check_remove(c, in_zombie, in_disposing); + bool ignored = in_zombie || in_disposing; + if (verbose_) { _srs_context->set_id(c->get_id()); - srs_trace("%s: before dispose resource(%s)(%p), zombies=%d", - label_.c_str(), c->desc().c_str(), c, (int)zombies_.size()); + srs_trace("%s: before dispose resource(%s)(%p), conns=%d, zombies=%d, ign=%d, inz=%d, ind=%d", + label_.c_str(), c->desc().c_str(), c, (int)conns_.size(), (int)zombies_.size(), ignored, + in_zombie, in_disposing); } - - // Only notify when not removed(in zombies_). - vector::iterator it = std::find(zombies_.begin(), zombies_.end(), c); - if (it != zombies_.end()) { + if (ignored) { return; } - // Also ignore when we are disposing it. - if (p_disposing_) { - it = std::find(p_disposing_->begin(), p_disposing_->end(), c); - if (it != p_disposing_->end()) { - return; - } - } - // Push to zombies, we will free it in another coroutine. zombies_.push_back(c); @@ -218,8 +214,8 @@ void SrsResourceManager::do_remove(ISrsResource* c) // Ignore if handler is unsubscribing. if (!unsubs_.empty() && std::find(unsubs_.begin(), unsubs_.end(), h) != unsubs_.end()) { - srs_warn2(TAG_RESOURCE_UNSUB, "%s: ignore before-dispose resource(%s)(%p) for %p", - label_.c_str(), c->desc().c_str(), c, h); + srs_warn2(TAG_RESOURCE_UNSUB, "%s: ignore before-dispose resource(%s)(%p) for %p, conns=%d", + label_.c_str(), c->desc().c_str(), c, h, (int)conns_.size()); continue; } @@ -230,6 +226,23 @@ void SrsResourceManager::do_remove(ISrsResource* c) srs_cond_signal(cond); } +void SrsResourceManager::check_remove(ISrsResource* c, bool& in_zombie, bool& in_disposing) +{ + // Only notify when not removed(in zombies_). + vector::iterator it = std::find(zombies_.begin(), zombies_.end(), c); + if (it != zombies_.end()) { + in_zombie = true; + } + + // Also ignore when we are disposing it. + if (p_disposing_) { + it = std::find(p_disposing_->begin(), p_disposing_->end(), c); + if (it != p_disposing_->end()) { + in_disposing = true; + } + } +} + void SrsResourceManager::clear() { if (zombies_.empty()) { @@ -238,7 +251,8 @@ void SrsResourceManager::clear() SrsContextRestore(cid_); if (verbose_) { - srs_trace("%s: clear zombies=%d connections", label_.c_str(), (int)zombies_.size()); + srs_trace("%s: clear zombies=%d resources, conns=%d, removing=%d, unsubs=%d", + label_.c_str(), (int)zombies_.size(), (int)conns_.size(), removing_, (int)unsubs_.size()); } // Clear all unsubscribing handlers, if not removing any resource. @@ -260,14 +274,13 @@ void SrsResourceManager::do_clear() copy.swap(zombies_); p_disposing_ = © - vector::iterator it; - for (it = copy.begin(); it != copy.end(); ++it) { - ISrsResource* conn = *it; + for (int i = 0; i < (int)copy.size(); i++) { + ISrsResource* conn = copy.at(i); if (verbose_) { _srs_context->set_id(conn->get_id()); - srs_trace("%s: disposing resource(%s)(%p), zombies=%d/%d", label_.c_str(), - conn->desc().c_str(), conn, (int)copy.size(), (int)zombies_.size()); + srs_trace("%s: disposing #%d resource(%s)(%p), conns=%d, disposing=%d, zombies=%d", label_.c_str(), + i, conn->desc().c_str(), conn, (int)conns_.size(), (int)copy.size(), (int)zombies_.size()); } dispose(conn); @@ -315,8 +328,8 @@ void SrsResourceManager::dispose(ISrsResource* c) // Ignore if handler is unsubscribing. if (!unsubs_.empty() && std::find(unsubs_.begin(), unsubs_.end(), h) != unsubs_.end()) { - srs_warn2(TAG_RESOURCE_UNSUB, "%s: ignore disposing resource(%s)(%p) for %p", - label_.c_str(), c->desc().c_str(), c, h); + srs_warn2(TAG_RESOURCE_UNSUB, "%s: ignore disposing resource(%s)(%p) for %p, conns=%d", + label_.c_str(), c->desc().c_str(), c, h, (int)conns_.size()); continue; } diff --git a/trunk/src/app/srs_app_conn.hpp b/trunk/src/app/srs_app_conn.hpp index ea2e30f7e..c61bbefbe 100644 --- a/trunk/src/app/srs_app_conn.hpp +++ b/trunk/src/app/srs_app_conn.hpp @@ -103,6 +103,7 @@ public: virtual void remove(ISrsResource* c); private: void do_remove(ISrsResource* c); + void check_remove(ISrsResource* c, bool& in_zombie, bool& in_disposing); void clear(); void do_clear(); void dispose(ISrsResource* c);