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

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