Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.2KB

  1. # ===========================================================================
  2. # https://www.gnu.org/software/autoconf-archive/ax_cpu_freq.html
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AX_CPU_FREQ
  8. #
  9. # DESCRIPTION
  10. #
  11. # Compute the CPU frequency and define CPU_FREQ accordingly.
  12. #
  13. # LICENSE
  14. #
  15. # Copyright (c) 2008 Christophe Tournayre <turn3r@users.sourceforge.net>
  16. #
  17. # Copying and distribution of this file, with or without modification, are
  18. # permitted in any medium without royalty provided the copyright notice
  19. # and this notice are preserved. This file is offered as-is, without any
  20. # warranty.
  21. #serial 8
  22. AC_DEFUN([AX_CPU_FREQ],
  23. [AC_REQUIRE([AC_PROG_CC])
  24. AC_LANG_PUSH([C++])
  25. AC_CACHE_CHECK(your cpu frequency, ax_cpu_freq,
  26. [AC_RUN_IFELSE([AC_LANG_PROGRAM([
  27. #include <iostream>
  28. #include <sys/time.h>
  29. #include <fstream>
  30. using namespace std;
  31. static __inline__ unsigned long long int rdtsc()
  32. {
  33. unsigned long long int x;
  34. __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
  35. return x;
  36. }
  37. static float estimate_MHz(long sleeptime = 250000)
  38. {
  39. struct timezone tz;
  40. struct timeval tvstart, tvstop;
  41. unsigned long long int cycles[[2]];
  42. float microseconds;
  43. double freq = 1.0f;
  44. memset(&tz, 0, sizeof(tz));
  45. gettimeofday(&tvstart, &tz);
  46. cycles[[0]] = rdtsc();
  47. gettimeofday(&tvstart, &tz);
  48. usleep(sleeptime);
  49. gettimeofday(&tvstop, &tz);
  50. cycles[[1]] = rdtsc();
  51. gettimeofday(&tvstop, &tz);
  52. microseconds = (tvstop.tv_sec - tvstart.tv_sec) * 1000000 +
  53. (tvstop.tv_usec - tvstart.tv_usec);
  54. return (float) (cycles[[1]] - cycles[[0]]) / (microseconds / freq);
  55. }
  56. static float average_MHz(int tries = 2)
  57. {
  58. float frequency = 0;
  59. for (int i = 1; i <= tries; i++)
  60. frequency += estimate_MHz(i * 150000);
  61. if (tries > 0)
  62. return frequency / (float) tries;
  63. else
  64. return 0;
  65. }
  66. ], [
  67. ofstream of("conftest_cpufreq");
  68. if(of.is_open())
  69. of << average_MHz();
  70. else
  71. return 1;
  72. of.close()
  73. ])],
  74. [ax_cpu_freq=`cat conftest_cpufreq`; rm -f conftest_cpufreq],
  75. [ax_cpu_freq=unknow; rm -f conftest_cpufreq]
  76. )])
  77. AC_LANG_POP([C++])
  78. AC_DEFINE_UNQUOTED([CPU_FREQ], ${ax_cpu_freq}, [The cpu frequency (in MHz)])
  79. ])