diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index 2e29a000..c27b90c7 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -10,6 +10,7 @@ from . import backups, config, facts, flakes, flash, history, machines, secrets, from .custom_logger import setup_logging from .dirs import get_clan_flake_toplevel from .errors import ClanCmdError, ClanError +from .profiler import profile from .ssh import cli as ssh_cli log = logging.getLogger(__name__) @@ -117,6 +118,7 @@ def create_parser(prog: str | None = None) -> argparse.ArgumentParser: # this will be the entrypoint under /bin/clan (see pyproject.toml config) +@profile def main() -> None: parser = create_parser() args = parser.parse_args() diff --git a/pkgs/clan-cli/clan_cli/cmd.py b/pkgs/clan-cli/clan_cli/cmd.py index c14a14da..932d8477 100644 --- a/pkgs/clan-cli/clan_cli/cmd.py +++ b/pkgs/clan-cli/clan_cli/cmd.py @@ -4,6 +4,7 @@ import select import shlex import subprocess import sys +from datetime import datetime from enum import Enum from pathlib import Path from typing import IO, Any @@ -67,6 +68,8 @@ def run( error_msg: str | None = None, ) -> CmdOut: glog.debug(f"$: {shlex.join(cmd)} \nCaller: {get_caller()}") + tstart = datetime.now() + # Start the subprocess process = subprocess.Popen( cmd, @@ -78,6 +81,9 @@ def run( stdout_buf, stderr_buf = handle_output(process, log) rc = process.wait() + tend = datetime.now() + + glog.debug(f"Command took {tend - tstart}s to run") # Wait for the subprocess to finish cmd_out = CmdOut( diff --git a/pkgs/clan-vm-manager/clan_vm_manager/components/profiler.py b/pkgs/clan-cli/clan_cli/profiler.py similarity index 100% rename from pkgs/clan-vm-manager/clan_vm_manager/components/profiler.py rename to pkgs/clan-cli/clan_cli/profiler.py diff --git a/pkgs/clan-vm-manager/clan_vm_manager/__init__.py b/pkgs/clan-vm-manager/clan_vm_manager/__init__.py index 2fce92cc..97d3317c 100644 --- a/pkgs/clan-vm-manager/clan_vm_manager/__init__.py +++ b/pkgs/clan-vm-manager/clan_vm_manager/__init__.py @@ -1,8 +1,9 @@ import logging import sys +from clan_cli.profiler import profile + from clan_vm_manager.app import MainApplication -from clan_vm_manager.components.profiler import profile log = logging.getLogger(__name__) diff --git a/pkgs/clan-vm-manager/clan_vm_manager/components/executor.py b/pkgs/clan-vm-manager/clan_vm_manager/components/executor.py index 33e7f570..7ca59d52 100644 --- a/pkgs/clan-vm-manager/clan_vm_manager/components/executor.py +++ b/pkgs/clan-vm-manager/clan_vm_manager/components/executor.py @@ -3,7 +3,6 @@ import os import signal import sys import traceback -from datetime import datetime from pathlib import Path from typing import Any @@ -64,7 +63,6 @@ def _init_proc( out_file: Path, proc_name: str, on_except: Callable[[Exception, mp.process.BaseProcess], None] | None, - tstart: datetime, **kwargs: Any, ) -> None: # Create a new process group @@ -88,7 +86,6 @@ def _init_proc( linebreak = "=" * 5 # Execute the main function print(linebreak + f" {func.__name__}:{pid} " + linebreak, file=sys.stderr) - print(f"Spawn overhead time: {datetime.now() - tstart}s", file=sys.stderr) try: func(**kwargs) except Exception as ex: @@ -113,8 +110,6 @@ def spawn( func: Callable, **kwargs: Any, ) -> MPProcess: - tstart = datetime.now() - # Decouple the process from the parent if mp.get_start_method(allow_none=True) is None: mp.set_start_method(method="forkserver") @@ -125,7 +120,7 @@ def spawn( # Start the process proc = mp.Process( target=_init_proc, - args=(func, out_file, proc_name, on_except, tstart), + args=(func, out_file, proc_name, on_except), name=proc_name, kwargs=kwargs, )