diff --git a/tinygrad/runtime/ops_qcom.py b/tinygrad/runtime/ops_qcom.py index edcd43d23d..ba14457050 100644 --- a/tinygrad/runtime/ops_qcom.py +++ b/tinygrad/runtime/ops_qcom.py @@ -1,5 +1,5 @@ from __future__ import annotations -import os, ctypes, functools, mmap, struct, array, math, sys, weakref +import os, ctypes, functools, mmap, struct, array, math, sys, weakref, contextlib assert sys.platform != 'win32' from types import SimpleNamespace from typing import Any, cast @@ -9,7 +9,8 @@ from tinygrad.runtime.support.hcq import FileIOInterface, MMIOInterface from tinygrad.runtime.autogen import kgsl, adreno from tinygrad.runtime.ops_cl import CLCompiler, CLDevice from tinygrad.renderer.cstyle import QCOMRenderer -from tinygrad.helpers import getenv, mv_address, to_mv, round_up, data64_le, prod, fromimport, cpu_profile, lo32, PROFILE, colored +from tinygrad.helpers import getenv, mv_address, to_mv, round_up, data64_le, prod, fromimport, cpu_profile, lo32, PROFILE +from tinygrad.runtime.support.system import System if getenv("IOCTL"): import extra.qcom_gpu_driver.opencl_ioctl # noqa: F401 # pylint: disable=unused-import BUFTYPE_BUF, BUFTYPE_TEX, BUFTYPE_IBO = 0, 1, 2 @@ -348,9 +349,8 @@ class QCOMDevice(HCQCompiled): # a7xx start with 730x or 'Cxxx', a8xx starts 'Exxx' if self.gpu_id[:2] >= (7, 3): raise RuntimeError(f"Unsupported GPU: chip_id={info.chip_id:#x}") - if PROFILE and self.gpu_id[:2] < (7, 3) and int(FileIOInterface('/sys/class/kgsl/kgsl-3d0/idle_timer', os.O_RDONLY).read(), 0) < 4000000000: - print(colored("WARNING: gpu can go into suspend mode and reset timestamps. " - "Run 'echo \"4294947000\" | sudo tee /sys/class/kgsl/kgsl-3d0/idle_timer' to prevent idle state.", "yellow")) + if PROFILE and self.gpu_id[:2] < (7, 3): + System.write_sysfs("/sys/class/kgsl/kgsl-3d0/idle_timer", value="4000000000", msg="Failed to disable suspend mode", expected="4294967276") compilers = [(QCOMRenderer, functools.partial(QCOMCompiler, device))] super().__init__(device, QCOMAllocator(self), compilers, functools.partial(QCOMProgram, self), QCOMSignal, @@ -376,3 +376,7 @@ class QCOMDevice(HCQCompiled): self.synchronize() self._gpu_free(self._stack) self._stack = self._gpu_alloc(sz) + + def _at_profile_finalize(self): + super()._at_profile_finalize() + with contextlib.suppress(RuntimeError): System.write_sysfs("/sys/class/kgsl/kgsl-3d0/idle_timer", "10", "Failed to reenable suspend mode") diff --git a/tinygrad/runtime/support/system.py b/tinygrad/runtime/support/system.py index b6ad01bc41..6ad514e851 100644 --- a/tinygrad/runtime/support/system.py +++ b/tinygrad/runtime/support/system.py @@ -12,6 +12,11 @@ MAP_FIXED, MAP_LOCKED, MAP_POPULATE, MAP_NORESERVE = 0x10, 0 if OSX else 0x2000, class PCIBarInfo: addr:int; size:int # noqa: E702 class _System: + def write_sysfs(self, path:str, value:str, msg:str, expected:str|None=None): + if FileIOInterface(path, os.O_RDONLY).read().splitlines()[0] != (expected or value): + os.system(cmd:=f"sudo sh -c 'echo {value} > {path}'") + if FileIOInterface(path, os.O_RDONLY).read().splitlines()[0] != (expected or value): raise RuntimeError(f"{msg}. Please run {cmd} manually.") + @functools.cached_property def atomic_lib(self): return ctypes.CDLL(ctypes.util.find_library('atomic')) if sys.platform == "linux" else None @@ -26,9 +31,7 @@ class _System: @functools.cached_property def pagemap(self) -> FileIOInterface: - if FileIOInterface(reloc_sysfs:="/proc/sys/vm/compact_unevictable_allowed", os.O_RDONLY).read()[0] != "0": - os.system(cmd:=f"sudo sh -c 'echo 0 > {reloc_sysfs}'") - assert FileIOInterface(reloc_sysfs, os.O_RDONLY).read()[0] == "0", f"Failed to disable migration of locked pages. Please run {cmd} manually." + self.write_sysfs("/proc/sys/vm/compact_unevictable_allowed", "0", "Failed to disable migration of locked pages") return FileIOInterface("/proc/self/pagemap", os.O_RDONLY) @functools.cached_property