stdlib: Update the default exit events and warning

This change updates the default actions taken when the user doesn't
specify generators for the exit events in the simulator. Rather than
defining default generators, this change makes the generators more
generic and gives a new decorator to mark them as default.

This change then updates the default generators in the simulator and
only makes some of them issue a default warning. For exit events such as
EXIT, the default will no longer print a warning.

Change-Id: I5552f52392f3aea577034ed278a9ff9e8b5b0b01
Signed-off-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/64251
Reviewed-by: Zhantong Qiu <ztqiu@ucdavis.edu>
Maintainer: Bobby Bruce <bbruce@ucdavis.edu>
Tested-by: kokoro <noreply+kokoro@google.com>
diff --git a/src/python/gem5/simulate/exit_event_generators.py b/src/python/gem5/simulate/exit_event_generators.py
index cdd8800..fc99dba 100644
--- a/src/python/gem5/simulate/exit_event_generators.py
+++ b/src/python/gem5/simulate/exit_event_generators.py
@@ -24,6 +24,7 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+from typing import Generator, Optional
 import m5.stats
 from ..components.processors.abstract_processor import AbstractProcessor
 from ..components.processors.switchable_processor import SwitchableProcessor
@@ -35,39 +36,37 @@
 """
 
 
-def defaultBehaviorWarning(type, effect):
-    warn(
-        "As no behavior was set by the user, default behavior is being carried"
-        f" out.\n Type: {type} \n Detail: {effect} \n"
-    )
+def warn_default_decorator(gen: Generator, type: str, effect: str):
+    """A decortator for generators which will print a warning that it is a
+    default generator.
+    """
+
+    def wrapped_generator(*args, **kw_args):
+        warn(
+            f"No behavior was set by the user for {type}."
+            f" Default behavior is {effect}."
+        )
+        for value in gen(*args, **kw_args):
+            yield value
+
+    return wrapped_generator
 
 
-def default_exit_generator():
+def exit_generator():
     """
     A default generator for an exit event. It will return True, indicating that
     the Simulator run loop should exit.
     """
-    defaultBehaviorWarning(
-        "default_exit_generator",
-        "A default generator for an exit event. It will return True, "
-        "indicating that the Simulator run loop should exit.",
-    )
     while True:
         yield True
 
 
-def default_switch_generator(processor: AbstractProcessor):
+def switch_generator(processor: AbstractProcessor):
     """
     A default generator for a switch exit event. If the processor is a
     SwitchableProcessor, this generator will switch it. Otherwise nothing will
     happen.
     """
-    defaultBehaviorWarning(
-        "default_switch_generator",
-        "A default generator for a switch exit event.If the processor is a "
-        "SwitchableProcessor, this generator will switch it. Otherwise nothing"
-        " will happen.",
-    )
     is_switchable = isinstance(processor, SwitchableProcessor)
     while True:
         if is_switchable:
@@ -76,50 +75,6 @@
             yield False
 
 
-def default_workbegin_generator():
-    """
-    A default generator for a workbegin exit event. It will reset the
-    simulation statistics.
-    """
-    defaultBehaviorWarning(
-        "default_workbegin_generator",
-        "A default generator for a workbegin exit event. It will reset the "
-        "simulation statistics.",
-    )
-    while True:
-        m5.stats.reset()
-        yield False
-
-
-def default_workend_generator():
-    """
-    A default generator for a workend exit event. It will dump the simulation
-    statistics.
-    """
-    defaultBehaviorWarning(
-        "default_workend_generator",
-        "A default generator for a workend exit event. It will dump the "
-        "simulation statistics.",
-    )
-    while True:
-        m5.stats.dump()
-        yield False
-
-
-def default_simpoint_generator():
-    """
-    A default generator for SimPoints. It will do nothing.
-    The Simulation run loop will continue after executing the behavior of the
-    generator.
-    """
-    defaultBehaviorWarning(
-        "default_simpoint_generator",
-        "A default generator for SimPoints. It will do nothing.",
-    )
-    while True:
-        yield False
-
-
 def dump_reset_generator():
     """
     A generator for doing statstic dump and reset. It will reset the simulation
@@ -133,13 +88,45 @@
         yield False
 
 
-def save_checkpoint_generator(checkpoint_dir: Path):
+def save_checkpoint_generator(checkpoint_dir: Optional[Path] = None):
     """
     A generator for taking a checkpoint. It will take a checkpoint with the
     input path and the current simulation Ticks.
     The Simulation run loop will continue after executing the behavior of the
     generator.
     """
+    if not checkpoint_dir:
+        from m5 import options
+
+        checkpoint_dir = Path(options.outdir)
     while True:
         m5.checkpoint((checkpoint_dir / f"cpt.{str(m5.curTick())}").as_posix())
         yield False
+
+
+def reset_stats_generator():
+    """
+    This generator resets the stats every time it is called. It does not dump
+    the stats before resetting them.
+    """
+    while True:
+        m5.stats.reset()
+        yield False
+
+
+def dump_stats_generator():
+    """
+    This generator dumps the stats every time it is called.
+    """
+    while True:
+        m5.stats.dump()
+        yield False
+
+
+def skip_generator():
+    """
+    This generator does nothing when on the exit event.
+    The simulation will continue after this generator.
+    """
+    while True:
+        yield False
diff --git a/src/python/gem5/simulate/simulator.py b/src/python/gem5/simulate/simulator.py
index efa7c42..f4edd51 100644
--- a/src/python/gem5/simulate/simulator.py
+++ b/src/python/gem5/simulate/simulator.py
@@ -37,11 +37,12 @@
 from typing import Optional, List, Tuple, Dict, Generator, Union
 
 from .exit_event_generators import (
-    default_exit_generator,
-    default_switch_generator,
-    default_workbegin_generator,
-    default_workend_generator,
-    default_simpoint_generator,
+    warn_default_decorator,
+    exit_generator,
+    switch_generator,
+    save_checkpoint_generator,
+    reset_stats_generator,
+    dump_stats_generator,
 )
 from .exit_event import ExitEvent
 from ..components.boards.abstract_board import AbstractBoard
@@ -147,14 +148,16 @@
         Each exit event has a default behavior if none is specified by the
         user. These are as follows:
 
-            * ExitEvent.EXIT:  default_exit_list
-            * ExitEvent.CHECKPOINT: default_exit_list
-            * ExitEvent.FAIL : default_exit_list
-            * ExitEvent.SWITCHCPU: default_switch_list
-            * ExitEvent.WORKBEGIN: default_workbegin_list
-            * ExitEvent.WORKEND: default_workend_list
-            * ExitEvent.USER_INTERRUPT: default_exit_generator
-            * ExitEvent.MAX_TICK: default_exit_generator()
+            * ExitEvent.EXIT:  exit simulation
+            * ExitEvent.CHECKPOINT: take a checkpoint
+            * ExitEvent.FAIL : exit simulation
+            * ExitEvent.SWITCHCPU: call `switch` on the processor
+            * ExitEvent.WORKBEGIN: reset stats
+            * ExitEvent.WORKEND: exit simulation
+            * ExitEvent.USER_INTERRUPT: exit simulation
+            * ExitEvent.MAX_TICK: exit simulation
+            * ExitEvent.SIMPOINT_BEGIN: reset stats
+            * ExitEvent.MAX_INSTS: exit simulation
 
         These generators can be found in the `exit_event_generator.py` module.
 
@@ -169,19 +172,40 @@
         # We specify a dictionary here outlining the default behavior for each
         # exit event. Each exit event is mapped to a generator.
         self._default_on_exit_dict = {
-            ExitEvent.EXIT: default_exit_generator(),
-            # TODO: Something else should be done here for CHECKPOINT
-            ExitEvent.CHECKPOINT: default_exit_generator(),
-            ExitEvent.FAIL: default_exit_generator(),
-            ExitEvent.SWITCHCPU: default_switch_generator(
-                processor=board.get_processor()
-            ),
-            ExitEvent.WORKBEGIN: default_workbegin_generator(),
-            ExitEvent.WORKEND: default_workend_generator(),
-            ExitEvent.USER_INTERRUPT: default_exit_generator(),
-            ExitEvent.MAX_TICK: default_exit_generator(),
-            ExitEvent.SIMPOINT_BEGIN: default_simpoint_generator(),
-            ExitEvent.MAX_INSTS: default_simpoint_generator(),
+            ExitEvent.EXIT: exit_generator(),
+            ExitEvent.CHECKPOINT: warn_default_decorator(
+                save_checkpoint_generator,
+                "checkpoint",
+                "creating a checkpoint and continuing",
+            )(),
+            ExitEvent.FAIL: exit_generator(),
+            ExitEvent.SWITCHCPU: warn_default_decorator(
+                switch_generator,
+                "switch CPU",
+                "switching the CPU type of the processor and continuing",
+            )(processor=board.get_processor()),
+            ExitEvent.WORKBEGIN: warn_default_decorator(
+                reset_stats_generator,
+                "work begin",
+                "resetting the stats and continuing",
+            )(),
+            ExitEvent.WORKEND: warn_default_decorator(
+                dump_stats_generator,
+                "work end",
+                "dumping the stats and continuing",
+            )(),
+            ExitEvent.USER_INTERRUPT: exit_generator(),
+            ExitEvent.MAX_TICK: exit_generator(),
+            ExitEvent.SIMPOINT_BEGIN: warn_default_decorator(
+                reset_stats_generator,
+                "simpoint begin",
+                "resetting the stats and continuing",
+            )(),
+            ExitEvent.MAX_INSTS: warn_default_decorator(
+                exit_generator,
+                "max instructions",
+                "exiting the simulation",
+            )(),
         }
 
         if on_exit_event: