Use udev for accessing sysfs
Nanley Chery
cover Use udev for accessing sysfs
From: Nanley Chery <nanley.g.chery(a)intel.com>
This patch series changes PowerTOP's method of accessing sysfs (for tunables mainly) from explicitly accessing files to using the standard udev device management library and API.
The current codebase collects data on devices by probing sysfs files directly with the usual low-level file handling API. This method is not recommended by the Linux kernel maintainers because the sysfs organization is not guaranteed to be stable over time. This can lead to dependent applications breaking with newer kernel releases. Instead using an abstraction layer, like udev, is suggested
(see https://www.kernel.org/doc/Documentation/sysfs-rules.txt).
The benefits of switching to udev include:
* The removal of libpci and a reduction in repetitive code.
* Improved readability of the tunable code.
* Faster reading of device state values due to caching provided by udev.
I've tried to make this transition as simple as possible by including the bare minimum of changes required for this feature. There's at least one more tunable that can be converted (i2c), but I would like to get feedback on the current implementation first.
Nanley Chery (9):
build with udev library
add udev helper functions
usb_tunable: use udev
syfs_tunable: use udev
runtime_tunable: use udev
replace pciutils usage with udevhwdb
runtime_pm: use udev
remove libpci and most dead code
calibrate: use udev
Android.mk | 1 - README | 1 - configure.ac | 20 ++----- src/Makefile.am | 6 +- src/calibrate/calibrate.cpp | 44 ++++---------- src/devices/runtime_pm.cpp | 105 +++++++++++--------------------- src/devices/runtime_pm.h | 1 + src/lib.cpp | 100 +++++++++++++++++++++---------- src/lib.h | 7 ++- src/main.cpp | 4 +- src/tuning/runtime.cpp | 139 +++++++++++++------------------------------ src/tuning/runtime.h | 6 +- src/tuning/tuningsysfs.cpp | 12 +++- src/tuning/tuningsysfs.h | 1 + src/tuning/tuningusb.cpp | 142 ++++++++++++++------------------------------ src/tuning/tuningusb.h | 5 +- 16 files changed, 231 insertions(+), 363 deletions(-)
Signature · 1 line
2.4.1
Nanley Chery
RFC 1/9 build with udev library
--- configure.ac | 5 +++++ src/Makefile.am | 3 +++ 2 files changed, 8 insertions(+)
configure.ac +5 -0
@@ -54,6 +54,7 @@ AC_CHECK_HEADERS([ \ locale.h \ malloc.h \ ncurses.h \ + libudev.h \ stdint.h \ stdlib.h \ string.h \
@@ -106,6 +107,10 @@ AC_CHECK_FUNCS([ \ strtoull \ ]) +PKG_CHECK_MODULES([UDEV], [libudev], [LIBS="$LIBS $udev_LIBS"], [ + AC_MSG_ERROR([libudev is required but was not found]) +]) + PKG_CHECK_MODULES([NCURSES], [ncursesw ncurses], [LIBS="$LIBS $ncurses_LIBS"], [ AC_SEARCH_LIBS([delwin], [ncursesw ncurses], [], [ AC_MSG_ERROR([ncurses is required but was not found])
src/Makefile.am +3 -0
@@ -133,6 +133,7 @@ powertop_CXXFLAGS = \ $(GLIB2_CFLAGS) \ $(LIBNL_CFLAGS) \ $(NCURSES_CFLAGS) \ + $(UDEV_CFLAGS) \ $(PCIUTILS_CFLAGS)
@@ -142,6 +143,7 @@ powertop_CPPFLAGS = \ $(LIBNL_CFLAGS) \ $(LIBZ_CFLAGS) \ $(NCURSES_CFLAGS) \ + $(UDEV_CFLAGS) \ $(PCIUTILS_CFLAGS) powertop_LDADD = \
@@ -152,6 +154,7 @@ AM_LDFLAGS = \ $(LIBS) \ $(LIBZ_LIBS) \ $(NCURSES_LIBS) \ + $(UDEV_LIBS) \ $(PCIUTILS_LIBS) \ $(PTHREAD_LIBS) \ $(RESOLV_LIBS)
Signature · 1 line
2.4.1
Nanley Chery
RFC 2/9 add udev helper functions
In reply to Nanley Chery--- src/lib.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.h | 4 ++++ src/main.cpp | 1 + 3 files changed, 59 insertions(+)
src/lib.cpp +54 -0
@@ -291,6 +291,29 @@ void format_watts(double W, char *buffer, unsigned int len) align_string(buffer, len, len); } +static struct udev *udev_cxt = NULL; + +struct udev * get_udev() +{ + if (udev_cxt == NULL) { + udev_cxt = udev_new(); + if (udev_cxt == NULL) { + printf("Couldn't create udev\n"); + exit(EXIT_FAILURE); + } + } + + return udev_cxt; +} + +void cleanup_udev() +{ + if (udev_cxt != NULL) { + udev_unref(udev_cxt); + udev_cxt = NULL; + } +} + #ifndef HAVE_NO_PCI static struct pci_access *pci_access;
@@ -437,6 +460,37 @@ int equals(double a, double b) return fabs(a - b) <= std::numeric_limits<double>::epsilon(); } +void process_subsystem(const char *subsystem, callback fn, int num_attr, ...) +{ + /* Find devices in subsystem with given attributes */ + struct udev_enumerate *enumerate = udev_enumerate_new(get_udev()); + udev_enumerate_add_match_subsystem(enumerate, subsystem); + va_list args; + va_start (args, num_attr); + while (num_attr--) { + const char *attr = va_arg(args, const char*); + const char *attr_val = va_arg(args, const char*); + if (*attr == '+') + udev_enumerate_add_match_sysattr(enumerate, &attr[1], attr_val); + else if (*attr == '-') + udev_enumerate_add_nomatch_sysattr(enumerate, &attr[1], attr_val); + else { + fprintf (stderr, "Bad format of attribute\n"); + exit(1); + } + } + va_end (args); + udev_enumerate_scan_devices(enumerate); + + /* Run callback on each device */ + struct udev_list_entry *dev_entry; + udev_list_entry_foreach(dev_entry, udev_enumerate_get_list_entry(enumerate)) + fn(udev_list_entry_get_name(dev_entry)); + + /* Cleanup */ + udev_enumerate_unref(enumerate); +} + void process_directory(const char *d_name, callback fn) { struct dirent *entry;
src/lib.h +4 -0
@@ -28,6 +28,7 @@#include <libintl.h> #include <stdint.h> #include <stdlib.h> +#include <libudev.h> /* Include only for Automake builds */ #ifdef HAVE_CONFIG_H
@@ -70,6 +71,8 @@ extern void format_watts(double W, char *buffer, unsigned int len);extern char *pci_id_to_name (uint16_t vendor,uint16_t device,char *buffer,int len);extern void end_pci_access (void ); +extern struct udev *get_udev (); +extern void cleanup_udev ();extern char *fmt_prefix (double n,char *buf);extern char *pretty_print (const char *str,char *buf,int len);
@@ -77,6 +80,7 @@ extern int equals(double a, double b);typedef void (*callback )(const char *);extern void process_directory (const char *d_name,callback fn); +extern void process_subsystem (const char *subsystem,callback fn,int num_attr, ...);extern int utf_ok;extern int get_user_input (char *buf,unsigned sz);extern int read_msr (int cpu,uint64_t offset,uint64_t *value);
src/main.cpp +1 -0
@@ -379,6 +379,7 @@ void clean_shutdown() clear_all_devices(); clear_all_devfreq(); clear_all_cpus(); + cleanup_udev(); return; }
Signature · 1 line
2.4.1
Nanley Chery
RFC 3/9 usb_tunable: use udev
In reply to Nanley Chery--- src/tuning/tuningusb.cpp | 142 +++++++++++++++-------------------------------- src/tuning/tuningusb.h | 5 +- 2 files changed, 47 insertions(+), 100 deletions(-)
src/tuning/tuningusb.cpp +44 -98
@@ -25,132 +25,78 @@ #include "tuning.h" #include "tunable.h" -#include "unistd.h" #include "tuningusb.h" #include <string.h> -#include <dirent.h> -#include <utility> -#include <iostream> -#include <fstream> -#include "../lib.h" - -usb_tunable::usb_tunable(const char *path, const char *name) : tunable("", 0.9, _("Good"), _("Bad"), _("Unknown")) +usb_tunable::usb_tunable(struct udev_device *usb_dev) : tunable("", 0.9, _("Good"), _("Bad"), _("Unknown")) { - ifstream file; - char filename[4096]; - char vendor[2048]; - char product[2048]; - string str1, str2; - sprintf(usb_path, "%s/power/control", path); - - vendor[0] = 0; - product[0] = 0; - - str1 = read_sysfs_string("%s/idVendor", path); - str2 = read_sysfs_string("%s/idProduct", path); + tun_dev = usb_dev; + udev_device_ref(tun_dev); - sprintf(desc, _("Autosuspend for unknown USB device %s (%s:%s)"), name, str1.c_str(), str2.c_str()); + const char * name = udev_device_get_sysname(usb_dev); + const char * vendor = udev_device_get_sysattr_value(usb_dev, "manufacturer"); + const char * product = udev_device_get_sysattr_value(usb_dev, "product"); - sprintf(filename, "%s/manufacturer", path); - file.open(filename, ios::in); - if (file) { - file.getline(vendor, 2047); - if (strstr(vendor, "Linux ")) - vendor[0] = 0; - file.close(); - }; - sprintf(filename, "%s/product", path); - file.open(filename, ios::in); - if (file) { - file.getline(product, 2040); - file.close(); - }; - if (strlen(vendor) && strlen(product)) + if (vendor && product && strstr(vendor, "Linux ")) sprintf(desc, _("Autosuspend for USB device %s [%s]"), product, vendor); - else if (strlen(product)) + else if (product) sprintf(desc, _("Autosuspend for USB device %s [%s]"), product, name); - else if (strlen(vendor)) + else if (vendor && strstr(vendor, "Linux ")) sprintf(desc, _("Autosuspend for USB device %s [%s]"), vendor, name); + else { + const char * str1 = udev_device_get_sysattr_value(usb_dev, "idVendor"); + const char * str2 = udev_device_get_sysattr_value(usb_dev, "idProduct"); + sprintf(desc, _("Autosuspend for unknown USB device %s (%s:%s)"), name, str1, str2); + } - sprintf(toggle_good, "echo 'auto' > '%s';", usb_path); - sprintf(toggle_bad, "echo 'on' > '%s';", usb_path); + const char * path = udev_device_get_syspath(usb_dev); + sprintf(toggle_good, "echo 'auto' > '%s/power/control';", path); + sprintf(toggle_bad, "echo 'on' > '%s/power/control';", path); } -int usb_tunable::good_bad(void) +usb_tunable::~usb_tunable() { - string content; - - content = read_sysfs_string(usb_path); - - if (strcmp(content.c_str(), "auto") == 0) - return TUNE_GOOD; + udev_device_unref(tun_dev); +} - return TUNE_BAD; +int usb_tunable::good_bad(void) +{ + const char * str1 = udev_device_get_sysattr_value(tun_dev, "power/control"); + return strcmp(str1, "auto") == 0 ? TUNE_GOOD : TUNE_BAD; } void usb_tunable::toggle(void) { - int good; - good = good_bad(); - - if (good == TUNE_GOOD) { - write_sysfs(usb_path, "on"); - return; - } - - write_sysfs(usb_path, "auto"); + std::string str = good_bad() == TUNE_GOOD ? "on" : "auto"; + udev_device_set_sysattr_value(tun_dev, "power/control", &str[0]); } const char *usb_tunable::toggle_script(void) { - int good; - good = good_bad(); - - if (good == TUNE_GOOD) { - return toggle_bad; - } - - return toggle_good; + return good_bad() == TUNE_GOOD ? toggle_bad : toggle_good; } static void add_usb_callback(const char *d_name) { - class usb_tunable *usb; - char filename[4096]; - DIR *dir; - - sprintf(filename, "/sys/bus/usb/devices/%s/power/control", d_name); - if (access(filename, R_OK) != 0) - return; - - sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", d_name); - if (access(filename, R_OK)!=0) - return; - - /* every interface of this device should support autosuspend */ - sprintf(filename, "/sys/bus/usb/devices/%s", d_name); - if ((dir = opendir(filename))) { - struct dirent *entry; - while ((entry = readdir(dir))) { - /* dirname: <busnum>-<devnum>...:<config num>-<interface num> */ - if (!isdigit(entry->d_name[0])) - continue; - sprintf(filename, "/sys/bus/usb/devices/%s/%s/supports_autosuspend", d_name, entry->d_name); - if (access(filename, R_OK) == 0 && read_sysfs(filename) == 0) - break; - } - closedir(dir); - if (entry) - return; - } - - sprintf(filename, "/sys/bus/usb/devices/%s", d_name); - usb = new class usb_tunable(filename, d_name); - all_tunables.push_back(usb); + struct udev *udev = get_udev(); + struct udev_device *dev = udev_device_new_from_syspath(udev, d_name); + struct udev_enumerate *dev_enumerate = udev_enumerate_new(udev); + udev_enumerate_add_match_parent(dev_enumerate, dev); + udev_enumerate_add_match_sysattr(dev_enumerate, "supports_autosuspend", "0"); + udev_enumerate_scan_devices(dev_enumerate); + + /* If no devices in tree lack autosuspend support, add to tunables */ + if (udev_enumerate_get_list_entry(dev_enumerate) == NULL) + all_tunables.push_back(new usb_tunable(dev)); + + /* Cleanup */ + udev_device_unref(dev); + udev_enumerate_unref(dev_enumerate); } void add_usb_tunables(void) { - process_directory("/sys/bus/usb/devices/", add_usb_callback); + process_subsystem("usb", add_usb_callback, 2, + "+power/control", NULL, + "+power/active_duration", NULL); }
src/tuning/tuningusb.h +3 -2
@@ -32,9 +32,10 @@ using namespace std; class usb_tunable : public tunable { -char usb_path[4096 ]; +struct udev_device *tun_dev; public: - usb_tunable(const char *usb_path, const char *path); + usb_tunable(struct udev_device *usb_dev); + ~usb_tunable (); virtual int good_bad(void);
Signature · 1 line
2.4.1
Nanley Chery
RFC 4/9 syfs_tunable: use udev
In reply to Nanley Chery--- src/tuning/tuningsysfs.cpp | 12 +++++++++--- src/tuning/tuningsysfs.h | 1 + 2 files changed, 10 insertions(+), 3 deletions(-)
src/tuning/tuningsysfs.cpp +9 -3
@@ -115,15 +115,21 @@ void add_sysfs_tunable(const char *str, const char *_sysfs_path, const char *_ta static void add_sata_tunables_callback(const char *d_name) { + struct udev_device *dev = udev_device_new_from_syspath(get_udev(), d_name); + const char *dev_name = udev_device_get_sysname(dev); char filename[4096]; char msg[4096]; - sprintf(filename, "/sys/class/scsi_host/%s/link_power_management_policy", d_name); - sprintf(msg, _("Enable SATA link power management for %s"), d_name); + sprintf(filename, "%s/link_power_management_policy", d_name); + sprintf(msg, _("Enable SATA link power management for %s"), dev_name); add_sysfs_tunable(msg, filename,"min_power"); + + /* Cleanup */ + udev_device_unref(dev); } void add_sata_tunables(void) { - process_directory("/sys/class/scsi_host", add_sata_tunables_callback); + process_subsystem("scsi_host", add_sata_tunables_callback, 1, + "+link_power_management_policy", NULL); }
src/tuning/tuningsysfs.h +1 -0
@@ -32,6 +32,7 @@ using namespace std; class sysfs_tunable : public tunable { +struct udev_device *tun_dev;char sysfs_path[4096 ];char target_value[4096 ];char bad_value[4096 ];
Signature · 1 line
2.4.1
Nanley Chery
RFC 5/9 runtime_tunable: use udev
In reply to Nanley Chery--- src/devices/runtime_pm.cpp | 7 +++ src/devices/runtime_pm.h | 1 + src/tuning/runtime.cpp | 140 ++++++++++++++------------------------------- src/tuning/runtime.h | 6 +- 4 files changed, 56 insertions(+), 98 deletions(-)
src/devices/runtime_pm.cpp +7 -0
@@ -143,6 +143,13 @@ void runtime_pmdevice::set_human_name(char *_name) strcpy(humanname, _name); } +int udevice_has_runtime_pm(struct udev_device* udevice) +{ + const char *sus_t = udev_device_get_sysattr_value(udevice, "power/runtime_suspended_time"); + const char *act_t = udev_device_get_sysattr_value(udevice, "power/runtime_active_time"); + return (sus_t != NULL && + (atoi(sus_t) || (act_t != NULL && atoi(act_t)))); +} int device_has_runtime_pm(const char *sysfs_path) {
src/devices/runtime_pm.h +1 -0
@@ -60,6 +60,7 @@ public:extern void create_all_runtime_pm_devices (void );extern int device_has_runtime_pm (const char *sysfs_path); +extern int udevice_has_runtime_pm (struct udev_device * udevice);#endif
src/tuning/runtime.cpp +44 -96
179 lines not on this page · raw message
src/tuning/runtime.h +4 -2
@@ -31,9 +31,11 @@ using namespace std; class runtime_tunable : public tunable { -char runtime_path[4096 ]; +struct udev_device *tun_dev; public: - runtime_tunable(const char *runtime_path, const char *bus, const char *dev); + runtime_tunable(struct udev_device* udev); + + ~runtime_tunable (void); virtual int good_bad(void);
Signature · 1 line
2.4.1
Nanley Chery
RFC 6/9 replace pciutils usage with udevhwdb
In reply to Nanley Chery--- src/lib.cpp | 27 +++++++++++++++++++++++++++ src/lib.h | 1 + src/tuning/runtime.cpp | 11 +++-------- 3 files changed, 31 insertions(+), 8 deletions(-)
src/lib.cpp +27 -0
@@ -314,6 +314,33 @@ void cleanup_udev() } } +const char *upci_id_to_name(struct udev_device* dev, char* buffer) +{ + const char *uvendor = udev_device_get_sysattr_value(dev, "vendor"); + const char *udevice = udev_device_get_sysattr_value(dev, "device"); + uint16_t vendor = stoul(string(uvendor), NULL, 16); + uint16_t device = stoul(string(udevice), NULL, 16); + char modalias[64]; + sprintf(modalias, "pci:v%08Xd%08X*", vendor, device); + const char *key1 = "ID_VENDOR_FROM_DATABASE"; + const char *key2 = "ID_MODEL_FROM_DATABASE"; + + struct udev_hwdb *hwdb = udev_hwdb_new(udev_device_get_udev(dev)); + struct udev_list_entry *entry; + udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0)) + if (strcmp(udev_list_entry_get_name(entry), key1) == 0) + uvendor = udev_list_entry_get_value(entry); + else if (strcmp(udev_list_entry_get_name(entry), key2) == 0) + udevice = udev_list_entry_get_value(entry); + + if (uvendor && udevice) { + sprintf(buffer, "%s %s", uvendor, udevice); + return buffer; + } + + return NULL; +} + #ifndef HAVE_NO_PCI static struct pci_access *pci_access;
src/lib.h +1 -0
@@ -68,6 +68,7 @@ extern string read_sysfs_string(const char *format, const char *param);extern void format_watts (double W ,char *buffer,unsigned int len); +extern const char *upci_id_to_name (struct udev_device * dev,char * buffer);extern char *pci_id_to_name (uint16_t vendor,uint16_t device,char *buffer,int len);extern void end_pci_access (void );
src/tuning/runtime.cpp +3 -8
@@ -42,16 +42,11 @@ runtime_tunable::runtime_tunable(struct udev_device* udev_device) : const char *dev = udev_device_get_sysname(udev_device); /* Give more accurate descriptions for PCI devices */ + char devname[4096]; if (strcmp(bus, "pci") == 0) { - const char *uvendor = udev_device_get_sysattr_value(udev_device, "vendor"); - const char *udevice = udev_device_get_sysattr_value(udev_device, "device"); - uint16_t vendor = stoul(string(uvendor), NULL, 16); - uint16_t device = stoul(string(udevice), NULL, 16); + if (upci_id_to_name(udev_device, devname)) + dev = devname; bus = "PCI"; - if (vendor && device) { - char filename[4096]; - dev = pci_id_to_name(vendor, device, filename, 4095); - } } if (!udevice_has_runtime_pm(udev_device))
Signature · 1 line
2.4.1
Arjan van de Ven
Re: [Powertop] [RFC 6/9] replace pciutils usage with udevhwdb
Quoted 1 line · Nanley Chery
Signed-off-by Nanley Chery <nanley.g.chery(a)intel.com>
no commit message?
also on some OSes the udevhwdb for PCI names is empty and the pciutils explicit file is needed
Nanley Chery
Re: [Powertop] [RFC 6/9] replace pciutils usage with udevhwdb
Quoted 6 lines · Arjan van de Ven
Quoted 2 lines · Nanley Chery
Signed-off-by Nanley Chery <nanley.g.chery(a)intel.com>no commit message?
I've been sitting on these patches for a while and just wanted to get them out into the open. Most of my comments are in the cover letter. I'll try to be better about commit messages in the future however.
Quoted 3 lines
also on some OSes the udevhwdb for PCI names is empty and the pciutils explicit file is needed
I can't think of any work-around to this issue. pciutils will have to stick around.
Nanley Chery
RFC 7/9 runtime_pm: use udev
In reply to Nanley Chery--- src/devices/runtime_pm.cpp | 98 +++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 71 deletions(-)
src/devices/runtime_pm.cpp +27 -71
@@ -29,7 +29,6 @@ #include <stdio.h> #include <unistd.h> #include <sys/types.h> -#include <dirent.h> #include "../parameters/parameters.h" #include "../lib.h"
@@ -178,85 +177,42 @@ int device_has_runtime_pm(const char *sysfs_path) return 0; } -static void do_bus(const char *bus) +static void do_bus(const char *filename) { /* /sys/bus/pci/devices/0000\:00\:1f.0/power/runtime_suspended_time */ + struct udev_device *udevice = udev_device_new_from_syspath(get_udev(), filename); + const char * name = udev_device_get_sysname(udevice); + const char * bus = udev_device_get_subsystem(udevice); + runtime_pmdevice *dev = new runtime_pmdevice(name, filename); + + if (strcmp(bus, "i2c") == 0) { + char dev_name[4096]; + const char *new_dev = udev_device_get_sysattr_value(udevice, "new_device"); + const char *devname = udev_device_get_sysattr_value(udevice, "name"); + sprintf(dev_name, _("I2C Device: %s"), devname); + sprintf(dev_name, _("I2C %s (%s): %s"), (new_dev ? _("Adapter") : _("Device")), name, devname); + dev->set_human_name(dev_name); + } - struct dirent *entry; - DIR *dir; - char filename[4096]; + if (strcmp(bus, "pci") == 0) { + char dev_name[4096]; + char devname[4096]; - sprintf(filename, "/sys/bus/%s/devices/", bus); - dir = opendir(filename); - if (!dir) - return; - while (1) { - ifstream file; - class runtime_pmdevice *dev; - entry = readdir(dir); - - if (!entry) - break; - if (entry->d_name[0] == '.') - continue; - - dev = new class runtime_pmdevice(entry->d_name, filename); - - if (strcmp(bus, "i2c") == 0) { - string devname; - char dev_name[4096]; - bool is_adapter = false; - - sprintf(filename, "/sys/bus/%s/devices/%s/new_device", bus, entry->d_name); - if (access(filename, W_OK) == 0) - is_adapter = true; - - sprintf(filename, "/sys/bus/%s/devices/%s/name", bus, entry->d_name); - file.open(filename, ios::in); - if (file) { - getline(file, devname); - file.close(); - } - - sprintf(dev_name, _("I2C %s (%s): %s"), (is_adapter ? _("Adapter") : _("Device")), entry->d_name, devname.c_str()); + if (upci_id_to_name(udevice, devname)) { + sprintf(dev_name, _("PCI Device: %s"), devname); dev->set_human_name(dev_name); } - - if (strcmp(bus, "pci") == 0) { - uint16_t vendor = 0, device = 0; - - sprintf(filename, "/sys/bus/%s/devices/%s/vendor", bus, entry->d_name); - - file.open(filename, ios::in); - if (file) { - file >> hex >> vendor; - file.close(); - } - - - sprintf(filename, "/sys/bus/%s/devices/%s/device", bus, entry->d_name); - file.open(filename, ios::in); - if (file) { - file >> hex >> device; - file.close(); - } - - if (vendor && device) { - char devname[4096]; - sprintf(devname, _("PCI Device: %s"), - pci_id_to_name(vendor, device, filename, 4095)); - dev->set_human_name(devname); - } - } - all_devices.push_back(dev); } - closedir(dir); + all_devices.push_back(dev); + + /* Cleanup */ + udev_device_unref(udevice); } void create_all_runtime_pm_devices(void) { - do_bus("pci"); - do_bus("spi"); - do_bus("platform"); - do_bus("i2c"); + process_subsystem("pci", do_bus, 0); + process_subsystem("spi", do_bus, 0); + process_subsystem("platform", do_bus, 0); + process_subsystem("i2c", do_bus, 0); }
Signature · 1 line
2.4.1
Nanley Chery
RFC 8/9 remove libpci and most dead code
In reply to Nanley CheryLeave device_has_runtime_pm for now.
--- Android.mk | 1 - README | 1 - configure.ac | 15 --------------- src/Makefile.am | 7 ++----- src/lib.cpp | 45 --------------------------------------------- src/lib.h | 2 -- src/main.cpp | 3 --- 7 files changed, 2 insertions(+), 72 deletions(-)
Android.mk +0 -1
@@ -4,7 +4,6 @@ include $(CLEAR_VARS) LOCAL_MODULE_TAGS := debug LOCAL_SHARED_LIBRARIES := libstlport \ libnl \ - libpci \ libtraceevnet \ LOCAL_MODULE := powertop
README +0 -1
@@ -20,7 +20,6 @@ development environment. in addition to that, PowerTOP requires the following components: -pciutils-devel (is only required if you have PCI) ncurses-devel (required) libnl-devel (required) kernel version => 2.6.38
configure.ac +0 -15
@@ -117,11 +117,6 @@ PKG_CHECK_MODULES([NCURSES], [ncursesw ncurses], [LIBS="$LIBS $ncurses_LIBS"], [ ], []) ]) -has_libpci=0 -PKG_CHECK_MODULES([PCIUTILS], [libpci],[has_libpci=1], [ - AC_SEARCH_LIBS([pci_get_dev], [pci],[has_libpci=1], [has_libpci=0]) -]) - has_libnl_ver=0 dnl libnl-2 provides only libnl-2.0.pc file, so we check for separate
@@ -138,16 +133,6 @@ AS_IF([test "$has_libnl_ver" -gt 1], [ AC_DEFINE([HAVE_LIBNL20], [1], [Define if you have libnl-2.0 or higher]) ]) -AS_IF([test "$has_libpci" -eq 0], [ - AC_DEFINE([HAVE_NO_PCI], [1], [Define if pci is not supported]) - AC_MSG_WARN([ - ************* LIBPCI SUPPORT NOT CONFIGURED************** - If you need or want pci support, please install libpci - and re-configure PowerTOP. - ********************************************************* - ]) -]) - AC_SEARCH_LIBS([inet_aton], [resolv], [], [ AC_MSG_ERROR([libresolv is required but was not found]) ], [])
src/Makefile.am +2 -5
@@ -133,8 +133,7 @@ powertop_CXXFLAGS = \ $(GLIB2_CFLAGS) \ $(LIBNL_CFLAGS) \ $(NCURSES_CFLAGS) \ - $(UDEV_CFLAGS) \ - $(PCIUTILS_CFLAGS) + $(UDEV_CFLAGS) powertop_CPPFLAGS = \
@@ -143,8 +142,7 @@ powertop_CPPFLAGS = \ $(LIBNL_CFLAGS) \ $(LIBZ_CFLAGS) \ $(NCURSES_CFLAGS) \ - $(UDEV_CFLAGS) \ - $(PCIUTILS_CFLAGS) + $(UDEV_CFLAGS) powertop_LDADD = \ ../traceevent/libtraceevnet.la
@@ -155,7 +153,6 @@ AM_LDFLAGS = \ $(LIBZ_LIBS) \ $(NCURSES_LIBS) \ $(UDEV_LIBS) \ - $(PCIUTILS_LIBS) \ $(PTHREAD_LIBS) \ $(RESOLV_LIBS)
src/lib.cpp +0 -45
59 lines not on this page · raw message
src/lib.h +0 -2
@@ -69,8 +69,6 @@ extern string read_sysfs_string(const char *format, const char *param);extern void format_watts (double W ,char *buffer,unsigned int len);extern const char *upci_id_to_name (struct udev_device * dev,char * buffer); -extern char *pci_id_to_name (uint16_t vendor,uint16_t device,char *buffer,int len); -extern void end_pci_access (void );extern struct udev *get_udev ();extern void cleanup_udev ();
src/main.cpp +0 -3
@@ -282,7 +282,6 @@ void make_report(int time, char *workload, int iterations, char *file) learn_parameters(50, 0); save_all_results("saved_results.powertop"); save_parameters("saved_parameters.powertop"); - end_pci_access(); exit(0); }
@@ -475,7 +474,6 @@ int main(int argc, char **argv) if (debug_learning) { learn_parameters(1000, 1); dump_parameter_bundle(); - end_pci_access(); exit(0); } if (!auto_tune)
@@ -512,7 +510,6 @@ int main(int argc, char **argv) save_parameters("saved_parameters.powertop"); learn_parameters(500, 0); save_parameters("saved_parameters.powertop"); - end_pci_access(); clear_tuning(); reset_display();
Signature · 1 line
2.4.1
Nanley Chery
RFC 9/9 calibrate: use udev
In reply to Nanley Chery--- src/calibrate/calibrate.cpp | 44 ++++++++++++-------------------------------- 1 file changed, 12 insertions(+), 32 deletions(-)
src/calibrate/calibrate.cpp +12 -32
@@ -88,29 +88,16 @@ static void restore_all_sysfs(void) static void find_all_usb_callback(const char *d_name) { char filename[4096]; - ifstream file; - - sprintf(filename, "/sys/bus/usb/devices/%s/power/active_duration", d_name); - if (access(filename, R_OK) != 0) - return; - - sprintf(filename, "/sys/bus/usb/devices/%s/power/idVendor", d_name); - file.open(filename, ios::in); - if (file) { - file.getline(filename, 4096); - file.close(); - if (strcmp(filename, "1d6b") == 0) - return; - } - - sprintf(filename, "/sys/bus/usb/devices/%s/power/control", d_name); + sprintf(filename, "%s/power/control", d_name); save_sysfs(filename); usb_devices.push_back(filename); } static void find_all_usb(void) { - process_directory("/sys/bus/usb/devices/", find_all_usb_callback); + process_subsystem("usb", find_all_usb_callback, 2, + "+power/active_duration", NULL, + "-power/idVendor", "1d6b"); } static void suspend_all_usb_devices(void)
@@ -124,16 +111,14 @@ static void suspend_all_usb_devices(void) static void find_all_rfkill_callback(const char *d_name) { char filename[4096]; - sprintf(filename, "/sys/class/rfkill/%s/soft", d_name); - if (access(filename, R_OK) != 0) - return; + sprintf(filename, "%s/soft", d_name); save_sysfs(filename); rfkill_devices.push_back(filename); } static void find_all_rfkill(void) { - process_directory("/sys/class/rfkill/", find_all_rfkill_callback); + process_subsystem("rfkill", find_all_rfkill_callback, 1, "+soft", NULL); } static void rfkill_all_radios(void)
@@ -154,19 +139,16 @@ static void unrfkill_all_radios(void) static void find_backlight_callback(const char *d_name) { char filename[4096]; - sprintf(filename, "/sys/class/backlight/%s/brightness", d_name); - if (access(filename, R_OK) != 0) - return; - + sprintf(filename, "%s/brightness", d_name); save_sysfs(filename); backlight_devices.push_back(filename); - sprintf(filename, "/sys/class/backlight/%s/max_brightness", d_name); + sprintf(filename, "%s/max_brightness", d_name); blmax = read_sysfs(filename); } static void find_backlight(void) { - process_directory("/sys/class/backlight/", find_backlight_callback); + process_subsystem("backlight", find_backlight_callback, 1, "+brightness", NULL); } static void lower_backlight(void)
@@ -180,17 +162,15 @@ static void lower_backlight(void) static void find_scsi_link_callback(const char *d_name) { char filename[4096]; - sprintf(filename, "/sys/class/scsi_host/%s/link_power_management_policy", d_name); - if (access(filename, R_OK)!=0) - return; - + sprintf(filename, "%s/link_power_management_policy", d_name); save_sysfs(filename); scsi_link_devices.push_back(filename); } static void find_scsi_link(void) { - process_directory("/sys/class/scsi_host/", find_scsi_link_callback); + process_subsystem("scsi_host", find_scsi_link_callback, 1, + "+link_power_management_policy", NULL); } static void set_scsi_link(const char *state)
Signature · 1 line
2.4.1
Arjan van de Ven
Quoted 6 lines · Nanley Chery
From: Nanley Chery <nanley.g.chery(a)intel.com>
This patch series changes PowerTOP's method of accessing sysfs (for tunables mainly) from explicitly accessing files to using the standard udev device management library and API.
Quoted 7 lines
The current codebase collects data on devices by probing sysfs files directly with the usual low-level file handling API. This method is not recommended by the Linux kernel maintainers because the sysfs organization is not guaranteed to be stable over time. This can lead to dependent applications breaking with newer kernel releases. Instead using an abstraction layer, like udev, is suggested
(see https://www.kernel.org/doc/Documentation/sysfs-rules.txt).
.... how does this work on Android ?
Nanley Chery
Quoted 17 lines · Arjan van de Ven
Quoted 7 lines · Nanley Chery
From: Nanley Chery <nanley.g.chery(a)intel.com>
This patch series changes PowerTOP's method of accessing sysfs (for tunables mainly) from explicitly accessing files to using the standard udev device management library and API.
Quoted 7 lines
The current codebase collects data on devices by probing sysfs files directly with the usual low-level file handling API. This method is not recommended by the Linux kernel maintainers because the sysfs organization is not guaranteed to be stable over time. This can lead to dependent applications breaking with newer kernel releases. Instead using an abstraction layer, like udev, is suggested
(see https://www.kernel.org/doc/Documentation/sysfs-rules.txt).
Quoted 1 line
.... how does this work on Android ?
That's a good question. I actually hadn't considered the affects on that platform. It seems like there is no udev-like abstraction layer on Android. It looks like the way forward would be to either keep a separate folder for android specific code or to create more helper functions to abstract how we access sysfs. The latter method seems preferable because it wouldn't increase the complexity of the project as a whole. Thoughts?
- Nanley
Arjan van de Ven
Quoted 10 lines · Nanley Chery
Quoted 2 lines
.... how does this work on Android ?
That's a good question. I actually hadn't considered the affects on that platform. It seems like there is no udev-like abstraction layer on Android. It looks like the way forward would be to either keep a separate folder for android specific code or to create more helper functions to abstract how we access sysfs. The latter method seems preferable because it wouldn't increase the complexity of the project as a whole. Thoughts?
but if we keep the code anyway, what's the benefit of going through one more layer of indirection????
Am I missing something here?
Nanley Chery
Quoted 15 lines · Arjan van de Ven
Quoted 11 lines · Nanley Chery
Quoted 2 lines
.... how does this work on Android ?
That's a good question. I actually hadn't considered the affects on that platform. It seems like there is no udev-like abstraction layer on Android. It looks like the way forward would be to either keep a separate folder for android specific code or to create more helper functions to abstract how we access sysfs. The latter method seems preferable because it wouldn't increase the complexity of the project as a whole. Thoughts?
Quoted 3 lines
but if we keep the code anyway, what's the benefit of going through one more layer of indirection????
Am I missing something here?
The main benefit would be increased protection from sysfs reorganizations with progressive kernel releases. If this is actually something we should be proactive about, I am not sure. Udev should provide faster reading of sysfs files due to caching, but I do not have any hard numbers to back this claim up. Lastly there are some general wins in the realm of software readability and maintainability (in my opinion). This should make the process of adding more features and fixing bugs easier.
- Nanley