Make terminate_threads() less likely to leave _threads containing invalid pointers.
[dcpomatic.git] / src / lib / timer.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3     Copyright (C) 2000-2007 Paul Davis
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 <string>
30 #include <map>
31
32 /** @class PeriodTimer
33  *  @brief A class to allow timing of a period within the caller.
34  *
35  *  On destruction, it will output the time since its construction.
36  */
37 class PeriodTimer
38 {
39 public:
40         explicit PeriodTimer (std::string n);
41         ~PeriodTimer ();
42
43 private:
44
45         /** name to use when giving output */
46         std::string _name;
47         /** time that this class was constructed */
48         struct timeval _start;
49 };
50
51 /** @class StateTimer
52  *  @brief A class to allow measurement of the amount of time a program
53  *  spends in one of a set of states.
54  *
55  *  Once constructed, the caller can call set_state() whenever
56  *  its state changes.  When StateTimer is destroyed, it will
57  *  output (to cout) a summary of the time spent in each state.
58  */
59 class StateTimer
60 {
61 public:
62         StateTimer (std::string n, std::string s);
63         ~StateTimer ();
64
65         void set_state (std::string s);
66
67 private:
68         /** name to add to the output */
69         std::string _name;
70         /** current state */
71         std::string _state;
72         /** time that _state was entered */
73         double _time;
74         /** time that has been spent in each state so far */
75         std::map<std::string, double> _totals;
76 };
77
78 #endif