Skip to content

Commit

Permalink
Added CPU profiling on MacOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Sep 14, 2024
1 parent 4d6de07 commit 3e7d60b
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions cutekit/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def debug(cmd: list[str], debugger: str = "lldb", wait: bool = False):
raise RuntimeError(f"Unknown debugger {debugger}")


def _profileCpu(cmd: list[str], rate=1000):
def _profileCpuLinux(cmd: list[str], rate=1000):
mkdir(const.TMP_DIR)
perfFile = f"{const.TMP_DIR}/cpu-profile.data"
try:
Expand Down Expand Up @@ -280,7 +280,38 @@ def _profileCpu(cmd: list[str], rate=1000):
rmrf(perfFile)


def _profileMem(cmd: list[str]):
def _profileCpuDarwin(cmd: list[str], rate=1000):
mkdir(const.TMP_DIR)
traceFile = f"{const.TMP_DIR}/cpu-profile.trace"
if rmrf(traceFile):
print("Removed trace file")

try:
exec(
"xcrun",
"xctrace",
"record",
"--template",
"Time Profiler",
"--output",
traceFile,
"--target-stdout",
"-",
"--launch",
"--",
*cmd,
)
except Exception as e:
if not os.path.exists(traceFile):
raise e

try:
exec("open", traceFile)
except Exception as e:
raise e


def _profileMemLinux(cmd: list[str]):
perfFile = f"{const.TMP_DIR}/mem-profile.data"
exec("heaptrack", "-o", perfFile, *cmd)

Expand All @@ -290,9 +321,17 @@ def profile(cmd: list[str], rate=1000, what: str = "cpu"):
raise RuntimeError("Only cpu and mem can be profile, not " + what)

if what == "cpu":
_profileCpu(cmd, rate)
if platform.system() == "Linux":
_profileCpuLinux(cmd, rate)
elif platform.system() == "Darwin":
_profileCpuDarwin(cmd, rate)
else:
raise RuntimeError(f"Unsupported platform {platform.system()}")
elif what == "mem":
_profileMem(cmd)
if platform.system() == "Linux":
_profileMemLinux(cmd)
else:
raise RuntimeError(f"Unsupported platform {platform.system()}")
else:
raise RuntimeError(f"Unknown profile type {what}")

Expand Down

0 comments on commit 3e7d60b

Please sign in to comment.