Added thread debug option
CoreConfig now have a boolean thread debug option, when this is enabled, it prints out the CPU usage of each thread to help identify which threads are the most CPU hungry for later optimisation
This commit is contained in:
4 files changed
+143
-13
No files matched your search
@@ -12,6 +12,8 @@ from threading import Lock, Thread
|
||||
import time
|
||||
from typing import Literal
|
||||
from uuid import UUID
|
||||
import os
|
||||
import psutil
|
||||
|
||||
|
||||
from dynalab_core.buffer import ValueBuffer
|
||||
@@ -69,6 +71,67 @@ class Core:
|
||||
|
||||
self._state = "initd"
|
||||
|
||||
self._debug_thread: Thread | None = None
|
||||
|
||||
if self._core_config.thread_debug:
|
||||
self._debug_thread = Thread(
|
||||
target=self._debug_worker, name="dynalab_core_debug_thread", daemon=True
|
||||
)
|
||||
self._debug_thread.start()
|
||||
|
||||
def _debug_worker(self) -> None:
|
||||
|
||||
process = psutil.Process(os.getpid())
|
||||
|
||||
previous = {}
|
||||
|
||||
while not self._stop_event.is_set():
|
||||
current = {
|
||||
thread.id: thread.user_time + thread.system_time
|
||||
for thread in process.threads()
|
||||
}
|
||||
|
||||
names = {
|
||||
thread.native_id: thread.name
|
||||
for thread in threading.enumerate()
|
||||
if thread.native_id is not None
|
||||
}
|
||||
|
||||
usage = []
|
||||
|
||||
for tid, cpu_time in current.items():
|
||||
previous_cpu_time = previous.get(tid, cpu_time)
|
||||
delta = cpu_time - previous_cpu_time
|
||||
|
||||
usage.append(
|
||||
(
|
||||
delta,
|
||||
tid,
|
||||
names.get(tid, "<unknown>"),
|
||||
)
|
||||
)
|
||||
|
||||
for delta, tid, thread_name in sorted(usage, reverse=True):
|
||||
cpu_percent = delta * 100
|
||||
|
||||
log.debug(
|
||||
(
|
||||
f"Thread CPU usage sampled: "
|
||||
f"name={thread_name}, "
|
||||
f"id={tid}, "
|
||||
f"cpu={cpu_percent:.2f}%"
|
||||
),
|
||||
extra={
|
||||
"event": "core.thread_cpu_usage",
|
||||
"thread_name": thread_name,
|
||||
"thread_id": tid,
|
||||
"cpu_percent": cpu_percent,
|
||||
},
|
||||
)
|
||||
|
||||
previous = current
|
||||
time.sleep(1)
|
||||
|
||||
def start(self) -> None:
|
||||
if self._state != "initd":
|
||||
log.warning(
|
||||
|
||||
@@ -8,6 +8,7 @@ from pydantic import BaseModel, Field
|
||||
class CoreConfig(BaseModel):
|
||||
host: str = "127.0.0.1"
|
||||
port: int = Field(default=58763, ge=0, le=65535)
|
||||
thread_debug: bool = False
|
||||
|
||||
def bind_str(self) -> str:
|
||||
return f"{self.host}:{self.port}"
|
||||
Reference in new issue
Block a user