ManaPlus
perfstat.cpp
Go to the documentation of this file.
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 
31 
32 size_t perfFrameId = 0;
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  }
75  perfFrameId ++;
77  {
78  perfFrameId = 0;
80  }
82  }
83 
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],
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 } // namespace Perf
#define CAST_S32
Definition: cast.h:30
void log(const char *const log_text,...)
Definition: logger.cpp:269
static const size_t PERFSTAT_MAX
Definition: perfstat.h:27
static const size_t PERFSTAT_FPS_STAT
Definition: perfstat.h:29
static const size_t PERFSTAT_LAST_STAT
Definition: perfstat.h:28
Logger * logger
Definition: logger.cpp:89
int getWorstTime(const size_t counterId)
Definition: perfstat.cpp:122
int getTime(const size_t frameId, const size_t counterId)
Definition: perfstat.cpp:111
void nextFrame()
Definition: perfstat.cpp:62
void selectWorstFrame()
Definition: perfstat.cpp:84
void init()
Definition: perfstat.cpp:41
int worstTime
Definition: perfstat.cpp:36
PerfStats worstFrameStats
Definition: perfstat.cpp:35
size_t prevPerfFrameId
Definition: perfstat.cpp:33
int * perfFrame
Definition: perfstat.cpp:34
int skipPerfFrames
Definition: perfstat.cpp:37
size_t perfFrameId
Definition: perfstat.cpp:32
PerfStats perfStats[PERFSTAT_MAX]
Definition: perfstat.cpp:30
int ticks[16]
Definition: perfstat.h:39