Skip to content

Commit

Permalink
move telemetry subdir constant, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ankona committed Jan 9, 2024
1 parent b6504bb commit ee56791
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 60 deletions.
4 changes: 4 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,3 +698,7 @@ def setup_test_colo(
assert colo_model.colocated
# Check to make sure that limit_db_cpus made it into the colo settings
return colo_model

@pytest.fixture
def config() -> smartsim._core.config.Config:
return CONFIG
3 changes: 3 additions & 0 deletions smartsim/_core/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ def telemetry_enabled(self) -> bool:
def telemetry_cooldown(self) -> int:
return int(os.environ.get("SMARTSIM_TELEMETRY_COOLDOWN", 90))

@property
def telemetry_subdir(self) -> str:
return ".smartsim/telemetry"

@lru_cache(maxsize=128, typed=False)
def get_config() -> Config:
Expand Down
3 changes: 2 additions & 1 deletion smartsim/_core/control/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from dataclasses import dataclass, field

from ...database import Orchestrator
from ..config import CONFIG
from ...entity import DBNode, Ensemble, EntitySequence, Model, SmartSimEntity
from ...error import SmartSimError
from ..utils import helpers as _helpers
Expand Down Expand Up @@ -343,7 +344,7 @@ def finalize(self) -> LaunchedManifest[_T]:
def _format_exp_telemetry_path(
exp_path: t.Union[str, "os.PathLike[str]"]
) -> pathlib.Path:
return pathlib.Path(exp_path, _serialize.TELMON_SUBDIR)
return pathlib.Path(exp_path, CONFIG.telemetry_subdir)


def _format_run_telemetry_path(
Expand Down
6 changes: 3 additions & 3 deletions smartsim/_core/entrypoints/telemetrymonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
from smartsim._core.launcher.slurm.slurmLauncher import SlurmLauncher
from smartsim._core.launcher.stepInfo import StepInfo
from smartsim._core.utils.helpers import get_ts
from smartsim._core.utils.serialize import MANIFEST_FILENAME, TELMON_SUBDIR
from smartsim._core.utils.serialize import MANIFEST_FILENAME
from smartsim.error.errors import SmartSimError
from smartsim.status import STATUS_COMPLETED, TERMINAL_STATUSES

Expand Down Expand Up @@ -582,7 +582,7 @@ def main(
poll for new jobs before attempting to shutdown
:type cooldown_duration: int
"""
manifest_relpath = pathlib.Path(TELMON_SUBDIR) / MANIFEST_FILENAME
manifest_relpath = pathlib.Path(CONFIG.telemetry_subdir) / MANIFEST_FILENAME
manifest_path = experiment_dir / manifest_relpath
monitor_pattern = str(manifest_relpath)

Expand Down Expand Up @@ -667,7 +667,7 @@ def get_parser() -> argparse.ArgumentParser:
log.setLevel(logging.DEBUG)
log.propagate = False

log_path = os.path.join(args.exp_dir, TELMON_SUBDIR, "telemetrymonitor.log")
log_path = os.path.join(args.exp_dir, CONFIG.telemetry_subdir, "telemetrymonitor.log")
fh = logging.FileHandler(log_path, "a")
log.addHandler(fh)

Expand Down
2 changes: 1 addition & 1 deletion smartsim/_core/utils/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
TStepLaunchMetaData = t.Tuple[
t.Optional[str], t.Optional[str], t.Optional[bool], str, str, Path
]
TELMON_SUBDIR: t.Final[str] = ".smartsim/telemetry"

MANIFEST_FILENAME: t.Final[str] = "manifest.json"

_LOGGER = smartsim.log.get_logger(__name__)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_configs/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import time


def echo(message: str, sleep_time: int):
def echo(message, sleep_time):
if sleep_time > 0:
time.sleep(sleep_time)
print(f"Echoing: {message}")
print("Echoing:", message)


if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions tests/test_indirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import psutil
import pytest

from smartsim._core.config import CONFIG
from smartsim._core.entrypoints.indirect import cleanup, get_parser, get_ts, main
from smartsim._core.utils.helpers import encode_cmd
from smartsim._core.utils.serialize import MANIFEST_FILENAME, TELMON_SUBDIR

ALL_ARGS = {
"+command",
Expand Down Expand Up @@ -152,7 +152,7 @@ def test_indirect_main_dir_check(test_dir):
cmd = ["echo", "unit-test"]
encoded_cmd = encode_cmd(cmd)

status_path = exp_dir / TELMON_SUBDIR
status_path = exp_dir / CONFIG.telemetry_subdir

# show that a missing status_path is created when missing
main(encoded_cmd, "application", exp_dir, status_path)
Expand All @@ -167,15 +167,15 @@ def test_indirect_main_cmd_check(capsys, test_dir, monkeypatch):
captured = capsys.readouterr() # throw away existing output
with monkeypatch.context() as ctx, pytest.raises(ValueError) as ex:
ctx.setattr("smartsim._core.entrypoints.indirect.logger.error", print)
_ = main("", "application", exp_dir, exp_dir / TELMON_SUBDIR)
_ = main("", "application", exp_dir, exp_dir / CONFIG.telemetry_subdir)

captured = capsys.readouterr()
assert "Invalid cmd supplied" in ex.value.args[0]

# test with non-emptystring cmd
with monkeypatch.context() as ctx, pytest.raises(ValueError) as ex:
ctx.setattr("smartsim._core.entrypoints.indirect.logger.error", print)
_ = main(" \n \t ", "application", exp_dir, exp_dir / TELMON_SUBDIR)
_ = main(" \n \t ", "application", exp_dir, exp_dir / CONFIG.telemetry_subdir)

captured = capsys.readouterr()
assert "Invalid cmd supplied" in ex.value.args[0]
Expand All @@ -190,13 +190,13 @@ def test_complete_process(fileutils, test_dir):
raw_cmd = f"{sys.executable} {script} --time=1"
cmd = encode_cmd(raw_cmd.split())

rc = main(cmd, "application", exp_dir, exp_dir / TELMON_SUBDIR)
rc = main(cmd, "application", exp_dir, exp_dir / CONFIG.telemetry_subdir)
assert rc == 0

assert exp_dir.exists()

# NOTE: don't have a manifest so we're falling back to default event path
data_dir = exp_dir / TELMON_SUBDIR
data_dir = exp_dir / CONFIG.telemetry_subdir
start_events = list(data_dir.rglob("start.json"))
stop_events = list(data_dir.rglob("stop.json"))

Expand Down
22 changes: 11 additions & 11 deletions tests/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from smartsim._core.utils import serialize
from smartsim.database.orchestrator import Orchestrator

_REL_MANIFEST_PATH = f"{serialize.TELMON_SUBDIR}/{serialize.MANIFEST_FILENAME}"

_CFG_TM_ENABLED_ATTR = "telemetry_enabled"

# The tests in this file belong to the group_b group
Expand All @@ -54,10 +54,10 @@ def turn_on_tm(monkeypatch):
yield


def test_serialize_creates_a_manifest_json_file_if_dne(test_dir):
def test_serialize_creates_a_manifest_json_file_if_dne(test_dir, config):
lmb = LaunchedManifestBuilder("exp", test_dir, "launcher")
serialize.save_launch_manifest(lmb.finalize())
manifest_json = Path(test_dir) / _REL_MANIFEST_PATH
manifest_json = Path(test_dir) / config.telemetry_subdir / serialize.MANIFEST_FILENAME

assert manifest_json.is_file()
with open(manifest_json, "r") as f:
Expand All @@ -69,7 +69,7 @@ def test_serialize_creates_a_manifest_json_file_if_dne(test_dir):


def test_serialize_does_not_write_manifest_json_if_telemetry_monitor_is_off(
test_dir, monkeypatch
test_dir, monkeypatch, config
):
monkeypatch.setattr(
smartsim._core.config.config.Config,
Expand All @@ -78,12 +78,12 @@ def test_serialize_does_not_write_manifest_json_if_telemetry_monitor_is_off(
)
lmb = LaunchedManifestBuilder("exp", test_dir, "launcher")
serialize.save_launch_manifest(lmb.finalize())
manifest_json = Path(test_dir) / _REL_MANIFEST_PATH
manifest_json = Path(test_dir) / config.telemetry_subdir / serialize.MANIFEST_FILENAME
assert not manifest_json.exists()


def test_serialize_appends_a_manifest_json_exists(test_dir):
manifest_json = Path(test_dir) / _REL_MANIFEST_PATH
def test_serialize_appends_a_manifest_json_exists(test_dir, config):
manifest_json = Path(test_dir) / config.telemetry_subdir / serialize.MANIFEST_FILENAME
serialize.save_launch_manifest(
LaunchedManifestBuilder("exp", test_dir, "launcher").finalize()
)
Expand All @@ -102,8 +102,8 @@ def test_serialize_appends_a_manifest_json_exists(test_dir):
assert len({run["run_id"] for run in manifest["runs"]}) == 3


def test_serialize_overwites_file_if_not_json(test_dir):
manifest_json = Path(test_dir) / _REL_MANIFEST_PATH
def test_serialize_overwites_file_if_not_json(test_dir, config):
manifest_json = Path(test_dir) / config.telemetry_subdir / serialize.MANIFEST_FILENAME
manifest_json.parent.mkdir(parents=True, exist_ok=True)
with open(manifest_json, "w") as f:
f.write("This is not a json\n")
Expand All @@ -114,7 +114,7 @@ def test_serialize_overwites_file_if_not_json(test_dir):
assert isinstance(json.load(f), dict)


def test_started_entities_are_serialized(test_dir):
def test_started_entities_are_serialized(test_dir, config):
exp_name = "test-exp"
test_dir = Path(test_dir) / exp_name
test_dir.mkdir(parents=True)
Expand All @@ -131,7 +131,7 @@ def test_started_entities_are_serialized(test_dir):
exp.start(hello_world_model, spam_eggs_model, block=False)
exp.start(hello_ensemble, block=False)

manifest_json = Path(exp.exp_path) / _REL_MANIFEST_PATH
manifest_json = Path(exp.exp_path) / config.telemetry_subdir / serialize.MANIFEST_FILENAME
try:
with open(manifest_json, "r") as f:
manifest = json.load(f)
Expand Down
Loading

0 comments on commit ee56791

Please sign in to comment.