-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmalatency.php
81 lines (68 loc) · 2.01 KB
/
dmalatency.php
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
#!/usr/bin/env php
<?php
/*****************
* DMA Latency Tuned
* Adjust /dev/cpu_dma_latency based on CPU load
*
* @author Olivier Doucet <olivier at oxeva dot fr>
***************/
// Interval chosen to update value (in seconds)
define('LOOPTIME', 10);
// if CPU > X, then disable all C-States
$latencyValues = array(
//CPU% => value (please order by CPU DESC)
40 => 0, // disable all (full speed)
15 => 11, // C1E state
5 => 201, // C6 state
);
$oldValues = array(
'busy' => 0,
'idle' => 0,
);
$currentLatency = -1;
// hold that file descriptor
$fp = fopen('/dev/cpu_dma_latency', 'w');
openlog('dmalatency', LOG_ODELAY, LOG_DAEMON);
while (true) {
// Get new values
$z = preg_match(
'@cpu\s+([0-9]{1,}) ([0-9]{1,}) ([0-9]{1,}) ([0-9]{1,}) ([0-9]{1,})'.
' ([0-9]{1,}) ([0-9]{1,}) @',
file_get_contents('/proc/stat'),
$data
);
if (!$z) {
// fail ... no infinite loop plz
sleep(1);
continue;
}
$newValues = array(
'busy' => $data[1]+$data[2]+$data[3]+$data[5]+$data[6]+$data[7],
'idle' => $data[4],
);
if ($oldValues['busy'] == 0) {
// Wait
$oldValues = $newValues;
sleep(LOOPTIME);
continue;
}
$busyPercent = ($newValues['busy'] - $oldValues['busy'])/
($newValues['busy'] - $oldValues['busy']+$newValues['idle'] - $oldValues['idle'])
*100;
printf("CPU Usage: %5.2f %% \r", $busyPercent);
// Do the stuff
foreach ($latencyValues as $cpuUsage => $latencyNeeded) {
if ($busyPercent >= $cpuUsage)
break;
}
if ($currentLatency != $latencyNeeded) {
fwrite($fp, pack('i', $latencyNeeded));
rewind($fp);
syslog(LOG_NOTICE, 'DMA Latency changed from '.$currentLatency.' to '.$latencyNeeded.'');
$currentLatency = $latencyNeeded;
}
$oldValues = $newValues;
sleep(LOOPTIME);
}
closelog();
fclose($fp);