Get progress during formatting.
[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
53 /** @class StateTimer
54  *  @brief A class to allow measurement of the amount of time a program
55  *  spends in one of a set of states.
56  *
57  *  Once constructed, the caller can call set_state() whenever
58  *  its state changes.  When StateTimer is destroyed, it will
59  *  output (to cout) a summary of the time spent in each state.
60  */
61 class StateTimer
62 {
63 public:
64         explicit StateTimer (std::string n);
65         /** @param n Name to use when giving output.
66          *  @param s Initial state.
67          */
68         StateTimer (std::string n, std::string s);
69         ~StateTimer ();
70
71         /** @param s New state that the caller is in */
72         void set (std::string s);
73         void unset ();
74
75         std::string name () const {
76                 return _name;
77         }
78
79         class Counts
80         {
81         public:
82                 double total_time = 0;
83                 int number = 0;
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
104 #endif