aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Senozhatsky <sergey.senozhatsky@gmail.com>2011-04-15 15:09:57 +0300
committerKristen Carlson Accardi <kristen@linux.intel.com>2011-04-21 15:54:29 -0700
commit97333592bb2efcd67ef03b9d000f2403cba0b9d9 (patch)
treea5923ea985a3f017ab0bdd2c2b9eab3d632a7071
parent343fec8dc488935027a9e971532a4d79d8a694e1 (diff)
downloadpowertop-97333592bb2efcd67ef03b9d000f2403cba0b9d9.tar.gz
remove "/sys/class/net/" traverse code duplicate
device/network and tuning/ethernet had the same "/sys/class/net/" directory traverse code with the only difference in post-actions (filling all_tunables or nics/all_devices). - Remove duplicate code and introduce function callback to perform mentioned post-actions: . netdev_callback -- (default) to fill nics and all_devices . ethtunable_callback -- to fill all_tunables. Signature of create_all_nics has been changed accordingly to accept `typedef void (*callback)(const char*);' and "/sys/class/net/" traverse moved to read_all_nics() (from original code removed unused `ifstream file', char filename[4096] replaced with std::string later in netdev_callback). - network::network(char*, char*) has been changed to accept const char* parameters (T* to const T* cast is trivial), stack usage optimized by replacing `char filename[4096]' with std::string. Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
-rw-r--r--devices/device.h3
-rw-r--r--devices/network.cpp42
-rw-r--r--devices/network.h6
-rw-r--r--lib.h2
-rw-r--r--po/powertop.pot512
-rw-r--r--tuning/ethernet.cpp30
-rw-r--r--tuning/ethernet.h1
7 files changed, 41 insertions, 555 deletions
diff --git a/devices/device.h b/devices/device.h
index cc444f1..20b20ec 100644
--- a/devices/device.h
+++ b/devices/device.h
@@ -77,7 +77,6 @@ extern void report_devices(void);
extern void html_report_devices(void);
-
extern void create_all_devices(void);
-#endif \ No newline at end of file
+#endif
diff --git a/devices/network.cpp b/devices/network.cpp
index e350eec..9be4401 100644
--- a/devices/network.cpp
+++ b/devices/network.cpp
@@ -107,10 +107,10 @@ static void do_proc_net_dev(void)
}
-network::network(char *_name, char *path): device()
+network::network(const char *_name, const char *path): device()
{
char line[4096];
- char filename[4096];
+ std::string filename(path);
char devname[128];
start_up = 0;
end_up = 0;
@@ -155,9 +155,9 @@ network::network(char *_name, char *path): device()
rindex_pkts = get_result_index(devname);
memset(line, 0, 4096);
- sprintf(filename, "%s/device/driver", path);
- if (readlink(filename, line, 4096) > 0) {
- sprintf(humanname, _("Network interface: %s (%s)"),_name, basename(line));
+ filename.append("/device/driver");
+ if (readlink(filename.c_str(), line, 4096) > 0) {
+ sprintf(humanname, _("Network interface: %s (%s)"), _name, basename(line));
};
}
@@ -327,17 +327,14 @@ const char * network::device_name(void)
return name;
}
-void create_all_nics(void)
+void read_all_nics(callback fn)
{
struct dirent *entry;
DIR *dir;
- char filename[4096];
dir = opendir("/sys/class/net/");
if (!dir)
return;
while (1) {
- class network *bl;
- ifstream file;
entry = readdir(dir);
if (!entry)
break;
@@ -346,16 +343,31 @@ void create_all_nics(void)
if (strcmp(entry->d_name, "lo")==0)
continue;
- sprintf(filename, "/sys/class/net/%s", entry->d_name);
- bl = new class network(entry->d_name, filename);
- all_devices.push_back(bl);
- nics[entry->d_name] = bl;
+ fn(entry->d_name);
}
+
closedir(dir);
+}
+void netdev_callback(const char *d_name)
+{
+ std::string f_name("/sys/class/net/");
+ f_name.append(d_name);
+
+ network *bl = new(std::nothrow) class network(d_name, f_name.c_str());
+ if (bl) {
+ all_devices.push_back(bl);
+ nics[d_name] = bl;
+ }
}
+void create_all_nics(callback fn)
+{
+ if (!fn)
+ fn = &netdev_callback;
+ read_all_nics(fn);
+}
double network::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
{
@@ -369,8 +381,6 @@ double network::power_usage(struct result_bundle *result, struct parameter_bundl
power += util * factor;
-
-
if (valid_100 == -1) {
valid_100 = utilization_power_valid(rindex_link_100);
valid_1000 = utilization_power_valid(rindex_link_1000);
@@ -411,4 +421,4 @@ double network::power_usage(struct result_bundle *result, struct parameter_bundl
power += util * factor / 100;
return power;
-} \ No newline at end of file
+}
diff --git a/devices/network.h b/devices/network.h
index 50bf61e..76e17ac 100644
--- a/devices/network.h
+++ b/devices/network.h
@@ -62,7 +62,7 @@ public:
uint64_t pkts;
double duration;
- network(char *_name, char *path);
+ network(const char *_name, const char *path);
virtual void start_measurement(void);
virtual void end_measurement(void);
@@ -79,7 +79,7 @@ public:
virtual int grouping_prio(void) { return 10; };
};
-extern void create_all_nics(void);
+extern void create_all_nics(callback fn = NULL);
+#endif
-#endif \ No newline at end of file
diff --git a/lib.h b/lib.h
index f6f924f..273c875 100644
--- a/lib.h
+++ b/lib.h
@@ -71,6 +71,8 @@ extern char *fmt_prefix(double n, char *buf);
extern char *pretty_print(const char *str, char *buf, int len);
extern int equals(double a, double b);
+typedef void (*callback)(const char*);
extern int utf_ok;
+
#endif
diff --git a/po/powertop.pot b/po/powertop.pot
deleted file mode 100644
index ba6224a..0000000
--- a/po/powertop.pot
+++ /dev/null
@@ -1,512 +0,0 @@
-# SOME DESCRIPTIVE TITLE.
-# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
-# This file is distributed under the same license as the PACKAGE package.
-# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
-#
-#, fuzzy
-msgid ""
-msgstr ""
-"Project-Id-Version: PACKAGE VERSION\n"
-"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2011-04-21 13:48-0700\n"
-"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
-"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
-"Language-Team: LANGUAGE <LL@li.org>\n"
-"Language: \n"
-"MIME-Version: 1.0\n"
-"Content-Type: text/plain; charset=CHARSET\n"
-"Content-Transfer-Encoding: 8bit\n"
-
-#: process/do_process.cpp:664
-msgid " Usage Events/s Category Description\n"
-msgstr ""
-
-#: devices/device.cpp:183
-msgid " Usage Device name\n"
-msgstr ""
-
-#: lib.cpp:237
-#, c-format
-msgid " 0 mW"
-msgstr ""
-
-#: cpu/cpu_core.cpp:46 cpu/cpu_core.cpp:190 cpu/intel_cpus.cpp:285
-#, c-format
-msgid " Core"
-msgstr ""
-
-#: cpu/cpu_package.cpp:104 cpu/intel_cpus.cpp:311
-#, c-format
-msgid " Package"
-msgstr ""
-
-#: display.cpp:130
-msgid " <ESC> Exit | "
-msgstr ""
-
-#: tuning/tuning.cpp:60
-msgid " <ESC> Exit | <Enter> Toggle tunable"
-msgstr ""
-
-#: cpu/cpu_linux.cpp:231 cpu/cpu_linux.cpp:289 cpu/intel_cpus.cpp:594
-#, c-format
-msgid " CPU %i"
-msgstr ""
-
-#: lib.cpp:93
-#, c-format
-msgid "%3.1f Ghz"
-msgstr ""
-
-#: lib.cpp:91
-#, c-format
-msgid "%4.2f Ghz"
-msgstr ""
-
-#: lib.cpp:84
-#, c-format
-msgid "%4lli Mhz"
-msgstr ""
-
-#: lib.cpp:86
-#, c-format
-msgid "%6lli Mhz"
-msgstr ""
-
-#: lib.cpp:234
-#, c-format
-msgid "%7sW"
-msgstr ""
-
-#: lib.cpp:80
-#, c-format
-msgid "%9lli"
-msgstr ""
-
-#: tuning/runtime.cpp:49
-#, c-format
-msgid "%s device %s has no runtime power management"
-msgstr ""
-
-#: main.cpp:81
-#, c-format
-msgid "--calibrate \t runs powertop in calibration mode\n"
-msgstr ""
-
-#: main.cpp:79
-#, c-format
-msgid "--debug \t run in \"debug\" mode\n"
-msgstr ""
-
-#: main.cpp:83
-#, c-format
-msgid "--help \t\t print this help menu\n"
-msgstr ""
-
-#: main.cpp:82
-#, c-format
-msgid "--html[=FILENAME]\t\t generate a html report\n"
-msgstr ""
-
-#: main.cpp:80
-#, c-format
-msgid "--version \t print version information\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:350 calibrate/calibrate.cpp:367
-#: calibrate/calibrate.cpp:375 calibrate/calibrate.cpp:392
-#, c-format
-msgid ".... device %s \n"
-msgstr ""
-
-#: devlist.cpp:308
-#, c-format
-msgid "<h2>Process device activity</h2>\n"
-msgstr ""
-
-#: devlist.cpp:310
-#, c-format
-msgid ""
-"<tr><th class=\"device\" width=40%%>Process</th><th class=\"device\">Device</"
-"th></tr>\n"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:577
-#, c-format
-msgid "Actual"
-msgstr ""
-
-#: devices/alsa.cpp:80 devices/alsa.cpp:82
-#, c-format
-msgid "Audio codec %s: %s"
-msgstr ""
-
-#: devices/alsa.cpp:78
-#, c-format
-msgid "Audio codec %s: %s (%s)"
-msgstr ""
-
-#: tuning/usb.cpp:72 tuning/usb.cpp:74 tuning/usb.cpp:76
-#, c-format
-msgid "Autosuspend for USB device %s [%s]"
-msgstr ""
-
-#: tuning/usb.cpp:55
-#, c-format
-msgid "Autosuspend for unknown USB device %s (%s:%s)"
-msgstr ""
-
-#: tuning/cpufreq.cpp:42 tuning/ethernet.cpp:50 tuning/runtime.cpp:41
-#: tuning/sysfs.cpp:37 tuning/tunable.cpp:49 tuning/usb.cpp:40
-#: tuning/wifi.cpp:45
-msgid "Bad"
-msgstr ""
-
-#: tuning/bluetooth.cpp:48
-#, c-format
-msgid "Bluetooth device interface status"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:518
-msgid "C0 active"
-msgstr ""
-
-#: cpu/cpu_linux.cpp:92
-msgid "C0 polling"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:340
-msgid "C2 (pc2)"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:92
-msgid "C3 (cc3)"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:342
-msgid "C3 (pc3)"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:93
-msgid "C6 (cc6)"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:343
-msgid "C6 (pc6)"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:95
-msgid "C7 (cc7)"
-msgstr ""
-
-#: cpu/intel_cpus.cpp:345
-msgid "C7 (pc7)"
-msgstr ""
-
-#: perf/perf.cpp:121
-#, c-format
-msgid ""
-"CONFIG_PERF_EVENTS=y\n"
-"CONFIG_PERF_COUNTERS=y\n"
-"CONFIG_TRACEPOINTS=y\n"
-"CONFIG_TRACING=y\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:348
-#, c-format
-msgid "Calibrating USB devices\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:389
-#, c-format
-msgid "Calibrating backlight\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:413 calibrate/calibrate.cpp:421
-#, c-format
-msgid "Calibrating idle\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:365
-#, c-format
-msgid "Calibrating radio devices\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:316
-#, c-format
-msgid "Calibrating: CPU usage on %i threads\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:331
-#, c-format
-msgid "Calibrating: CPU wakeup power consumption\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:432
-#, c-format
-msgid "Calibrating: disk usage \n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:297
-#, c-format
-msgid "Cannot create temp file\n"
-msgstr ""
-
-#: parameters/persistent.cpp:72 parameters/persistent.cpp:152
-msgid "Cannot load from file "
-msgstr ""
-
-#: parameters/persistent.cpp:43 parameters/persistent.cpp:130
-msgid "Cannot save to file "
-msgstr ""
-
-#: display.cpp:73
-msgid "Device stats"
-msgstr ""
-
-#: tuning/tuning.cpp:61
-msgid "Enable Audio codec power management"
-msgstr ""
-
-#: tuning/tuning.cpp:62
-msgid "Enable SATA link power management for /dev/sda"
-msgstr ""
-
-#: process/do_process.cpp:636
-#, c-format
-msgid ""
-"Estimated power: %5.1f Measured power: %5.1f Sum: %5.1f\n"
-"\n"
-msgstr ""
-
-#: calibrate/calibrate.cpp:480
-msgid "Finishing PowerTOP power estimate calibration \n"
-msgstr ""
-
-#: main.cpp:85
-#, c-format
-msgid ""
-"For more help please refer to the README\n"
-"\n"
-msgstr ""
-
-#: display.cpp:72
-msgid "Frequency stats"
-msgstr ""
-
-#: tuning/cpufreq.cpp:42 tuning/ethernet.cpp:50 tuning/runtime.cpp:41
-#: tuning/sysfs.cpp:37 tuning/tunable.cpp:48 tuning/usb.cpp:40
-#: tuning/wifi.cpp:45
-msgid "Good"
-msgstr ""
-
-#: cpu/cpu_core.cpp:116 cpu/cpu_linux.cpp:329 cpu/cpu_package.cpp:145
-#: cpu/intel_cpus.cpp:211 cpu/intel_cpus.cpp:446 cpu/intel_cpus.cpp:647
-msgid "Idle"
-msgstr ""
-
-#: display.cpp:71
-msgid "Idle stats"
-msgstr ""
-
-#: lib.cpp:347
-msgid "Intel built in USB hub"
-msgstr ""
-
-#: main.cpp:317
-#, c-format
-msgid "Leaving PowerTOP\n"
-msgstr ""
-
-#: parameters/persistent.cpp:116
-#, c-format
-msgid "Loaded %i prior measurements\n"
-msgstr ""
-
-#: main.cpp:258
-#, c-format
-msgid "Measuring for 20 seconds\n"
-msgstr ""
-
-#: tuning/tuning.cpp:63
-msgid "NMI watchdog should be turned off"
-msgstr ""
-
-#: devices/network.cpp:160
-#, c-format
-msgid "Network interface: %s (%s)"
-msgstr ""
-
-#: display.cpp:70
-msgid "Overview"
-msgstr ""
-
-#: tuning/runtime.cpp:73
-#, c-format
-msgid "PCI Device %s has no runtime power management"
-msgstr ""
-
-#: devices/runtime_pm.cpp:225
-#, c-format
-msgid "PCI Device: %s"
-msgstr ""
-
-#: lib.cpp:345
-msgid "PS/2 Touchpad / Keyboard / Mouse"
-msgstr ""
-
-#: cpu/cpu_package.cpp:46
-#, c-format
-msgid "Package"
-msgstr ""
-
-#: calibrate/calibrate.cpp:484
-#, c-format
-msgid "Parameters after calibration:\n"
-msgstr ""
-
-#: tuning/tuning.cpp:64
-msgid "Power Aware CPU scheduler"
-msgstr ""
-
-#: process/do_process.cpp:662
-msgid "Power est. Usage Events/s Category Description\n"
-msgstr ""
-
-#: devices/device.cpp:181
-msgid "Power est. Usage Device name\n"
-msgstr ""
-
-#: main.cpp:208
-#, c-format
-msgid "PowerTOP "
-msgstr ""
-
-#: perf/perf.cpp:119
-#, c-format
-msgid "PowerTOP %s needs the kernel to support the 'perf' subsystem\n"
-msgstr ""
-
-#: main.cpp:73
-#, c-format
-msgid "Powertop version"
-msgstr ""
-
-#: devices/rfkill.cpp:64 devices/rfkill.cpp:68
-#, c-format
-msgid "Radio device: %s"
-msgstr ""
-
-#: tuning/runtime.cpp:47
-#, c-format
-msgid "Runtime PM for %s device %s"
-msgstr ""
-
-#: tuning/runtime.cpp:75
-#, c-format
-msgid "Runtime PM for PCI Device %s"
-msgstr ""
-
-#: lib.cpp:346
-msgid "SATA controller"
-msgstr ""
-
-#: devices/ahci.cpp:142
-#, c-format
-msgid "SATA disk: %s"
-msgstr ""
-
-#: devices/ahci.cpp:140
-#, c-format
-msgid "SATA link: %s"
-msgstr ""
-
-#: calibrate/calibrate.cpp:457
-msgid "Starting PowerTOP power estimate calibration \n"
-msgstr ""
-
-#: devices/device.cpp:174
-#, c-format
-msgid "System baseline power is estimated at %sW\n"
-msgstr ""
-
-#: devices/device.cpp:168 process/do_process.cpp:644
-#, c-format
-msgid "The battery reports a discharge rate of %sW\n"
-msgstr ""
-
-#: process/do_process.cpp:649
-#, c-format
-msgid "The estimated remaining time is %i minutes\n"
-msgstr ""
-
-#: tuning/tuning.cpp:60
-msgid "Tunables"
-msgstr ""
-
-#: cpu/cpu_core.cpp:118 cpu/cpu_linux.cpp:331 cpu/cpu_package.cpp:147
-#: cpu/intel_cpus.cpp:213 cpu/intel_cpus.cpp:448
-#, c-format
-msgid "Turbo Mode"
-msgstr ""
-
-#: devices/usb.cpp:51 devices/usb.cpp:94 devices/usb.cpp:96
-#, c-format
-msgid "USB device: %s"
-msgstr ""
-
-#: devices/usb.cpp:92
-#, c-format
-msgid "USB device: %s (%s)"
-msgstr ""
-
-#: tuning/cpufreq.cpp:42 tuning/ethernet.cpp:50 tuning/runtime.cpp:41
-#: tuning/sysfs.cpp:37 tuning/tunable.cpp:50 tuning/usb.cpp:40
-#: tuning/wifi.cpp:45
-msgid "Unknown"
-msgstr ""
-
-#: main.cpp:78
-#, c-format
-msgid ""
-"Usage: powertop [OPTIONS]\n"
-"\n"
-msgstr ""
-
-#: tuning/cpufreq.cpp:45
-#, c-format
-msgid "Using 'ondemand' cpufreq governor"
-msgstr ""
-
-#: tuning/tuning.cpp:65
-msgid "VM writeback timeout"
-msgstr ""
-
-#: tuning/ethernet.cpp:54
-#, c-format
-msgid "Wake-on-lan status for device %s"
-msgstr ""
-
-#: tuning/wifi.cpp:48
-#, c-format
-msgid "Wireless Power Saving for interface %s"
-msgstr ""
-
-#: perf/perf.cpp:120
-#, c-format
-msgid "as well as support for trace points in the kernel:\n"
-msgstr ""
-
-#: cpu/cpu.cpp:88
-msgid "cpu package"
-msgstr ""
-
-#: cpu/cpu.cpp:87
-#, c-format
-msgid "cpu package %i"
-msgstr ""
-
-#: main.cpp:209
-#, c-format
-msgid "exiting...\n"
-msgstr ""
diff --git a/tuning/ethernet.cpp b/tuning/ethernet.cpp
index f13f720..4666ac1 100644
--- a/tuning/ethernet.cpp
+++ b/tuning/ethernet.cpp
@@ -47,6 +47,8 @@
#include "../lib.h"
#include "ethernet.h"
+extern void create_all_nics(callback fn);
+
ethernet_tunable::ethernet_tunable(const char *iface) : tunable("", 0.3, _("Good"), _("Bad"), _("Unknown"))
{
memset(interf, 0, sizeof(interf));
@@ -127,29 +129,15 @@ void ethernet_tunable::toggle(void)
}
-
+void ethtunable_callback(const char *d_name)
+{
+ class ethernet_tunable *eth = new(std::nothrow) class ethernet_tunable(d_name);
+ if (eth)
+ all_tunables.push_back(eth);
+}
void add_ethernet_tunable(void)
{
- class ethernet_tunable *eth;
- struct dirent *entry;
- DIR *dir;
- dir = opendir("/sys/class/net/");
- if (!dir)
- return;
- while ((entry = readdir(dir))) {
- if (!entry)
- break;
- if (entry->d_name[0] == '.')
- continue;
- if (strcmp(entry->d_name, "lo") == 0)
- continue;
-
- eth = new(std::nothrow) class ethernet_tunable(entry->d_name);
- if (eth)
- all_tunables.push_back(eth);
- }
-
- closedir(dir);
+ create_all_nics(&ethtunable_callback);
}
diff --git a/tuning/ethernet.h b/tuning/ethernet.h
index 8050aca..eeb6ebb 100644
--- a/tuning/ethernet.h
+++ b/tuning/ethernet.h
@@ -44,5 +44,4 @@ public:
extern void add_ethernet_tunable(void);
-
#endif