Patrick Winnertz wrote:
> Hello,
> 
> One user of the debian's powertop package wrote a powerpc patch for powertop 
> and would like to see it included into powertop. 
> 
> ATM there is powertop v1.10.. he wrote the patch for 1.9, I hope there is not 
> that much difference
> 
> ps: For a patch see: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=463034

Patrick,

I've significantly modofied the PMU part of the patch - I did not like the
#ifdef's thrown all around the code and I think we can make this much more 
simple
as is by just plugging in the pmu reading code just like we do with the 
sysfs/proc
reading code.

Attached is a modified patch which takes the code from those patches and does a
bit more auto-detection-style reading of the pmu battery info. Can you give 
this a
test run?

I didn't fix the PROC_ symbol discrepancy. I'm thinking of fixing the kernel
symbol testing method in a completely other way.

Cheers,

Auke

Index: display.c
===================================================================
--- display.c   (revision 298)
+++ display.c   (working copy)
@@ -38,7 +38,7 @@
 static WINDOW *title_bar_window;
 static WINDOW *cstate_window;
 static WINDOW *wakeup_window;
-static WINDOW *acpi_power_window;
+static WINDOW *battery_power_window;
 static WINDOW *timerstat_window;
 static WINDOW *suggestion_window;
 static WINDOW *status_bar_window;
@@ -65,9 +65,9 @@
                delwin(wakeup_window);
                wakeup_window = NULL;
        }
-       if (acpi_power_window) {
-               delwin(acpi_power_window);
-               acpi_power_window = NULL;
+       if (battery_power_window) {
+               delwin(battery_power_window);
+               battery_power_window = NULL;
        }
        if (timerstat_window) {
                delwin(timerstat_window);
@@ -98,7 +98,7 @@
        title_bar_window = subwin(stdscr, 1, maxx, 0, 0);
        cstate_window = subwin(stdscr, 7, maxx, 2, 0);
        wakeup_window = subwin(stdscr, 1, maxx, 9, 0);
-       acpi_power_window = subwin(stdscr, 2, maxx, 10, 0);
+       battery_power_window = subwin(stdscr, 2, maxx, 10, 0);
        timerstat_window = subwin(stdscr, maxy-16, maxx, 12, 0);
        maxtimerstats = maxy-16  -2;
        maxwidth = maxx - 18;
@@ -195,7 +195,7 @@
 
        sprintf(buffer,  _("no ACPI power usage estimate available") );
 
-       werase(acpi_power_window);
+       werase(battery_power_window);
        if (rate > 0.001) {
                char *c;
                sprintf(buffer, _("Power usage (ACPI estimate): %3.1fW (%3.1f 
hours)"), rate, cap/rate);
@@ -207,10 +207,48 @@
        else if (ti>120 && capdelta > 0.001)
                sprintf(buffer, _("Power usage (5 minute ACPI estimate) : %5.1f 
W (%3.1f hours left)"), 3600*capdelta / ti, cap / (3600*capdelta/ti+0.01));
 
-       print(acpi_power_window, 0, 0, "%s\n", buffer); 
-       wrefresh(acpi_power_window);
+       print(battery_power_window, 0, 0, "%s\n", buffer);      
+       wrefresh(battery_power_window);
 }
 
+void show_pmu_power_line(unsigned num_batteries, unsigned sum_voltage_mV,
+                         unsigned sum_charge_mAh, unsigned sum_max_charge_mAh,
+                         int sum_discharge_mA)
+{
+       char buffer[1024];
+
+       if (sum_discharge_mA != 0)
+       {
+               unsigned remaining_charge_mAh;
+
+               if (sum_discharge_mA < 0)
+               {
+                       /* we are currently discharging */
+                       sum_discharge_mA = -sum_discharge_mA;
+                       remaining_charge_mAh = sum_charge_mAh;
+               }
+               else
+               {
+                       /* we are currently charging */
+                       remaining_charge_mAh = (sum_max_charge_mAh
+                                               - sum_charge_mAh);
+               }
+
+               snprintf(buffer, sizeof(buffer),
+                        _("Power usage: %3.1fW (%3.1f hours)"),
+                        sum_voltage_mV * sum_discharge_mA / 1e6,
+                        (double)remaining_charge_mAh / sum_discharge_mA);
+       }
+       else
+               snprintf(buffer, sizeof(buffer),
+                        _("no power usage estimate available") );
+
+       werase(battery_power_window);
+       print(battery_power_window, 0, 0, "%s\n", buffer);
+       wrefresh(battery_power_window);
+}
+
+
 void show_wakeups(double d, double interval, double C0time)
 {
        werase(wakeup_window);
Index: powertop.c
===================================================================
--- powertop.c  (revision 298)
+++ powertop.c  (working copy)
@@ -480,7 +480,7 @@
 
 
 
-int print_battery_proc(void)
+int print_battery_proc_acpi(void)
 {
        DIR *dir;
        struct dirent *dirent;
@@ -567,6 +567,79 @@
        return 1;
 }
 
+int print_battery_proc_pmu(void)
+{
+       char line[80];
+       int i;
+       int power_present = 0;
+       int num_batteries = 0;
+       /* unsigned rem_time_sec = 0; */
+       unsigned charge_mAh = 0, max_charge_mAh = 0, voltage_mV = 0;
+       int discharge_mA = 0;
+       FILE *fd;
+
+       fd = fopen("/proc/pmu/info", "r");
+       if (fd == NULL)
+               return 0;
+
+       while ( fgets(line, sizeof(line), fd) != NULL )
+       {
+               if (strncmp("AC Power", line, strlen("AC Power")) == 0)
+                       sscanf(strchr(line, ':')+2, "%d", &power_present);
+               else if (strncmp("Battery count", line, strlen("Battery 
count")) == 0)
+                       sscanf(strchr(line, ':')+2, "%d", &num_batteries);
+       }
+       fclose(fd);
+
+       for (i = 0; i < num_batteries; ++i)
+       {
+               char file_name[20];
+               int flags = 0;
+               /* int battery_charging, battery_full; */
+               /* unsigned this_rem_time_sec = 0; */
+               unsigned this_charge_mAh = 0, this_max_charge_mAh = 0;
+               unsigned this_voltage_mV = 0, this_discharge_mA = 0;
+
+               snprintf(file_name, sizeof(file_name), "/proc/pmu/battery_%d", 
i);
+               fd = fopen(file_name, "r");
+               if (fd == NULL)
+                       continue;
+
+               while (fgets(line, sizeof(line), fd) != NULL)
+               {
+                       if (strncmp("flags", line, strlen("flags")) == 0)
+                               sscanf(strchr(line, ':')+2, "%x", &flags);
+                       else if (strncmp("charge", line, strlen("charge")) == 0)
+                               sscanf(strchr(line, ':')+2, "%d", 
&this_charge_mAh);
+                       else if (strncmp("max_charge", line, 
strlen("max_charge")) == 0)
+                               sscanf(strchr(line, ':')+2, "%d", 
&this_max_charge_mAh);
+                       else if (strncmp("voltage", line, strlen("voltage")) == 
0)
+                               sscanf(strchr(line, ':')+2, "%d", 
&this_voltage_mV);
+                       else if (strncmp("current", line, strlen("current")) == 
0)
+                               sscanf(strchr(line, ':')+2, "%d", 
&this_discharge_mA);
+                       /* else if (strncmp("time rem.", line, strlen("time 
rem.")) == 0) */
+                       /*   sscanf(strchr(line, ':')+2, "%d", 
&this_rem_time_sec); */
+               }
+               fclose(fd);
+
+               if ( !(flags & 0x1) )
+                       /* battery isn't present */
+                       continue;
+
+               /* battery_charging = flags & 0x2; */
+               /* battery_full = !battery_charging && power_present; */
+
+               charge_mAh += this_charge_mAh;
+               max_charge_mAh += this_max_charge_mAh;
+               voltage_mV += this_voltage_mV;
+               discharge_mA += this_discharge_mA;
+               /* rem_time_sec += this_rem_time_sec; */
+       }
+       show_pmu_power_line(num_batteries, voltage_mV, charge_mAh, 
max_charge_mAh,
+                           discharge_mA);
+       return 1;
+}
+
 void print_battery_sysfs(void)
 {
        DIR *dir;
@@ -577,8 +650,11 @@
 
        char filename[256];
        
-       if (print_battery_proc())
+       if (print_battery_proc_acpi())
                return;
+       
+       if (print_battery_proc_pmu())
+               return;
 
        dir = opendir("/sys/class/power_supply");
        if (!dir) {
Index: powertop.h
===================================================================
--- powertop.h  (revision 298)
+++ powertop.h  (working copy)
@@ -103,6 +103,9 @@
 void setup_windows(void);
 void initialize_curses(void);
 void show_acpi_power_line(double rate, double cap, double capdelta, time_t 
time);
+void show_pmu_power_line(unsigned num_batteries, unsigned sum_voltage_mV,
+                         unsigned sum_charge_mAh, unsigned sum_max_charge_mAh,
+                         int sum_discharge_mA);
 void show_cstates(void);
 void show_wakeups(double d, double interval, double c0time);
 void show_timerstats(int nostats, int ticktime);
_______________________________________________
Power mailing list
[email protected]
http://www.bughost.org/mailman/listinfo/power

Reply via email to