updated on Thu Jan 26 00:18:00 UTC 2012
[aur-mirror.git] / powertop2 / powertop-1.98-sysfs.patch
bloba821a7527e6cb37bc26e287aba664da2c2f7941a
1 diff -N -r -u powertop-1.98/Makefile powertop-patched/Makefile
2 --- powertop-1.98/Makefile 2011-05-11 06:48:37.000000000 +0200
3 +++ powertop-patched/Makefile 2011-10-20 16:06:37.753900039 +0200
4 @@ -13,7 +13,7 @@
5 OBJS += process/process.o process/do_process.o process/interrupt.o process/timer.o process/work.o process/powerconsumer.o process/device.o
6 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
7 DEVS += devices/runtime_pm.o
8 -DEVS += measurement/measurement.o measurement/acpi.o measurement/extech.o
9 +DEVS += measurement/measurement.o measurement/acpi.o measurement/sysfs.o measurement/extech.o
10 OBJS += $(DEVS)
11 OBJS += parameters/parameters.o parameters/learn.o parameters/persistent.o
12 OBJS += calibrate/calibrate.o
13 diff -N -r -u powertop-1.98/measurement/measurement.cpp powertop-patched/measurement/measurement.cpp
14 --- powertop-1.98/measurement/measurement.cpp 2011-05-11 06:48:37.000000000 +0200
15 +++ powertop-patched/measurement/measurement.cpp 2011-10-20 16:00:50.305782326 +0200
16 @@ -24,6 +24,7 @@
18 #include "measurement.h"
19 #include "acpi.h"
20 +#include "sysfs.h"
21 #include "extech.h"
22 #include "../parameters/parameters.h"
23 #include "../lib.h"
24 @@ -103,7 +104,7 @@
25 return total;
28 -void power_meters_callback(const char *d_name)
29 +void power_meters_callback_acpi(const char *d_name)
31 class acpi_power_meter *meter;
32 meter = new(std::nothrow) class acpi_power_meter(d_name);
33 @@ -111,9 +112,23 @@
34 power_meters.push_back(meter);
37 +void power_meters_callback_sysfs(const char *d_name)
39 + if (strstr(d_name, "AC") ||
40 + strstr(d_name, ".")) {
41 + return;
42 + }
44 + class sysfs_power_meter *meter;
45 + meter = new(std::nothrow) class sysfs_power_meter(d_name);
46 + if (meter)
47 + power_meters.push_back(meter);
50 void detect_power_meters(void)
52 - process_directory("/proc/acpi/battery", power_meters_callback);
53 + process_directory("/proc/acpi/battery", power_meters_callback_acpi);
54 + process_directory("/sys/class/power_supply", power_meters_callback_sysfs);
57 void extech_power_meter(const char *devnode)
58 diff -N -r -u powertop-1.98/measurement/sysfs.cpp powertop-patched/measurement/sysfs.cpp
59 --- powertop-1.98/measurement/sysfs.cpp 1970-01-01 01:00:00.000000000 +0100
60 +++ powertop-patched/measurement/sysfs.cpp 2011-10-20 16:19:29.289720272 +0200
61 @@ -0,0 +1,134 @@
62 +/*
63 + * Copyright 2010, Intel Corporation
64 + *
65 + * This file is part of PowerTOP
66 + *
67 + * This program file is free software; you can redistribute it and/or modify it
68 + * under the terms of the GNU General Public License as published by the
69 + * Free Software Foundation; version 2 of the License.
70 + *
71 + * This program is distributed in the hope that it will be useful, but WITHOUT
72 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
73 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
74 + * for more details.
75 + *
76 + * You should have received a copy of the GNU General Public License
77 + * along with this program in a file named COPYING; if not, write to the
78 + * Free Software Foundation, Inc,
79 + * 51 Franklin Street, Fifth Floor,
80 + * Boston, MA 02110-1301 USA
81 + * or just google for it.
82 + *
83 + * Authors:
84 + * Lukas Barth <mail@tinloaf.de>
85 + */
86 +#include "measurement.h"
87 +#include "sysfs.h"
88 +#include <iostream>
89 +#include <fstream>
90 +#include <string.h>
91 +#include <stdio.h>
92 +#include <stdlib.h>
94 +using namespace std;
96 +sysfs_power_meter::sysfs_power_meter(const char *sysfs_name)
98 + rate = 0.0;
99 + capacity = 0.0;
100 + voltage = 0.0;
101 + strncpy(battery_name, sysfs_name, sizeof(battery_name));
104 +void sysfs_power_meter::measure(void)
106 + char filename[4096];
107 + char line[4096];
108 + ifstream file;
110 + rate = 0;
111 + voltage = 0;
112 + capacity = 0;
114 + sprintf(filename, "/sys/class/power_supply/%s/status", battery_name);
116 + file.open(filename, ios::in);
117 + if (!file)
118 + return;
119 + memset(line, 0, 4096);
120 + file.getline(line, 4096);
121 + if (strstr(line, "Discharging") == NULL) {
122 + return; /* not discharging */
124 + file.close();
126 + sprintf(filename, "/sys/class/power_supply/%s/present", battery_name);
127 + file.open(filename, ios::in);
128 + if (!file)
129 + return;
130 + char s;
131 + if (!file.eof()) {
132 + file >> s;
133 + if (s == '0') {
134 + return;
137 + file.close();
139 + sprintf(filename, "/sys/class/power_supply/%s/voltage_now", battery_name);
140 + file.open(filename, ios::in);
141 + if (!file)
142 + return;
143 + memset(line, 0, 4096);
144 + file.getline(line, 4096);
145 + voltage = strtoull(line, NULL, 10) / 1000000.0;
146 + file.close();
148 + sprintf(filename, "/sys/class/power_supply/%s/power_now", battery_name);
149 + file.open(filename, ios::in);
150 + if (!file) {
151 + sprintf(filename, "/sys/class/power_supply/%s/current_now", battery_name);
152 + file.open(filename, ios::in);
153 + }
154 + if (!file)
155 + return;
156 + memset(line, 0, 4096);
157 + file.getline(line, 4096);
158 + rate = strtoull(line, NULL, 10) / 1000000.0;
159 + file.close();
161 + sprintf(filename, "/sys/class/power_supply/%s/energy_now", battery_name);
162 + file.open(filename, ios::in);
163 + capacity = 1;
164 + if (!file) {
165 + file.close();
166 + sprintf(filename, "/sys/class/power_supply/%s/charge_now", battery_name);
167 + file.open(filename, ios::in);
168 + if (!file)
169 + return;
171 + /* W = A * V */
172 + capacity = voltage;
174 + memset(line, 0, 4096);
175 + file.getline(line, 4096);
176 + capacity *= strtoull(line, NULL, 10) / 1000000.0;
177 + file.close();
181 +void sysfs_power_meter::end_measurement(void)
183 + measure();
186 +void sysfs_power_meter::start_measurement(void)
188 + /* Sysfs battery state is a lagging indication, lets only measure at the end */
192 +double sysfs_power_meter::joules_consumed(void)
194 + return rate;
196 diff -N -r -u powertop-1.98/measurement/sysfs.h powertop-patched/measurement/sysfs.h
197 --- powertop-1.98/measurement/sysfs.h 1970-01-01 01:00:00.000000000 +0100
198 +++ powertop-patched/measurement/sysfs.h 2011-10-20 16:18:33.016691795 +0200
199 @@ -0,0 +1,46 @@
201 + * Copyright 2010, Intel Corporation
203 + * This file is part of PowerTOP
205 + * This program file is free software; you can redistribute it and/or modify it
206 + * under the terms of the GNU General Public License as published by the
207 + * Free Software Foundation; version 2 of the License.
209 + * This program is distributed in the hope that it will be useful, but WITHOUT
210 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
211 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
212 + * for more details.
214 + * You should have received a copy of the GNU General Public License
215 + * along with this program in a file named COPYING; if not, write to the
216 + * Free Software Foundation, Inc,
217 + * 51 Franklin Street, Fifth Floor,
218 + * Boston, MA 02110-1301 USA
219 + * or just google for it.
221 + * Authors:
222 + * Lukas Barth <mail@tinloaf.de>
223 + */
224 +#ifndef __INCLUDE_GUARD_SYSFS_H
225 +#define __INCLUDE_GUARD_SYSFS_H
227 +#include "measurement.h"
229 +class sysfs_power_meter: public power_meter {
230 + char battery_name[256];
232 + double capacity;
233 + double rate;
234 + double voltage;
235 + void measure(void);
236 +public:
237 + sysfs_power_meter(const char *_battery_name);
238 + virtual void start_measurement(void);
239 + virtual void end_measurement(void);
241 + virtual double joules_consumed(void);
242 + virtual double dev_capacity(void) { return capacity; };
245 +#endif