Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]DisplayEpollWatch causes a 20% increase in CPU usage #3030

Open
yxungh opened this issue Sep 26, 2024 · 2 comments
Open

[BUG]DisplayEpollWatch causes a 20% increase in CPU usage #3030

yxungh opened this issue Sep 26, 2024 · 2 comments
Labels
[core] Area: Changes in SRT library core Type: Bug Indicates an unexpected problem or unintended behavior

Comments

@yxungh
Copy link

yxungh commented Sep 26, 2024

Describe the bug

When I enable the macro IF_HEAVY_LOGGING.
The string processing here will increase the usage by 20%, and I believe it’s not worth it.
IF_HEAVY_LOGGING(string tracking = " TRACKING: " + ed.DisplayEpollWatch());

image
@yxungh yxungh added the Type: Bug Indicates an unexpected problem or unintended behavior label Sep 26, 2024
@ethouris
Copy link
Collaborator

Why is that a bug? This log is exclusively for development support to track any problems, and as the name shows, it's a "heavy" logging and it is known that this will burden the performance, by definition.

If you want, apply this below patch - it changes this string into stringstream so that this is displayed directly buffer-to-buffer, without involving a string intermediately. Maybe this will improve the performance a bit.

diff --git a/srtcore/epoll.cpp b/srtcore/epoll.cpp
index 8cd8440c..a0b8a503 100644
--- a/srtcore/epoll.cpp
+++ b/srtcore/epoll.cpp
@@ -901,7 +901,8 @@ int srt::CEPoll::update_events(const SRTSOCKET& uid, std::set<int>& eids, const
             continue;
         }

-        IF_HEAVY_LOGGING(string tracking = " TRACKING: " + ed.DisplayEpollWatch());
+        //IF_HEAVY_LOGGING(string tracking = " TRACKING: " + ed.DisplayEpollWatch());
+        IF_HEAVY_LOGGING(std::ostringstream tracking; tracking << " TRACKING: "; ed.DisplayEpollWatch(tracking));
         // compute new states

         // New state to be set into the permanent state
@@ -912,7 +913,7 @@ int srt::CEPoll::update_events(const SRTSOCKET& uid, std::set<int>& eids, const
         int changes = pwait->state ^ newstate; // oldState XOR newState
         if (!changes)
         {
-            HLOGC(eilog.Debug, log << debug.str() << ": E" << (*i)
+            HLOGC(eilog.Debug, log << debug << ": E" << (*i)
                     << tracking << " NOT updated: no changes");
             continue; // no changes!
         }
@@ -967,6 +968,12 @@ string DisplayEpollResults(const std::map<SRTSOCKET, int>& sockset)
 string CEPollDesc::DisplayEpollWatch()
 {
     ostringstream os;
+    DisplayEpollWatch(os);
+    return os.str();
+}
+
+std::ostream& CEPollDesc::DisplayEpollWatch(std::ostream& os)
+{
     for (ewatch_t::const_iterator i = m_USockWatchState.begin(); i != m_USockWatchState.end(); ++i)
     {
         os << "@" << i->first << ":";
@@ -974,7 +981,7 @@ string CEPollDesc::DisplayEpollWatch()
         os << " ";
     }

-    return os.str();
+    return os;
 }

 } // namespace srt
diff --git a/srtcore/epoll.h b/srtcore/epoll.h
index 00d46ceb..0c4a9010 100644
--- a/srtcore/epoll.h
+++ b/srtcore/epoll.h
@@ -151,6 +151,7 @@ class CEPollDesc

 #if ENABLE_HEAVY_LOGGING
 std::string DisplayEpollWatch();
+std::ostream& DisplayEpollWatch(std::ostream& log);
 #endif

    /// Sockets that are subscribed for events in this eid.

@maxsharabayko maxsharabayko added the [core] Area: Changes in SRT library core label Sep 26, 2024
@yxungh
Copy link
Author

yxungh commented Sep 27, 2024

Why is that a bug?
I believe it’s somewhat unreasonable and not user-friendly for those who have high requirements for CPU usage.
The key point is to process the string first; that is, even if I don’t enable eilog, it will still waste CPU.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[core] Area: Changes in SRT library core Type: Bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants