]> git.mxchange.org Git - simgear.git/blob - simgear/structure/SGPerfMon.cxx
Boolean uniforms are now updatable by properties
[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., 675 Mass Ave, Cambridge, MA 02139, 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/sound/soundmgr_openal.hxx>
38 #include <simgear/structure/exception.hxx>
39
40 using std::string;
41
42 SGPerformanceMonitor::SGPerformanceMonitor(SGSubsystemMgr* subSysMgr, SGPropertyNode_ptr root) :
43     _isEnabled(false),
44     _count(0)
45 {
46     _root = root;
47     _subSysMgr = subSysMgr;
48 }
49
50 void
51 SGPerformanceMonitor::bind(void)
52 {
53     _statiticsSubsystems = _root->getChild("subsystems",    0, true);
54     _statisticsFlag      = _root->getChild("enabled",       0, true);
55     _statisticsInterval  = _root->getChild("interval-s",    0, true);
56 //    _statiticsMinJitter  = _root->getChild("min-jitter-ms", 0, true);
57 //    _statiticsMinTime    = _root->getChild("min-time-ms",   0, true);
58 }
59
60 void
61 SGPerformanceMonitor::unbind(void)
62 {
63     _statiticsSubsystems = 0;
64     _statisticsFlag = 0;
65     _statisticsInterval = 0;
66 //    _statiticsMinJitter = 0;
67 //    _statiticsMinTime = 0;
68 }
69
70 void
71 SGPerformanceMonitor::init(void)
72 {
73
74 }
75
76 void
77 SGPerformanceMonitor::update(double dt)
78 {
79     if (_isEnabled != _statisticsFlag->getBoolValue())
80     {
81         // flag has changed, update subsystem manager
82         _isEnabled = _statisticsFlag->getBoolValue();
83         if (_isEnabled)
84             _subSysMgr->setReportTimingCb(this,&subSystemMgrHook);
85         else
86             _subSysMgr->setReportTimingCb(this,0);
87     }
88
89     if (!_isEnabled)
90         return;
91
92     if (_lastUpdate.elapsedMSec() > 1000 * _statisticsInterval->getDoubleValue())
93     {
94         _count = 0;
95         // grab timing statistics
96         _subSysMgr->reportTiming();
97         _lastUpdate.stamp();
98     }
99 }
100
101 /** Callback hooked into the subsystem manager. */
102 void
103 SGPerformanceMonitor::subSystemMgrHook(void* userData, const std::string& name, SampleStatistic* timeStat)
104 {
105     ((SGPerformanceMonitor*) userData)->reportTiming(name, timeStat);
106 }
107
108 /** Grabs and exposes timing information to properties */
109 void
110 SGPerformanceMonitor::reportTiming(const string& name, SampleStatistic* timeStat)
111 {
112     SGPropertyNode* node = _statiticsSubsystems->getChild("subsystem",_count++,true);
113
114     double minMs    = timeStat->min()    / 1000;
115     double maxMs    = timeStat->max()    / 1000;
116     double meanMs   = timeStat->mean()   / 1000;
117     double stdDevMs = timeStat->stdDev() / 1000;
118     double totalMs  = timeStat->total()  / 1000;
119     int    samples  = timeStat->samples();
120
121     node->setStringValue("name", name);
122     node->setDoubleValue("min-ms", minMs);
123     node->setDoubleValue("max-ms", maxMs);
124     node->setDoubleValue("mean-ms", meanMs);
125     node->setDoubleValue("stddev-ms", stdDevMs);
126     node->setDoubleValue("total-ms", totalMs);
127     node->setDoubleValue("count",samples);
128
129     timeStat->reset();
130 }