Skip to content

Commit

Permalink
stats per t
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Jun 6, 2024
1 parent fa7555b commit 1f08099
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
20 changes: 20 additions & 0 deletions pyphare/pyphare/core/phare_utilities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import math
import numpy as np

Expand Down Expand Up @@ -174,3 +175,22 @@ def deep_copy(item, memo, excludes=[]):
else:
setattr(that, key, deepcopy(value, memo))
return that


def write_system_state_stats(file_path):
import json, psutil, pathlib, datetime

file_path = pathlib.Path(file_path) if isinstance(file_path, str) else file_path
proc = psutil.Process(pid=os.getpid())
file_path.parent.mkdir(exist_ok=True, parents=True)

with open(file_path, "w") as file:
json.dump(
dict(
time=datetime.datetime.utcnow().timestamp(),
cpu_use=proc.cpu_percent(interval=0.1),
open_files=len(proc.open_files()),
mem_use=proc.memory_info().rss / 1024**2,
),
file,
)
21 changes: 13 additions & 8 deletions pyphare/pyphare/pharein/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,14 +581,16 @@ def check_clustering(**kwargs):
return clustering


def check_loadbalancing(**kwargs):
valid_keys = ["nppc", "homogeneous"]
loadbalancing = kwargs.get("loadbalancing", "nppc")
if loadbalancing not in valid_keys:
raise ValueError(
f"Error: loadbalancing type is not supported, supported types are {valid_keys}"
)
return loadbalancing
def check_system_state_writes(**kwargs):
write_time = kwargs.get("system_state_write_every_t", 0)
if write_time > 0:
try:
import psutil
except ImportError as e:
raise ValueError(
"psutil is not available for writing system usage information"
)
return write_time


# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -626,6 +628,7 @@ def wrapper(simulation_object, **kwargs):
"description",
"dry_run",
"write_reports",
"system_state_write_every_t",
]

accepted_keywords += check_optional_keywords(**kwargs)
Expand Down Expand Up @@ -699,6 +702,8 @@ def wrapper(simulation_object, **kwargs):
"write_reports", os.environ.get("PHARE_TESTING", "0") != "1"
)

kwargs["system_state_write_every_t"] = check_system_state_writes(**kwargs)

return func(simulation_object, **kwargs)

return wrapper
Expand Down
20 changes: 19 additions & 1 deletion pyphare/pyphare/simulator/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, simulation, auto_dump=True, **kwargs):
self.print_eol = "\r"
self.print_eol = kwargs.get("print_eol", self.print_eol)
self.log_to_file = kwargs.get("log_to_file", True)

self.write_system_state_time = 0
self.auto_dump = auto_dump
import pyphare.simulator._simulator as _simulator

Expand Down Expand Up @@ -161,6 +161,7 @@ def advance(self, dt=None):

if self._auto_dump() and self.post_advance != None:
self.post_advance(self.cpp_sim.currentTime())
self.write_system_state_stats()
return self

def times(self):
Expand Down Expand Up @@ -255,6 +256,23 @@ def _check_init(self):
if self.cpp_sim is None:
self.initialize()

def write_system_state_stats(self):
import pyphare.core.phare_utilities as pu
from pyphare.cpp import cpp_lib

if self.simulation.system_state_write_every_t == 0:
return
diff_time = self.cpp_sim.currentTime() - self.write_system_state_time
if (
pu.fp_equal(diff_time, self.simulation.system_state_write_every_t)
or diff_time > self.simulation.system_state_write_every_t
):
self.write_system_state_time = self.cpp_sim.currentTime()
time = "{:.10f}".format(self.cpp_sim.currentTime())
pu.write_system_state_stats(
f".phare/sys/stats/{cpp_lib().mpi_rank()}/{time}"
)

def _log_to_file(self):
"""
send C++ std::cout logs to files with env var PHARE_LOG
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ scipy
matplotlib
seaborn
sphinx_rtd_theme
psutil

0 comments on commit 1f08099

Please sign in to comment.