1 commit a1c28f69847480cc54f36046e96e93a02a2c661d
2 Author: John Mathew <johnx.mathew@intel.com>
3 Date: Thu Jun 30 09:58:26 2011 +0300
5 measurement: add support for universal power supply monitor
7 This patch enables powertop to display power estimates on devices that
8 implement power_supply class and donot have acpi battery interface.
10 First the acpi/battery interface is checked and if it is not accesible
11 the sysfs/power_class interface is used
13 diff --git a/Makefile b/Makefile
14 index b0b6cb7..f0ebf21 100644
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
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
31 #include "measurement.h"
34 +#include "power_supply.h"
35 #include "../parameters/parameters.h"
38 #include <sys/types.h>
44 double min_power = 50000.0;
46 @@ -111,16 +114,45 @@ void power_meters_callback(const char *d_name)
47 power_meters.push_back(meter);
50 +void power_supply_callback(const char *d_name)
52 + char filename[4096];
55 + bool discharging = false;
57 + sprintf(filename, "/sys/class/power_supply/%s/uevent", d_name);
58 + file.open(filename, ios::in);
63 + file.getline(line, 4096);
65 + if (strstr(line, "POWER_SUPPLY_STATUS") && strstr(line, "POWER_SUPPLY_STATUS"))
73 + class power_supply *power;
74 + power = new(std::nothrow) class power_supply(d_name);
76 + power_meters.push_back(power);
79 void detect_power_meters(void)
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);
88 void extech_power_meter(const char *devnode)
91 - struct dirent *entry;
93 class extech_power_meter *meter;
95 meter = new class extech_power_meter(devnode);
96 diff --git a/measurement/power_supply.cpp b/measurement/power_supply.cpp
98 index 0000000..b4fe3af
100 +++ b/measurement/power_supply.cpp
103 + * Copyright 2011, Intel Corporation
105 + * This file is part of PowerTOP
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.
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.
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.
124 + * John Mathew <johnx.mathew@intel.com>
126 +#include "measurement.h"
127 +#include "power_supply.h"
134 +using namespace std;
136 +power_supply::power_supply(const char *supply_name)
141 + strncpy(battery_name, supply_name, sizeof(battery_name));
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
166 +Quoting include/linux/power_supply.h:
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.
174 +void power_supply::measure(void)
176 + char filename[4096];
181 + double _capacity = 0;
182 + double _voltage = 0;
184 + char rate_units[16];
185 + char capacity_units[16];
186 + char voltage_units[16];
189 + capacity_units[0] = 0;
190 + voltage_units[0] = 0;
196 + sprintf(filename, "/sys/class/power_supply/%s/uevent", battery_name);
198 + file.open(filename, ios::in);
204 + file.getline(line, 4096);
206 + if (strstr(line, "PRESENT")) {
207 + c = strchr(line, '=');
210 + printf ("Battery not present");
214 + if (strstr(line, "CURRENT_NOW")) {
215 + c = strchr(line, '=');
217 + if(*c == '-') c++; // ignoring the negative sign
218 + _rate = strtoull(c, NULL, 10);
220 + //printf ("CURRENT: %f. \n",_rate);
225 + if (strstr(line, "CAPACITY=")) {
226 + c = strchr(line, '=');
228 + _capacity = strtoull(c, NULL, 10);
230 + //printf ("CAPACITY: %f. \n",_capacity);
235 + if (strstr(line, "VOLTAGE_NOW")) {
236 + c = strchr(line, '=');
238 + while (*c == ' ') c++;
239 + _voltage = strtoull(c, NULL, 10);
241 + //printf ("VOLTAGE_NOW: %f. \n",_voltage);
250 + _voltage = _voltage / 1000.0;
251 + voltage = _voltage;
257 + _rate = _rate / 1000.0;
258 + _rate = _rate * _voltage;
265 + capacity = _capacity;
271 +void power_supply::end_measurement(void)
276 +void power_supply::start_measurement(void)
278 + /* ACPI battery state is a lagging indication, lets only measure at the end */
282 +double power_supply::joules_consumed(void)
286 diff --git a/measurement/power_supply.h b/measurement/power_supply.h
288 index 0000000..5ad776e
290 +++ b/measurement/power_supply.h
293 + * Copyright 2011, Intel Corporation
295 + * This file is part of PowerTOP
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.
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.
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.
314 + * John Mathew <johnx.mathew@intel.com>
316 +#ifndef __INCLUDE_GUARD_POWER_SUPPLY_H
317 +#define __INCLUDE_GUARD_POWER_SUPPLY_H
319 +#include "measurement.h"
321 +class power_supply:public power_meter {
322 + char battery_name[256];
327 + void measure(void);
329 + power_supply(const char *_battery_name);
330 + virtual void start_measurement(void);
331 + virtual void end_measurement(void);
333 + virtual double joules_consumed(void);
334 + virtual double dev_capacity(void) { return capacity; };