diff --git a/autosubmit/autosubmit.py b/autosubmit/autosubmit.py index 627c2bb31e0d69111b3129370fde728227236137..786ce16954e7f4b899a327f21db2ce62e86fa4d6 100644 --- a/autosubmit/autosubmit.py +++ b/autosubmit/autosubmit.py @@ -2625,14 +2625,18 @@ class Autosubmit: expid, as_conf, notransitive=notransitive, monitor=True, new=False) Log.debug("Job list restored from {0} files", pkl_dir) except AutosubmitError as e: + if profile: + profiler.stop() raise AutosubmitCritical(e.message, e.code, e.trace) except AutosubmitCritical as e: + if profile: + profiler.stop() raise except BaseException as e: - raise - finally: if profile: profiler.stop() + raise + try: jobs = [] if not isinstance(job_list, type([])): @@ -2690,10 +2694,9 @@ class Autosubmit: else: jobs = job_list.get_job_list() except BaseException as e: - raise AutosubmitCritical("Issues during the job_list generation. Maybe due I/O error", 7040, str(e)) - finally: if profile: profiler.stop() + raise AutosubmitCritical("Issues during the job_list generation. Maybe due I/O error", 7040, str(e)) # WRAPPERS try: @@ -2718,10 +2721,9 @@ class Autosubmit: packages = JobPackagePersistence(os.path.join(BasicConfig.LOCAL_ROOT_DIR, expid, "pkl"), "job_packages_" + expid).load() except BaseException as e: - raise AutosubmitCritical("Issues during the wrapper loading, may be related to IO issues", 7040, str(e)) - finally: if profile: profiler.stop() + raise AutosubmitCritical("Issues during the wrapper loading, may be related to IO issues", 7040, str(e)) groups_dict = dict() try: @@ -2735,12 +2737,11 @@ class Autosubmit: jobs), job_list, expand_list=expand, expanded_status=status) groups_dict = job_grouping.group_jobs() except BaseException as e: + if profile: + profiler.stop() raise AutosubmitCritical( "Jobs can't be grouped, perhaps you're using an invalid format. Take a look into readthedocs", 7011, str(e)) - finally: - if profile: - profiler.stop() monitor_exp = Monitor() try: diff --git a/autosubmit/profiler/profiler.py b/autosubmit/profiler/profiler.py index b73ccea09f2aba409167ad292082d2ffd02e650f..82fdfceae808de0620a8a0b0808f90c312319c63 100644 --- a/autosubmit/profiler/profiler.py +++ b/autosubmit/profiler/profiler.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -# Copyright 2015-2023 Earth Sciences Department, BSC-CNS +# Copyright 2015-2024 Earth Sciences Department, BSC-CNS # This file is part of Autosubmit. @@ -21,6 +21,7 @@ import cProfile import io import os import pstats +from enum import Enum from datetime import datetime from pathlib import Path from pstats import SortKey @@ -33,6 +34,11 @@ from log.log import Log, AutosubmitCritical _UNITS = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"] +class ProfilerState(Enum): + """Enumeration of profiler states""" + STOPPED = "stopped" + STARTED = "started" + class Profiler: """Class to profile the execution of experiments.""" @@ -44,28 +50,38 @@ class Profiler: self._mem_init = 0 self._mem_final = 0 - # Error handling flags - self._started = False - self._finished = False + # Error handling + self._state = ProfilerState.STOPPED + + @property + def started(self): + return self._state == ProfilerState.STARTED + + @property + def stopped(self): + return self._state == ProfilerState.STOPPED def start(self) -> None: """Function to start the profiling process.""" - if self._started: + if self.started: raise AutosubmitCritical('The profiling process was already started.', 7074) - self._started = True + self._state = ProfilerState.STARTED self._profiler.enable() self._mem_init += _get_current_memory() def stop(self) -> None: """Function to finish the profiling process.""" - if not self._started or self._finished: + if not self.started: raise AutosubmitCritical('Cannot stop the profiler because was not running.', 7074) + if self.stopped: + Log.info("Cannot stop the profiler because was not running.") + return self._profiler.disable() self._mem_final += _get_current_memory() self._report() - self._finished = True + self._state = ProfilerState.STOPPED def _report(self) -> None: """Function to print the final report into the stdout, log and filesystem.""" diff --git a/test/unit/test_profiler.py b/test/unit/test_profiler.py index cf99067eaaf349f507a6c59f53063d45493cd9e8..01e2eb81580bcd2948c977864ef1e47506711603 100644 --- a/test/unit/test_profiler.py +++ b/test/unit/test_profiler.py @@ -30,9 +30,9 @@ class TestProfiler(TestCase): self.profiler.start() self.assertRaises(AutosubmitCritical, self.profiler.start) - # stop -> stop - self.profiler.stop() - self.assertRaises(AutosubmitCritical, self.profiler.stop) + # # stop -> stop + # self.profiler.stop() + # self.assertRaises(AutosubmitCritical, self.profiler.stop) # White box tests @mock.patch("os.access")