[PATCH libevdev] tools: print the mean frequency together with the max frequency

Peter Hutterer peter.hutterer at who-t.net
Mon Sep 19 01:03:31 UTC 2016


And if they're 30% out, print a warning. On the ThinkPad X1 Wireless Touch
Mouse (when connected via bluetooth) we get a bunch of events at the start of
the movement, all less than 1ms apart. Best guess is that the device goes to
low-power, then notices the movement and buffers the event until the BT
connection is back up. Then it sends all events at once. Usually they're less
than 1ms apart, but at one recording showed a 37ms delay before we go back to
the normal 70ms (~15Hz) the mouse has otherwise.

This is unpredictable enough that we can't just work around it so instead
print a warning to the user so they can go investigate.

https://bugs.freedesktop.org/show_bug.cgi?id=97812

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 tools/mouse-dpi-tool.c | 43 +++++++++++++++++++++++++++++++++++++++----
 1 file changed, 39 insertions(+), 4 deletions(-)

diff --git a/tools/mouse-dpi-tool.c b/tools/mouse-dpi-tool.c
index 30d689a..852ff07 100644
--- a/tools/mouse-dpi-tool.c
+++ b/tools/mouse-dpi-tool.c
@@ -44,6 +44,9 @@
 struct measurements {
 	int distance;
 	double max_frequency;
+	double *frequencies;
+	size_t frequencies_sz;
+	size_t nfrequencies;
 	uint64_t us;
 };
 
@@ -70,6 +73,21 @@ get_frequency(uint64_t last, uint64_t current)
 	return 1000000.0/(current - last);
 }
 
+static inline void
+push_frequency(struct measurements *m, double freq)
+{
+	if (m->nfrequencies == m->frequencies_sz) {
+		m->frequencies_sz += 100;
+		m->frequencies = realloc(m->frequencies,
+					 m->frequencies_sz * sizeof *m->frequencies);
+		if (!m->frequencies)
+			abort();
+	}
+
+	m->frequencies[m->nfrequencies] = freq;
+	m->nfrequencies++;
+}
+
 static int
 print_current_values(const struct measurements *m)
 {
@@ -109,8 +127,8 @@ handle_event(struct measurements *m, const struct input_event *ev)
 			m->distance = 0;
 		} else {
 			double freq = get_frequency(last_us, m->us);
-			if (freq < 1200)
-				m->max_frequency = max(freq, m->max_frequency);
+			push_frequency(m, freq);
+			m->max_frequency = max(freq, m->max_frequency);
 			return print_current_values(m);
 		}
 
@@ -166,12 +184,29 @@ mainloop(struct libevdev *dev, struct measurements *m) {
 	return 0;
 }
 
+static inline double
+mean_frequency(struct measurements *m)
+{
+	int idx;
+
+	idx = m->nfrequencies/2;
+	return m->frequencies[idx];
+}
+
 static void
 print_summary(struct measurements *m)
 {
 	int res;
+	int max_freq = (int)m->max_frequency,
+	    mean_freq = (int)mean_frequency(m);
+
+	printf("Estimated sampling frequency: %dHz (mean %dHz)\n",
+	       max_freq, mean_freq);
+
+	if (max_freq > mean_freq * 1.3)
+		printf("WARNING: Max frequency is more than 30%% higher "
+		       "than mean frequency. Manual verification required!\n");
 
-	printf("Estimated sampling frequency: %dHz\n", (int)m->max_frequency);
 	printf("To calculate resolution, measure physical distance covered\n"
 	       "and look up the matching resolution in the table below\n");
 
@@ -213,7 +248,7 @@ main (int argc, char **argv) {
 	int fd;
 	const char *path;
 	struct libevdev *dev;
-	struct measurements measurements = {0, 0, 0};
+	struct measurements measurements = {0};
 
 	if (argc < 2)
 		return usage();
-- 
2.7.4



More information about the Input-tools mailing list