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
|
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/mach_time.h>
#define timebase mach_absolute_time
static int cpumib[2] = { CTL_HW, HW_CPU_FREQ } ;
static int tbmib[2] = { CTL_HW, HW_TB_FREQ } ;
static long myround(double u)
{
long result = u;
while (result + 0.5 < u) result += 1;
while (result - 0.5 > u) result -= 1;
return result;
}
static long tbcycles = 0;
static void init(void)
{
unsigned int cpufrequency = 0; size_t cpufrequencylen = sizeof(unsigned int);
unsigned int tbfrequency = 0; size_t tbfrequencylen = sizeof(unsigned int);
sysctl(cpumib,2,&cpufrequency,&cpufrequencylen,0,0);
sysctl(tbmib,2,&tbfrequency,&tbfrequencylen,0,0);
if (tbfrequency > 0)
tbcycles = myround((double) (unsigned long long) cpufrequency
/ (double) (unsigned long long) tbfrequency);
}
long long cpucycles_powerpcmacos(void)
{
if (!tbcycles) init();
return timebase() * tbcycles;
}
long long cpucycles_powerpcmacos_persecond(void)
{
unsigned int result = 0; size_t resultlen = sizeof(unsigned int);
sysctl(cpumib,2,&result,&resultlen,0,0);
return (unsigned long long) result;
}
|