summaryrefslogtreecommitdiff
path: root/gtk2_ardour/idleometer.cc
blob: 556aeac2a2341116de7e7d0105da234148e216b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <glib.h>
#include <gtkmm/button.h>
#include <gtkmm/table.h>

#ifdef PLATFORM_WINDOWS
#include <pbd/windows_timer_utils.h>
#endif

#include "temporal/time.h"

#include "idleometer.h"
#include "pbd/i18n.h"

static int64_t _x_get_monotonic_usec() {
#ifdef PLATFORM_WINDOWS
	return PBD::get_microseconds();
#endif
	return g_get_monotonic_time();
}

using namespace Gtk;

IdleOMeter::IdleOMeter ()
	: ArdourDialog (_("Idle O Meter"))
{
	get_vbox()->set_spacing (8);
	Label* l = manage (new Label (_("<b>GUI Idle Timing Statistics</b>"), ALIGN_CENTER));
	l->set_use_markup ();

	HBox* hbox = manage (new HBox ());
	Table* t = manage (new Table ());
	hbox->pack_start (*t, true, false);

	Button* b = manage (new Button (_("Reset")));
	b->signal_clicked().connect (sigc::mem_fun(*this, &IdleOMeter::reset));

	get_vbox()->pack_start (*l, false, false);
	get_vbox()->pack_start (*hbox, false, false);
	get_vbox()->pack_start (*b, false, false);

	_label_cur.set_alignment (ALIGN_RIGHT, ALIGN_CENTER);
	_label_min.set_alignment (ALIGN_RIGHT, ALIGN_CENTER);
	_label_max.set_alignment (ALIGN_RIGHT, ALIGN_CENTER);
	_label_avg.set_alignment (ALIGN_RIGHT, ALIGN_CENTER);
	_label_dev.set_alignment (ALIGN_RIGHT, ALIGN_CENTER);
	_label_acq.set_alignment (ALIGN_CENTER, ALIGN_CENTER);

	int row = 0;
	t->attach (*manage (new Label (_("Current:"), ALIGN_RIGHT)), 0, 1, row, row + 1, FILL, SHRINK);
	t->attach (_label_cur, 1, 2, row, row + 1, FILL, SHRINK);
	++row;
	t->attach (*manage (new Label (_("Min:"),     ALIGN_RIGHT)), 0, 1, row, row + 1, FILL, SHRINK);
	t->attach (_label_min, 1, 2, row, row + 1, FILL, SHRINK);
	++row;
	t->attach (*manage (new Label (_("Max:"),     ALIGN_RIGHT)), 0, 1, row, row + 1, FILL, SHRINK);
	t->attach (_label_max, 1, 2, row, row + 1, FILL, SHRINK);
	++row;
	t->attach (*manage (new Label (_("Mean:"),    ALIGN_RIGHT)), 0, 1, row, row + 1, FILL, SHRINK);
	t->attach (_label_avg, 1, 2, row, row + 1, FILL, SHRINK);
	++row;
	t->attach (*manage (new Label (_("\u03c3:"),  ALIGN_RIGHT)), 0, 1, row, row + 1, FILL, SHRINK);
	t->attach (_label_dev, 1, 2, row, row + 1, FILL, SHRINK);
	++row;
	t->attach (*manage (new Label (_("Elapsed:"),  ALIGN_RIGHT)), 0, 1, row, row + 1, FILL, SHRINK);
	t->attach (_label_acq, 1, 2, row, row + 1, FILL, SHRINK);
}

IdleOMeter::~IdleOMeter ()
{
	_idle_connection.disconnect ();
}

bool
IdleOMeter::idle ()
{
	const int64_t now = _x_get_monotonic_usec ();
	const int64_t elapsed = now - _last;

	_max = std::max (_max, elapsed);
	_min = std::min (_min, elapsed);
	_last = now;
	_total += elapsed;
	++_cnt;

	const double cnt = _cnt;

	/* running variance */
	if (_cnt <= 1) {
		_var_m = elapsed;
		_var_s = 0;
	} else {
		const double var_m1 = _var_m;
		const double t = elapsed;
		_var_m += (t - _var_m) / cnt;
		_var_s += (t - _var_m) * (t - var_m1);
	}

	if (now - _last_display < 100000 || _cnt < 2) {
		return true;
	}

	const double avg = _total / cnt;
	const double stddev = sqrt (_var_s / (_cnt - 1.0));
	_last_display = now;

	char buf[128];

	snprintf (buf, sizeof(buf), "%8.2f ms", elapsed / 1000.0);
	_label_cur.set_text (buf);
	snprintf (buf, sizeof(buf), "%8.2f ms", _min / 1000.0);
	_label_min.set_text (buf);
	snprintf (buf, sizeof(buf), "%8.2f ms", _max / 1000.0);
	_label_max.set_text (buf);
	snprintf (buf, sizeof(buf), "%8.3f ms", avg / 1000.0);
	_label_avg.set_text (buf);
	snprintf (buf, sizeof(buf), "%8.3f ms", stddev / 1000.0);
	_label_dev.set_text (buf);
	_label_acq.set_text (Timecode::timecode_format_sampletime (now - _start, 1000000, 100, false));

	return true;
}

void
IdleOMeter::reset ()
{
	_last = _x_get_monotonic_usec ();
	_last_display = _last;
	_start = _last;
	_max = 0;
	_min = INT64_MAX;
	_cnt = 0;
	_total = _var_m = _var_s = 0;

	_label_cur.set_text ("-");
	_label_min.set_text ("-");
	_label_max.set_text ("-");
	_label_avg.set_text ("-");
	_label_dev.set_text ("-");
	_label_acq.set_text ("-");
}

void
IdleOMeter::on_show ()
{
	ArdourDialog::on_show ();
	reset ();
	_idle_connection = Glib::signal_idle().connect (sigc::mem_fun (*this, &IdleOMeter::idle));
}

void
IdleOMeter::on_hide ()
{
	_idle_connection.disconnect ();
	ArdourDialog::on_hide ();
}