GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/perfstat.cpp Lines: 3 55 5.5 %
Date: 2021-03-17 Branches: 2 28 7.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2018-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "utils/perfstat.h"
23
24
#include "utils/cast.h"
25
26
#include "logger.h"
27
28
#include "debug.h"
29
30
241
PerfStats perfStats[PERFSTAT_MAX];
31
32
size_t perfFrameId = 0;
33
size_t prevPerfFrameId = PERFSTAT_MAX;
34
1
int *perfFrame = &perfStats[perfFrameId].ticks[0];
35
PerfStats worstFrameStats;
36
int worstTime = -1;
37
int skipPerfFrames = -1;
38
39
namespace Perf
40
{
41
    void init()
42
    {
43
        logger->log("perf stats init");
44
        worstTime = -1;
45
        perfFrameId = 0;
46
        prevPerfFrameId = 0;
47
        for (size_t f = 0; f < PERFSTAT_LAST_STAT; f ++)
48
        {
49
            worstFrameStats.ticks[f] = 0;
50
        }
51
        for (size_t f = 0; f < PERFSTAT_MAX; f ++)
52
        {
53
            PerfStats &perf = perfStats[f];
54
            for (size_t d = 0; d < PERFSTAT_LAST_STAT; d ++)
55
            {
56
                perf.ticks[d] = 0;
57
            }
58
        }
59
        skipPerfFrames = 0;
60
    }
61
62
    void nextFrame()
63
    {
64
        if (skipPerfFrames > 0)
65
        {
66
            skipPerfFrames --;
67
//            logger->log("skip frames: %d", skipPerfFrames);
68
            return;
69
        }
70
        else if (skipPerfFrames < 0)
71
        {
72
            return;
73
        }
74
        prevPerfFrameId = perfFrameId;
75
        perfFrameId ++;
76
        if (perfFrameId >= PERFSTAT_MAX)
77
        {
78
            perfFrameId = 0;
79
            selectWorstFrame();
80
        }
81
        perfFrame = &perfStats[perfFrameId].ticks[0];
82
    }
83
84
    void selectWorstFrame()
85
    {
86
        int time = worstTime;
87
        int index = -1;
88
        for (size_t f = 0; f < PERFSTAT_MAX; f ++)
89
        {
90
            if (f == perfFrameId)
91
                continue;
92
            const int time1 = Perf::getTime(f, PERFSTAT_FPS_STAT - 1);
93
            if (time1 > time)
94
            {
95
                time = time1;
96
                index = CAST_S32(f);
97
            }
98
        }
99
        if (index >= 0)
100
        {
101
            worstFrameStats = perfStats[index];
102
            logger->log("worst frame: %d, %d",
103
                perfStats[index].ticks[PERFSTAT_FPS_STAT - 1] -
104
                perfStats[index].ticks[0],
105
                worstFrameStats.ticks[PERFSTAT_FPS_STAT - 1] -
106
                worstFrameStats.ticks[0]);
107
            worstTime = time;
108
        }
109
    }
110
111
    int getTime(const size_t frameId,
112
                const size_t counterId)
113
    {
114
        const PerfStats &perf = perfStats[frameId];
115
        const int val1 = perf.ticks[0];
116
        const int val2 = perf.ticks[counterId];
117
        if (val2 >= val1)
118
            return val2 - val1;
119
        return val1 - val2;
120
    }
121
122
    int getWorstTime(const size_t counterId)
123
    {
124
        const int val1 = worstFrameStats.ticks[0];
125
        const int val2 = worstFrameStats.ticks[counterId];
126
        if (val2 >= val1)
127
            return val2 - val1;
128
        return val1 - val2;
129
    }
130
2
}  // namespace Perf