El 18/06/08 23:55 Kok, Auke escribió: > Felipe Sateler wrote: > > I see that powertop 1.10 reads the sysfs interface to acpi. Great. > > However, I think that the reading is currently broken, for the following > > reasons: > > > > 1. It reads /sys/class/power_supply/<device>/energy_now which (at least > > on my debian box) doesn't exist. I think it should read charge_now. > > 2. It reads current_now as watts_drawn, I think it should be > > amperes_drawn > > > > It also appears that the units are not correctly calculated too. > > ok. feel free to take a stab at making a patch and post it here. I'll be > offline for a week but certainly will look into this next week when I get > back.
OK, I took a shot at fixing this. The problem is that there are several charge sources available from sysfs, depending on the capabilities of the battery. In my case, the battery wasn't providing the energy_now attribute which tells the amount of µWh left, but I have charge_now which has the µAh left. Thus, I changed powertop.c to read both, and use energy_now if available, and use charge_now*voltage otherwise (which should be a reasonable approximation). Saludos, Felipe Sateler
Binary files ../powertop-1.10/.display.c.swp and ./.display.c.swp differ
diff -Nru ../powertop-1.10/powertop.c ./powertop.c
--- ../powertop-1.10/powertop.c 2008-06-10 16:18:45.000000000 -0400
+++ ./powertop.c 2008-06-18 23:53:13.000000000 -0400
@@ -586,8 +586,8 @@
int dontcount = 0;
double voltage = 0.0;
double amperes_drawn = 0.0;
- double watts_drawn = 0.0;
double watts_left = 0.0;
+ double coulombs_left = 0.0;
char line[1024];
if (strstr(dirent->d_name, "AC"))
@@ -627,13 +627,31 @@
sprintf(filename, "/sys/class/power_supply/%s/energy_now", dirent->d_name);
file = fopen(filename, "r");
- if (!file)
- continue;
- memset(line, 0, 1024);
- if (fgets(line, 1024, file) != NULL) {
- watts_left = strtoull(line, NULL, 10) / 1000000.0;
+ if (file) {
+ memset(line, 0, 1024);
+ if (fgets(line, 1024, file) != NULL) {
+ watts_left = strtoull(line, NULL, 10) / 1000000.0;
+ }
+ fclose(file);
}
- fclose(file);
+ else
+ watts_left = -1.0;
+
+ sprintf(filename, "/sys/class/power_supply/%s/charge_now", dirent->d_name);
+ file = fopen(filename, "r");
+ if (file) {
+ memset(line, 0, 1024);
+ if (fgets(line, 1024, file) != NULL) {
+ coulombs_left = strtoull(line, NULL, 10) / 1000000.0;
+ }
+ fclose(file);
+ }
+ else
+ coulombs_left = -1.0;
+
+ /* We need at least one to guess battery capacity */
+ if (watts_left < 0 && coulombs_left < 0)
+ continue;
sprintf(filename, "/sys/class/power_supply/%s/current_now", dirent->d_name);
file = fopen(filename, "r");
@@ -641,14 +659,19 @@
continue;
memset(line, 0, 1024);
if (fgets(line, 1024, file) != NULL) {
- watts_drawn = strtoull(line, NULL, 10) / 1000000.0;
+ amperes_drawn = strtoull(line, NULL, 10) / 1000000.0;
}
fclose(file);
if (!dontcount) {
- rate += watts_drawn + voltage * amperes_drawn;
+ rate += voltage * amperes_drawn;
}
- cap += watts_left;
+ /* If the battery reports a value, we use it
+ * else guess it */
+ if ( watts_left > 0 )
+ cap += watts_left;
+ else
+ cap += coulombs_left*voltage;
}
diff -Nru ../powertop-1.10/sysfs.diff ./sysfs.diff
--- ../powertop-1.10/sysfs.diff 1969-12-31 21:00:00.000000000 -0300
+++ ./sysfs.diff 2008-06-18 23:55:13.000000000 -0400
@@ -0,0 +1,75 @@
+Binary files ../powertop-1.10/.display.c.swp and ./.display.c.swp differ
+diff -Nru ../powertop-1.10/powertop.c ./powertop.c
+--- ../powertop-1.10/powertop.c 2008-06-10 16:18:45.000000000 -0400
++++ ./powertop.c 2008-06-18 23:53:13.000000000 -0400
+@@ -586,8 +586,8 @@
+ int dontcount = 0;
+ double voltage = 0.0;
+ double amperes_drawn = 0.0;
+- double watts_drawn = 0.0;
+ double watts_left = 0.0;
++ double coulombs_left = 0.0;
+ char line[1024];
+
+ if (strstr(dirent->d_name, "AC"))
+@@ -627,13 +627,31 @@
+
+ sprintf(filename, "/sys/class/power_supply/%s/energy_now", dirent->d_name);
+ file = fopen(filename, "r");
+- if (!file)
+- continue;
+- memset(line, 0, 1024);
+- if (fgets(line, 1024, file) != NULL) {
+- watts_left = strtoull(line, NULL, 10) / 1000000.0;
++ if (file) {
++ memset(line, 0, 1024);
++ if (fgets(line, 1024, file) != NULL) {
++ watts_left = strtoull(line, NULL, 10) / 1000000.0;
++ }
++ fclose(file);
+ }
+- fclose(file);
++ else
++ watts_left = -1.0;
++
++ sprintf(filename, "/sys/class/power_supply/%s/charge_now", dirent->d_name);
++ file = fopen(filename, "r");
++ if (file) {
++ memset(line, 0, 1024);
++ if (fgets(line, 1024, file) != NULL) {
++ coulombs_left = strtoull(line, NULL, 10) / 1000000.0;
++ }
++ fclose(file);
++ }
++ else
++ coulombs_left = -1.0;
++
++ /* We need at least one to guess battery capacity */
++ if (watts_left < 0 && coulombs_left < 0)
++ continue;
+
+ sprintf(filename, "/sys/class/power_supply/%s/current_now", dirent->d_name);
+ file = fopen(filename, "r");
+@@ -641,14 +659,19 @@
+ continue;
+ memset(line, 0, 1024);
+ if (fgets(line, 1024, file) != NULL) {
+- watts_drawn = strtoull(line, NULL, 10) / 1000000.0;
++ amperes_drawn = strtoull(line, NULL, 10) / 1000000.0;
+ }
+ fclose(file);
+
+ if (!dontcount) {
+- rate += watts_drawn + voltage * amperes_drawn;
++ rate += voltage * amperes_drawn;
+ }
+- cap += watts_left;
++ /* If the battery reports a value, we use it
++ * else guess it */
++ if ( watts_left > 0 )
++ cap += watts_left;
++ else
++ cap += coulombs_left*voltage;
+
+
+ }
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ Power mailing list [email protected] http://www.bughost.org/mailman/listinfo/power
