/usr/share/phoronix-test-suite/pts-core/objects/phodevi/sensors/sys_power.php is in phoronix-test-suite 4.8.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | <?php
/*
Phoronix Test Suite
URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
Copyright (C) 2009 - 2013, Phoronix Media
Copyright (C) 2009 - 2013, Michael Larabel
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class sys_power implements phodevi_sensor
{
private static $battery_sys = false;
private static $battery_cur = false;
private static $wattsup_meter = false;
private static $ipmitool = false;
public static function get_type()
{
return 'sys';
}
public static function get_sensor()
{
return 'power';
}
public static function get_unit()
{
if(self::$battery_sys)
{
$unit = 'Milliwatts';
}
else if(self::$battery_cur)
{
$unit = 'microAmps';
}
else if(self::$wattsup_meter || self::$ipmitool)
{
$unit = 'Watts';
}
return $unit;
}
public static function support_check()
{
$test = self::sys_battery_power();
if(is_numeric($test) && $test != -1)
{
self::$battery_sys = true;
return true;
}
$test = self::sys_power_current();
if(is_numeric($test) && $test != -1)
{
self::$battery_cur = true;
return true;
}
if(pts_client::executable_in_path('wattsup'))
{
$wattsup = self::watts_up_power_meter();
if($wattsup > 0.5 && is_numeric($wattsup))
{
self::$wattsup_meter = true;
return true;
}
}
if(pts_client::executable_in_path('ipmitool'))
{
$ipmi_read = phodevi_linux_parser::read_ipmitool_sensor('Node Power');
if($ipmi_read > 0 && is_numeric($ipmi_read))
{
self::$ipmitool = true;
return true;
}
}
}
public static function read_sensor()
{
if(self::$battery_sys)
{
return self::sys_battery_power();
}
else if(self::$battery_cur)
{
return self::sys_power_current();
}
else if(self::$wattsup_meter)
{
return self::watts_up_power_meter();
}
else if(self::$ipmitool)
{
return phodevi_linux_parser::read_ipmitool_sensor('Node Power');
}
}
private static function watts_up_power_meter()
{
$output = trim(shell_exec('wattsup -c 1 ttyUSB0 watts 2>&1'));
$output = explode(PHP_EOL, $output);
do
{
$value = array_pop($output);
}
while(!is_numeric($value) && count($output) > 0);
return is_numeric($value) ? $value : -1;
}
private static function sys_power_current()
{
// Returns power consumption rate in uA
$current = -1;
if(phodevi::is_linux())
{
$raw_current = phodevi_linux_parser::read_sysfs_node('/sys/devices/w1_bus_master1/*/getcurrent', 'NO_CHECK');
if($raw_current != -1)
{
if(substr($raw_current, 0, 1) == '-')
{
$current = substr($raw_current, 1);
}
}
}
else if(phodevi::is_macosx())
{
$current = abs(phodevi_osx_parser::read_osx_system_profiler('SPPowerDataType', 'Amperage')); // in mA
}
return $current;
}
private static function sys_battery_power()
{
// Returns power consumption rate in mW
$rate = -1;
if(phodevi::is_linux())
{
$power_now = phodevi_linux_parser::read_sysfs_node('/sys/class/power_supply/*/power_now', 'POSITIVE_NUMERIC', array('status' => 'Discharging'));
if($power_now != -1)
{
// sysfs power_now seems to be displayed in microWatts
$rate = pts_math::set_precision($power_now / 1000, 2);
}
if($rate == -1)
{
$battery = array('/battery/BAT0/state', '/battery/BAT1/state');
$state = phodevi_linux_parser::read_acpi($battery, 'charging state');
$power = phodevi_linux_parser::read_acpi($battery, 'present rate');
$voltage = phodevi_linux_parser::read_acpi($battery, 'present voltage');
if($state == 'discharging')
{
$power_unit = substr($power, strrpos($power, ' ') + 1);
$power = substr($power, 0, strpos($power, ' '));
if($power_unit == 'mA')
{
$voltage_unit = substr($voltage, strrpos($voltage, ' ') + 1);
$voltage = substr($voltage, 0, strpos($voltage, ' '));
if($voltage_unit == 'mV')
{
$rate = round(($power * $voltage) / 1000);
}
}
else if($power_unit == 'mW')
{
$rate = $power;
}
}
}
if($rate == -1 && is_file('/sys/class/power_supply/BAT0/voltage_now') && is_file('/sys/class/power_supply/BAT0/current_now'))
{
$voltage_now = pts_file_io::file_get_contents('/sys/class/power_supply/BAT0/voltage_now') / 1000;
$current_now = pts_file_io::file_get_contents('/sys/class/power_supply/BAT0/current_now') / 1000;
$power_now = $voltage_now * $current_now / 1000;
if($power_now > 1)
{
$rate = $power_now;
}
}
}
else if(phodevi::is_macosx())
{
$amperage = abs(phodevi_osx_parser::read_osx_system_profiler('SPPowerDataType', 'Amperage')); // in mA
$voltage = phodevi_osx_parser::read_osx_system_profiler('SPPowerDataType', 'Voltage'); // in mV
if($amperage > 0 && $voltage > 0)
{
$rate = round(($amperage * $voltage) / 1000);
}
else if(pts_client::executable_in_path('ioreg'))
{
$ioreg = trim(shell_exec("ioreg -l | grep LegacyBatteryInfo | cut -d '{' -f 2 | tr -d \} | tr ',' '=' | awk -F'=' '{print ($2*$10/10^22)}' 2>&1"));
if(is_numeric($ioreg) && $ioreg > 0)
{
$rate = $ioreg;
}
}
}
else if(phodevi::is_solaris())
{
$battery = phodevi_solaris_parser::read_hal_property('/org/freedesktop/Hal/devices/pseudo/acpi_drv_0_battery0_0', 'battery.reporting.rate');
if(is_numeric($battery))
{
$rate = $battery;
}
}
else if(phodevi::is_bsd())
{
$battery = phodevi_bsd_parser::read_acpiconf('Present rate');
if($battery && substr($battery, -2) == 'mW')
{
$rate = substr($battery, 0, strpos($battery, ' '));
}
}
return $rate;
}
}
?>
|