Merge pull request 'clan_cli,clan_vm_manager: Moved profiler to clan_cli. Add cmd timer to see how long run commands take' (#912) from Qubasa-main into main
This commit is contained in:
commit
8d72a36298
@ -10,6 +10,7 @@ from . import backups, config, facts, flakes, flash, history, machines, secrets,
|
|||||||
from .custom_logger import setup_logging
|
from .custom_logger import setup_logging
|
||||||
from .dirs import get_clan_flake_toplevel
|
from .dirs import get_clan_flake_toplevel
|
||||||
from .errors import ClanCmdError, ClanError
|
from .errors import ClanCmdError, ClanError
|
||||||
|
from .profiler import profile
|
||||||
from .ssh import cli as ssh_cli
|
from .ssh import cli as ssh_cli
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
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)
|
# this will be the entrypoint under /bin/clan (see pyproject.toml config)
|
||||||
|
@profile
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
parser = create_parser()
|
parser = create_parser()
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
@ -4,6 +4,7 @@ import select
|
|||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
from datetime import datetime
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import IO, Any
|
from typing import IO, Any
|
||||||
@ -67,6 +68,8 @@ def run(
|
|||||||
error_msg: str | None = None,
|
error_msg: str | None = None,
|
||||||
) -> CmdOut:
|
) -> CmdOut:
|
||||||
glog.debug(f"$: {shlex.join(cmd)} \nCaller: {get_caller()}")
|
glog.debug(f"$: {shlex.join(cmd)} \nCaller: {get_caller()}")
|
||||||
|
tstart = datetime.now()
|
||||||
|
|
||||||
# Start the subprocess
|
# Start the subprocess
|
||||||
process = subprocess.Popen(
|
process = subprocess.Popen(
|
||||||
cmd,
|
cmd,
|
||||||
@ -78,6 +81,9 @@ def run(
|
|||||||
stdout_buf, stderr_buf = handle_output(process, log)
|
stdout_buf, stderr_buf = handle_output(process, log)
|
||||||
|
|
||||||
rc = process.wait()
|
rc = process.wait()
|
||||||
|
tend = datetime.now()
|
||||||
|
|
||||||
|
glog.debug(f"Command took {tend - tstart}s to run")
|
||||||
|
|
||||||
# Wait for the subprocess to finish
|
# Wait for the subprocess to finish
|
||||||
cmd_out = CmdOut(
|
cmd_out = CmdOut(
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from clan_cli.profiler import profile
|
||||||
|
|
||||||
from clan_vm_manager.app import MainApplication
|
from clan_vm_manager.app import MainApplication
|
||||||
from clan_vm_manager.components.profiler import profile
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ import os
|
|||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
from datetime import datetime
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -64,7 +63,6 @@ def _init_proc(
|
|||||||
out_file: Path,
|
out_file: Path,
|
||||||
proc_name: str,
|
proc_name: str,
|
||||||
on_except: Callable[[Exception, mp.process.BaseProcess], None] | None,
|
on_except: Callable[[Exception, mp.process.BaseProcess], None] | None,
|
||||||
tstart: datetime,
|
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
# Create a new process group
|
# Create a new process group
|
||||||
@ -88,7 +86,6 @@ def _init_proc(
|
|||||||
linebreak = "=" * 5
|
linebreak = "=" * 5
|
||||||
# Execute the main function
|
# Execute the main function
|
||||||
print(linebreak + f" {func.__name__}:{pid} " + linebreak, file=sys.stderr)
|
print(linebreak + f" {func.__name__}:{pid} " + linebreak, file=sys.stderr)
|
||||||
print(f"Spawn overhead time: {datetime.now() - tstart}s", file=sys.stderr)
|
|
||||||
try:
|
try:
|
||||||
func(**kwargs)
|
func(**kwargs)
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
@ -113,8 +110,6 @@ def spawn(
|
|||||||
func: Callable,
|
func: Callable,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> MPProcess:
|
) -> MPProcess:
|
||||||
tstart = datetime.now()
|
|
||||||
|
|
||||||
# Decouple the process from the parent
|
# Decouple the process from the parent
|
||||||
if mp.get_start_method(allow_none=True) is None:
|
if mp.get_start_method(allow_none=True) is None:
|
||||||
mp.set_start_method(method="forkserver")
|
mp.set_start_method(method="forkserver")
|
||||||
@ -125,7 +120,7 @@ def spawn(
|
|||||||
# Start the process
|
# Start the process
|
||||||
proc = mp.Process(
|
proc = mp.Process(
|
||||||
target=_init_proc,
|
target=_init_proc,
|
||||||
args=(func, out_file, proc_name, on_except, tstart),
|
args=(func, out_file, proc_name, on_except),
|
||||||
name=proc_name,
|
name=proc_name,
|
||||||
kwargs=kwargs,
|
kwargs=kwargs,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user