File: stats.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (620 lines) | stat: -rw-r--r-- 21,071 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
#include "stats.hpp"

#include <algorithm>
#include <iomanip>
#include <span>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>

#include <osg/PolygonMode>

#include <osgText/Font>
#include <osgText/Text>

#include <osgDB/Registry>

#include <osgViewer/Renderer>
#include <osgViewer/Viewer>

#include <components/vfs/manager.hpp>

#include "cachestats.hpp"

namespace Resource
{
    namespace
    {
        constexpr float statsWidth = 1280.0f;
        constexpr float statsHeight = 1024.0f;
        constexpr float characterSize = 17.0f;
        constexpr float backgroundMargin = 5;
        constexpr float backgroundSpacing = 3;
        constexpr float maxStatsHeight = 420.0f;
        constexpr std::size_t pageSize
            = static_cast<std::size_t>((maxStatsHeight - 2 * backgroundMargin) / characterSize);
        constexpr int statsHandlerKey = osgGA::GUIEventAdapter::KEY_F4;
        const VFS::Path::Normalized fontName("Fonts/DejaVuLGCSansMono.ttf");

        bool collectStatRendering = false;
        bool collectStatCameraObjects = false;
        bool collectStatViewerObjects = false;
        bool collectStatResource = false;
        bool collectStatGPU = false;
        bool collectStatEvent = false;
        bool collectStatFrameRate = false;
        bool collectStatUpdate = false;
        bool collectStatEngine = false;

        std::vector<std::string> generateAllStatNames()
        {
            constexpr std::size_t itemsPerPage = 24;

            constexpr std::string_view firstPage[] = {
                "FrameNumber",
                "",
                "Loading",
                "Compiling",
                "WorkQueue",
                "WorkThread",
                "UnrefQueue",
                "",
                "Texture",
                "StateSet",
                "Composite",
                "",
                "Mechanics Actors",
                "Mechanics Objects",
                "",
                "Physics Actors",
                "Physics Objects",
                "Physics Projectiles",
                "Physics HeightFields",
                "",
                "Lua UsedMemory",
                "",
                "",
                "",
            };

            static_assert(std::size(firstPage) == itemsPerPage);

            constexpr std::string_view caches[] = {
                "Node",
                "Shape",
                "Shape Instance",
                "Image",
                "Nif",
                "Keyframe",
                "BSShader Material",
                "Groundcover Chunk",
                "Object Chunk",
                "Terrain Chunk",
                "Terrain Texture",
                "Land",
                "Blending Rules",
            };

            constexpr std::string_view cellPreloader[] = {
                "CellPreloader Count",
                "CellPreloader Added",
                "CellPreloader Evicted",
                "CellPreloader Loaded",
                "CellPreloader Expired",
            };

            constexpr std::string_view navMesh[] = {
                "NavMesh Jobs",
                "NavMesh Removing",
                "NavMesh Updating",
                "NavMesh Delayed",
                "NavMesh Pushed",
                "NavMesh Processing",
                "NavMesh DbJobs Write",
                "NavMesh DbJobs Read",
                "NavMesh DbCache Get",
                "NavMesh DbCache Hit",
                "NavMesh CacheSize",
                "NavMesh UsedTiles",
                "NavMesh CachedTiles",
                "NavMesh Cache Get",
                "NavMesh Cache Hit",
                "NavMesh Recast Tiles",
                "NavMesh Recast Objects",
                "NavMesh Recast Heightfields",
                "NavMesh Recast Water",
            };

            std::vector<std::string> statNames;

            for (std::string_view name : firstPage)
                statNames.emplace_back(name);

            for (std::size_t i = 0; i < std::size(caches); ++i)
            {
                Resource::addCacheStatsAttibutes(caches[i], statNames);
                if ((i + 1) % 5 != 0)
                    statNames.emplace_back();
            }

            for (std::string_view name : cellPreloader)
                statNames.emplace_back(name);

            while (statNames.size() % itemsPerPage != 0)
                statNames.emplace_back();

            for (std::string_view name : navMesh)
                statNames.emplace_back(name);

            return statNames;
        }

        void setupStatCollection()
        {
            const char* envList = getenv("OPENMW_OSG_STATS_LIST");
            if (envList == nullptr)
                return;

            std::string_view kwList(envList);

            auto kwBegin = kwList.begin();

            while (kwBegin != kwList.end())
            {
                auto kwEnd = std::find(kwBegin, kwList.end(), ';');

                const auto kw = kwList.substr(std::distance(kwList.begin(), kwBegin), std::distance(kwBegin, kwEnd));

                if (kw == "gpu")
                    collectStatGPU = true;
                else if (kw == "event")
                    collectStatEvent = true;
                else if (kw == "frame_rate")
                    collectStatFrameRate = true;
                else if (kw == "update")
                    collectStatUpdate = true;
                else if (kw == "engine")
                    collectStatEngine = true;
                else if (kw == "rendering")
                    collectStatRendering = true;
                else if (kw == "cameraobjects")
                    collectStatCameraObjects = true;
                else if (kw == "viewerobjects")
                    collectStatViewerObjects = true;
                else if (kw == "resource")
                    collectStatResource = true;
                else if (kw == "times")
                {
                    collectStatGPU = true;
                    collectStatEvent = true;
                    collectStatFrameRate = true;
                    collectStatUpdate = true;
                    collectStatEngine = true;
                    collectStatRendering = true;
                }

                if (kwEnd == kwList.end())
                    break;

                kwBegin = std::next(kwEnd);
            }
        }

        osg::ref_ptr<osg::Geometry> createBackgroundRectangle(
            const osg::Vec3& pos, const float width, const float height, const osg::Vec4& color)
        {
            osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;

            geometry->setUseDisplayList(false);

            osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet;
            geometry->setStateSet(stateSet);

            osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
            vertices->push_back(osg::Vec3(pos.x(), pos.y(), 0));
            vertices->push_back(osg::Vec3(pos.x(), pos.y() - height, 0));
            vertices->push_back(osg::Vec3(pos.x() + width, pos.y() - height, 0));
            vertices->push_back(osg::Vec3(pos.x() + width, pos.y(), 0));
            geometry->setVertexArray(vertices);

            osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
            colors->push_back(color);
            geometry->setColorArray(colors, osg::Array::BIND_OVERALL);

            osg::ref_ptr<osg::DrawElementsUShort> base
                = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_FAN, 0);
            base->push_back(0);
            base->push_back(1);
            base->push_back(2);
            base->push_back(3);
            geometry->addPrimitiveSet(base);

            return geometry;
        }

        osg::ref_ptr<osgText::Font> getMonoFont(const VFS::Manager& vfs)
        {
            if (osgDB::Registry::instance()->getReaderWriterForExtension("ttf") && vfs.exists(fontName))
            {
                const Files::IStreamPtr streamPtr = vfs.get(fontName);
                return osgText::readRefFontStream(*streamPtr);
            }

            return nullptr;
        }

        class SetFontVisitor : public osg::NodeVisitor
        {
        public:
            SetFontVisitor(osgText::Font* font)
                : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
                , mFont(font)
            {
            }

            void apply(osg::Drawable& node) override
            {
                if (osgText::Text* text = dynamic_cast<osgText::Text*>(&node))
                {
                    text->setFont(mFont);
                }
            }

        private:
            osgText::Font* mFont;
        };
    }

    Profiler::Profiler(bool offlineCollect, const VFS::Manager& vfs)
        : mOfflineCollect(offlineCollect)
        , mTextFont(getMonoFont(vfs))
    {
        _characterSize = characterSize;
        _font.clear();

        setKeyEventTogglesOnScreenStats(osgGA::GUIEventAdapter::KEY_F3);
        setupStatCollection();
    }

    void Profiler::setUpFonts()
    {
        if (mTextFont != nullptr)
        {
            SetFontVisitor visitor(mTextFont);
            _switch->accept(visitor);
        }

        mInitFonts = true;
    }

    bool Profiler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
    {
        osgViewer::ViewerBase* viewer = nullptr;

        bool handled = StatsHandler::handle(ea, aa);
        if (_initialized && !mInitFonts)
            setUpFonts();

        auto* view = dynamic_cast<osgViewer::View*>(&aa);
        if (view)
            viewer = view->getViewerBase();

        if (viewer != nullptr)
        {
            // Add/remove openmw stats to the osd as necessary
            viewer->getViewerStats()->collectStats("engine", _statsType >= StatsHandler::StatsType::VIEWER_STATS);

            if (mOfflineCollect)
                collectStatistics(*viewer);
        }
        return handled;
    }

    StatsHandler::StatsHandler(bool offlineCollect, const VFS::Manager& vfs)
        : mOfflineCollect(offlineCollect)
        , mSwitch(new osg::Switch)
        , mCamera(new osg::Camera)
        , mTextFont(getMonoFont(vfs))
        , mStatNames(generateAllStatNames())
    {
        osg::ref_ptr<osg::StateSet> stateset = mSwitch->getOrCreateStateSet();
        stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
        stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
#ifdef OSG_GL1_AVAILABLE
        stateset->setAttribute(new osg::PolygonMode(), osg::StateAttribute::PROTECTED);
#endif

        mCamera->getOrCreateStateSet()->setGlobalDefaults();
        mCamera->setRenderer(new osgViewer::Renderer(mCamera.get()));
        mCamera->setProjectionResizePolicy(osg::Camera::FIXED);
        mCamera->addChild(mSwitch);
    }

    bool StatsHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
    {
        if (ea.getHandled())
            return false;

        switch (ea.getEventType())
        {
            case (osgGA::GUIEventAdapter::KEYDOWN):
            {
                if (ea.getKey() == statsHandlerKey)
                {
                    osgViewer::View* const view = dynamic_cast<osgViewer::View*>(&aa);
                    if (view == nullptr)
                        return false;

                    osgViewer::ViewerBase* const viewer = view->getViewerBase();

                    if (viewer == nullptr)
                        return false;

                    toggle(*viewer);

                    if (mOfflineCollect)
                        collectStatistics(*viewer);

                    aa.requestRedraw();
                    return true;
                }
                break;
            }
            case osgGA::GUIEventAdapter::RESIZE:
            {
                setWindowSize(ea.getWindowWidth(), ea.getWindowHeight());
                break;
            }
            default:
                break;
        }
        return false;
    }

    void StatsHandler::setWindowSize(int width, int height)
    {
        if (width <= 0 || height <= 0)
            return;

        mCamera->setViewport(0, 0, width, height);
        if (std::abs(height * statsWidth) <= std::abs(width * statsHeight))
        {
            mCamera->setProjectionMatrix(
                osg::Matrix::ortho2D(statsWidth - width * statsHeight / height, statsWidth, 0.0, statsHeight));
        }
        else
        {
            mCamera->setProjectionMatrix(
                osg::Matrix::ortho2D(0.0, statsWidth, statsHeight - height * statsWidth / width, statsHeight));
        }
    }

    void StatsHandler::toggle(osgViewer::ViewerBase& viewer)
    {
        if (!mInitialized)
        {
            setUpHUDCamera(viewer);
            setUpScene(viewer);
            mInitialized = true;
        }

        if (mPage == mSwitch->getNumChildren())
        {
            mPage = 0;

            mCamera->setNodeMask(0);
            mSwitch->setAllChildrenOff();

            viewer.getViewerStats()->collectStats("resource", false);
        }
        else
        {
            mCamera->setNodeMask(0xffffffff);
            mSwitch->setSingleChildOn(mPage);

            viewer.getViewerStats()->collectStats("resource", true);

            ++mPage;
        }
    }

    void StatsHandler::setUpHUDCamera(osgViewer::ViewerBase& viewer)
    {
        // Try GraphicsWindow first so we're likely to get the main viewer window
        osg::GraphicsContext* context = dynamic_cast<osgViewer::GraphicsWindow*>(mCamera->getGraphicsContext());

        if (!context)
        {
            osgViewer::Viewer::Windows windows;
            viewer.getWindows(windows);

            if (!windows.empty())
                context = windows.front();
            else
            {
                // No GraphicsWindows were found, so let's try to find a GraphicsContext
                context = mCamera->getGraphicsContext();

                if (!context)
                {
                    osgViewer::Viewer::Contexts contexts;
                    viewer.getContexts(contexts);

                    if (contexts.empty())
                        return;

                    context = contexts.front();
                }
            }
        }

        mCamera->setGraphicsContext(context);

        mCamera->setRenderOrder(osg::Camera::POST_RENDER, 11);

        mCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        mCamera->setViewMatrix(osg::Matrix::identity());
        setWindowSize(context->getTraits()->width, context->getTraits()->height);

        // only clear the depth buffer
        mCamera->setClearMask(0);
        mCamera->setAllowEventFocus(false);

        mCamera->setRenderer(new osgViewer::Renderer(mCamera.get()));
    }

    namespace
    {
        class ResourceStatsTextDrawCallback : public osg::Drawable::DrawCallback
        {
        public:
            explicit ResourceStatsTextDrawCallback(osg::Stats* stats, std::span<const std::string> statNames)
                : mStats(stats)
                , mStatNames(statNames)
            {
            }

            void drawImplementation(osg::RenderInfo& renderInfo, const osg::Drawable* drawable) const override
            {
                if (mStats == nullptr)
                    return;

                osgText::Text* text = (osgText::Text*)(drawable);

                std::ostringstream viewStr;
                viewStr.setf(std::ios::left, std::ios::adjustfield);
                viewStr.width(14);
                // Used fixed formatting, as scientific will switch to "...e+.." notation for
                // large numbers of vertices/drawables/etc.
                viewStr.setf(std::ios::fixed);
                viewStr.precision(0);

                const unsigned int frameNumber = renderInfo.getState()->getFrameStamp()->getFrameNumber() - 1;

                for (const std::string& statName : mStatNames)
                {
                    if (statName.empty())
                        viewStr << std::endl;
                    else
                    {
                        double value = 0.0;
                        if (mStats->getAttribute(frameNumber, statName, value))
                            viewStr << std::setw(8) << value << std::endl;
                        else
                            viewStr << std::setw(8) << "." << std::endl;
                    }
                }

                text->setText(viewStr.str());

                text->drawImplementation(renderInfo);
            }

        private:
            osg::ref_ptr<osg::Stats> mStats;
            std::span<const std::string> mStatNames;
        };
    }

    void StatsHandler::setUpScene(osgViewer::ViewerBase& viewer)
    {
        const osg::Vec4 backgroundColor(0.0, 0.0, 0.0f, 0.3);
        const osg::Vec4 staticTextColor(1.0, 1.0, 0.0f, 1.0);
        const osg::Vec4 dynamicTextColor(1.0, 1.0, 1.0f, 1.0);

        const auto longest = std::max_element(mStatNames.begin(), mStatNames.end(),
            [](const std::string& lhs, const std::string& rhs) { return lhs.size() < rhs.size(); });
        const std::size_t longestSize = longest->size();
        const float statNamesWidth = longestSize * characterSize * 0.6 + 2 * backgroundMargin;
        const float statTextWidth = 7 * characterSize + 2 * backgroundMargin;
        const float statHeight = pageSize * characterSize + 2 * backgroundMargin;
        const float width = statNamesWidth + backgroundSpacing + statTextWidth;

        for (std::size_t offset = 0; offset < mStatNames.size(); offset += pageSize)
        {
            osg::ref_ptr<osg::Group> group = new osg::Group;

            group->setCullingActive(false);

            const std::size_t count = std::min(mStatNames.size() - offset, pageSize);
            std::span<const std::string> currentStatNames(mStatNames.data() + offset, count);
            osg::Vec3 pos(statsWidth - width, statHeight - characterSize, 0.0f);

            group->addChild(
                createBackgroundRectangle(pos + osg::Vec3(-backgroundMargin, backgroundMargin + characterSize, 0),
                    statNamesWidth, statHeight, backgroundColor));

            osg::ref_ptr<osgText::Text> staticText = new osgText::Text;
            group->addChild(staticText.get());
            staticText->setColor(staticTextColor);
            staticText->setCharacterSize(characterSize);
            staticText->setPosition(pos);

            std::ostringstream viewStr;
            viewStr.clear();
            viewStr.setf(std::ios::left, std::ios::adjustfield);
            viewStr.width(longestSize);
            for (const std::string& statName : currentStatNames)
                viewStr << statName << std::endl;

            staticText->setText(viewStr.str());

            pos.x() += statNamesWidth + backgroundSpacing;

            group->addChild(
                createBackgroundRectangle(pos + osg::Vec3(-backgroundMargin, backgroundMargin + characterSize, 0),
                    statTextWidth, statHeight, backgroundColor));

            osg::ref_ptr<osgText::Text> statsText = new osgText::Text;
            group->addChild(statsText.get());

            statsText->setColor(dynamicTextColor);
            statsText->setCharacterSize(characterSize);
            statsText->setPosition(pos);
            statsText->setText("");
            statsText->setDrawCallback(new ResourceStatsTextDrawCallback(viewer.getViewerStats(), currentStatNames));

            if (mTextFont != nullptr)
            {
                staticText->setFont(mTextFont);
                statsText->setFont(mTextFont);
            }

            mSwitch->addChild(group, false);
        }
    }

    void StatsHandler::getUsage(osg::ApplicationUsage& usage) const
    {
        usage.addKeyboardMouseBinding(statsHandlerKey, "On screen resource usage stats.");
    }

    void collectStatistics(osgViewer::ViewerBase& viewer)
    {
        osgViewer::Viewer::Cameras cameras;
        viewer.getCameras(cameras);
        for (auto* camera : cameras)
        {
            if (collectStatGPU)
                camera->getStats()->collectStats("gpu", true);
            if (collectStatRendering)
                camera->getStats()->collectStats("rendering", true);
            if (collectStatCameraObjects)
                camera->getStats()->collectStats("scene", true);
        }
        if (collectStatEvent)
            viewer.getViewerStats()->collectStats("event", true);
        if (collectStatFrameRate)
            viewer.getViewerStats()->collectStats("frame_rate", true);
        if (collectStatUpdate)
            viewer.getViewerStats()->collectStats("update", true);
        if (collectStatResource)
            viewer.getViewerStats()->collectStats("resource", true);
        if (collectStatViewerObjects)
            viewer.getViewerStats()->collectStats("scene", true);
        if (collectStatEngine)
            viewer.getViewerStats()->collectStats("engine", true);
    }
}