]> git.ipfire.org Git - people/amarx/ipfire-3.x.git/blob - powertop/patches/powertop-1.98-add-power-supply-class-support.patch
openssl: Fix some unreliable lines in the makefile.
[people/amarx/ipfire-3.x.git] / powertop / patches / powertop-1.98-add-power-supply-class-support.patch
1 commit a1c28f69847480cc54f36046e96e93a02a2c661d
2 Author: John Mathew <johnx.mathew@intel.com>
3 Date: Thu Jun 30 09:58:26 2011 +0300
4
5 measurement: add support for universal power supply monitor
6
7 This patch enables powertop to display power estimates on devices that
8 implement power_supply class and donot have acpi battery interface.
9
10 First the acpi/battery interface is checked and if it is not accesible
11 the sysfs/power_class interface is used
12
13 diff --git a/Makefile b/Makefile
14 index b0b6cb7..f0ebf21 100644
15 --- a/Makefile
16 +++ b/Makefile
17 @@ -13,7 +13,7 @@ OBJS += perf/perf.o perf/perf_bundle.o
18 OBJS += process/process.o process/do_process.o process/interrupt.o process/timer.o process/work.o process/powerconsumer.o process/device.o
19 DEVS += devices/device.o devices/backlight.o devices/usb.o devices/ahci.o devices/alsa.o devices/rfkill.o devices/i915-gpu.o devices/thinkpad-fan.o devices/network.o devices/thinkpad-light.o
20 DEVS += devices/runtime_pm.o
21 -DEVS += measurement/measurement.o measurement/acpi.o measurement/extech.o
22 +DEVS += measurement/measurement.o measurement/acpi.o measurement/extech.o measurement/power_supply.o
23 OBJS += $(DEVS)
24 OBJS += parameters/parameters.o parameters/learn.o parameters/persistent.o
25 OBJS += calibrate/calibrate.o
26 diff --git a/measurement/measurement.cpp b/measurement/measurement.cpp
27 index 3dbaab9..0d679f7 100644
28 --- a/measurement/measurement.cpp
29 +++ b/measurement/measurement.cpp
30 @@ -25,11 +25,15 @@
31 #include "measurement.h"
32 #include "acpi.h"
33 #include "extech.h"
34 +#include "power_supply.h"
35 #include "../parameters/parameters.h"
36 #include "../lib.h"
37
38 #include <sys/types.h>
39 #include <dirent.h>
40 +#include <unistd.h>
41 +#include <stdio.h>
42 +#include <fstream>
43
44 double min_power = 50000.0;
45
46 @@ -111,16 +114,45 @@ void power_meters_callback(const char *d_name)
47 power_meters.push_back(meter);
48 }
49
50 +void power_supply_callback(const char *d_name)
51 +{
52 + char filename[4096];
53 + char line[4096];
54 + ifstream file;
55 + bool discharging = false;
56 +
57 + sprintf(filename, "/sys/class/power_supply/%s/uevent", d_name);
58 + file.open(filename, ios::in);
59 + if (!file)
60 + return;
61 +
62 + while (file) {
63 + file.getline(line, 4096);
64 +
65 + if (strstr(line, "POWER_SUPPLY_STATUS") && strstr(line, "POWER_SUPPLY_STATUS"))
66 + discharging = true;
67 + }
68 + file.close();
69 +
70 + if (!discharging)
71 + return;
72 +
73 + class power_supply *power;
74 + power = new(std::nothrow) class power_supply(d_name);
75 + if (power)
76 + power_meters.push_back(power);
77 +}
78 +
79 void detect_power_meters(void)
80 {
81 - process_directory("/proc/acpi/battery", power_meters_callback);
82 + if (access("/sys/class/power_supply", R_OK ) == 0)
83 + process_directory("/sys/class/power_supply", power_supply_callback);
84 + else if (access("/proc/acpi/battery", R_OK ) == 0)
85 + process_directory("/proc/acpi/battery", power_meters_callback);
86 }
87
88 void extech_power_meter(const char *devnode)
89 {
90 - DIR *dir;
91 - struct dirent *entry;
92 -
93 class extech_power_meter *meter;
94
95 meter = new class extech_power_meter(devnode);
96 diff --git a/measurement/power_supply.cpp b/measurement/power_supply.cpp
97 new file mode 100755
98 index 0000000..b4fe3af
99 --- /dev/null
100 +++ b/measurement/power_supply.cpp
101 @@ -0,0 +1,184 @@
102 +/*
103 + * Copyright 2011, Intel Corporation
104 + *
105 + * This file is part of PowerTOP
106 + *
107 + * This program file is free software; you can redistribute it and/or modify it
108 + * under the terms of the GNU General Public License as published by the
109 + * Free Software Foundation; version 2 of the License.
110 + *
111 + * This program is distributed in the hope that it will be useful, but WITHOUT
112 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
113 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
114 + * for more details.
115 + *
116 + * You should have received a copy of the GNU General Public License
117 + * along with this program in a file named COPYING; if not, write to the
118 + * Free Software Foundation, Inc,
119 + * 51 Franklin Street, Fifth Floor,
120 + * Boston, MA 02110-1301 USA
121 + * or just google for it.
122 + *
123 + * Authors:
124 + * John Mathew <johnx.mathew@intel.com>
125 + */
126 +#include "measurement.h"
127 +#include "power_supply.h"
128 +#include <iostream>
129 +#include <fstream>
130 +#include <string.h>
131 +#include <stdio.h>
132 +#include <stdlib.h>
133 +
134 +using namespace std;
135 +
136 +power_supply::power_supply(const char *supply_name)
137 +{
138 + rate = 0.0;
139 + capacity = 0.0;
140 + voltage = 0.0;
141 + strncpy(battery_name, supply_name, sizeof(battery_name));
142 +}
143 +
144 +/*
145 +POWER_SUPPLY_NAME=msic-battery
146 +POWER_SUPPLY_STATUS=Discharging
147 +POWER_SUPPLY_HEALTH=Cold
148 +POWER_SUPPLY_PRESENT=1
149 +POWER_SUPPLY_TECHNOLOGY=Li-ion
150 +POWER_SUPPLY_VOLTAGE_MAX_DESIGN=4200000
151 +POWER_SUPPLY_VOLTAGE_NOW=4119000
152 +POWER_SUPPLY_CURRENT_NOW=-290000
153 +POWER_SUPPLY_CHARGE_NOW=1503000
154 +POWER_SUPPLY_CHARGE_COUNTER=-254923
155 +POWER_SUPPLY_CHARGE_FULL_DESIGN=1500000
156 +POWER_SUPPLY_CHARGE_FULL=1500000
157 +POWER_SUPPLY_CHARGE_AVG=32762000
158 +POWER_SUPPLY_ENERGY_FULL=6300000
159 +POWER_SUPPLY_ENERGY_NOW=6235000
160 +POWER_SUPPLY_CAPACITY_LEVEL=Full
161 +POWER_SUPPLY_CAPACITY=100
162 +POWER_SUPPLY_TEMP=-340
163 +POWER_SUPPLY_MODEL_NAME=CDK0
164 +POWER_SUPPLY_MANUFACTURER=IN
165 +
166 +Quoting include/linux/power_supply.h:
167 +
168 +All voltages, currents, charges, energies, time and temperatures in µV,
169 +µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
170 +stated. It's driver's job to convert its raw values to units in which
171 +this class operates.
172 +*/
173 +
174 +void power_supply::measure(void)
175 +{
176 + char filename[4096];
177 + char line[4096];
178 + ifstream file;
179 +
180 + double _rate = 0;
181 + double _capacity = 0;
182 + double _voltage = 0;
183 +
184 + char rate_units[16];
185 + char capacity_units[16];
186 + char voltage_units[16];
187 +
188 + rate_units[0] = 0;
189 + capacity_units[0] = 0;
190 + voltage_units[0] = 0;
191 +
192 + rate = 0;
193 + voltage = 0;
194 + capacity = 0;
195 +
196 + sprintf(filename, "/sys/class/power_supply/%s/uevent", battery_name);
197 +
198 + file.open(filename, ios::in);
199 + if (!file)
200 + return;
201 +
202 + while (file) {
203 + char *c;
204 + file.getline(line, 4096);
205 +
206 + if (strstr(line, "PRESENT")) {
207 + c = strchr(line, '=');
208 + c++;
209 + if(*c == '0'){
210 + printf ("Battery not present");
211 + return;
212 + }
213 + }
214 + if (strstr(line, "CURRENT_NOW")) {
215 + c = strchr(line, '=');
216 + c++;
217 + if(*c == '-') c++; // ignoring the negative sign
218 + _rate = strtoull(c, NULL, 10);
219 + if (c) {
220 + //printf ("CURRENT: %f. \n",_rate);
221 + } else {
222 + _rate = 0;
223 + }
224 + }
225 + if (strstr(line, "CAPACITY=")) {
226 + c = strchr(line, '=');
227 + c++;
228 + _capacity = strtoull(c, NULL, 10);
229 + if (c) {
230 + //printf ("CAPACITY: %f. \n",_capacity);
231 + } else {
232 + _capacity = 0;
233 + }
234 + }
235 + if (strstr(line, "VOLTAGE_NOW")) {
236 + c = strchr(line, '=');
237 + c++;
238 + while (*c == ' ') c++;
239 + _voltage = strtoull(c, NULL, 10);
240 + if (c) {
241 + //printf ("VOLTAGE_NOW: %f. \n",_voltage);
242 + } else {
243 + _voltage = 0;
244 + }
245 + }
246 + }
247 + file.close();
248 +
249 + if(_voltage) {
250 + _voltage = _voltage / 1000.0;
251 + voltage = _voltage;
252 + } else {
253 + voltage = 0.0;
254 + }
255 +
256 + if(_rate) {
257 + _rate = _rate / 1000.0;
258 + _rate = _rate * _voltage;
259 + rate = _rate;
260 + } else {
261 + rate = 0.0;
262 + }
263 +
264 + if(_capacity)
265 + capacity = _capacity;
266 + else
267 + capacity = 0.0;
268 +}
269 +
270 +
271 +void power_supply::end_measurement(void)
272 +{
273 + measure();
274 +}
275 +
276 +void power_supply::start_measurement(void)
277 +{
278 + /* ACPI battery state is a lagging indication, lets only measure at the end */
279 +}
280 +
281 +
282 +double power_supply::joules_consumed(void)
283 +{
284 + return rate;
285 +}
286 diff --git a/measurement/power_supply.h b/measurement/power_supply.h
287 new file mode 100755
288 index 0000000..5ad776e
289 --- /dev/null
290 +++ b/measurement/power_supply.h
291 @@ -0,0 +1,46 @@
292 +/*
293 + * Copyright 2011, Intel Corporation
294 + *
295 + * This file is part of PowerTOP
296 + *
297 + * This program file is free software; you can redistribute it and/or modify it
298 + * under the terms of the GNU General Public License as published by the
299 + * Free Software Foundation; version 2 of the License.
300 + *
301 + * This program is distributed in the hope that it will be useful, but WITHOUT
302 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
303 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
304 + * for more details.
305 + *
306 + * You should have received a copy of the GNU General Public License
307 + * along with this program in a file named COPYING; if not, write to the
308 + * Free Software Foundation, Inc,
309 + * 51 Franklin Street, Fifth Floor,
310 + * Boston, MA 02110-1301 USA
311 + * or just google for it.
312 + *
313 + * Authors:
314 + * John Mathew <johnx.mathew@intel.com>
315 + */
316 +#ifndef __INCLUDE_GUARD_POWER_SUPPLY_H
317 +#define __INCLUDE_GUARD_POWER_SUPPLY_H
318 +
319 +#include "measurement.h"
320 +
321 +class power_supply:public power_meter {
322 + char battery_name[256];
323 +
324 + double capacity;
325 + double rate;
326 + double voltage;
327 + void measure(void);
328 +public:
329 + power_supply(const char *_battery_name);
330 + virtual void start_measurement(void);
331 + virtual void end_measurement(void);
332 +
333 + virtual double joules_consumed(void);
334 + virtual double dev_capacity(void) { return capacity; };
335 +};
336 +
337 +#endif