File: ax_cpu_freq.m4

package info (click to toggle)
autoconf-archive 20160916-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 7,960 kB
  • sloc: sh: 694; python: 62; makefile: 49
file content (94 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# ===========================================================================
#        http://www.gnu.org/software/autoconf-archive/ax_cpu_freq.html
# ===========================================================================
#
# SYNOPSIS
#
#   AX_CPU_FREQ
#
# DESCRIPTION
#
#   Compute the CPU frequency and define CPU_FREQ accordingly.
#
# LICENSE
#
#   Copyright (c) 2008 Christophe Tournayre <turn3r@users.sourceforge.net>
#
#   Copying and distribution of this file, with or without modification, are
#   permitted in any medium without royalty provided the copyright notice
#   and this notice are preserved. This file is offered as-is, without any
#   warranty.

#serial 7

AC_DEFUN([AX_CPU_FREQ],
[AC_REQUIRE([AC_PROG_CC])
 AC_LANG_PUSH([C++])
 AC_CACHE_CHECK(your cpu frequency, ax_cpu_freq,
 [AC_RUN_IFELSE([AC_LANG_PROGRAM([
#include <iostream>
#include <sys/time.h>
#include <fstream>
using namespace std;

static __inline__ unsigned long long int rdtsc()
{
  unsigned long long int x;
  __asm__ volatile (".byte 0x0f, 0x31":"=A" (x));
  return x;
}

static float estimate_MHz(long sleeptime = 250000)
{
  struct timezone tz;
  struct timeval tvstart, tvstop;
  unsigned long long int cycles[[2]];
  float microseconds;
  double freq = 1.0f;

  memset(&tz, 0, sizeof(tz));

  gettimeofday(&tvstart, &tz);
  cycles[[0]] = rdtsc();
  gettimeofday(&tvstart, &tz);

  usleep(sleeptime);

  gettimeofday(&tvstop, &tz);
  cycles[[1]] = rdtsc();
  gettimeofday(&tvstop, &tz);

  microseconds = (tvstop.tv_sec - tvstart.tv_sec) * 1000000 +
    (tvstop.tv_usec - tvstart.tv_usec);

  return (float) (cycles[[1]] - cycles[[0]]) / (microseconds / freq);
}

static float average_MHz(int tries = 2)
{
  float frequency = 0;

  for (int i = 1; i <= tries; i++)
    frequency += estimate_MHz(i * 150000);

  if (tries > 0)
    return frequency / (float) tries;
  else
    return 0;
}
], [
    ofstream of("conftest_cpufreq");
    if(of.is_open())
      of << average_MHz();
    else
      return 1;

    of.close()
])],
     [ax_cpu_freq=`cat conftest_cpufreq`; rm -f conftest_cpufreq],
     [ax_cpu_freq=unknow; rm -f conftest_cpufreq]
 )])
AC_LANG_POP([C++])

  AC_DEFINE_UNQUOTED([CPU_FREQ], ${ax_cpu_freq}, [The cpu frequency (in MHz)])
])