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 248 249 250 251 252 253
|
/***************************************************************************
* *
* Powersave Daemon *
* *
* Copyright (C) 2004,2005,2006 SUSE Linux Products GmbH *
* *
* Author(s): Holger Macht <hmacht@suse.de> *
* *
* 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 2 of the License, or (at you *
* 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, write to the Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#include "cpufreq_management.h"
#include "stringutil.h"
#include "globals.h"
#include "cpu.h"
#include "dbus_server.h"
#include <liblazy.h>
#include <string.h>
using namespace Powersave::Globals;
CpufreqManagement::CpufreqManagement() : _is_supported(false)
{
if (!config_obj->current_scheme->CPUFREQ_ENABLED) {
pDebug(DBG_INFO, "cpufrequency scaling is disabled in configuration file");
return;
}
_initial_cpu_count = CPU::enableAll();
char **capabilities;
liblazy_hal_find_device_by_capability("cpufreq_control", &capabilities);
if (capabilities != NULL && capabilities[0] != NULL) {
_is_supported = true;
_hal_device = capabilities[0];
} else
pDebug(DBG_WARN, "No capability cpufreq_control");
}
CpufreqManagement::~CpufreqManagement()
{
}
void CpufreqManagement::checkCPUHotplug()
{
bool cpu_enabled = false;
for (int i = _initial_cpu_count - 1; i > 0; --i) {
CPU h(i);
if (i >= config_obj->current_scheme->MAX_CPUS_ONLINE
&& config_obj->current_scheme->MAX_CPUS_ONLINE != 0) {
h.disable();
} else {
int ret = h.enable();
if (ret > 0)
cpu_enabled = true;
}
}
if (cpu_enabled)
setModes(config_obj->current_scheme->CPUFREQUENCY, NULL);
}
bool CpufreqManagement::isSupported()
{
return _is_supported;
}
bool CpufreqManagement::setGovernor(const char *governor)
{
return DBus_Server::sendMethodCallAndBlock(DBUS_HAL_INTERFACE,
_hal_device.c_str(),
DBUS_HAL_CPUFREQ_INTERFACE,
"SetCPUFreqGovernor",
DBUS_TYPE_STRING,
&governor,
DBUS_TYPE_INVALID);
}
bool CpufreqManagement::setPerformance(int performance)
{
return DBus_Server::sendMethodCallAndBlock(DBUS_HAL_INTERFACE,
_hal_device.c_str(),
DBUS_HAL_CPUFREQ_INTERFACE,
"SetCPUFreqPerformance",
DBUS_TYPE_INT32,
&performance,
DBUS_TYPE_INVALID);
}
bool CpufreqManagement::setConsiderNice(int consider)
{
return DBus_Server::sendMethodCallAndBlock(DBUS_HAL_INTERFACE,
_hal_device.c_str(),
DBUS_HAL_CPUFREQ_INTERFACE,
"SetCPUFreqConsiderNice",
DBUS_TYPE_BOOLEAN,
&consider,
DBUS_TYPE_INVALID);
}
bool CpufreqManagement::setModes(CPUFREQ_MODE mode, EventManagement *eM)
{
if (DBus_Server::haveClient())
return true;
std::string governor;
if (!_is_supported)
return false;
if (eM != NULL) {
switch (mode) {
case _DYNAMIC:
eM->executeEvent("processor.dynamic");
break;
case _PERFORMANCE:
eM->executeEvent("processor.performance");
break;
case _POWERSAVE:
eM->executeEvent("processor.powersave");
break;
}
}
switch (mode) {
case _DYNAMIC: {
if (config_obj->current_scheme->CPUFREQ_CONTROL == CPUFREQ_KERNEL)
governor.append("ondemand");
else if (config_obj->current_scheme->CPUFREQ_CONTROL == CPUFREQ_USERSPACE)
governor.append("userspace");
break;
}
case _PERFORMANCE:
governor.append("performance");
break;
case _POWERSAVE:
governor.append("powersave");
break;
}
if (!setGovernor((char*)governor.c_str())) {
pDebug(DBG_ERR, "Could not set governor '%s'", governor.c_str());
if (governor == "ondemand") {
pDebug(DBG_WARN, "Kernel ondemand governor not working, "
"trying userspace instead");
if (!setGovernor("userspace")) {
pDebug(DBG_ERR, "Neither ondemand nor userspace governor working");
return false;
}
}
}
if (mode == _DYNAMIC) {
if (!setPerformance(config_obj->current_scheme->CPUFREQ_DYNAMIC_PERFORMANCE))
pDebug(DBG_WARN, "Could not set dynamic performance setting");
if (!setConsiderNice(config_obj->current_scheme->CONSIDER_NICE))
pDebug(DBG_WARN, "Could not set consider nice setting");
}
return true;
}
char *CpufreqManagement::getGovernor()
{
char *governor = NULL;
if (!DBus_Server::sendMethodCallWithReturn(DBUS_HAL_INTERFACE,
_hal_device.c_str(),
DBUS_HAL_CPUFREQ_INTERFACE,
"GetCPUFreqGovernor",
DBUS_TYPE_STRING,
&governor)) {
return NULL;
}
return governor;
}
CPUFREQ_MODE CpufreqManagement::getMode()
{
CPUFREQ_MODE mode = _DYNAMIC;
char *governor = getGovernor();
if (governor == NULL)
return mode;
if (!strcmp(governor, "ondemand") || !strcmp(governor, "userspace"))
mode = _DYNAMIC;
else if (!strcmp(governor, "powersave"))
mode = _POWERSAVE;
else if (!strcmp(governor, "performance"))
mode = _PERFORMANCE;
return mode;
}
CPUFREQ_CONTROL_MODE CpufreqManagement::controlMode()
{
if (!_is_supported)
return CPUFREQ_NONE;
char *governor = getGovernor();
if (governor == NULL)
return CPUFREQ_NONE;
CPUFREQ_CONTROL_MODE control = CPUFREQ_KERNEL;
if (!strcmp(governor, "ondemand"))
control = CPUFREQ_KERNEL;
else if (!strcmp(governor, "userspace"))
control = CPUFREQ_USERSPACE;
return control;
}
int CpufreqManagement::enableCPU(int cpu)
{
CPU c(cpu);
if (!c.hotpluggable())
return -1;
int ret = c.enable();
if (ret > 0)
setModes(config_obj->current_scheme->CPUFREQUENCY, NULL);
return ret;
}
int CpufreqManagement::disableCPU(int cpu)
{
// we cannot disable the first CPU, it runs the main timer
if (cpu == 0)
return -1;
CPU c(cpu);
if (!c.hotpluggable())
return -1;
return c.disable();
}
|