Skip to contentSkip to conversation

Show evil usbdevfs users

PATCH New Oleg Drokin 1 participant 3 messages Jun 19, 2016

lore
Share & tools
lore

Oleg Drokin

author
#0 lore raw

cover Show evil usbdevfs users

It turns out that powertop does not yet recognise that usbdevfs users that hold usb device open like that cause it to operate at full power, kind of like ALSA sound device users.
So the first patch adds support for ussbdevfs users to shame them for needlessly draining power (I am looking at you, fwupd), and patch two fixes an issue where such processes are not displayed in the summary because they did not have any runtime at all, as if that would somehow prevent the device-opening-damage.

Oleg Drokin (2):
Add support for usbdevfs
Show idle processes holding devices at full power

 src/devices/usb.cpp     | 28 ++++++++++++++++++++++++++++
 src/devices/usb.h       |  3 +++
 src/process/process.cpp |  3 ++-
 3 files changed, 33 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/2 Add support for usbdevfs

Similar to ALSA, usb devices could be held at full power by opening them via usbdevfs, so it's good to track such processes and attribute the power to them.

Signed-off-by Oleg Drokin <green(a)linuxhacker.ru>
---
 src/devices/usb.cpp | 28 ++++++++++++++++++++++++++++
 src/devices/usb.h   |  3 +++
 2 files changed, 31 insertions(+)
src/devices/usb.cpp +28 -0
index aa9c227..5042699 100644
@@ -31,6 +31,7 @@
 #include <limits.h>
 
 #include "../lib.h"
+#include "../devlist.h"
 #include "../parameters/parameters.h"
 
 #include <iostream>
@@ -52,6 +53,8 @@ usbdevice::usbdevice(const char *_name, const char *path, const char *devid): de
 	active_after = 0;
 	connected_before = 0;
 	connected_after = 0;
+	busnum = 0;
+	devnum = 0;
 
 	index = get_param_index(devname);
 	r_index = get_result_index(name);
@@ -93,6 +96,22 @@ usbdevice::usbdevice(const char *_name, const char *path, const char *devid): de
 		snprintf(humanname, sizeof(humanname), _("USB device: %s"), product);
 	else if (strlen(vendor))
 		snprintf(humanname, sizeof(humanname), _("USB device: %s"), vendor);
+
+	/* For usbdevfs we need bus number and device number */
+	snprintf(filename, sizeof(filename), "%s/busnum", path);
+	file.open(filename, ios::in);
+	if (file) {
+
+		file >> busnum;
+		file.close();
+	};
+	snprintf(filename, sizeof(filename), "%s/devnum", path);
+	file.open(filename, ios::in);
+	if (file) {
+
+		file >> devnum;
+		file.close();
+	};
 }
 
 
@@ -165,6 +184,15 @@ const char * usbdevice::human_name(void)
 	return humanname;
 }
 
+void usbdevice::register_power_with_devlist(struct result_bundle *results, struct parameter_bundle *bundle)
+{
+	char devfs_name[1024];
+
+	snprintf(devfs_name, sizeof(devfs_name), "usb/%03d/%03d", busnum,
+		 devnum);
+
+        register_devpower(devfs_name, power_usage(results, bundle), this);
+}
 
 double usbdevice::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
 {
src/devices/usb.h +3 -0
index 097df51..7e76a55 100644
@@ -40,6 +40,8 @@ class usbdevice: public device {
 	int index;
 	int r_index;
 	int rootport;
+	int busnum;
+	int devnum;
 public:
 
 	usbdevice(const char *_name, const char *path, const char *devid);
@@ -53,6 +55,7 @@ public:
 
 	virtual const char * device_name(void);
 	virtual const char * human_name(void);
+	virtual void register_power_with_devlist(struct result_bundle *results, struct parameter_bundle *bundle);
 	virtual double power_usage(struct result_bundle *result, struct parameter_bundle *bundle);
 	virtual int power_valid(void) { return utilization_power_valid(r_index);};
 	virtual int grouping_prio(void) { return 4; };
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH 2/2 Show idle processes holding devices at full power

In reply to Oleg Drokin

Even if process did not spend any time running but holds a device open, it deserves its line on the "Wall of Shame". Otherwise we get erratic "now we show it, now we don't" behavior which is confusing.

Signed-off-by Oleg Drokin <green(a)linuxhacker.ru>
---
 src/process/process.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/process/process.cpp +2 -1
index 25de86f..caeccec 100644
@@ -230,7 +230,8 @@ void all_processes_to_all_power(void)
 {
 	unsigned int i;
 	for (i = 0; i < all_processes.size() ; i++)
-		if (all_processes[i]->accumulated_runtime)
+		if (all_processes[i]->accumulated_runtime ||
+		    all_processes[i]->power_charge)
 			all_power.push_back(all_processes[i]);
 }
 
Signature · 1 line
2.7.4