osfreq.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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,"clock : %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. // This is not an accurate measure of MHz but it gets things to build when other measures fail.
  41. f = fopen("/proc/cpuinfo","r");
  42. if (f) {
  43. for (;;) {
  44. s = fscanf(f,"BogoMIPS : %lf",&result);
  45. if (s > 0) break;
  46. if (s == 0) s = fscanf(f,"%*[^\n]\n");
  47. if (s < 0) { result = 0; break; }
  48. }
  49. fclose(f);
  50. if (result) return 1000000.0 * result;
  51. }
  52. f = popen("/usr/sbin/lsattr -E -l proc0 -a frequency 2>/dev/null","r");
  53. if (f) {
  54. s = fscanf(f,"frequency %lf",&result);
  55. pclose(f);
  56. if (s > 0) return result;
  57. }
  58. f = popen("/usr/sbin/psrinfo -v 2>/dev/null","r");
  59. if (f) {
  60. for (;;) {
  61. s = fscanf(f," The %*s processor operates at %lf MHz",&result);
  62. if (s > 0) break;
  63. if (s == 0) s = fscanf(f,"%*[^\n]\n");
  64. if (s < 0) { result = 0; break; }
  65. }
  66. pclose(f);
  67. if (result) return 1000000.0 * result;
  68. }
  69. return 0;
  70. }