PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/phoronix-test-suite/pts-core/objects/phodevi/sensors/cpu_freq.php

#
PHP | 91 lines | 63 code | 7 blank | 21 comment | 14 complexity | 4f9e672df2ca4c1765e8203ba93a550d MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Phoronix Test Suite
  4. URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
  5. Copyright (C) 2009 - 2011, Phoronix Media
  6. Copyright (C) 2009 - 2011, Michael Larabel
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class cpu_freq implements phodevi_sensor
  19. {
  20. public static function get_type()
  21. {
  22. return 'cpu';
  23. }
  24. public static function get_sensor()
  25. {
  26. return 'freq';
  27. }
  28. public static function get_unit()
  29. {
  30. return 'Megahertz';
  31. }
  32. public static function support_check()
  33. {
  34. $test = self::read_sensor();
  35. return is_numeric($test) && $test != -1;
  36. }
  37. public static function read_sensor()
  38. {
  39. // Determine the current processor frequency
  40. $cpu_core = 0; // TODO: for now just monitoring the first core
  41. $info = 0;
  42. if(phodevi::is_linux())
  43. {
  44. // First, the ideal way, with modern CPUs using CnQ or EIST and cpuinfo reporting the current
  45. if(is_file('/sys/devices/system/cpu/cpu' . $cpu_core . '/cpufreq/scaling_cur_freq'))
  46. {
  47. $info = pts_file_io::file_get_contents('/sys/devices/system/cpu/cpu' . $cpu_core . '/cpufreq/scaling_cur_freq');
  48. $info = intval($info) / 1000;
  49. }
  50. else if(is_file('/proc/cpuinfo')) // fall back for those without cpufreq
  51. {
  52. $cpu_speeds = phodevi_linux_parser::read_cpuinfo('cpu MHz');
  53. if(isset($cpu_speeds[0]))
  54. {
  55. $cpu_core = (isset($cpu_speeds[$cpu_core]) ? $cpu_core : 0);
  56. $info = intval($cpu_speeds[$cpu_core]);
  57. }
  58. }
  59. }
  60. else if(phodevi::is_solaris())
  61. {
  62. $info = shell_exec('psrinfo -v | grep MHz');
  63. $info = substr($info, strrpos($info, 'at') + 3);
  64. $info = trim(substr($info, 0, strpos($info, 'MHz')));
  65. }
  66. else if(phodevi::is_bsd())
  67. {
  68. $info = phodevi_bsd_parser::read_sysctl('dev.cpu.0.freq');
  69. }
  70. else if(phodevi::is_macosx())
  71. {
  72. $info = phodevi_osx_parser::read_osx_system_profiler('SPHardwareDataType', 'ProcessorSpeed');
  73. if(($cut_point = strpos($info, ' ')) > 0)
  74. {
  75. $info = substr($info, 0, $cut_point);
  76. }
  77. }
  78. return pts_math::set_precision($info, 2);
  79. }
  80. }
  81. ?>