Initialize uninitialized variable
[ardour.git] / libs / ardour / cycle_timer.cc
1 /*
2  * Copyright (C) 2006-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2009 David Robillard <d@drobilla.net>
4  * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
5  *
6  * This program 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  * This program 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 along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <cstdio>
22 #include <fstream>
23 #include "pbd/error.h"
24 #include "ardour/cycle_timer.h"
25
26 #include "ardour/libardour_visibility.h"
27
28 #include "pbd/i18n.h"
29
30 using namespace std;
31 using namespace PBD;
32
33 float CycleTimer::cycles_per_usec = 0;
34
35 float
36 get_mhz()
37 {
38         FILE *f;
39
40         if ((f = fopen("/proc/cpuinfo", "r")) == 0) {
41                 fatal << _("CycleTimer::get_mhz(): can't open /proc/cpuinfo") << endmsg;
42                 abort(); /*NOTREACHED*/
43                 return 0.0f;
44         }
45
46         while (true) {
47
48                 float mhz;
49                 int ret;
50                 char buf[1000];
51
52                 if (fgets (buf, sizeof(buf), f) == 0) {
53                         fatal << _("CycleTimer::get_mhz(): cannot locate cpu MHz in /proc/cpuinfo") << endmsg;
54                         abort(); /*NOTREACHED*/
55                         return 0.0f;
56                 }
57
58 #ifdef __powerpc__
59
60                 int   imhz;
61
62                 /* why can't the PPC crew standardize their /proc/cpuinfo format ? */
63                 ret = sscanf (buf, "clock\t: %dMHz", &imhz);
64                 mhz = (float) imhz;
65
66 #else /* XXX don't assume its x86 just because its not power pc */
67                 ret = sscanf (buf, "cpu MHz         : %f", &mhz);
68
69 #endif
70                 if (ret == 1) {
71                         fclose(f);
72                         return mhz;
73                 }
74         }
75
76         fatal << _("cannot locate cpu MHz in /proc/cpuinfo") << endmsg;
77         abort(); /*NOTREACHED*/
78         return 0.0f;
79 }
80
81 StoringTimer::StoringTimer (int N)
82 {
83         _point = new int[N];
84         _value = new cycles_t[N];
85         _ref = new cycles_t[N];
86         _max_points = N;
87         _points = 0;
88 }
89
90 #ifndef NDEBUG
91 void
92 StoringTimer::dump (string const & file)
93 {
94         ofstream f (file.c_str ());
95
96         f << min (_points, _max_points) << "\n";
97         f << get_mhz () << "\n";
98         for (int i = 0; i < min (_points, _max_points); ++i) {
99                 f << _point[i] << " " << _ref[i] << " " << _value[i] << "\n";
100         }
101 }
102 #endif
103
104 void
105 StoringTimer::ref ()
106 {
107         _current_ref = get_cycles ();
108 }
109
110 void
111 StoringTimer::check (int p)
112 {
113         if (_points == _max_points) {
114                 ++_points;
115                 return;
116         } else if (_points > _max_points) {
117                 return;
118         }
119
120         _point[_points] = p;
121         _value[_points] = get_cycles ();
122         _ref[_points] = _current_ref;
123
124         ++_points;
125 }
126
127 #ifdef PT_TIMING
128 StoringTimer ST (64 * 1024);
129 #endif
130
131