SST 15.0
Structural Simulation Toolkit
realtime.h
1// Copyright 2009-2025 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2025, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_REAL_TIME_ALARM_MANAGER_H
13#define SST_CORE_REAL_TIME_ALARM_MANAGER_H
14
15#include "sst/core/realtimeAction.h"
16#include "sst/core/serialization/serializable.h"
17#include "sst/core/sst_types.h"
18#include "sst/core/threadsafe.h"
19#include "sst/core/warnmacros.h"
20
21#include <atomic>
22#include <cstdint>
23#include <map>
24#include <set>
25#include <signal.h>
26#include <time.h>
27#include <vector>
28
29namespace SST {
30
31class Output;
32class RankInfo;
33class UnitAlgebra;
34
35/* Action cleanly exit simulation */
36class ExitCleanRealTimeAction : public RealTimeAction
37{
38public:
39 SST_ELI_REGISTER_REALTIMEACTION(ExitCleanRealTimeAction, "sst", "rt.exit.clean", SST_ELI_ELEMENT_VERSION(0, 1, 0),
40 "Signal handler that causes an immediate, but non-emergency shutdown. This is the default action for the "
41 "'--exit-after' option.")
42
44 virtual void execute() override;
45 virtual void begin(time_t scheduled_time) override;
46};
47
48/* Action to immediately exit simulation */
49class ExitEmergencyRealTimeAction : public RealTimeAction
50{
51public:
52 SST_ELI_REGISTER_REALTIMEACTION(ExitEmergencyRealTimeAction, "sst", "rt.exit.emergency",
53 SST_ELI_ELEMENT_VERSION(0, 1, 0),
54 "Signal handler that causes an emergency shutdown. This is the default action for SIGTERM and SIGINT.")
55
57 virtual void execute() override;
58};
59
60/* Action to output core status */
61class CoreStatusRealTimeAction : public RealTimeAction
62{
63public:
64 SST_ELI_REGISTER_REALTIMEACTION(CoreStatusRealTimeAction, "sst", "rt.status.core", SST_ELI_ELEMENT_VERSION(0, 1, 0),
65 "Signal handler that causes SST-Core to print its status. This is the default action for SIGUSR1.")
66
68 void execute() override;
69};
70
71/* Action to output component status */
72class ComponentStatusRealTimeAction : public RealTimeAction
73{
74public:
75 SST_ELI_REGISTER_REALTIMEACTION(ComponentStatusRealTimeAction, "sst", "rt.status.all",
76 SST_ELI_ELEMENT_VERSION(0, 1, 0),
77 "Signal handler that causes SST-Core to print its status along with component status. This is the default "
78 "action for SIGUSR2.")
79
81 void execute() override;
82};
83
84/* Action to trigger a checkpoint on a time interval */
85class CheckpointRealTimeAction : public RealTimeAction
86{
87public:
88 SST_ELI_REGISTER_REALTIMEACTION(CheckpointRealTimeAction, "sst", "rt.checkpoint", SST_ELI_ELEMENT_VERSION(0, 1, 0),
89 "Signal handler that causes SST to generate a checkpoint. This is the default action for the "
90 "'--checkpoint-wall-period' option.")
91
93 virtual void execute() override;
94 virtual void begin(time_t scheduled_time) override;
95
96 bool canInitiateCheckpoint() override { return true; }
97};
98
99/* Action to generate a heartbeat message */
100class HeartbeatRealTimeAction : public RealTimeAction
101{
102public:
103 SST_ELI_REGISTER_REALTIMEACTION(HeartbeatRealTimeAction, "sst", "rt.heartbeat", SST_ELI_ELEMENT_VERSION(0, 1, 0),
104 "Signal handler that causes SST to generate a heartbeat message (status and some resource usage information). "
105 "This is the default action for the '--heartbeat-wall-period' option.")
106
108 virtual void execute() override;
109 virtual void begin(time_t scheduled_time) override;
110
111private:
112 double last_time_;
113 static std::atomic<uint64_t> thr_max_tv_depth_;
114 static Core::ThreadSafe::Barrier exchange_barrier_;
115};
116
117
118/* Action to trigger an interactive console */
119class InteractiveRealTimeAction : public RealTimeAction
120{
121public:
122 SST_ELI_REGISTER_REALTIMEACTION(InteractiveRealTimeAction, "sst", "rt.interactive",
123 SST_ELI_ELEMENT_VERSION(0, 1, 0),
124 "Signal handler that causes SST to break into an interactive console based on the --interactive-console flag.")
125
127 void execute() override;
128 bool isValidSigalrmAction() override { return false; }
129};
130
131/* Wrapper for RealTimeActions that occur on a time interval */
132class RealTimeIntervalAction
133{
134public:
135 RealTimeIntervalAction(uint32_t interval, RealTimeAction* action);
136
137 void begin(time_t begin_time);
138
139 void execute(uint32_t elapsed);
140 uint32_t getNextAlarmTime() const;
141
142private:
143 uint32_t alarm_interval_; /* Interval to trigger alarm at (seconds) */
144 uint32_t next_alarm_time_; /* Next time an alarm should be triggered for this alarm */
145 RealTimeAction* action_; /* Action to take on when alarm triggers */
146};
147
148/* This class manages alarms but does not do any actions itself
149 * All times are stored in seconds
150 */
151class AlrmSignalAction : public RealTimeAction
152{
153public:
154 AlrmSignalAction();
155 void execute() override;
156 void addIntervalAction(uint32_t interval, RealTimeAction* action);
157 virtual void begin(time_t scheduled_time) override; // Start alarms
158
159private:
160 std::vector<RealTimeIntervalAction> interval_actions_;
161 bool alarm_manager_; /* The instance on thread 0/rank 0 is the manager */
162 bool rank_leader_; /* The instance on thread 0 of each rank participates in MPI exchanges */
163 time_t last_time_; /* Last time a SIGALRM was received */
164 static uint32_t elapsed_; /* A static so that each threads' instance of this class share the same one */
165 static Core::ThreadSafe::Barrier exchange_barrier_;
166};
167
168/** Class to manage real-time events (signals and alarms) */
169class RealTimeManager : public SST::Core::Serialization::serializable
170{
171public:
172 explicit RealTimeManager(RankInfo num_ranks);
173 RealTimeManager();
174
175 /** Register actions */
176 void registerSignal(RealTimeAction* action, int signum);
177 void registerInterval(uint32_t interval, RealTimeAction* action);
178
179 /** Begin monitoring signals */
180 void begin();
181
182 /** Simulation run loop calls this when a signal has been received
183 * from the OS. One or more of the sig_X_from_os_ vars will be non-zero.
184 *
185 * Serial - this executes the relevant signal handler(s)
186 * Parallel - this saves the signals until the next global sync
187 */
188 void notifySignal();
189
190 /** This is a request to execute the handler in response to a particular signal */
191 void performSignal(int signum);
192
193 /* OS signal handling */
194 static void installSignalHandlers();
195 static void SimulationSigEndHandler(int sig);
196 static void SimulationSigUsrHandler(int sig);
197 static void SimulationSigAlrmHandler(int sig);
198
199 /* SyncManager request to get signals. Also clears local signals */
200 bool getSignals(int& sig_end, int& sig_usr, int& sig_alrm);
201
202 /**
203 Check whether or not any of the Actions registered with the
204 manager can initiate a checkpoint.
205 */
206 bool canInitiateCheckpoint() { return can_checkpoint_; }
207
208 void serialize_order(SST::Core::Serialization::serializer& ser) override;
209 ImplementSerializable(SST::RealTimeManager)
210
211private:
212 bool serial_exec_; // Whether execution is serial or parallel
213 bool can_checkpoint_ = false; // Set to true if any Actions can trigger checkpoint
214
215 /* The set of signal handlers for all signals */
216 std::map<int, RealTimeAction*> signal_actions_;
217
218 static sig_atomic_t sig_alrm_from_os_;
219 static sig_atomic_t sig_usr_from_os_;
220 static sig_atomic_t sig_end_from_os_;
221
222 int sig_alrm_;
223 int sig_usr_;
224 int sig_end_;
225};
226
227} // namespace SST
228#endif /* SST_CORE_REAL_TIME_ALARM_MANAGER_H */
Definition realtime.h:86
bool canInitiateCheckpoint() override
Let's the core know if this action may trigger a checkpoint so that all the checkpoint infrastructure...
Definition realtime.h:96
Definition realtime.h:73
Definition realtime.h:62
Definition serializable.h:24
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition serializer.h:45
Definition threadsafe.h:46
Definition realtime.h:37
Definition realtime.h:50
Definition realtime.h:101
Definition realtime.h:120
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:54
Definition rankInfo.h:24
An event to trigger at a real-time interval.
Definition realtimeAction.h:32
Class to manage real-time events (signals and alarms)
Definition realtime.h:170
void notifySignal()
Simulation run loop calls this when a signal has been received from the OS.
Definition realtime.cc:606
bool canInitiateCheckpoint()
Check whether or not any of the Actions registered with the manager can initiate a checkpoint.
Definition realtime.h:206
void performSignal(int signum)
This is a request to execute the handler in response to a particular signal.
Definition realtime.cc:662
void registerSignal(RealTimeAction *action, int signum)
Register actions.
Definition realtime.cc:574
void begin()
Begin monitoring signals.
Definition realtime.cc:593
Performs Unit math in full precision.
Definition unitAlgebra.h:107