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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/***************************************************************************
* *
* addon-cpufreq-userspace.c *
* *
* Copyright (C) 2006 SUSE Linux Products GmbH *
* *
* Author(s): Holger Macht <hmacht@suse.de> *
* Speed adjustments based on code by *
* Thomas Renninger <trenn@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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "addon-cpufreq.h"
#include "addon-cpufreq-userspace.h"
#include "../../logger.h"
/* at which load difference (in percent) we should immediately switch to
* the maximum possible frequency */
#define JUMP_CPUFREQ_LIMIT_MIN 20
/* the load difference at which we jump up to the maximum freq
* immediately is calculated by the UP_THRESHOLD multiplied with this
* relation value */
#define THRESHOLD_JUMP_LIMIT_RELATION 0.625
/* how many frequency steps we should consider */
#define HYSTERESIS 5
#define DEFAULT_CONSIDER_NICE FALSE
#define PROC_STAT_FILE "/proc/stat"
static const char SYSFS_SCALING_SETSPEED_FILE[] =
"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_setspeed";
static const char SYSFS_SCALING_AVAILABLE_FREQS_FILE[] =
"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_available_frequencies";
/* shortcut for g_array_index */
#define g_a_i(a,i) g_array_index(a, unsigned, i)
struct userspace_config {
int up_threshold;
int cpu_high_limit;
int consider_nice;
int performance;
};
static struct userspace_config config = { UP_THRESHOLD_MAX,
JUMP_CPUFREQ_LIMIT_MIN,
DEFAULT_CONSIDER_NICE,
DEFAULT_PERFORMANCE };
/********************* CPU load calculation *********************/
struct cpuload_data {
int num_cpus;
int *load;
unsigned long *last_total_time;
unsigned long *last_working_time;
};
static struct cpuload_data cpuload = { -1,
NULL,
NULL,
NULL };
/**
* free_cpu_load_data:
*
* frees data needed for CPU load calculation
*/
void free_cpu_load_data(void)
{
if (cpuload.num_cpus != -1) {
free(cpuload.last_working_time);
free(cpuload.last_total_time);
free(cpuload.load);
cpuload.num_cpus = -1;
cpuload.load = NULL;
cpuload.last_total_time = NULL;
cpuload.last_working_time = NULL;
}
}
/**
* calc_cpu_load:
* @consider_nice:
*
* Returns:
*
* calculates current cpu load and stores it in cpuload_data object
*/
static int calc_cpu_load(const int consider_nice)
{
unsigned long total_elapsed, working_elapsed;
char what[32];
unsigned long user_time, nice_time, system_time, idle_time;
unsigned long total_time, iowait_time;
unsigned scan_ret;
char line[256];
char cpu_string[7];
FILE *fp;
int new_num_cpus, i;
new_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
if (new_num_cpus == -1 || new_num_cpus != cpuload.num_cpus) {
free_cpu_load_data();
cpuload.num_cpus = new_num_cpus;
if (cpuload.num_cpus <= 0) {
errno = ENODEV;
return -20;
}
cpuload.last_total_time = (unsigned long *)calloc(cpuload.num_cpus + 1,
sizeof(unsigned long));
cpuload.last_working_time = (unsigned long *)calloc(cpuload.num_cpus + 1,
sizeof(unsigned long));
cpuload.load = (int *)calloc(cpuload.num_cpus + 1, sizeof(int));
}
if ((fp = fopen(PROC_STAT_FILE, "r")) == NULL) {
HAL_DEBUG(("Could not open %s: %s", PROC_STAT_FILE, strerror(errno)));
return -1;
}
/* start with the first line, "overall" cpu load */
/* if cpuload.num_cpus == 1, we do not need to evaluate "overall" and "per-cpu" load */
sprintf(cpu_string, "cpu ");
for (i = 0; i <= cpuload.num_cpus - (cpuload.num_cpus == 1); i++) {
unsigned long working_time;
if (fgets(line,255,fp) == NULL) {
HAL_WARNING(("%s too short (%s)", PROC_STAT_FILE, cpu_string));
fclose(fp);
return -1;
}
if (memcmp(line, cpu_string, strlen(cpu_string))) {
HAL_WARNING(("no '%s' string in %s line %d",
cpu_string, PROC_STAT_FILE, i));
fclose(fp);
return -1;
}
/* initialized, since it is simply not there in 2.4 */
iowait_time = 0;
scan_ret = sscanf(line, "%s %lu %lu %lu %lu %lu", what, &user_time, &nice_time,
&system_time, &idle_time, &iowait_time);
if (scan_ret < 5) {
HAL_WARNING(("only %d values in %s. Please report.",
scan_ret, PROC_STAT_FILE));
fclose(fp);
return -1;
}
if (consider_nice) {
working_time = user_time + system_time + nice_time;
idle_time += iowait_time;
} else {
working_time = user_time + system_time;
idle_time += (nice_time + iowait_time);
}
total_time = working_time + idle_time;
total_elapsed = total_time - cpuload.last_total_time[i];
working_elapsed = working_time - cpuload.last_working_time[i];
cpuload.last_working_time[i] = working_time;
cpuload.last_total_time[i] = total_time;
if (!total_elapsed) {
/* not once per CPU, only once per check. */
if (!i)
HAL_DEBUG(("%s not updated yet, poll slower.", PROC_STAT_FILE));
} else
cpuload.load[i] = working_elapsed * 100 / total_elapsed;
sprintf(cpu_string, "cpu%d ", i);
}
/* shortcut for UP systems */
if (cpuload.num_cpus == 1)
cpuload.load[1] = cpuload.load[0];
fclose(fp);
return 0;
}
/**
* get_cpu_load:
* @cpu_id: The ID of the CPU
*
* Returns: returns current cpuload which has been caluclated before
*
* To get the current CPU load for a given CPU.
*/
static int get_cpu_load(const int cpu_id)
{
if (cpu_id < -1) {
errno = EINVAL;
return -10;
}
if (cpuload.load == NULL) {
HAL_WARNING(("cpuload.load uninitialized"));
errno = EFAULT;
return -40;
}
if (cpu_id >= cpuload.num_cpus) {
errno = ENODEV;
return -30;
}
return cpuload.load[cpu_id + 1];
}
/********************* CPU load end *********************/
/********************* userspace interface *********************/
static gboolean write_speed(unsigned kHz, int cpu_id)
{
char *speed_file = NULL;
if (!cpu_online(cpu_id))
return FALSE;
speed_file = g_strdup_printf(SYSFS_SCALING_SETSPEED_FILE, cpu_id);
if(!write_line(speed_file, "%u", kHz)){
HAL_WARNING(("Could not set speed to: %u kHz; %s", kHz, strerror(errno)));
g_free(speed_file);
return FALSE;
}
g_free(speed_file);
HAL_DEBUG(("Speed set to: %uKHz for CPU %d", kHz, cpu_id));
return TRUE;
}
static void reinit_speed(struct userspace_interface *iface, int current_speed)
{
if (!cpu_online(iface->base_cpu))
return;
write_speed(g_a_i(iface->speeds_kHz, current_speed), iface->base_cpu);
HAL_DEBUG(("forced speed to %d kHz", g_a_i(iface->speeds_kHz, current_speed)));
}
/**
* set_speed:
* @iface: struct with the userspace interface
* @target_speed the speed to set
*
* Returns: the current speed as integer value
*
* Set a speed with traversing all intermediary speeds
*/
static int set_speed(struct userspace_interface *iface, int target_speed)
{
int delta;
int current_speed = iface->current_speed;
if (current_speed == target_speed)
return -1;
if (current_speed > target_speed)
delta = -1;
else
delta = 1;
do {
current_speed += delta;
write_speed(g_a_i(iface->speeds_kHz, current_speed), iface->base_cpu);
} while (current_speed != target_speed);
return current_speed;
}
/**
* increase_speed:
* @iface: struct with the userspace interface
*
* Returns: integer with result of increase speed
* 0 if maximum is already reached
* 1 if new speed could be set
* -1 if mode is not userspace
*
* set speed to the next higher supported value
*/
static int increase_speed(struct userspace_interface *iface)
{
int new_speed = iface->current_speed;
int current_speed = iface->current_speed;
if (current_speed != 0)
new_speed--;
else
return current_speed;
if (current_speed != new_speed) {
HAL_DEBUG(("current: %u new: %u", g_a_i(iface->speeds_kHz, current_speed),
g_a_i(iface->speeds_kHz, new_speed)));
set_speed(iface, new_speed);
}
return new_speed;
}
/**
* decrease_speed:
* @iface: struct with the userspace interface
*
* Returns: integer with result of decrease speed
*
* set speed to the next lower supported value
*/
static int decrease_speed(struct userspace_interface *iface)
{
int new_speed = iface->current_speed;
int current_speed = iface->current_speed;
if (g_a_i(iface->speeds_kHz, new_speed + 1) != 0)
new_speed++;
else
return current_speed;
if (current_speed != new_speed) {
HAL_DEBUG(("current: %u new: %u", g_a_i(iface->speeds_kHz, current_speed),
g_a_i(iface->speeds_kHz, new_speed)));
set_speed(iface, new_speed);
}
return new_speed;
}
/**
* adjust_speed:
* @iface: struct with the userspace interface
*
* Returns: TRUE/FALSE
*
* increases and decreases speeds
*/
static gboolean adjust_speed(struct userspace_interface *iface)
{
GSList *cpus = (GSList*)iface->cpus;
GSList *it = NULL;
int cpu_load = 0;
for (it = cpus; it != NULL; it = g_slist_next(it)) {
HAL_DEBUG(("checking cpu %d: cpu_core: %d",
GPOINTER_TO_INT(it->data), GPOINTER_TO_INT(it->data)));
if (get_cpu_load(GPOINTER_TO_INT(it->data)) > cpu_load)
cpu_load = get_cpu_load(GPOINTER_TO_INT(it->data));
}
HAL_DEBUG(("cpu_max: %d cpu_high_limit: %d consider_nice: %d",
config.up_threshold, config.cpu_high_limit,
config.consider_nice));
HAL_DEBUG(("Current: %u; current speed: %u MHz",
iface->current_speed, g_a_i(iface->speeds_kHz, iface->current_speed)));
HAL_DEBUG(("CPU load: %d, Previous CPU load %d, cpu_load diff: %d, last_step: %d, demotion: %u",
cpu_load, iface->prev_cpu_load, cpu_load - iface->prev_cpu_load, iface->last_step,
g_a_i(iface->demotion, iface->current_speed)));
/* directly increase speed to maximum if cpu load jumped */
if (config.cpu_high_limit &&
(cpu_load - iface->prev_cpu_load) > config.cpu_high_limit) {
if (iface->current_speed != 0) {
set_speed(iface, 0);
iface->current_speed = 0;
HAL_DEBUG(("jumped to max (%d kHz)",
g_a_i(iface->speeds_kHz, iface->current_speed)));
}
} else if (cpu_load > config.up_threshold && iface->current_speed > 0) {
iface->current_speed = increase_speed(iface);
HAL_DEBUG(("increased to %d kHz", g_a_i(iface->speeds_kHz, iface->current_speed)));
} else if (cpu_load < (int)g_a_i(iface->demotion, iface->current_speed) &&
iface->current_speed < iface->last_step) {
iface->current_speed = decrease_speed(iface);
HAL_DEBUG(("decreased to %d kHz", g_a_i(iface->speeds_kHz, iface->current_speed)));
} else {
HAL_DEBUG(("Speed not changed"));
}
iface->prev_cpu_load = cpu_load;
return TRUE;
}
/* create the hysteresis array */
static void create_hysteresis_array(struct userspace_interface *iface)
{
int i;
g_array_free(iface->demotion, TRUE);
iface->demotion = g_array_new(TRUE, TRUE, sizeof(unsigned));
if (iface->last_step > 0) {
for (i = 0; i < iface->last_step; i++) {
int demotion = (config.up_threshold - HYSTERESIS) *
g_a_i(iface->speeds_kHz, i + 1) /
g_a_i(iface->speeds_kHz, i);
g_array_append_val(iface->demotion, demotion);
HAL_DEBUG(("Speed: %2u, kHz: %9u, demotion: %3u %%", i,
g_a_i(iface->speeds_kHz, i), g_a_i(iface->demotion, i)));
}
}
}
static gboolean read_frequencies(struct userspace_interface *iface)
{
int num_speeds = 0;
GSList *it = NULL;
GSList *available_freqs = NULL;
char *available_frequencies_file = NULL;
if (!cpu_online(iface->base_cpu))
return FALSE;
available_frequencies_file = g_strdup_printf(SYSFS_SCALING_AVAILABLE_FREQS_FILE,
iface->base_cpu);
if (!read_line_int_split(available_frequencies_file, " ", &available_freqs)) {
g_free(available_frequencies_file);
return FALSE;
}
g_free(available_frequencies_file);
if (available_freqs == NULL) {
iface->last_step = 0;
return FALSE;
}
for (num_speeds = 0, it = available_freqs; it != NULL;
num_speeds++, it = g_slist_next(it)) {
unsigned index = GPOINTER_TO_UINT(it->data);
g_array_append_val(iface->speeds_kHz, index);
}
g_slist_free(available_freqs);
iface->last_step = num_speeds - 1;
HAL_DEBUG(("Number of speeds: %d, last_step: %d", num_speeds, iface->last_step));
reinit_speed(iface, 0);
HAL_DEBUG(("Available speeds:"));
for (num_speeds = 0; g_a_i(iface->speeds_kHz, num_speeds); num_speeds++) {
HAL_DEBUG((" %2u: %9uKHz", num_speeds, g_a_i(iface->speeds_kHz, num_speeds)));
}
return TRUE;
}
/**
* userspace_adjust_speeds:
* @cpufreq_objs: List with with CPU Freq objects
*
* Returns: Result of the call (TRUE/FALSE)
*
* calculates current cpu load and traverses all existing interfaces
*/
gboolean userspace_adjust_speeds(GSList *cpufreq_objs)
{
GSList *it = NULL;
HAL_DEBUG(("Adjusting speeds..."));
if ((calc_cpu_load(DEFAULT_CONSIDER_NICE) < 0)) {
HAL_DEBUG(("calc_cpu_load failed. Cannot adjust speeds"));
return TRUE;
}
for (it = cpufreq_objs; it != NULL; it = g_slist_next(it)) {
struct cpufreq_obj *obj = it->data;
adjust_speed(obj->iface);
}
return TRUE;
}
/**
* userspace_init:
* @iface: allocated struct with the userspace interface
* @cpus: list of CPU cores
*
* Returns: TRUE/FALSE
*
* Inits one userspace interface with the given cores list. iface has to
* be allocated before passing it to that fucntion
*/
gboolean userspace_init(struct userspace_interface *iface, GSList *cpus)
{
if (iface == NULL)
return FALSE;
iface->demotion = g_array_new(TRUE, TRUE, sizeof(unsigned));
iface->speeds_kHz = g_array_new(TRUE, TRUE, sizeof(unsigned));
iface->last_step = -1;
iface->current_speed = 0;
iface->cpus = cpus;
iface->prev_cpu_load = 50;
iface->base_cpu = GPOINTER_TO_INT(cpus->data);
if (!write_governor(USERSPACE_STRING, GPOINTER_TO_INT(cpus->data))) {
HAL_WARNING(("Could not set userspace governor."));
return FALSE;
}
if (!read_frequencies(iface)) {
HAL_WARNING(("Could not read available frequencies"));
return FALSE;
}
return TRUE;
}
/**
* userspace_free:
* @data: userspace interface object to free
*
* frees the userspace data
*/
void userspace_free(void *data)
{
struct userspace_interface *iface = data;
free_cpu_load_data();
g_array_free(iface->speeds_kHz, TRUE);
g_array_free(iface->demotion, TRUE);
}
/**
* userspace_set_performance:
* @data: userspace interface struct
* @up_threshold: performance value for the userspace governor
*
* Returns: TRUE/FALSE
*
* Sets the performance of the userspace governor. num has to be between 1 and 100
*/
gboolean userspace_set_performance(void *data, int up_threshold)
{
struct userspace_interface *iface = data;
config.up_threshold = up_threshold;
config.cpu_high_limit = (int)(up_threshold * THRESHOLD_JUMP_LIMIT_RELATION);
if (config.cpu_high_limit < JUMP_CPUFREQ_LIMIT_MIN)
config.cpu_high_limit = JUMP_CPUFREQ_LIMIT_MIN;
HAL_DEBUG(("cpu_max set to %d, cpu_high_limit set to %d",
config.up_threshold, config.cpu_high_limit));
create_hysteresis_array(iface);
return TRUE;
}
/**
* userspace_get_performance:
*
* Returns: current performance setting
*
* Return the current performance setting
*/
int userspace_get_performance(void)
{
return config.up_threshold;
}
/**
* userspace_set_consider_nice_
* @data: void pointer
* @consider: if process should be considered nice
*
* Returns: TRUE/FALSE
*
* sets whether niced processes should be considered when calculating CPU load
*/
gboolean userspace_set_consider_nice(void *data, gboolean consider)
{
HAL_DEBUG(("consider nice set to %d for userspace", consider));
config.consider_nice = consider;
return TRUE;
}
/**
* userspace_get_consider_nice:
*
* Returns: TRUE/FALSE
*
* Return the current consider nice setting.
*/
gboolean userspace_get_consider_nice(void)
{
return config.consider_nice;
}
/********************* userspace end *********************/
|