diff options
author | Sergey Senozhatsky <sergey.senozhatsky@gmail.com> | 2011-05-04 18:49:19 +0300 |
---|---|---|
committer | Arjan van de Ven <arjan@linux.intel.com> | 2011-05-11 00:04:24 -0400 |
commit | 4c4b72a9fb55681261cfb5a307d9a8e45ecc4943 (patch) | |
tree | 1d134004d3b848185e8af3b43a28e6a07643a920 | |
parent | 8e815ce49a9c5bd6a3849cea081af0dc68d19820 (diff) | |
download | powertop-4c4b72a9fb55681261cfb5a307d9a8e45ecc4943.tar.gz |
Use callback in detect_power_meters() to read /proc/* directory
detect_power_meters() has the same /proc/* directory traverse code as network
and ethernet_tunable did before we switched to callback. Move read_all_nics()
code to process_directory(), which now accepts 2 parameters -- directory to
read and callback; introduce power_meters_callback() to fill power_meters.
Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
-rw-r--r-- | devices/network.cpp | 24 | ||||
-rw-r--r-- | lib.cpp | 21 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | measurement/measurement.cpp | 29 |
4 files changed, 31 insertions, 44 deletions
diff --git a/devices/network.cpp b/devices/network.cpp index 9be4401..5f3ddf2 100644 --- a/devices/network.cpp +++ b/devices/network.cpp @@ -327,28 +327,6 @@ const char * network::device_name(void) return name; } -void read_all_nics(callback fn) -{ - struct dirent *entry; - DIR *dir; - dir = opendir("/sys/class/net/"); - if (!dir) - return; - while (1) { - entry = readdir(dir); - if (!entry) - break; - if (entry->d_name[0] == '.') - continue; - if (strcmp(entry->d_name, "lo")==0) - continue; - - fn(entry->d_name); - } - - closedir(dir); -} - void netdev_callback(const char *d_name) { std::string f_name("/sys/class/net/"); @@ -366,7 +344,7 @@ void create_all_nics(callback fn) { if (!fn) fn = &netdev_callback; - read_all_nics(fn); + process_directory("/sys/class/net/", fn); } double network::power_usage(struct result_bundle *result, struct parameter_bundle *bundle) @@ -372,4 +372,25 @@ int equals(double a, double b) return fabs(a - b) <= std::numeric_limits<double>::epsilon(); } +void process_directory(const char *d_name, callback fn) +{ + struct dirent *entry; + DIR *dir; + dir = opendir(d_name); + if (!dir) + return; + while (1) { + entry = readdir(dir); + if (!entry) + break; + if (entry->d_name[0] == '.') + continue; + if (strcmp(entry->d_name, "lo")==0) + continue; + + fn(entry->d_name); + } + + closedir(dir); +} @@ -72,6 +72,7 @@ extern char *pretty_print(const char *str, char *buf, int len); extern int equals(double a, double b); typedef void (*callback)(const char*); +extern void process_directory(const char *d_name, callback fn); extern int utf_ok; #endif diff --git a/measurement/measurement.cpp b/measurement/measurement.cpp index 1a67363..3dbaab9 100644 --- a/measurement/measurement.cpp +++ b/measurement/measurement.cpp @@ -103,30 +103,17 @@ double global_time_left(void) return total; } - -void detect_power_meters(void) +void power_meters_callback(const char *d_name) { - DIR *dir; - struct dirent *entry; - - dir = opendir("/proc/acpi/battery"); - if (!dir) - return; - while (1) { - class acpi_power_meter *meter; - entry = readdir(dir); - if (!entry) - break; - if (entry->d_name[0] == '.') - continue; - - meter = new class acpi_power_meter(entry->d_name); - + class acpi_power_meter *meter; + meter = new(std::nothrow) class acpi_power_meter(d_name); + if (meter) power_meters.push_back(meter); - - } - closedir(dir); +} +void detect_power_meters(void) +{ + process_directory("/proc/acpi/battery", power_meters_callback); } void extech_power_meter(const char *devnode) |