Skip to content

Commit 5db4458

Browse files
committed
feat: [launcher] add fake launcher to produce random results
This is useful when generating fake run data for developing report templates for example. It is also useful for investigating the scheduler behaviour without having to spawn sub processes. With further development this could be useful for testing the scheduler. Signed-off-by: James McCorrie <[email protected]>
1 parent 7ff0cc2 commit 5db4458

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

src/dvsim/cli.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from dvsim.job.deploy import RunTest
3535
from dvsim.launcher.base import Launcher
3636
from dvsim.launcher.factory import set_launcher_type
37+
from dvsim.launcher.fake import FakeLauncher
3738
from dvsim.launcher.local import LocalLauncher
3839
from dvsim.launcher.lsf import LsfLauncher
3940
from dvsim.launcher.nc import NcLauncher
@@ -785,6 +786,12 @@ def parse_args():
785786
help=("Print dvsim tool messages but don't actually run any command"),
786787
)
787788

789+
dvg.add_argument(
790+
"--fake",
791+
action="store_true",
792+
help=("Use a fake launcher that generates random results"),
793+
)
794+
788795
args = parser.parse_args()
789796

790797
if args.version:
@@ -843,7 +850,7 @@ def main() -> None:
843850
args.cfg = os.path.join(proj_root, cfg_path)
844851

845852
# Add timestamp to args that all downstream objects can use.
846-
curr_ts = datetime.datetime.utcnow()
853+
curr_ts = datetime.datetime.now(datetime.UTC)
847854
args.timestamp_long = curr_ts.strftime(TS_FORMAT_LONG)
848855
args.timestamp = curr_ts.strftime(TS_FORMAT)
849856

@@ -863,11 +870,11 @@ def main() -> None:
863870
LsfLauncher.max_parallel = args.max_parallel
864871
NcLauncher.max_parallel = args.max_parallel
865872
Launcher.max_odirs = args.max_odirs
866-
set_launcher_type(args.local)
873+
FakeLauncher.max_parallel = args.max_parallel
874+
set_launcher_type(is_local=args.local, fake=args.fake)
867875

868876
# Build infrastructure from hjson file and create the list of items to
869877
# be deployed.
870-
global cfg
871878
cfg = make_cfg(args.cfg, args, proj_root)
872879

873880
# List items available for run if --list switch is passed, and exit.

src/dvsim/launcher/factory.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66

77
from dvsim.launcher.base import Launcher
8+
from dvsim.launcher.fake import FakeLauncher
89
from dvsim.launcher.local import LocalLauncher
910
from dvsim.launcher.lsf import LsfLauncher
1011
from dvsim.launcher.nc import NcLauncher
@@ -23,7 +24,7 @@
2324
_LAUNCHER_CLS: type[Launcher] | None = None
2425

2526

26-
def set_launcher_type(is_local: bool = False) -> None:
27+
def set_launcher_type(is_local: bool = False, fake: bool = False) -> None:
2728
"""Set the launcher type that will be used to launch the jobs.
2829
2930
The env variable `DVSIM_LAUNCHER` is used to identify what launcher system
@@ -35,6 +36,8 @@ def set_launcher_type(is_local: bool = False) -> None:
3536
launcher = os.environ.get("DVSIM_LAUNCHER", "local")
3637
if is_local:
3738
launcher = "local"
39+
if fake:
40+
launcher = "fake"
3841
Launcher.variant = launcher
3942

4043
global _LAUNCHER_CLS
@@ -53,6 +56,9 @@ def set_launcher_type(is_local: bool = False) -> None:
5356
elif launcher == "slurm":
5457
_LAUNCHER_CLS = SlurmLauncher
5558

59+
elif launcher == "fake":
60+
_LAUNCHER_CLS = FakeLauncher
61+
5662
# These custom launchers are site specific. They may not be committed to
5763
# the open source repo.
5864
elif launcher == "edacloud" and EDACLOUD_LAUNCHER_EXISTS:

src/dvsim/launcher/fake.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright lowRISC contributors (OpenTitan project).
2+
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
"""Fake Launcher that returns random results."""
6+
7+
from random import choice, random
8+
from typing import TYPE_CHECKING
9+
10+
from dvsim.launcher.base import ErrorMessage, Launcher
11+
12+
if TYPE_CHECKING:
13+
from dvsim.job.deploy import CovReport, Deploy, RunTest
14+
15+
16+
def _run_test_handler(deploy: "RunTest") -> str:
17+
"""Handle a RunTest deploy job."""
18+
return choice(("P", "F"))
19+
20+
21+
def _cov_report_handler(deploy: "CovReport") -> str:
22+
"""Handle a CompileSim deploy job."""
23+
keys = [
24+
"score",
25+
"line",
26+
"cond",
27+
"toggle",
28+
"fsm",
29+
"branch",
30+
"assert",
31+
"group",
32+
]
33+
34+
deploy.cov_results_dict = {k: f"{random() * 100:.2f} %" for k in keys}
35+
36+
return "P"
37+
38+
39+
_DEPLOY_HANDLER = {
40+
"RunTest": _run_test_handler,
41+
"CovReport": _cov_report_handler,
42+
}
43+
44+
45+
class FakeLauncher(Launcher):
46+
"""Launch jobs and return fake results."""
47+
48+
# Poll job's completion status every this many seconds
49+
poll_freq = 0
50+
51+
def __init__(self, deploy: "Deploy") -> None:
52+
"""Initialize common class members."""
53+
super().__init__(deploy)
54+
55+
def _do_launch(self) -> None:
56+
"""Do the launch."""
57+
58+
def poll(self) -> str | None:
59+
"""Check status of the running process."""
60+
deploy_cls = self.deploy.__class__.__name__
61+
if deploy_cls in _DEPLOY_HANDLER:
62+
return _DEPLOY_HANDLER[deploy_cls](deploy=self.deploy)
63+
64+
# Default result is Pass
65+
return "P"
66+
67+
def kill(self) -> None:
68+
"""Kill the running process."""
69+
self._post_finish(
70+
"K",
71+
ErrorMessage(line_number=None, message="Job killed!", context=[]),
72+
)

0 commit comments

Comments
 (0)