Advertisement
Guest User

cpu-freq-chart-maker

a guest
Jun 6th, 2013
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/php
  2. <?php
  3. // 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
  4. $time=60;
  5. $silent=false;
  6. $delay=1;
  7. foreach($argv as $key => $val){
  8.     if($val=='-h'||$val=='--help'){
  9.         $script=strrpos($_SERVER["SCRIPT_NAME"],'/');
  10.         if(!is_bool($script))
  11.             $script=substr($_SERVER["SCRIPT_NAME"],$script+1);
  12.         else
  13.             $script=$_SERVER["SCRIPT_NAME"];
  14.         echo "Use the -t option to specify the number of times to poll the cores\n\t$script -t 60\n";
  15.         echo "Use the -s option to run the script silently\n\t$script -s yes\n\t$script -s\n";
  16.         echo "Use the -d option sets the poling interval\n\t$script -d 0.75\n";
  17.         die();
  18.     }
  19.     if(isset($argv[$key+1]))
  20.         $val2=$argv[$key+1];
  21.     else
  22.         unset($val2);
  23.     if($val=='-t'&&!is_null($val2)){
  24.         if(is_numeric($val2))
  25.             $time=$val2;
  26.         else
  27.             echo "$val2 is not a number, ignoring\n";
  28.     }
  29.     if($val=='-s')
  30.         $silent=true;
  31.     if($val=='-d'&&!is_null($val2)){
  32.         if(is_numeric($val2))
  33.             $delay=floatval($val2);
  34.         else
  35.             echo "$val2 is not a number, ignoring\n";
  36.     }
  37. }
  38. $arr=json_decode('{}');
  39. $ct=intval(shell_exec("ls /sys/devices/system/cpu/ | grep 'cpu[0-9]' | wc -l"));
  40. for ($i=0;$i<$ct;$i=$i+1){
  41.     $arr->{'core_'.$i}=Array();
  42. }
  43. for($i=0;$i<$time;$i=$i+1){
  44.     for($x=0;$x<$ct;$x=$x+1){
  45.         $val=intval(file_get_contents("/sys/devices/system/cpu/cpu$x/cpufreq/cpuinfo_cur_freq"));
  46.         if(!$silent)
  47.             echo "Core$x=$val\n";
  48.         array_push($arr->{'core_'.$x},$val);
  49.     }
  50.     shell_exec("sleep $delay");
  51.     if(!$silent)
  52.         echo "-------------\n";
  53. }
  54. $arr2="[['Time',";
  55. for($i=1;$i<=$ct;$i=$i+1){
  56.     $arr2.="'Core $i',";
  57. }
  58. $arr2=substr($arr2,0,-1);
  59. for($i=0;$i<$time;$i=$i+1){
  60.     $arr2.='],['.($delay*$i).',';
  61.     for($x=0;$x<$ct;$x=$x+1){
  62.         $arr2.=($arr->{'core_'.$x}[$i]/1000).',';
  63.     }
  64.     $arr2=substr($arr2,0,-1);
  65. }
  66. $arr2.=']]';
  67. $name='/tmp/CPU_FREQ_Chart-'.time().'.html';
  68. $file=fopen($name,'x+');
  69. 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>');
  70. fclose($file);
  71. 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";
  72. //shell_exec("xdg-open $name");
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement