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
28class StateMachineProfiler : public QObject
29{
30 Q_OBJECT
31
32public:
33 struct StateProfile {
34 QString name;
35 int entryCount = 0;
36 qint64 totalTimeMs = 0;
37 qint64 minTimeMs = std::numeric_limits<qint64>::max();
38 qint64 maxTimeMs = 0;
39 qint64 lastEntryTime = 0;
40
41 double averageTimeMs() const {
42 return entryCount > 0 ? static_cast<double>(totalTimeMs) / entryCount : 0.0;
43 }
44 };
45
46 explicit StateMachineProfiler(QGCStateMachine* machine);
47 ~StateMachineProfiler() override = default;
48
50 void setEnabled(bool enabled);
51 bool isEnabled() const { return _enabled; }
52
54 void reset();
55
57 StateProfile profile(const QString& stateName) const;
58
60 QHash<QString, StateProfile> allProfiles() const { return _profiles; }
61
63 qint64 totalRuntimeMs() const { return _totalRuntimeMs; }
64
66 int transitionCount() const { return _transitionCount; }
67
69 QString summary() const;
70
72 QJsonObject toJson() const;
73
75 void logProfile() const;
76
77private slots:
78 void _onMachineStarted();
79 void _onMachineStopped();
80 void _onStateEntered();
81 void _onStateExited();
82
83private:
84 QGCStateMachine* _machine = nullptr;
85 bool _enabled = false;
86
87 QHash<QString, StateProfile> _profiles;
88 QElapsedTimer _machineTimer;
89 QElapsedTimer _stateTimer;
90 qint64 _totalRuntimeMs = 0;
91 int _transitionCount = 0;
92
93 QString _currentStateName;
94 QList<QMetaObject::Connection> _stateConnections;
95};
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.
StateMachineProfiler(QGCStateMachine *machine)
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.