UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ThreadHeartBeat.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Containers/Map.h"
5#include "CoreTypes.h"
7#include "Delegates/DelegateCombinations.h" // for DECLARE_DELEGATE_OneParam
9#include "HAL/Runnable.h"
10#include "HAL/ThreadSafeBool.h"
13#include "UObject/NameTypes.h"
14
15#if PLATFORM_UNIX
17#endif
18
22
33{
34 uint64 CurrentCycles;
35 uint64 LastRealTickCycles;
36 const uint64 MaxTimeStepCycles;
37
38public:
40
41 CORE_API void Tick();
42 CORE_API double Seconds();
43};
44
50{
51 static CORE_API FThreadHeartBeat* Singleton;
52
54 struct FHeartBeatInfo
55 {
56 FHeartBeatInfo()
57 : LastHeartBeatTime(0.0)
58 , LastHangTime(0.0)
59 , SuspendedCount(0)
60 , HangDuration(0)
61 , LastStuckTime(0.0)
62 , StuckDuration(0.0)
63 , HeartBeatName()
64 {}
65
67 double LastHeartBeatTime;
69 double LastHangTime;
71 int32 SuspendedCount;
73 double HangDuration;
74
76 double LastStuckTime;
78 double StuckDuration;
80 FName HeartBeatName;
81
83 void Suspend()
84 {
85 SuspendedCount++;
86 }
88 void Resume(double CurrentTime)
89 {
90 check(SuspendedCount > 0);
91 if (--SuspendedCount == 0)
92 {
93 LastHeartBeatTime = CurrentTime;
94 }
95 }
96 };
98 FRunnableThread* Thread;
100 FThreadSafeCounter StopTaskCounter;
102 FCriticalSection HeartBeatCritical;
104 TMap<uint32, FHeartBeatInfo> ThreadHeartBeat;
106 FHeartBeatInfo PresentHeartBeat;
107
109 FCriticalSection FunctionHeartBeatCritical;
111 TMap<uint32, FHeartBeatInfo> FunctionHeartBeat;
112
114 FCriticalSection CheckpointHeartBeatCritical;
116 TMap<FName, FHeartBeatInfo> CheckpointHeartBeat;
117
119 FThreadSafeBool bReadyToCheckHeartbeat;
121 double ConfigHangDuration;
122 double CurrentHangDuration;
123 double ConfigPresentDuration;
124 double CurrentPresentDuration;
125 double ConfigStuckDuration;
126 double CurrentStuckDuration;
127
128 double HangDurationMultiplier;
129
131 uint32 LastHangCallstackCRC;
133 uint32 LastHungThreadId;
134
135 uint32 LastStuckThreadId;
136
137 bool bHangsAreFatal;
138
139 FThreadSafeCounter GlobalSuspendCount;
140 int32 CheckpointSuspendCount;
141
143
144 FOnThreadStuck OnStuck;
145 FOnThreadUnstuck OnUnstuck;
146 FOnHangDelegate OnHangDelegate;
147
149 CORE_API virtual ~FThreadHeartBeat();
150
151 CORE_API void FORCENOINLINE OnHang(double HangDuration, uint32 ThreadThatHung);
152 CORE_API void FORCENOINLINE OnPresentHang(double HangDuration);
153
154 CORE_API bool IsEnabled();
155
156 void InitSettingsInternal();
157
158public:
159
168
170 static CORE_API FThreadHeartBeat& Get();
172
174 CORE_API void Start();
176 CORE_API void InitSettings();
178 UE_DEPRECATED(5.7, "HeartBeat(bool bReadConfig) is deprecated, please use HeartBeat() instead.")
179 CORE_API void HeartBeat(bool bReadConfig);
180 CORE_API void HeartBeat();
182 CORE_API void PresentFrame();
186 CORE_API void KillHeartBeat();
187
194
195 /*
196 Called from a thread to register a checkpoint to be monitored
197 @param EndCheckPoint name of the checkpoint that needs to be reached. TimeToReachCheckPoint the time duration we have to reach the specified checkpoint.
198 */
200 /* Called from a thread when a checkpoint has ended */
202 /* Called by a supervising thread to check all checkpoints forward progress */
204
215
219 CORE_API bool IsBeating();
220
226
227 /*
228 * Get the Id of the last thread to trigger the hang detector.
229 * Returns InvalidThreadId if hang detector has not been triggered.
230 */
231 uint32 GetLastHungThreadId() const { return LastHungThreadId; }
232
233 /*
234 * Get the Id of the last thread to pass the stuck thread time.
235 * Returns InvalidThreadId if hang detector has not been triggered.
236 */
237 uint32 GetLastStuckThreadId() const { return LastStuckThreadId; }
238
239 /*
240 * Get delegate for callback on stuck or unstuck thread.
241 */
242 FOnThreadStuck& GetOnThreadStuck() { return OnStuck; }
243 FOnThreadUnstuck& GetOnThreadUnstuck() { return OnUnstuck; }
244
245 /*
246 * Get delegate for callback on hang.
247 * Delegate implementation will be called from the hang detector thread and not from the hung thread
248 */
249 FOnHangDelegate& GetOnHangDelegate() { return OnHangDelegate; }
250
251 /*
252 * Get hang duration threshold.
253 */
254 double GetHangDuration() const { return ConfigHangDuration; };
255
256 //~ Begin FRunnable Interface.
257 CORE_API virtual bool Init();
258 CORE_API virtual uint32 Run();
259 CORE_API virtual void Stop();
260 //~ End FRunnable Interface
261};
262
265{
266private:
267 bool bSuspendedAllThreads;
268 bool bSuspended;
269public:
270 inline FSlowHeartBeatScope(bool bAllThreads = false)
271 : bSuspendedAllThreads(bAllThreads)
272 , bSuspended(false)
273 {
275 {
276 bSuspended = true;
277 HB->SuspendHeartBeat(bSuspendedAllThreads);
278 }
279 }
281 {
282 if (bSuspended)
283 {
285 {
286 HB->ResumeHeartBeat(bSuspendedAllThreads);
287 }
288 }
289 }
290};
291
294{
295private:
296 bool bStartedMonitor;
297public:
299 : bStartedMonitor(false)
300 {
302 {
303 bStartedMonitor = true;
304 HB->MonitorFunctionStart();
305 }
306 }
308 {
309 if (bStartedMonitor)
310 {
312 {
313 HB->MonitorFunctionEnd();
314 }
315 }
316 }
317};
318
319
320// When 1, performs a full symbol lookup in hitch call stacks, otherwise only
321// a backtrace is performed and the raw addresses are written to the log.
322#ifndef LOOKUP_SYMBOLS_IN_HITCH_STACK_WALK
323#define LOOKUP_SYMBOLS_IN_HITCH_STACK_WALK 0
324#endif
325
326#ifndef WALK_STACK_ON_HITCH_DETECTED
327#define WALK_STACK_ON_HITCH_DETECTED 0
328#endif
329
331{
333
337 FThreadSafeCounter StopTaskCounter;
339 FCriticalSection HeartBeatCritical;
340
341#if USE_HITCH_DETECTION
342 float HangDuration = -1.0f;
344 double FirstStartTime;
345 double FrameStartTime;
346 int32 SuspendedCount;
347 bool bStartSuspended = false;
348
349#if WALK_STACK_ON_HITCH_DETECTED
350#if LOOKUP_SYMBOLS_IN_HITCH_STACK_WALK
351 static constexpr SIZE_T StackTraceSize = 65535;
352 ANSICHAR StackTrace[StackTraceSize];
353#else
354 static constexpr uint32 MaxStackDepth = 128;
355 uint64 StackTrace[MaxStackDepth];
356#endif // LOOKUP_SYMBOLS_IN_HITCH_STACK_WALK
357#endif // WALK_STACK_ON_HITCH_DETECTED
358#endif // USE_HITCH_DETECTION
359
361
364
365 void InitSettingsInternal();
366
367public:
368
370 {
373 };
374
378
380 CORE_API void InitSettings();
381
385 CORE_API void FrameStart(bool bSkipThisFrame = false);
386
388 CORE_API double GetCurrentTime();
389
394
399
404
405 // No-op, used in FUnixSignalGameHitchHeartBeat
406 void Restart() {}
407 void PostFork() {}
408
409 //~ Begin FRunnable Interface.
410 CORE_API virtual bool Init();
411 CORE_API virtual uint32 Run();
412 CORE_API virtual void Stop();
413 //~ End FRunnable Interface
414};
415
416#if PLATFORM_UNIX
418#else
420#endif
421
#define FORCENOINLINE
Definition AndroidPlatform.h:142
#define check(expr)
Definition AssertionMacros.h:314
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
#define DECLARE_DELEGATE_OneParam(DelegateName, Param1Type)
Definition DelegateCombinations.h:48
FGameThreadHitchHeartBeatThreaded FGameThreadHitchHeartBeat
Definition ThreadHeartBeat.h:419
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ThreadHeartBeat.h:331
EConstants
Definition ThreadHeartBeat.h:370
@ InvalidThreadId
Definition ThreadHeartBeat.h:372
CORE_API void InitSettings()
Definition ThreadHeartBeat.cpp:965
CORE_API void FrameStart(bool bSkipThisFrame=false)
Definition ThreadHeartBeat.cpp:1158
void PostFork()
Definition ThreadHeartBeat.h:407
static CORE_API FGameThreadHitchHeartBeatThreaded * GetNoInit()
Definition ThreadHeartBeat.cpp:953
CORE_API void ResumeHeartBeat()
Definition ThreadHeartBeat.cpp:1199
virtual CORE_API bool Init()
Definition ThreadHeartBeat.cpp:959
CORE_API double GetCurrentTime()
Definition ThreadHeartBeat.cpp:1243
static CORE_API FGameThreadHitchHeartBeatThreaded & Get()
Definition ThreadHeartBeat.cpp:925
virtual CORE_API void Stop()
Definition ThreadHeartBeat.cpp:1153
virtual CORE_API uint32 Run()
Definition ThreadHeartBeat.cpp:1057
CORE_API double GetFrameStartTime()
Definition ThreadHeartBeat.cpp:1234
CORE_API bool IsStartedSuspended()
Definition ThreadHeartBeat.cpp:1225
CORE_API void SuspendHeartBeat()
Definition ThreadHeartBeat.cpp:1181
void Restart()
Definition ThreadHeartBeat.h:406
Definition NameTypes.h:617
Definition RunnableThread.h:20
Definition Runnable.h:20
Definition ThreadHeartBeat.h:33
CORE_API void Tick()
Definition ThreadHeartBeat.cpp:44
CORE_API double Seconds()
Definition ThreadHeartBeat.cpp:54
Definition ThreadHeartBeat.h:50
virtual CORE_API void Stop()
Definition ThreadHeartBeat.cpp:340
static CORE_API FThreadHeartBeat * GetNoInit()
Definition ThreadHeartBeat.cpp:147
CORE_API bool IsBeating()
Definition ThreadHeartBeat.cpp:863
CORE_API void SetDurationMultiplier(double NewMultiplier)
Definition ThreadHeartBeat.cpp:878
CORE_API void MonitorCheckpointEnd(FName CheckPoint)
Definition ThreadHeartBeat.cpp:694
CORE_API uint32 CheckFunctionHeartBeat(double &OutHangDuration)
Definition ThreadHeartBeat.cpp:641
uint32 GetLastHungThreadId() const
Definition ThreadHeartBeat.h:231
CORE_API void KillHeartBeat()
Definition ThreadHeartBeat.cpp:748
FOnThreadStuck & GetOnThreadStuck()
Definition ThreadHeartBeat.h:242
FOnThreadUnstuck & GetOnThreadUnstuck()
Definition ThreadHeartBeat.h:243
CORE_API uint32 CheckCheckpointHeartBeat(double &OutHangDuration)
Definition ThreadHeartBeat.cpp:707
CORE_API void HeartBeat()
Definition ThreadHeartBeat.cpp:429
CORE_API void MonitorFunctionStart()
Definition ThreadHeartBeat.cpp:488
CORE_API void PresentFrame()
Definition ThreadHeartBeat.cpp:470
virtual CORE_API bool Init()
Definition ThreadHeartBeat.cpp:153
double GetHangDuration() const
Definition ThreadHeartBeat.h:254
CORE_API void Start()
Definition ThreadHeartBeat.cpp:346
CORE_API void MonitorCheckpointStart(FName EndCheckPoint, double TimeToReachCheckpoint)
Definition ThreadHeartBeat.cpp:672
CORE_API void SuspendHeartBeat(bool bAllThreads=false)
Definition ThreadHeartBeat.cpp:757
EConstants
Definition ThreadHeartBeat.h:161
@ PresentThreadId
Definition ThreadHeartBeat.h:166
@ InvalidThreadId
Definition ThreadHeartBeat.h:163
CORE_API void MonitorFunctionEnd()
Definition ThreadHeartBeat.cpp:507
UE_DEPRECATED(5.7, "HeartBeat(bool bReadConfig) is deprecated, please use HeartBeat() instead.") CORE_API void HeartBeat(bool bReadConfig)
CORE_API void ResumeHeartBeat(bool bAllThreads=false)
Definition ThreadHeartBeat.cpp:797
virtual CORE_API uint32 Run()
Definition ThreadHeartBeat.cpp:290
uint32 GetLastStuckThreadId() const
Definition ThreadHeartBeat.h:237
CORE_API void InitSettings()
Definition ThreadHeartBeat.cpp:351
static CORE_API FThreadHeartBeat & Get()
Definition ThreadHeartBeat.cpp:119
FOnHangDelegate & GetOnHangDelegate()
Definition ThreadHeartBeat.h:249
CORE_API uint32 CheckHeartBeat(double &OutHangDuration)
Definition ThreadHeartBeat.cpp:549
Definition ThreadSafeBool.h:17
Definition ThreadSafeCounter.h:14
Definition UnixSignalHeartBeat.h:10
Definition UnrealString.h.inl:34
@ false
Definition radaudio_common.h:23
Definition ThreadHeartBeat.h:424
UE_FORCEINLINE_HINT FDisableHitchDetectorScope()
Definition ThreadHeartBeat.h:425
UE_FORCEINLINE_HINT ~FDisableHitchDetectorScope()
Definition ThreadHeartBeat.h:429
Definition ThreadHeartBeat.h:294
FFunctionHeartBeatScope()
Definition ThreadHeartBeat.h:298
~FFunctionHeartBeatScope()
Definition ThreadHeartBeat.h:307
Definition ThreadHeartBeat.h:265
FSlowHeartBeatScope(bool bAllThreads=false)
Definition ThreadHeartBeat.h:270
~FSlowHeartBeatScope()
Definition ThreadHeartBeat.h:280