Skip to content

Commit d2f74e0

Browse files
authored
Fix high CPU usage (#300)
1 parent 0774003 commit d2f74e0

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
- Fixes:
99
- Fix sw8 loss when use aiohttp.(#299,issue#10669)
10+
- Fix the bug with high cpu usage.(#300,issue#10672)
1011

1112
### 1.0.0
1213

skywalking/agent/__init__.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ def backoff_wrapper(self, *args, **kwargs):
5151
wait = base = init_wait
5252
while not self._finished.is_set():
5353
try:
54-
func(self, *args, **kwargs)
55-
wait = base # reset to base wait time on success
54+
flag = func(self, *args, **kwargs)
55+
# for segment/log reporter, if the queue not empty(return True), we should keep reporter working
56+
# for other cases(return false or None), reset to base wait time on success
57+
wait = 0 if flag else base
5658
except Exception: # noqa
5759
wait = min(60, wait * 2 or 1) # double wait time with each consecutive error up to a maximum
5860
logger.exception(f'Exception in {reporter_name} service in pid {os.getpid()}, '
@@ -307,15 +309,24 @@ def stop(self) -> None:
307309
def __heartbeat(self) -> None:
308310
self.__protocol.heartbeat()
309311

310-
@report_with_backoff(reporter_name='segment', init_wait=0)
311-
def __report_segment(self) -> None:
312-
if not self.__segment_queue.empty():
312+
# segment/log init_wait is set to 0.02 to prevent threads from hogging the cpu too much
313+
# The value of 0.02(20 ms) is set to be consistent with the queue delay of the Java agent
314+
315+
@report_with_backoff(reporter_name='segment', init_wait=0.02)
316+
def __report_segment(self) -> bool:
317+
"""Returns True if the queue is not empty"""
318+
queue_not_empty_flag = not self.__segment_queue.empty()
319+
if queue_not_empty_flag:
313320
self.__protocol.report_segment(self.__segment_queue)
321+
return queue_not_empty_flag
314322

315-
@report_with_backoff(reporter_name='log', init_wait=0)
316-
def __report_log(self) -> None:
317-
if not self.__log_queue.empty():
323+
@report_with_backoff(reporter_name='log', init_wait=0.02)
324+
def __report_log(self) -> bool:
325+
"""Returns True if the queue is not empty"""
326+
queue_not_empty_flag = not self.__log_queue.empty()
327+
if queue_not_empty_flag:
318328
self.__protocol.report_log(self.__log_queue)
329+
return queue_not_empty_flag
319330

320331
@report_with_backoff(reporter_name='meter', init_wait=config.agent_meter_reporter_period)
321332
def __report_meter(self) -> None:

0 commit comments

Comments
 (0)