resource_limits.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """Best-effort process resource-limit adjustments for long-running services."""
  2. from __future__ import annotations
  3. import ctypes
  4. import logging
  5. import os
  6. from collections.abc import Mapping
  7. from typing import Any
  8. from hermes_cli.config_defaults import DEFAULT_CONFIG
  9. try: # ``resource`` is POSIX-only (and unavailable on Windows).
  10. import resource as _resource
  11. except (ImportError, ModuleNotFoundError): # pragma: no cover - Windows only
  12. _resource = None # type: ignore[assignment]
  13. logger = logging.getLogger(__name__)
  14. DEFAULT_NOFILE_SOFT_LIMIT = int(DEFAULT_CONFIG["runtime"]["nofile_soft_limit"])
  15. DEFAULT_MALLOC_ARENA_MAX = int(DEFAULT_CONFIG["runtime"]["malloc_arena_max"])
  16. _M_ARENA_MAX = -8 # glibc <malloc.h> mallopt parameter
  17. _MISSING = object()
  18. def _runtime_int(config: Mapping[str, Any] | None, key: str, default: int) -> int | None:
  19. """Positive int ``runtime.<key>`` from a loaded config, or ``None`` when disabled/unresolvable.
  20. Missing key → *default*. Explicit ``0``/``false``/``null`` disable; other non-int or negative
  21. values are ignored (caller fails open).
  22. """
  23. if config is None:
  24. try:
  25. # Profile-aware loader (applies managed-scope overlays and defaults).
  26. from hermes_cli.config import load_config_readonly
  27. config = load_config_readonly()
  28. except Exception:
  29. logger.debug("Could not load config for runtime.%s", key, exc_info=True)
  30. return None
  31. if not isinstance(config, Mapping):
  32. return None
  33. runtime = config.get("runtime", _MISSING)
  34. if runtime is _MISSING:
  35. return default
  36. if not isinstance(runtime, Mapping):
  37. return None
  38. raw_value = runtime.get(key, _MISSING)
  39. if raw_value is _MISSING:
  40. return default
  41. if isinstance(raw_value, bool) or not isinstance(raw_value, int) or raw_value <= 0:
  42. return None
  43. return raw_value
  44. def configured_nofile_soft_limit(config: Mapping[str, Any] | None = None) -> int | None:
  45. """``runtime.nofile_soft_limit`` (see :func:`_runtime_int`). Shared by the in-process floor and
  46. service-definition generators (launchd plist) so both use one knob."""
  47. return _runtime_int(config, "nofile_soft_limit", DEFAULT_NOFILE_SOFT_LIMIT)
  48. def apply_nofile_soft_limit(config: Mapping[str, Any] | None = None) -> bool:
  49. """Best-effort raise of this process's ``RLIMIT_NOFILE`` soft limit; ``True`` iff changed.
  50. Target = ``runtime.nofile_soft_limit`` (default :data:`DEFAULT_NOFILE_SOFT_LIMIT`), clamped
  51. to a finite hard limit; never lowers a higher soft limit. Unsupported platforms, malformed
  52. settings, and denied ``setrlimit`` must never prevent a server from starting.
  53. """
  54. if _resource is None:
  55. return False
  56. target = configured_nofile_soft_limit(config)
  57. if target is None:
  58. return False
  59. try:
  60. nofile = _resource.RLIMIT_NOFILE
  61. current_soft, current_hard = _resource.getrlimit(nofile)
  62. # RLIM_INFINITY may be -1, which ordinary ordering would treat as "lower than any
  63. # target"; never replace infinity with a finite limit.
  64. infinity = getattr(_resource, "RLIM_INFINITY", object())
  65. if current_soft == infinity or current_soft >= target:
  66. return False
  67. new_soft = target if current_hard == infinity else min(target, current_hard)
  68. if new_soft <= current_soft:
  69. return False
  70. _resource.setrlimit(nofile, (new_soft, current_hard))
  71. return True
  72. except Exception:
  73. logger.debug("Could not raise RLIMIT_NOFILE soft limit", exc_info=True)
  74. return False
  75. def glibc_malloc() -> Any | None:
  76. """This process's libc handle when it is glibc with ``mallopt`` (else ``None``: musl, macOS, Windows)."""
  77. try:
  78. libc = ctypes.CDLL(None)
  79. except (OSError, TypeError): # Windows: no process-global symbol namespace
  80. return None
  81. return libc if hasattr(libc, "gnu_get_libc_version") and hasattr(libc, "mallopt") else None
  82. def _arena_max_setting(env: Mapping[str, str]) -> str | None:
  83. """The arena cap an environment asks glibc for (exact ``glibc.malloc.arena_max`` tunable, else
  84. ``MALLOC_ARENA_MAX``), or ``None``."""
  85. for item in env.get("GLIBC_TUNABLES", "").split(":"):
  86. name, sep, value = item.partition("=")
  87. if sep and name == "glibc.malloc.arena_max":
  88. return value
  89. return env.get("MALLOC_ARENA_MAX")
  90. def _launch_environ() -> dict[str, str] | None:
  91. """The environment this process was exec'd with (what glibc read at startup), or ``None``.
  92. ``/proc/self/environ`` keeps the exec-time block; ``os.environ`` also holds values Hermes loaded
  93. later from the profile ``.env``, which glibc never saw.
  94. """
  95. try:
  96. raw = open("/proc/self/environ", "rb").read()
  97. except OSError:
  98. return None
  99. entries = (os.fsdecode(entry).partition("=") for entry in raw.split(b"\0") if entry)
  100. return {name: value for name, sep, value in entries if sep}
  101. def _positive_c_int(value: str) -> int | None:
  102. try:
  103. number = int(value.strip())
  104. except ValueError:
  105. return None
  106. return number if 0 < number <= 2**31 - 1 else None
  107. def apply_malloc_arena_max(config: Mapping[str, Any] | None = None) -> bool:
  108. """Cap glibc's malloc arena count for this process (``mallopt(M_ARENA_MAX)``); ``True`` iff set.
  109. glibc gives every thread that contends on an arena lock a new arena, up to 8 x cores, and never
  110. unmaps them. A long-lived server with hundreds of short-lived Python threads plus native pools
  111. (tokio, rayon, sqlite) fills every slot (157 of 160 on a 20-core host), and each arena keeps its
  112. own free pages. The GIL already serialises Python's allocations, so a small cap costs no
  113. measurable throughput. Applied in-process rather than via an exported ``MALLOC_ARENA_MAX`` so
  114. exec'd children (terminal commands, MCP servers, builds) keep glibc's default.
  115. Call before the process starts any thread: arenas that exist never go away, and glibc latches its
  116. limit once a thread asks for an arena past ``glibc.malloc.arena_test``. (The gateway's startup
  117. watchdog thread arms before config loads, so a gateway may hold one arena beyond a cap of 1.)
  118. An arena setting in the exec-time environment was already applied by glibc and wins. One that
  119. is only in ``os.environ`` because Hermes loaded it from the profile ``.env`` is applied here with
  120. the operator's number (glibc never read it). glibc only; never raises.
  121. """
  122. libc = glibc_malloc()
  123. if libc is None:
  124. return False
  125. current = _arena_max_setting(os.environ)
  126. launch_env = _launch_environ()
  127. launched = current if launch_env is None else _arena_max_setting(launch_env)
  128. if launched is not None and current in (None, launched):
  129. return False
  130. if current is not None:
  131. target = _positive_c_int(current)
  132. else:
  133. target = _runtime_int(config, "malloc_arena_max", DEFAULT_MALLOC_ARENA_MAX)
  134. target = target if target is not None and target <= 2**31 - 1 else None
  135. if target is None:
  136. return False
  137. try:
  138. return bool(libc.mallopt(_M_ARENA_MAX, target))
  139. except Exception:
  140. logger.debug("Could not cap malloc arenas", exc_info=True)
  141. return False
  142. def server_process_argv(argv: list[str]) -> bool:
  143. """True when *argv* (``sys.argv[1:]``) launches a long-lived server: ``serve``, ``dashboard`` or a
  144. foreground ``gateway`` (``gateway`` / ``gateway run``)."""
  145. from hermes_cli._parser import command_argv
  146. args = command_argv(argv)
  147. if not args:
  148. return False
  149. if args[0] in ("serve", "dashboard"):
  150. return True
  151. return args[0] == "gateway" and (len(args) == 1 or args[1] == "run" or args[1].startswith("-"))
  152. __all__ = [
  153. "DEFAULT_MALLOC_ARENA_MAX", "DEFAULT_NOFILE_SOFT_LIMIT", "apply_malloc_arena_max",
  154. "apply_nofile_soft_limit", "configured_nofile_soft_limit", "glibc_malloc", "server_process_argv"]