Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- // Be sure to run `bash -c 'chmod 444 /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq'` unless you intend to run this as root
- $time=60;
- $silent=false;
- $delay=1;
- foreach($argv as $key => $val){
- if($val=='-h'||$val=='--help'){
- $script=strrpos($_SERVER["SCRIPT_NAME"],'/');
- if(!is_bool($script))
- $script=substr($_SERVER["SCRIPT_NAME"],$script+1);
- else
- $script=$_SERVER["SCRIPT_NAME"];
- echo "Use the -t option to specify the number of times to poll the cores\n\t$script -t 60\n";
- echo "Use the -s option to run the script silently\n\t$script -s yes\n\t$script -s\n";
- echo "Use the -d option sets the poling interval\n\t$script -d 0.75\n";
- die();
- }
- if(isset($argv[$key+1]))
- $val2=$argv[$key+1];
- else
- unset($val2);
- if($val=='-t'&&!is_null($val2)){
- if(is_numeric($val2))
- $time=$val2;
- else
- echo "$val2 is not a number, ignoring\n";
- }
- if($val=='-s')
- $silent=true;
- if($val=='-d'&&!is_null($val2)){
- if(is_numeric($val2))
- $delay=floatval($val2);
- else
- echo "$val2 is not a number, ignoring\n";
- }
- }
- $arr=json_decode('{}');
- $ct=intval(shell_exec("ls /sys/devices/system/cpu/ | grep 'cpu[0-9]' | wc -l"));
- for ($i=0;$i<$ct;$i=$i+1){
- $arr->{'core_'.$i}=Array();
- }
- for($i=0;$i<$time;$i=$i+1){
- for($x=0;$x<$ct;$x=$x+1){
- $val=intval(file_get_contents("/sys/devices/system/cpu/cpu$x/cpufreq/cpuinfo_cur_freq"));
- if(!$silent)
- echo "Core$x=$val\n";
- array_push($arr->{'core_'.$x},$val);
- }
- shell_exec("sleep $delay");
- if(!$silent)
- echo "-------------\n";
- }
- $arr2="[['Time',";
- for($i=1;$i<=$ct;$i=$i+1){
- $arr2.="'Core $i',";
- }
- $arr2=substr($arr2,0,-1);
- for($i=0;$i<$time;$i=$i+1){
- $arr2.='],['.($delay*$i).',';
- for($x=0;$x<$ct;$x=$x+1){
- $arr2.=($arr->{'core_'.$x}[$i]/1000).',';
- }
- $arr2=substr($arr2,0,-1);
- }
- $arr2.=']]';
- $name='/tmp/CPU_FREQ_Chart-'.time().'.html';
- $file=fopen($name,'x+');
- fwrite($file,'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><script type="text/javascript" src="https://www.google.com/jsapi"></script><script type="text/javascript">google.load("visualization","1",{packages:["corechart"]});google.setOnLoadCallback(drawChart);function drawChart(){var data=google.visualization.arrayToDataTable('.$arr2.');var options={title:\'CPU Graph\',hAxis:{title:\'Time in Seconds\'},vAxis:{title:\'Frequency in Megahertz\'}};var chart=new google.visualization.LineChart(document.body);chart.draw(data, options);}</script><style type="text/css">html,body{width:100%;height:100%;margin:0;overflow:hidden;}</style></head><body></body></html>');
- fclose($file);
- echo "Chart saved to $name\nOpen by running one of these commands:\n\txdg-open $name\n\tgnome-open $name\n\tfirefox $name\n\tchromium-browser $name\n";
- //shell_exec("xdg-open $name");
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement