[xplane] Add monitoring of the number of track miles lost due to low FPS

This commit is contained in:
Mat Sutcliffe
2020-01-23 18:42:41 +00:00
parent 3a73933cf1
commit 377b2f6a3e
10 changed files with 52 additions and 16 deletions

View File

@@ -261,6 +261,7 @@ namespace BlackCore
this->clearAllRemoteAircraftData(); // reset
m_averageFps = -1.0;
m_simTimeRatio = 1.0;
m_trackMilesShort = 0.0;
}
bool ISimulator::isUpdateAllRemoteAircraft(qint64 currentTimestamp) const
@@ -1037,6 +1038,7 @@ namespace BlackCore
{
m_averageFps = -1.0;
m_simTimeRatio = 1.0;
m_trackMilesShort = 0.0;
return true;
}

View File

@@ -221,6 +221,9 @@ namespace BlackCore
//! Ratio of simulation time to real time, due to low FPS
double getSimTimeRatio() const { return m_simTimeRatio; }
//! Number of track miles over-reported, due to low FPS
double getTrackMilesShort() const { return m_trackMilesShort; }
//! Send situation/parts for testing
virtual bool testSendSituationAndParts(const BlackMisc::Aviation::CCallsign &callsign, const BlackMisc::Aviation::CAircraftSituation &situation, const BlackMisc::Aviation::CAircraftParts &parts) = 0;
@@ -583,6 +586,7 @@ namespace BlackCore
double m_statsUpdateAircraftTimeAvgMs = 0; //!< statistics average update time
double m_averageFps = -1.0; //!< FPS
double m_simTimeRatio = 1.0; //!< ratio of simulation time to real time, due to low FPS (X-Plane)
double m_trackMilesShort = 0.0; //!< difference between real and reported groundspeed, multiplied by time
qint64 m_updateAllRemoteAircraftUntil = 0; //!< force an update of all remote aircraft, used when own aircraft is moved, paused to make sure all remote aircraft are updated
qint64 m_statsUpdateAircraftTimeTotalMs = 0; //!< statistics total update time
qint64 m_statsCurrentUpdateTimeMs = 0; //!< statistics current update time

View File

@@ -153,7 +153,10 @@ namespace BlackGui
this->addOrUpdateLiveDataByName(QStringLiteral("FPS"), fps < 0 ? QStringLiteral("N/A") : QString::number(fps, 'f', 1), CIconList::allIcons().findByIndex(CIcons::ApplicationSimulator));
const double ratio = sGui->getISimulator()->getSimTimeRatio();
this->addOrUpdateLiveDataByName(QStringLiteral("Time Ratio"), QString::number(ratio), CIconList::allIcons().findByIndex(CIcons::ApplicationSimulator));
this->addOrUpdateLiveDataByName(QStringLiteral("Time Ratio"), QString::number(ratio, 'f', 2), CIconList::allIcons().findByIndex(CIcons::ApplicationSimulator));
const double miles = sGui->getISimulator()->getTrackMilesShort();
this->addOrUpdateLiveDataByName(QStringLiteral("Miles Short"), QString::number(miles, 'f', 1), CIconList::allIcons().findByIndex(CIcons::ApplicationSimulator));
}
}

View File

@@ -369,7 +369,7 @@ namespace BlackSimPlugin
if ((m_slowTimerCalls % 5u) == 0u)
{
// reading FPS resets average, so we only monitor over some time
m_serviceProxy->getFrameStats(&m_averageFps, &m_simTimeRatio);
m_serviceProxy->getFrameStats(&m_averageFps, &m_simTimeRatio, &m_trackMilesShort);
}
}
}

View File

@@ -319,30 +319,32 @@ namespace BlackSimPlugin
m_dbusInterface->callDBusAsync(QLatin1String("isUsingRealTime"), setterCallback(o_isRealTime));
}
void CXSwiftBusServiceProxy::getFrameStats(double *o_averageFps, double *o_simTimeRatio) const
void CXSwiftBusServiceProxy::getFrameStats(double *o_averageFps, double *o_simTimeRatio, double *o_trackMilesLost) const
{
std::function<void(QDBusPendingCallWatcher *)> callback = [ = ](QDBusPendingCallWatcher * watcher)
{
QDBusPendingReply<double, double> reply = *watcher;
QDBusPendingReply<double, double, double> reply = *watcher;
if (!reply.isError())
{
*o_averageFps = reply.argumentAt<0>();
*o_simTimeRatio = reply.argumentAt<1>();
*o_trackMilesLost = reply.argumentAt<2>();
}
watcher->deleteLater();
};
m_dbusInterface->callDBusAsync(QLatin1String("getFrameStats"), callback)->waitForFinished();
}
void CXSwiftBusServiceProxy::getFrameStatsAsync(double *o_averageFps, double *o_simTimeRatio)
void CXSwiftBusServiceProxy::getFrameStatsAsync(double *o_averageFps, double *o_simTimeRatio, double *o_trackMilesLost)
{
std::function<void(QDBusPendingCallWatcher *)> callback = [ = ](QDBusPendingCallWatcher * watcher)
{
QDBusPendingReply<double, double> reply = *watcher;
QDBusPendingReply<double, double, double> reply = *watcher;
if (!reply.isError())
{
*o_averageFps = reply.argumentAt<0>();
*o_simTimeRatio = reply.argumentAt<1>();
*o_trackMilesLost = reply.argumentAt<2>();
}
watcher->deleteLater();
};

View File

@@ -210,8 +210,8 @@ namespace BlackSimPlugin
//! \copydoc XSwiftBus::CService::getFrameStats
//! @{
void getFrameStats(double *o_averageFps, double *o_simTimeRatio) const;
void getFrameStatsAsync(double *o_averageFps, double *o_simTimeRatio);
void getFrameStats(double *o_averageFps, double *o_simTimeRatio, double *o_trackMilesLost) const;
void getFrameStatsAsync(double *o_averageFps, double *o_simTimeRatio, double *o_trackMilesLost);
//! @}
//! \copydoc XSwiftBus::CService::getLatitudeDeg

View File

@@ -66,6 +66,7 @@ R"XML(<node>
<method name="getFrameStats">
<arg name="averageFps" type="d" direction="out"/>
<arg name="simTimeRatio" type="d" direction="out"/>
<arg name="trackMilesShort" type="d" direction="out"/>
</method>
<method name="getLatitudeDeg">
<arg type="d" direction="out"/>

View File

@@ -161,6 +161,7 @@ namespace XSwiftBus
if (m_service)
{
m_service->onAircraftModelChanged();
m_service->resetMilesLost();
}
}
@@ -169,6 +170,7 @@ namespace XSwiftBus
if (m_service)
{
m_service->updateAirportsInRange();
m_service->resetMilesLost();
}
}

View File

@@ -14,6 +14,7 @@
#include <XPLM/XPLMPlanes.h>
#include <XPLM/XPLMUtilities.h>
#include <cmath>
#include <cstring>
#include <algorithm>
@@ -28,31 +29,35 @@ namespace XSwiftBus
{
DataRef<xplane::data::sim::operation::misc::frame_rate_period> m_thisFramePeriod;
DataRef<xplane::data::sim::time::framerate_period> m_thisFramePeriodXP11;
DataRef<xplane::data::sim::flightmodel::position::groundspeed> m_groundSpeed;
std::vector<float> m_samples;
float m_total = 0;
float m_totalOverBudget = 0;
float m_totalMetersShort = 0;
size_t m_lastSampleIndex = 0;
static constexpr size_t c_maxSampleCount = 500;
static constexpr float c_framePeriodBudget = 0.05f;
FramePeriodSampler() : CDrawable(xplm_Phase_LastCockpit, false) {}
std::pair<float, float> getFrameStats()
std::tuple<float, float, float> getFrameStats()
{
if (m_total == 0) { return {}; }
const float fps = m_samples.size() / m_total;
const float ratio = 1 - m_totalOverBudget / m_total;
const float miles = m_totalMetersShort / 1852.0f;
m_total = 0;
m_totalOverBudget = 0;
m_samples.clear();
m_lastSampleIndex = 0;
return { fps, ratio };
return std::make_tuple(fps, ratio, miles);
}
protected:
virtual void draw() override // called once per frame
{
float current = m_thisFramePeriodXP11.isValid() ? m_thisFramePeriodXP11.get() : m_thisFramePeriod.get();
const float current = m_thisFramePeriodXP11.isValid() ? m_thisFramePeriodXP11.get() : m_thisFramePeriod.get();
++m_lastSampleIndex %= c_maxSampleCount;
if (m_samples.size() == c_maxSampleCount)
@@ -72,6 +77,9 @@ namespace XSwiftBus
{
m_totalOverBudget += current - c_framePeriodBudget;
}
const float metersShort = m_groundSpeed.get() * std::max(0.0f, current - c_framePeriodBudget);
m_totalMetersShort += metersShort;
}
};
@@ -110,11 +118,20 @@ namespace XSwiftBus
return XSWIFTBUS_COMMIT;
}
std::pair<double, double> CService::getFrameStats()
std::tuple<double, double, double> CService::getFrameStats()
{
if (!m_framePeriodSampler) { return {}; }
const auto result = m_framePeriodSampler->getFrameStats();
return { static_cast<double>(result.first), static_cast<double>(result.second) };
return std::make_tuple(static_cast<double>(std::get<0>(result)), static_cast<double>(std::get<1>(result)), static_cast<double>(std::get<2>(result)));
}
void CService::resetMilesLost()
{
if (m_framePeriodSampler)
{
m_framePeriodSampler->m_totalMetersShort = 0;
m_framePeriodSampler->m_totalSecondsLate = 0;
}
}
void CService::addTextMessage(const std::string &text, double red, double green, double blue)
@@ -570,8 +587,9 @@ namespace XSwiftBus
const auto stats = getFrameStats();
CDBusMessage reply = CDBusMessage::createReply(sender, serial);
reply.beginArgumentWrite();
reply.appendArgument(stats.first);
reply.appendArgument(stats.second);
reply.appendArgument(std::get<0>(stats));
reply.appendArgument(std::get<1>(stats));
reply.appendArgument(std::get<2>(stats));
sendDBusMessage(reply);
});
}

View File

@@ -113,8 +113,12 @@ namespace XSwiftBus
//! Frames-per-second, averaged over the last 500 frames,
//! or since this function was last called, whichever is later.
//! Second part is the average simulation time ratio during the same period.
//! Third part is the total track miles over-reported during the same period.
//! \return Zero if no samples were collected since this function was last called.
std::pair<double, double> getFrameStats();
std::tuple<double, double, double> getFrameStats();
//! Reset the monitoring of total track miles lost due to low frame rate.
void resetMilesLost();
//! Get aircraft latitude in degrees
double getLatitudeDeg() const { return m_latitude.get(); }