reasonableframework/helper/perftool.php

45 lines
1.1 KiB
PHP
Raw Normal View History

2020-02-19 05:43:06 +00:00
<?php
/**
* @file perftool.php
* @created_on 2020-02-19
2020-02-24 03:29:09 +00:00
* @updated_on 2020-02-24
2020-02-19 05:43:06 +00:00
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief PerfTool helper
*/
if(!is_fn("get_cpu_idle")) {
function get_cpu_idle() {
2020-02-19 05:43:33 +00:00
$idle = false;
2020-02-19 05:43:06 +00:00
if(loadHelper("exectool")) {
2020-02-19 06:01:39 +00:00
$idle = floatval(trim(exec_command("top -n 1 -b | grep -i Cpu\(s\) | awk '{print \$8}'"))) / 100.0;
2020-02-19 05:43:06 +00:00
}
return $idle;
}
}
2020-02-24 03:29:09 +00:00
if(!is_fn("set_min_cpu_idle")) {
2020-02-24 05:51:56 +00:00
function set_min_cpu_idle($idle=0.01) {
2020-02-24 03:29:09 +00:00
$wait = 0;
// default (cpu_sleep_time): 3 seconds
$cpu_sleep_time = floatval(get_value_in_array("cpu_sleep_time", $config, 3));
2020-02-24 05:51:56 +00:00
if($idle > 0 && $idle < 1) {
while(get_cpu_idle() < $idle) {
2020-02-24 03:29:09 +00:00
if($wait == 0) {
2020-02-24 05:50:29 +00:00
write_common_log("CPU usage exceeded. wait a few seconds...", "helper/preftool");
2020-02-24 03:29:09 +00:00
}
sleep($cpu_sleep_time);
$wait++;
}
}
if($wait > 0) {
2020-02-24 05:50:29 +00:00
write_common_log(sprintf("CPU usage recovered. waited %s seconds ago", ($wait * $cpu_sleep_time)), "helper/preftool");
2020-02-24 03:29:09 +00:00
}
}
}