1 // SGPerfMon.cxx -- Performance Monitoring
3 // Written by Thorsten Brehm, started November 2011.
5 // Copyright (C) 2011 Thorsten Brehm - brehmt (at) gmail com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # include <simgear_config.h>
26 #include "SGPerfMon.hxx"
27 #include <simgear/structure/SGSmplstat.hxx>
33 #include <simgear/constants.h>
34 #include <simgear/sg_inlines.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/math/sg_geodesy.hxx>
37 #include <simgear/structure/exception.hxx>
41 SGPerformanceMonitor::SGPerformanceMonitor(SGSubsystemMgr* subSysMgr, SGPropertyNode_ptr root) :
46 _subSysMgr = subSysMgr;
50 SGPerformanceMonitor::bind(void)
52 _statiticsSubsystems = _root->getChild("subsystems", 0, true);
53 _statisticsFlag = _root->getChild("enabled", 0, true);
54 _statisticsInterval = _root->getChild("interval-s", 0, true);
58 SGPerformanceMonitor::unbind(void)
60 _statiticsSubsystems = 0;
62 _statisticsInterval = 0;
66 SGPerformanceMonitor::init(void)
72 SGPerformanceMonitor::update(double dt)
74 if (_isEnabled != _statisticsFlag->getBoolValue())
76 // flag has changed, update subsystem manager
77 _isEnabled = _statisticsFlag->getBoolValue();
79 _subSysMgr->setReportTimingCb(this,&subSystemMgrHook);
81 _subSysMgr->setReportTimingCb(this,0);
87 if (_lastUpdate.elapsedMSec() > 1000 * _statisticsInterval->getDoubleValue())
90 // grab timing statistics
91 _subSysMgr->reportTiming();
96 /** Callback hooked into the subsystem manager. */
98 SGPerformanceMonitor::subSystemMgrHook(void* userData, const std::string& name, SampleStatistic* timeStat)
100 ((SGPerformanceMonitor*) userData)->reportTiming(name, timeStat);
103 /** Grabs and exposes timing information to properties */
105 SGPerformanceMonitor::reportTiming(const string& name, SampleStatistic* timeStat)
107 SGPropertyNode* node = _statiticsSubsystems->getChild("subsystem",_count++,true);
109 double minMs = timeStat->min() / 1000;
110 double maxMs = timeStat->max() / 1000;
111 double meanMs = timeStat->mean() / 1000;
112 double stdDevMs = timeStat->stdDev() / 1000;
113 double totalMs = timeStat->total() / 1000;
114 double cumulativeMs = timeStat->cumulative() / 1000;
115 int samples = timeStat->samples();
117 node->setStringValue("name", name);
118 node->setDoubleValue("min-ms", minMs);
119 node->setDoubleValue("max-ms", maxMs);
120 node->setDoubleValue("mean-ms", meanMs);
121 node->setDoubleValue("stddev-ms", stdDevMs);
122 node->setDoubleValue("total-ms", totalMs);
123 node->setDoubleValue("cumulative-ms", cumulativeMs);
124 node->setDoubleValue("count",samples);