00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef POWERSAVE_CPUFREQ_H
00026 #define POWERSAVE_CPUFREQ_H
00027
00028 #include "pm_interface.h"
00029 #include "cpu.h"
00030 #ifdef CPUFREQ_MEASURE
00031 #include <time.h>
00032 #endif
00033
00034
00035
00036
00037
00038 #define MAX_SPEEDS 20
00039 #define MIN_SPEED_STEP 25000
00040 #define SYSFS_FILES \
00041 "/sys/devices/system/cpu/"
00042
00043 class CPUFreq_Interface : public CPU {
00044 public:
00050 CPUFreq_Interface(std::list< int > cpu_list);
00051
00053 virtual ~CPUFreq_Interface();
00054
00060 virtual bool init() = 0;
00061
00069 virtual int adjustSpeed() = 0;
00070
00079 virtual void reinitSpeed() = 0;
00080
00082 virtual void setConfig() = 0;
00083
00095 void setConfigs(int high_cpu, int max_limit, int hysters, int consider_nice);
00096
00098 int setGovernor(const std::string &new_governor);
00099
00100
00119 int setMode(CPUFREQ_MODE mode);
00120
00125 virtual bool readFrequencies() = 0;
00126
00131 CPUFREQ_MODE getMode();
00132
00141 static bool read_line(const char *filename, char *line, unsigned len);
00142
00151 static bool write_line(const char *filename, const char *fmt, ...);
00152
00153 protected:
00154
00161 static unsigned read_value(const char *filename);
00162
00164 string GOVERNOR_FILE;
00165
00167 string MIN_SPEED_FILE;
00168
00170 string MAX_SPEED_FILE;
00171
00173 string AVAILABLE_FREQS_FILE;
00174
00176 int _cpu_max;
00177
00179 int _cpu_hysteresis;
00180
00183 int _high_cpu_limit;
00184
00189 int _consider_nice;
00190
00195 CPUFREQ_MODE _mode;
00196
00199 std::list< int > _cpu_cores;
00200 };
00201
00203 class CPUFreq_Userspace:public CPUFreq_Interface {
00204 public:
00210 CPUFreq_Userspace(std::list< int > cpu_cores);
00211
00213 ~CPUFreq_Userspace();
00214
00219 bool init();
00220
00221 #ifdef CPUFREQ_MEASURE
00222 unsigned long time_spent[MAX_SPEEDS + 1];
00223 unsigned int count;
00224 unsigned long cpu_load_sum;
00225
00226 static time_t *start_time;
00227
00228 static unsigned long polling_interval;
00229
00231 static void startMeasures();
00233 static void stopMeasures();
00234
00236 #endif
00237
00238 private:
00250 bool getMinMaxSpeeds(unsigned long *min, unsigned long *max);
00251
00256 unsigned getSpeed();
00257
00265 int increaseSpeed();
00266
00274 int decreaseSpeed();
00275
00282 bool setSpeed(unsigned kHz);
00283
00294 void setSpeed(unsigned current, unsigned target);
00295
00297 void createHysteresisArray();
00298
00299 bool readFrequencies();
00300
00301 int adjustSpeed();
00302
00303 void reinitSpeed();
00304
00309 int initFreqsViaFile();
00310
00315 int initFreqsViaTesting();
00316
00317 void setConfig();
00318
00320 string CURRENT_SPEED_FILE;
00321
00323 unsigned _speeds_kHz[MAX_SPEEDS + 1];
00324
00326 unsigned _demotion[MAX_SPEEDS + 1];
00327
00329 unsigned _current_speed;
00330
00332 unsigned _new_speed;
00333
00335 unsigned _last_step;
00336
00338 int _last_cpu_load;
00339
00341 string USERSPACE_STRING;
00342 };
00343
00345 class CPUFreq_Kernel:public CPUFreq_Interface {
00346 public:
00353 CPUFreq_Kernel(std::list< int > cpu_list, unsigned long sampling_rate = 333000);
00354
00356 ~CPUFreq_Kernel();
00357
00362 bool init();
00363
00364 private:
00365 bool readFrequencies();
00366
00367 int adjustSpeed();
00368
00369 void reinitSpeed();
00370
00371 void setConfig();
00372
00385 int writeOndemand(const string &name, int value);
00386
00388 unsigned long _sampling_rate;
00389
00391 int _down_threshold;
00392
00394 int _sampling_down_factor;
00395
00397 string ON_DEMAND_STRING;
00398
00400 string POWERSAVE_STRING;
00401
00403 string PERFORMANCE_STRING;
00404 };
00405
00406 #endif