More player debugging for butler video-full states.
[dcpomatic.git] / src / lib / timer.cc
1 /*
2     Copyright (C) 2012 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 <iostream>
26 #include <sys/time.h>
27 #include "timer.h"
28 #include "util.h"
29
30 #include "i18n.h"
31
32 using namespace std;
33
34 /** @param n Name to use when giving output */
35 PeriodTimer::PeriodTimer (string n)
36         : _name (n)
37 {
38         gettimeofday (&_start, 0);
39 }
40
41 /** Destroy PeriodTimer and output the time elapsed since its construction */
42 PeriodTimer::~PeriodTimer ()
43 {
44         struct timeval stop;
45         gettimeofday (&stop, 0);
46         cout << N_("T: ") << _name << N_(": ") << (seconds (stop) - seconds (_start)) << N_("\n");
47 }
48
49 /** @param n Name to use when giving output.
50  *  @param s Initial state.
51  */
52 StateTimer::StateTimer (string n, string s)
53         : _name (n)
54 {
55         struct timeval t;
56         gettimeofday (&t, 0);
57         _time = seconds (t);
58         _state = s;
59 }
60
61 /** @param s New state that the caller is in */
62 void
63 StateTimer::set_state (string s)
64 {
65         double const last = _time;
66         struct timeval t;
67         gettimeofday (&t, 0);
68         _time = seconds (t);
69
70         if (_totals.find (s) == _totals.end ()) {
71                 _totals[s] = 0;
72         }
73
74         _totals[_state] += _time - last;
75         _state = s;
76 }
77
78 /** Destroy StateTimer and generate a summary of the state timings on cout */
79 StateTimer::~StateTimer ()
80 {
81         if (_state.empty ()) {
82                 return;
83         }
84
85         set_state (N_(""));
86
87         cout << _name << N_(":\n");
88         for (map<string, double>::iterator i = _totals.begin(); i != _totals.end(); ++i) {
89                 cout << N_("\t") << i->first << " " << i->second << N_("\n");
90         }
91 }