Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f67351a9 authored by Zhuoyao Zhang's avatar Zhuoyao Zhang Committed by Gerrit Code Review
Browse files

Merge "Set the edit monitor memory threshold as a percentage" into main

parents 9b0ea33c 6988272f
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ from proto import edit_event_pb2

DEFAULT_PROCESS_TERMINATION_TIMEOUT_SECONDS = 5
DEFAULT_MONITOR_INTERVAL_SECONDS = 5
DEFAULT_MEMORY_USAGE_THRESHOLD = 3 * 1024  # 3GB
DEFAULT_MEMORY_USAGE_THRESHOLD = 0.02  # 2% of total memory
DEFAULT_CPU_USAGE_THRESHOLD = 200
DEFAULT_REBOOT_TIMEOUT_SECONDS = 60 * 60 * 24
BLOCK_SIGN_FILE = "edit_monitor_block_sign"
@@ -70,6 +70,9 @@ class DaemonManager:

    self.max_memory_usage = 0
    self.max_cpu_usage = 0
    self.total_memory_size = os.sysconf("SC_PAGE_SIZE") * os.sysconf(
        "SC_PHYS_PAGES"
    )

    pid_file_dir = pathlib.Path(tempfile.gettempdir()).joinpath("edit_monitor")
    pid_file_dir.mkdir(parents=True, exist_ok=True)
@@ -345,7 +348,13 @@ class DaemonManager:
      stat_data = f.readline().split()
      # RSS is the 24th field in /proc/[pid]/stat
      rss_pages = int(stat_data[23])
      return rss_pages * 4 / 1024  # Covert to MB
      process_memory = rss_pages * 4 * 1024  # Convert to bytes

    return (
        process_memory / self.total_memory_size
        if self.total_memory_size
        else 0.0
    )

  def _get_process_cpu_percent(self, pid: int, interval: int = 1) -> float:
    total_start_time = self._get_total_cpu_time(pid)
+5 −2
Original line number Diff line number Diff line
@@ -204,16 +204,19 @@ class DaemonManagerTest(unittest.TestCase):

  def test_monitor_daemon_subprocess_killed_high_memory_usage(self):
    fake_cclient = FakeClearcutClient()

    dm = daemon_manager.DaemonManager(
        TEST_BINARY_FILE,
        daemon_target=memory_consume_daemon_target,
        daemon_args=(2,),
        cclient=fake_cclient,
    )
    # set the fake total_memory_size
    dm.total_memory_size = 100 * 1024 *1024
    dm.start()
    dm.monitor_daemon(interval=1, memory_threshold=2)
    dm.monitor_daemon(interval=1)

    self.assertTrue(dm.max_memory_usage >= 2)
    self.assertTrue(dm.max_memory_usage >= 0.02)
    self.assert_no_subprocess_running()
    self._assert_error_event_logged(
        fake_cclient,