ManaPlus
perfomance.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2012-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 "localconsts.h"
23 
24 #ifdef USE_PROFILER
25 
26 #include "utils/perfomance.h"
27 
28 #include "configuration.h"
29 #include "game.h"
30 
31 #include "utils/timer.h"
32 
33 #include <algorithm>
34 #include <cstdarg>
35 #include <cstdio>
36 #include <fcntl.h>
37 #include <fstream>
38 #include <iostream>
39 
40 #include "debug.h"
41 
42 static const clockid_t clockType = CLOCK_MONOTONIC;
43 
44 #define timeData ((static_cast<long long int>(time.tv_sec) * 1000000000LL \
45  + static_cast<long long int>(time.tv_nsec)) / 1)
46 
47 namespace Perfomance
48 {
49  std::ofstream file;
50  std::string temp;
51  long long unsigned int startTime;
52 
53  void init(const std::string &path)
54  {
55  file.open(path.c_str(), std::ios_base::trunc);
56  timespec time;
57  clock_gettime(clockType, &time);
58  temp.reserve(10000000U);
59  startTime = timeData;
60  }
61 
62  void clear()
63  {
64  if (file.is_open())
65  file.close();
66  }
67 
68  void start()
69  {
70  timespec time;
71  clock_gettime(clockType, &time);
72  temp.append(toString(timeData - startTime)).append(
73  " __init__\n");
74  }
75 
76  void blockStart(const std::string &name)
77  {
78  timespec time;
79  clock_gettime(clockType, &time);
80  temp.append(toString(timeData - startTime)).append(
81  " start: ").append(name).append("\n");
82  }
83 
84  void blockEnd(const std::string &name)
85  {
86  timespec time;
87  clock_gettime(clockType, &time);
88  temp.append(toString(timeData - startTime)).append(
89  " end: ").append(name).append("\n");
90  }
91 
92  void flush()
93  {
94  if (fps < 40)
95  file << temp;
96  temp.clear();
97 // file.flush();
98  }
99 } // namespace Perfomance
100 
101 #endif // USE_PROFILER
std::string toString(T const &value)
converts any type to a string
Definition: catch.hpp:1774
void init()
Definition: playerinfo.cpp:434
void clear()
Definition: playerinfo.cpp:452
volatile int fps
Definition: timer.cpp:54