-
Notifications
You must be signed in to change notification settings - Fork 8
/
custom-fanbytemp-daemon.php
497 lines (443 loc) · 12.1 KB
/
custom-fanbytemp-daemon.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
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
<?php
/*
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. See LICENSE for more details.
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 LICENSE for more details.
The author can't BE LIABLE TO YOU FOR ANY DAMAGE, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM. See LICENSE for more details.
*/
// tick use required as of PHP 4.3.0
declare(ticks=1);
// Init constants
const FAN_COMMAND = 'fan';
const CUR_WORKER = 'c94e13';
const GLOBAL_FAN_COMMAND = 'globalfan';
const GLOBAL_FAN_DEFAULT = 85;
const CR = "\n";
const LOCAL_CONF = '/home/ethos/local.conf';
#const LOCAL_CONF = 'test/sample_local.conf';
const LOG_FILE = '/var/log/custom-fanbytemp.log';
#const LOG_FILE = 'test/custom-fanbytemp.log';
const OVERCLOCK_LOG = '/var/log/ethos-overclock.log';
#const OVERCLOCK_LOG = 'test/ethos-overclock.log';
const RELOAD = 0;
const READ_TEMP = 1;
const READ_FAN = 2;
const HYSTERESIS = 1;
const MIN_STABLE_TIME = 3;
const MAX_STABLE_TIME = 6;
const MAX_DIFF = 3;
// Init global variables
$REF_TEMPS_ARRAY = NULL;
$REF_CONF_ARRAY = NULL;
$DIFF_CONF_ARRAY = NULL;
$READ_FAN = NULL;
$STABLE_TIME = -6;
$LOG_ROTATE_DATE = '';
$STANDBY = FALSE;
/**
* execute a command ID
* @param $command_id
* @return mixed command output
*/
function execute($command_id)
{
switch ($command_id) {
case RELOAD:
$command = '/opt/ethos/sbin/ethos-overclock';
#$command = "echo 'Conf Reloaded !'";
break;
case READ_TEMP:
$command = '/opt/ethos/sbin/ethos-readdata temps';
#$command = 'cat test/sample_temps.txt';
break;
case READ_FAN:
$command = '/opt/ethos/sbin/ethos-readdata fan';
#$command = 'cat test/sample_fan.txt';
break;
default:
$command = '';
break;
}
return `$command`;
}
/**
* fan percent for conf file according to old temperatures
* @param $old_temps_array
* @return array
*/
function fan_percent_conf(&$old_temps_array)
{
$temps_array = read_temp();
$array_percent = fan_percent_array($temps_array, $old_temps_array);
$old_temps_array = $temps_array;
return $array_percent;
}
/**
* read the temperature
* @return array
*/
function read_temp()
{
$temps = trim(execute(READ_TEMP));
$temps_array = explode(' ', $temps);
return $temps_array;
}
/**
* read the fan speed
* @return array
*/
function read_fan()
{
$fans = trim(execute(READ_FAN));
$fans_array = explode(' ', $fans);
return $fans_array;
}
/**
* calculate fan percent speed according to temperature value
* @param $temp
* @return int
*/
function fan_percent($temp)
{
if ($temp < 30) {
return 40;
}
if ($temp >= 30 && $temp < 50) {
return round($temp / 2 + 25);
}
if ($temp >= 50 && $temp < 80) {
return round($temp);
}
if ($temp >= 80 && $temp < 90) {
return round($temp * 2 - 80);
}
//$temp>=90
return 100;
}
/**
* calculate hysteresis temperature with old one
* if the temperature is going up nothing is changed
* @param float $temp
* @param float $temp_old
* @return mixed
*/
function hysteresis($temp, $temp_old)
{
$diff = abs($temp - $temp_old);
if ($temp > $temp_old || $diff > HYSTERESIS) {
return $temp;
} else {
return $temp_old;
}
}
/**
* loop on every temps to get fan percent speed
* @param array $temp_array
* @param array $old_temp_array
* @return array
*/
function fan_percent_array(&$temp_array, $old_temp_array = [0, 0, 0, 0, 0, 0, 0, 0])
{
$i = 0;
$percent_array = array();
foreach ($temp_array as &$value) {
$value = hysteresis($value, $old_temp_array[$i]);
array_push($percent_array, fan_percent($value));
$i++;
}
return $percent_array;
}
/**
* convert the array to string for display
* @param $percent_array
* @return string
*/
function display_string($percent_array)
{
$display = '';
if ($percent_array != NULL) {
foreach ($percent_array as $value) {
$display = "$display $value";
}
}
return $display;
}
/**
* put the new conf to ethOS in local.conf file
* @param array $fan_values
* @param string $filename
* @return bool
*/
function put_conf($fan_values, $filename = LOCAL_CONF)
{
$fh = fopen($filename, 'r+');
$new_conf = '';
$changed = FALSE;
if ($fh != NULL) {
while (!feof($fh)) {
$line = fgets($fh);
$conf_line = explode(' ', $line);
$command = '';
$worker = '';
if (count($conf_line) >= 2) {
$command = trim($conf_line[0]);
$worker = trim($conf_line[1]);
}
// check for empty indexes
if ($command == FAN_COMMAND AND $worker == CUR_WORKER) {
$display = display_string($fan_values);
$new_line = FAN_COMMAND . ' ' . CUR_WORKER . $display . CR;
if ($new_line != $line) {
daemon_log(CR . "Final Percents: " . $display);
$changed = TRUE;
}
} else {
$new_line = $line;
}
$new_conf .= $new_line;
}
if ($changed) {
// using file_put_contents() instead of fwrite()
file_put_contents($filename, $new_conf);
}
fclose($fh);
} else {
daemon_log("File not found!");
}
return $changed;
}
/**
* get the old value from globalfan in local.conf file
* @param string $filename
* @return string $fan_value
*/
function read_conf($filename = LOCAL_CONF)
{
$fh = fopen($filename, 'r');
$fan_value = GLOBAL_FAN_DEFAULT;
if ($fh != NULL) {
while (!feof($fh)) {
$line = fgets($fh);
$conf_line = explode(' ', $line);
if (count($conf_line) == 2) {
$command = trim($conf_line[0]);
$global_value = trim($conf_line[1]);
if ($command == GLOBAL_FAN_COMMAND && is_numeric($global_value)) {
$fan_value = $global_value + 0;
break;
}
}
}
fclose($fh);
} else {
daemon_log("File not found!");
}
return $fan_value;
}
/**
* logger for this daemon
* @param $raw_text
* @param bool $carriage_return
* @internal param bool $clean
*/
function daemon_log($raw_text, $carriage_return = TRUE)
{
global $LOG_ROTATE_DATE;
$current_date = date("Y-m-d");
if ($current_date != $LOG_ROTATE_DATE) {
$fh = fopen(LOG_FILE, 'w');
$LOG_ROTATE_DATE = $current_date;
} else {
$fh = fopen(LOG_FILE, 'a');
}
if ($fh != NULL) {
$text = $raw_text;
if ($carriage_return) {
$text .= CR;
}
fwrite($fh, $text);
fclose($fh);
}
}
/**
* check if overclock has finished reading log
* @return bool
*/
function reload_finished()
{
$content = file_get_contents(OVERCLOCK_LOG);
if ($content != FALSE) {
return (strstr($content, "overclock finished") != FALSE);
}
daemon_log(OVERCLOCK_LOG . " not found!");
return TRUE;
}
/**
* reload conf with overclock ethOS command
* @param $reload
*/
function reload_conf($reload)
{
global $STANDBY;
$finished = reload_finished();
if (!$finished) {
daemon_log('Overclock not finished!');
} else if ($reload) {
log_stats();
$STANDBY = FALSE;
daemon_log(execute(RELOAD));
} else if ($STANDBY) {
daemon_log(".", FALSE);
} else {
log_stats();
$STANDBY = TRUE;
}
}
/**
* log all stats variables
*/
function log_stats()
{
global $DIFF_CONF_ARRAY;
global $REF_CONF_ARRAY;
global $STABLE_TIME;
global $READ_FAN;
daemon_log("Ref Percents: " . display_string($REF_CONF_ARRAY));
daemon_log("Fan Percents: " . display_string($READ_FAN));
daemon_log("Diff Percents: " . display_string($DIFF_CONF_ARRAY));
daemon_log("Stable time: " . $STABLE_TIME);
}
/**
* compare reference fan percent to real fan percent
* @return array
*/
function ref_conf_compare()
{
global $REF_CONF_ARRAY;
global $DIFF_CONF_ARRAY;
global $READ_FAN;
$current_fan_percent = read_fan();
// Store fan value for logs
$READ_FAN = $current_fan_percent;
$diff_fan_percent_array = array();
for ($i = 0; $i < count($REF_CONF_ARRAY); $i++) {
$diff = $REF_CONF_ARRAY[$i] - $current_fan_percent[$i];
if ($DIFF_CONF_ARRAY != NULL) {
// check moy value with previous one
$diff = round((2 * $DIFF_CONF_ARRAY[$i] + $diff) / 2);
}
$diff = min($diff, MAX_DIFF);
$diff = max($diff, -MAX_DIFF);
array_push($diff_fan_percent_array, $diff);
}
return $diff_fan_percent_array;
}
/**
* compare diff array and return true if $DIFF_CONF_ARRAY < $diff_fan_percent
* @param $diff_fan_percent
* @return bool
*/
function diff_compare($diff_fan_percent)
{
global $DIFF_CONF_ARRAY;
$changed = FALSE;
for ($i = 0; $i < count($DIFF_CONF_ARRAY); $i++) {
$diff = abs($diff_fan_percent[$i]) - abs($DIFF_CONF_ARRAY[$i]);
if ($diff > 0) {
$DIFF_CONF_ARRAY[$i] = $diff_fan_percent[$i];
$changed = TRUE;
}
}
return $changed;
}
/**
* apply the previous difference between fan percent and real fan percent to the new change
* @return array|null
*/
function new_fan_percent_conf()
{
global $REF_CONF_ARRAY;
global $DIFF_CONF_ARRAY;
if ($DIFF_CONF_ARRAY != NULL) {
$new_reference_fan_percent = array();
for ($i = 0; $i < count($REF_CONF_ARRAY); $i++) {
array_push($new_reference_fan_percent, $REF_CONF_ARRAY[$i] + $DIFF_CONF_ARRAY[$i]);
}
return $new_reference_fan_percent;
} else {
return $REF_CONF_ARRAY;
}
}
/**
* put real conf with diff from real fan speed
* @return bool
*/
function put_real_conf()
{
$new_fan_percent_conf = new_fan_percent_conf();
$changed = put_conf($new_fan_percent_conf);
return $changed;
}
/**
* Callback in case of Kill signal
* Revert conf to global conf default and reload
* @param $signo
*/
function revert_handler($signo)
{
global $REF_CONF_ARRAY;
global $DIFF_CONF_ARRAY;
$global_fan = read_conf();
daemon_log("Back to default (SIG=" . $signo . "): " . $global_fan);
$DIFF_CONF_ARRAY = NULL;
foreach ($REF_CONF_ARRAY as &$value) {
$value = $global_fan;
}
reload_conf(put_real_conf());
exit($signo);
}
if (php_uname('s') == 'Linux') {
// Install the signal handlers
pcntl_signal(SIGTERM, "revert_handler"); // kill
pcntl_signal(SIGHUP, "revert_handler"); // kill -s HUP or kill -1
pcntl_signal(SIGINT, "revert_handler"); // Ctrl-C
}
/**
* Infinite loop for daemon
*/
while (TRUE) {
$changed = FALSE;
$fan_percent_conf = fan_percent_conf($REF_TEMPS_ARRAY);
if ($REF_CONF_ARRAY != $fan_percent_conf) {
$REF_CONF_ARRAY = $fan_percent_conf;
if ($STABLE_TIME > 0) {
$STABLE_TIME = 0;
}
$changed = put_real_conf();
} else {
$diff_fan_percent = ref_conf_compare();
if ($STABLE_TIME >= MIN_STABLE_TIME && $STABLE_TIME < MAX_STABLE_TIME && $DIFF_CONF_ARRAY != $diff_fan_percent) {
$DIFF_CONF_ARRAY = $diff_fan_percent;
$changed = put_real_conf();
$STABLE_TIME++;
} else if ($STABLE_TIME >= MAX_STABLE_TIME && diff_compare($diff_fan_percent)) {
$changed = put_real_conf();
$STABLE_TIME++;
} else if ($STABLE_TIME < MIN_STABLE_TIME) {
$STABLE_TIME++;
}
}
reload_conf($changed);
sleep(5);
}
// Reset the signal handlers
pcntl_signal(SIGHUP, SIG_DFL);
pcntl_signal(SIGINT, SIG_DFL);
pcntl_signal(SIGTERM, SIG_DFL);