Browse Source

Log profile data on exit.

chirpmodem-v8.1.0
Jon Beniston 3 months ago
parent
commit
394224ed42
  1. 75
      sdrbase/util/profiler.cpp
  2. 7
      sdrbase/util/profiler.h
  3. 6
      sdrgui/mainwindow.cpp
  4. 4
      sdrsrv/mainserver.cpp

75
sdrbase/util/profiler.cpp

@ -18,15 +18,39 @@ @@ -18,15 +18,39 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <QDebug>
#include "profiler.h"
QHash<QString, ProfileData> 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<QString, ProfileData>& 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() @@ -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<QString, ProfileData>& 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<QString> 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();
}

7
sdrbase/util/profiler.h

@ -117,7 +117,12 @@ public: @@ -117,7 +117,12 @@ public:
static QHash<QString, ProfileData>& 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:

6
sdrgui/mainwindow.cpp

@ -84,6 +84,7 @@ @@ -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() @@ -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 @@ -3468,9 +3472,9 @@ void MainWindow::startStop(const Workspace *workspace, bool start, bool includeF
ChannelWebAPIUtils::run(deviceIndex);
} else {
ChannelWebAPIUtils::stop(deviceIndex);
}
}
}
}
if (includeFeatures)
{

4
sdrsrv/mainserver.cpp

@ -42,6 +42,7 @@ @@ -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 @@ -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();
}

Loading…
Cancel
Save