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