Profiling support and better compression support.

This commit is contained in:
Sleepy Monax 2024-01-17 06:32:38 +01:00
parent 1fd19e757a
commit 9c9db6c36b
2 changed files with 60 additions and 4 deletions

View file

@ -354,6 +354,7 @@ def _(args: cli.Args):
@cli.command("r", "builder/run", "Run a component") @cli.command("r", "builder/run", "Run a component")
def runCmd(args: cli.Args): def runCmd(args: cli.Args):
debug = args.consumeOpt("debug", False) is True debug = args.consumeOpt("debug", False) is True
profile = args.consumeOpt("profile", False) is True
wait = args.consumeOpt("wait", False) is True wait = args.consumeOpt("wait", False) is True
debugger = args.consumeOpt("debugger", "lldb") debugger = args.consumeOpt("debugger", "lldb")
componentSpec = args.consumeArg() or "__main__" componentSpec = args.consumeArg() or "__main__"
@ -390,6 +391,29 @@ def runCmd(args: cli.Args):
) )
else: else:
raise RuntimeError(f"Unknown debugger {debugger}") raise RuntimeError(f"Unknown debugger {debugger}")
elif profile:
# Profile the executable using perf
shell.exec(
"perf",
"record",
"-g",
"-o",
"perf.data",
"--call-graph",
"dwarf",
str(object=product.path),
*args.extra,
)
# Run this command using subprocess
# perf script -i perf.data | speedscope -
import subprocess
proc = subprocess.Popen(
["perf", "script", "-i", "perf.data"], stdout=subprocess.PIPE
)
subprocess.run(["speedscope", "-"], stdin=proc.stdout)
proc.wait()
else: else:
shell.exec(str(product.path), *args.extra) shell.exec(str(product.path), *args.extra)

View file

@ -101,6 +101,9 @@ def rmrf(path: str) -> bool:
if not os.path.exists(path): if not os.path.exists(path):
return False return False
if os.path.isfile(path):
os.remove(path)
else:
shutil.rmtree(path, ignore_errors=True) shutil.rmtree(path, ignore_errors=True)
return True return True
@ -285,20 +288,49 @@ def nproc() -> int:
return os.cpu_count() or 1 return os.cpu_count() or 1
def compress(path: str, dest: Optional[str] = None, format: str = "zstd") -> str: def gzip(path: str, dest: Optional[str] = None) -> str:
""" """
Compress a file or directory Compress a file or directory
""" """
if dest is None: if dest is None:
dest = path + "." + format dest = path + ".gz"
with open(dest, "wb") as f:
proc = subprocess.run(
["gzip", "-c", path],
stdout=f,
stderr=sys.stderr,
)
if proc.returncode != 0:
raise RuntimeError(f"gzip: Process exited with code {proc.returncode}")
return dest
def compress(path: str, dest: Optional[str] = None, format: str = "zstd") -> str:
"""
Compress a file or directory
"""
EXTS = {
"zip": "zip",
"zstd": "zst",
"gzip": "gz",
}
if dest is None:
dest = path + "." + EXTS[format]
_logger.debug(f"Compressing {path} to {dest}") _logger.debug(f"Compressing {path} to {dest}")
if format == "zip": if format == "zip":
exec("zip", "-r", dest, path) exec("zip", "-r", dest, path)
elif format == "zstd": elif format == "zstd":
exec("zsdt", "-q", "-o", dest, path) exec("zstd", "-q", "-o", dest, path)
elif format == "gzip":
gzip(path, dest)
else: else:
raise RuntimeError(f"Unknown compression format {format}") raise RuntimeError(f"Unknown compression format {format}")