From 394224ed4235437fcb6073febfc52f0beb2e5717 Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Fri, 28 Aug 2026 22:03:05 +0100 Subject: [PATCH] Log profile data on exit. --- sdrbase/util/profiler.cpp | 75 +++++++++++++++++++++++++++++++++++++++ sdrbase/util/profiler.h | 7 +++- sdrgui/mainwindow.cpp | 6 +++- sdrsrv/mainserver.cpp | 4 +++ 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/sdrbase/util/profiler.cpp b/sdrbase/util/profiler.cpp index 081532ce7..2ffc1fa3e 100644 --- a/sdrbase/util/profiler.cpp +++ b/sdrbase/util/profiler.cpp @@ -18,15 +18,39 @@ // along with this program. If not, see . // ////////////////////////////////////////////////////////////////////////////////// +#include + +#include + #include "profiler.h" QHash GlobalProfileData::m_profileData; QMutex GlobalProfileData::m_mutex; QElapsedTimer GlobalProfileData::m_startTimer; +// Format a time in nanoseconds as the Profile dialog does +static QString formatNanoSecs(double timeInNanoSec) +{ + if (timeInNanoSec < 1e3) { + return QString("%1 ns").arg(timeInNanoSec, 0, 'f', 3); + } else if (timeInNanoSec < 1e6) { + return QString("%1 us").arg(timeInNanoSec/1e3, 0, 'f', 3); + } else if (timeInNanoSec < 1e9) { + return QString("%1 ms").arg(timeInNanoSec/1e6, 0, 'f', 3); + } else { + return QString("%1 s").arg(timeInNanoSec/1e9, 0, 'f', 3); + } +} + QHash& GlobalProfileData::getProfileData() { m_mutex.lock(); + // The timer is only otherwise started by resetProfileData, so without this the + // elapsed time - and therefore the percentages - would be meaningless unless + // the profile data happened to have been reset + if (!m_startTimer.isValid()) { + m_startTimer.start(); + } return m_profileData; } @@ -42,3 +66,54 @@ void GlobalProfileData::resetProfileData() m_startTimer.start(); m_mutex.unlock(); } + +qint64 GlobalProfileData::getMSSinceStart() +{ + m_mutex.lock(); + qint64 elapsed = m_startTimer.isValid() ? m_startTimer.elapsed() : 0; + m_mutex.unlock(); + return elapsed; +} + +void GlobalProfileData::logProfileData() +{ + QHash& profileData = getProfileData(); + + if (profileData.isEmpty()) + { + // Either the profiler wasn't enabled at build time, or nothing profiled ran + releaseProfileData(); + return; + } + + // Sort by total time, so the most expensive code is first + QList names = profileData.keys(); + std::sort(names.begin(), names.end(), [&profileData] (const QString& a, const QString& b) { + return profileData[a].getTotal() > profileData[b].getTotal(); + }); + + const qint64 msecSinceStart = m_startTimer.isValid() ? m_startTimer.elapsed() : 0; + + qInfo() << "Profile data over" << formatNanoSecs(msecSinceStart * 1e6) + << "- percentages are of that elapsed time, so they can exceed 100% in total" + << "when code runs on more than one thread"; + qInfo("%-40s %14s %8s %16s %14s %14s %12s", + "Name", "Total", "%", "Average", "Max", "Last", "Samples"); + + for (const auto& name : names) + { + const ProfileData& data = profileData[name]; + double percent = (msecSinceStart > 0) ? data.getTotal() / (msecSinceStart * 1e6) * 100.0 : 0.0; + + qInfo("%-40s %14s %7.2f%% %16s %14s %14s %12llu", + qPrintable(name), + qPrintable(formatNanoSecs(data.getTotal())), + percent, + qPrintable(formatNanoSecs(data.getAverage())), + qPrintable(formatNanoSecs(data.getMax())), + qPrintable(formatNanoSecs(data.getLast())), + (unsigned long long) data.getNumSamples()); + } + + releaseProfileData(); +} diff --git a/sdrbase/util/profiler.h b/sdrbase/util/profiler.h index 8bdcd9b7a..8d03e2464 100644 --- a/sdrbase/util/profiler.h +++ b/sdrbase/util/profiler.h @@ -117,7 +117,12 @@ public: static QHash& getProfileData(); static void releaseProfileData(); static void resetProfileData(); - static qint64 getMSSinceStart() { return m_startTimer.elapsed(); } + static qint64 getMSSinceStart(); + + // Write the collected profile data to the log. Called on exit, so profile runs + // can be compared without having to keep the Profile dialog open, and so it + // works in the server, which has no GUI + static void logProfileData(); private: diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index ba1acc3b2..03db1b3b5 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -84,6 +84,7 @@ #include "gui/dialogpositioner.h" #include "gui/welcomedialog.h" #include "gui/profiledialog.h" +#include "util/profiler.h" #include "dsp/dspengine.h" #include "dsp/spectrumvis.h" #include "dsp/dspcommands.h" @@ -292,6 +293,9 @@ MainWindow::~MainWindow() { qDebug() << "MainWindow::~MainWindow"; + // Log before tearing anything down, so the data survives a problem during shutdown + GlobalProfileData::logProfileData(); + m_statusTimer.stop(); m_apiServer->stop(); delete m_apiServer; @@ -3468,9 +3472,9 @@ void MainWindow::startStop(const Workspace *workspace, bool start, bool includeF ChannelWebAPIUtils::run(deviceIndex); } else { ChannelWebAPIUtils::stop(deviceIndex); + } } } -} if (includeFeatures) { diff --git a/sdrsrv/mainserver.cpp b/sdrsrv/mainserver.cpp index ffebf660c..24514a1a5 100644 --- a/sdrsrv/mainserver.cpp +++ b/sdrsrv/mainserver.cpp @@ -42,6 +42,7 @@ #include "mainparser.h" #include "mainserver.h" +#include "util/profiler.h" MainServer *MainServer::m_instance = nullptr; @@ -89,6 +90,9 @@ MainServer::MainServer(qtwebapp::LoggerWithFile *logger, const MainParser& parse MainServer::~MainServer() { + // Log before tearing anything down, so the data survives a problem during shutdown + GlobalProfileData::logProfileData(); + while (!m_mainCore->m_deviceSets.empty()) { removeLastDevice(); }