You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/04/20 04:36:33 UTC

[skywalking-python] branch master updated: Fix high CPU usage (#300)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git


The following commit(s) were added to refs/heads/master by this push:
     new d2f74e0  Fix high CPU usage (#300)
d2f74e0 is described below

commit d2f74e0a37f52076c47fd8b4283189426acf3f01
Author: Kevin Ling <ke...@foxmail.com>
AuthorDate: Thu Apr 20 12:36:27 2023 +0800

    Fix high CPU usage (#300)
---
 CHANGELOG.md                 |  1 +
 skywalking/agent/__init__.py | 27 +++++++++++++++++++--------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9cf85e0..0bf1597 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@
 
 - Fixes:
   - Fix sw8 loss when use aiohttp.(#299,issue#10669)
+  - Fix the bug with high cpu usage.(#300,issue#10672)
 
 ### 1.0.0
 
diff --git a/skywalking/agent/__init__.py b/skywalking/agent/__init__.py
index 5daf271..cad45fb 100644
--- a/skywalking/agent/__init__.py
+++ b/skywalking/agent/__init__.py
@@ -51,8 +51,10 @@ def report_with_backoff(reporter_name, init_wait):
             wait = base = init_wait
             while not self._finished.is_set():
                 try:
-                    func(self, *args, **kwargs)
-                    wait = base  # reset to base wait time on success
+                    flag = func(self, *args, **kwargs)
+                    # for segment/log reporter, if the queue not empty(return True), we should keep reporter working
+                    # for other cases(return false or None), reset to base wait time on success
+                    wait = 0 if flag else base
                 except Exception:  # noqa
                     wait = min(60, wait * 2 or 1)  # double wait time with each consecutive error up to a maximum
                     logger.exception(f'Exception in {reporter_name} service in pid {os.getpid()}, '
@@ -307,15 +309,24 @@ class SkyWalkingAgent(Singleton):
     def __heartbeat(self) -> None:
         self.__protocol.heartbeat()
 
-    @report_with_backoff(reporter_name='segment', init_wait=0)
-    def __report_segment(self) -> None:
-        if not self.__segment_queue.empty():
+    # segment/log init_wait is set to 0.02 to prevent threads from hogging the cpu too much
+    # The value of 0.02(20 ms) is set to be consistent with the queue delay of the Java agent
+
+    @report_with_backoff(reporter_name='segment', init_wait=0.02)
+    def __report_segment(self) -> bool:
+        """Returns True if the queue is not empty"""
+        queue_not_empty_flag = not self.__segment_queue.empty()
+        if queue_not_empty_flag:
             self.__protocol.report_segment(self.__segment_queue)
+        return queue_not_empty_flag
 
-    @report_with_backoff(reporter_name='log', init_wait=0)
-    def __report_log(self) -> None:
-        if not self.__log_queue.empty():
+    @report_with_backoff(reporter_name='log', init_wait=0.02)
+    def __report_log(self) -> bool:
+        """Returns True if the queue is not empty"""
+        queue_not_empty_flag = not self.__log_queue.empty()
+        if queue_not_empty_flag:
             self.__protocol.report_log(self.__log_queue)
+        return queue_not_empty_flag
 
     @report_with_backoff(reporter_name='meter', init_wait=config.agent_meter_reporter_period)
     def __report_meter(self) -> None: