Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / timer.h
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.h
22  *  @brief Some timing classes for debugging and profiling.
23  */
24
25 #ifndef DCPOMATIC_TIMER_H
26 #define DCPOMATIC_TIMER_H
27
28 #include <sys/time.h>
29 #include <boost/optional.hpp>
30 #include <string>
31 #include <map>
32
33 /** @class PeriodTimer
34  *  @brief A class to allow timing of a period within the caller.
35  *
36  *  On destruction, it will output the time since its construction.
37  */
38 class PeriodTimer
39 {
40 public:
41         explicit PeriodTimer (std::string n);
42         ~PeriodTimer ();
43
44 private:
45
46         /** name to use when giving output */
47         std::string _name;
48         /** time that this class was constructed */
49         struct timeval _start;
50 };
51
52 /** @class StateTimer
53  *  @brief A class to allow measurement of the amount of time a program
54  *  spends in one of a set of states.
55  *
56  *  Once constructed, the caller can call set_state() whenever
57  *  its state changes.  When StateTimer is destroyed, it will
58  *  output (to cout) a summary of the time spent in each state.
59  */
60 class StateTimer
61 {
62 public:
63         explicit StateTimer (std::string n);
64         StateTimer (std::string n, std::string s);
65         ~StateTimer ();
66
67         void set (std::string s);
68         void unset ();
69
70         std::string name () const {
71                 return _name;
72         }
73
74         class Counts
75         {
76         public:
77                 Counts ()
78                         : total_time (0)
79                         , number (0)
80                 {}
81
82                 double total_time;
83                 int number;
84         };
85
86         std::map<std::string, Counts> counts () const {
87                 return _counts;
88         }
89
90 private:
91         void set_internal (boost::optional<std::string> s);
92
93         /** name to add to the output */
94         std::string _name;
95         /** current state */
96         boost::optional<std::string> _state;
97         /** time that _state was entered */
98         double _time;
99         /** total time and number of entries for each state */
100         std::map<std::string, Counts> _counts;
101 };
102
103 #endif