osfreq.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. static double osfreq(void)
  2. {
  3. FILE *f;
  4. double result;
  5. int s;
  6. f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", "r");
  7. if (f) {
  8. s = fscanf(f,"%lf",&result);
  9. fclose(f);
  10. if (s > 0) return 1000.0 * result;
  11. }
  12. f = fopen("/sys/devices/system/cpu/cpu0/clock_tick", "r");
  13. if (f) {
  14. s = fscanf(f,"%lf",&result);
  15. fclose(f);
  16. if (s > 0) return result;
  17. }
  18. f = fopen("/proc/cpuinfo","r");
  19. if (f) {
  20. for (;;) {
  21. s = fscanf(f,"cpu MHz : %lf",&result);
  22. if (s > 0) break;
  23. if (s == 0) s = fscanf(f,"%*[^\n]\n");
  24. if (s < 0) { result = 0; break; }
  25. }
  26. fclose(f);
  27. if (result) return 1000000.0 * result;
  28. }
  29. f = fopen("/proc/cpuinfo","r");
  30. if (f) {
  31. for (;;) {
  32. s = fscanf(f,"cpu MHz dynamic : %lf",&result);
  33. if (s > 0) break;
  34. if (s == 0) s = fscanf(f,"%*[^\n]\n");
  35. if (s < 0) { result = 0; break; }
  36. }
  37. fclose(f);
  38. if (result) return 1000000.0 * result;
  39. }
  40. f = fopen("/proc/cpuinfo","r");
  41. if (f) {
  42. for (;;) {
  43. s = fscanf(f,"clock : %lf",&result);
  44. if (s > 0) break;
  45. if (s == 0) s = fscanf(f,"%*[^\n]\n");
  46. if (s < 0) { result = 0; break; }
  47. }
  48. fclose(f);
  49. if (result) return 1000000.0 * result;
  50. }
  51. // This is not an accurate measure of MHz but it gets things to build when other measures fail.
  52. f = fopen("/proc/cpuinfo","r");
  53. if (f) {
  54. for (;;) {
  55. s = fscanf(f,"BogoMIPS : %lf",&result);
  56. if (s > 0) break;
  57. if (s == 0) s = fscanf(f,"%*[^\n]\n");
  58. if (s < 0) { result = 0; break; }
  59. }
  60. fclose(f);
  61. if (result) return 1000000.0 * result;
  62. }
  63. f = popen("/usr/sbin/lsattr -E -l proc0 -a frequency 2>/dev/null","r");
  64. if (f) {
  65. s = fscanf(f,"frequency %lf",&result);
  66. pclose(f);
  67. if (s > 0) return result;
  68. }
  69. f = popen("/usr/sbin/psrinfo -v 2>/dev/null","r");
  70. if (f) {
  71. for (;;) {
  72. s = fscanf(f," The %*s processor operates at %lf MHz",&result);
  73. if (s > 0) break;
  74. if (s == 0) s = fscanf(f,"%*[^\n]\n");
  75. if (s < 0) { result = 0; break; }
  76. }
  77. pclose(f);
  78. if (result) return 1000000.0 * result;
  79. }
  80. return 0;
  81. }