]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGPerfMon.cxx
Working 'noshadow' animation
[simgear.git] / simgear / structure / SGPerfMon.cxx
1 // SGPerfMon.cxx -- Performance Monitoring
2 //
3 // Written by Thorsten Brehm, started November 2011.
4 //
5 // Copyright (C) 2011 Thorsten Brehm - brehmt (at) gmail com
6 //
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.
11 //
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.
16 //
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21
22 #ifdef HAVE_CONFIG_H
23 #  include <simgear_config.h>
24 #endif
25
26 #include "SGPerfMon.hxx"
27 #include <simgear/structure/SGSmplstat.hxx>
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <string>
32
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>
38
39 using std::string;
40
41 SGPerformanceMonitor::SGPerformanceMonitor(SGSubsystemMgr* subSysMgr, SGPropertyNode_ptr root) :
42     _isEnabled(false),
43     _count(0)
44 {
45     _root = root;
46     _subSysMgr = subSysMgr;
47 }
48
49 void
50 SGPerformanceMonitor::bind(void)
51 {
52     _statiticsSubsystems = _root->getChild("subsystems",    0, true);
53     _statisticsFlag      = _root->getChild("enabled",       0, true);
54     _statisticsInterval  = _root->getChild("interval-s",    0, true);
55 }
56
57 void
58 SGPerformanceMonitor::unbind(void)
59 {
60     _statiticsSubsystems = 0;
61     _statisticsFlag = 0;
62     _statisticsInterval = 0;
63 }
64
65 void
66 SGPerformanceMonitor::init(void)
67 {
68
69 }
70
71 void
72 SGPerformanceMonitor::update(double dt)
73 {
74     if (_isEnabled != _statisticsFlag->getBoolValue())
75     {
76         // flag has changed, update subsystem manager
77         _isEnabled = _statisticsFlag->getBoolValue();
78         if (_isEnabled)
79         {
80             _subSysMgr->setReportTimingCb(this,&subSystemMgrHook);
81             _lastUpdate.stamp();
82         }
83         else
84             _subSysMgr->setReportTimingCb(this,0);
85     }
86
87     if (!_isEnabled)
88         return;
89
90     if (_lastUpdate.elapsedMSec() > 1000 * _statisticsInterval->getDoubleValue())
91     {
92         _count = 0;
93         // grab timing statistics
94         _subSysMgr->reportTiming();
95         _lastUpdate.stamp();
96     }
97 }
98
99 /** Callback hooked into the subsystem manager. */
100 void
101 SGPerformanceMonitor::subSystemMgrHook(void* userData, const std::string& name, SampleStatistic* timeStat)
102 {
103     ((SGPerformanceMonitor*) userData)->reportTiming(name, timeStat);
104 }
105
106 /** Grabs and exposes timing information to properties */
107 void
108 SGPerformanceMonitor::reportTiming(const string& name, SampleStatistic* timeStat)
109 {
110     SGPropertyNode* node = _statiticsSubsystems->getChild("subsystem",_count++,true);
111
112     double minMs        = timeStat->min()    / 1000;
113     double maxMs        = timeStat->max()    / 1000;
114     double meanMs       = timeStat->mean()   / 1000;
115     double stdDevMs     = timeStat->stdDev() / 1000;
116     double totalMs      = timeStat->total()  / 1000;
117     double cumulativeMs = timeStat->cumulative() / 1000;
118     int    samples      = timeStat->samples();
119
120     node->setStringValue("name", name);
121     node->setDoubleValue("min-ms", minMs);
122     node->setDoubleValue("max-ms", maxMs);
123     node->setDoubleValue("mean-ms", meanMs);
124     node->setDoubleValue("stddev-ms", stdDevMs);
125     node->setDoubleValue("total-ms", totalMs);
126     node->setDoubleValue("cumulative-ms", cumulativeMs);
127     node->setDoubleValue("count",samples);
128
129     timeStat->reset();
130 }