Barely-functioning GL playback with new arrangement.
[dcpomatic.git] / src / lib / timer.cc
1 /*
2     Copyright (C) 2012-2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file src/timer.cc
22  *  @brief Some timing classes for debugging and profiling.
23  */
24
25 #include "timer.h"
26 #include "util.h"
27 #include "compose.hpp"
28 #include <iostream>
29 #include <sys/time.h>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using boost::optional;
35
36 /** @param n Name to use when giving output */
37 PeriodTimer::PeriodTimer (string n)
38         : _name (n)
39 {
40         gettimeofday (&_start, 0);
41 }
42
43 /** Destroy PeriodTimer and output the time elapsed since its construction */
44 PeriodTimer::~PeriodTimer ()
45 {
46         struct timeval stop;
47         gettimeofday (&stop, 0);
48         cout << N_("T: ") << _name << N_(": ") << (seconds (stop) - seconds (_start)) << N_("\n");
49 }
50
51 /** @param n Name to use when giving output.
52  *  @param s Initial state.
53  */
54 StateTimer::StateTimer (string n, string s)
55         : _name (n)
56 {
57         struct timeval t;
58         gettimeofday (&t, 0);
59         _time = seconds (t);
60         _state = s;
61 }
62
63 StateTimer::StateTimer (string n)
64         : _name (n)
65 {
66
67 }
68
69 void
70 StateTimer::set (string s)
71 {
72         set_internal (s);
73 }
74
75 /** @param s New state that the caller is in */
76 void
77 StateTimer::set_internal (optional<string> s)
78 {
79         double const last = _time;
80         struct timeval t;
81         gettimeofday (&t, 0);
82         _time = seconds (t);
83
84         if (s && _counts.find(*s) == _counts.end()) {
85                 _counts[*s] = Counts();
86         }
87
88         if (_state) {
89                 _counts[*_state].total_time += _time - last;
90                 _counts[*_state].number++;
91         }
92         _state = s;
93 }
94
95 void
96 StateTimer::unset ()
97 {
98         set_internal (optional<string>());
99 }
100
101 bool compare (pair<double, string> a, pair<double, string> b)
102 {
103         return a.first > b.first;
104 }
105
106 /** Destroy StateTimer and generate a summary of the state timings on cout */
107 StateTimer::~StateTimer ()
108 {
109         if (!_state) {
110                 return;
111         }
112
113         unset ();
114
115         int longest = 0;
116         for (map<string, Counts>::iterator i = _counts.begin(); i != _counts.end(); ++i) {
117                 longest = max (longest, int(i->first.length()));
118         }
119
120         list<pair<double, string> > sorted;
121
122         for (map<string, Counts>::iterator i = _counts.begin(); i != _counts.end(); ++i) {
123                 string name = i->first + string(longest + 1 - i->first.size(), ' ');
124                 char buffer[64];
125                 snprintf (buffer, 64, "%.4f", i->second.total_time);
126                 string total_time (buffer);
127                 sorted.push_back (make_pair(i->second.total_time, String::compose("\t%1%2 %3 %4", name, total_time, i->second.number, (i->second.total_time / i->second.number))));
128         }
129
130         sorted.sort (compare);
131
132         cout << _name << N_(":\n");
133         for (list<pair<double, string> >::iterator i = sorted.begin(); i != sorted.end(); ++i) {
134                 cout << N_("\t") << i->second << "\n";
135         }
136 }