QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
StateMachineProfiler.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QElapsedTimer>
4#include <QtCore/QHash>
5#include <QtCore/QJsonObject>
6#include <QtCore/QObject>
7#include <QtCore/QString>
8
9class QAbstractState;
10class QGCStateMachine;
11
27class StateMachineProfiler : public QObject
28{
29 Q_OBJECT
30
31public:
32 struct StateProfile {
33 QString name;
34 int entryCount = 0;
35 qint64 totalTimeMs = 0;
36 qint64 minTimeMs = std::numeric_limits<qint64>::max();
37 qint64 maxTimeMs = 0;
38 qint64 lastEntryTime = 0;
39
40 double averageTimeMs() const {
41 return entryCount > 0 ? static_cast<double>(totalTimeMs) / entryCount : 0.0;
42 }
43 };
44
45 explicit StateMachineProfiler(QGCStateMachine* machine);
46 ~StateMachineProfiler() override = default;
47
49 void setEnabled(bool enabled);
50 bool isEnabled() const { return _enabled; }
51
53 void reset();
54
56 StateProfile profile(const QString& stateName) const;
57
59 QHash<QString, StateProfile> allProfiles() const { return _profiles; }
60
62 qint64 totalRuntimeMs() const { return _totalRuntimeMs; }
63
65 int transitionCount() const { return _transitionCount; }
66
68 QString summary() const;
69
71 QJsonObject toJson() const;
72
74 void logProfile() const;
75
76private slots:
77 void _onMachineStarted();
78 void _onMachineStopped();
79 void _onStateEntered();
80 void _onStateExited();
81
82private:
83 QGCStateMachine* _machine = nullptr;
84 bool _enabled = false;
85
86 QHash<QString, StateProfile> _profiles;
87 QElapsedTimer _machineTimer;
88 QElapsedTimer _stateTimer;
89 qint64 _totalRuntimeMs = 0;
90 int _transitionCount = 0;
91
92 QString _currentStateName;
93 QList<QMetaObject::Connection> _stateConnections;
94};
QGroundControl specific state machine with enhanced error handling.
QString summary() const
Get a human-readable summary.
qint64 totalRuntimeMs() const
Get total machine runtime.
void logProfile() const
Log the profile to debug output.
QJsonObject toJson() const
Export profile data as JSON.
int transitionCount() const
Get the number of state transitions.
QHash< QString, StateProfile > allProfiles() const
Get profile data for all states.
void reset()
Reset all profiling data.
StateProfile profile(const QString &stateName) const
Get profile data for a specific state.
~StateMachineProfiler() override=default
void setEnabled(bool enabled)
Enable or disable profiling.