summaryrefslogtreecommitdiff
path: root/bench/MHz.c
blob: 1cb46973e2dbb06003987cddecd93f1d130d7039 (plain)
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
/*
 *  (C) 2001 Dave Jones.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 */

#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>

#include <string.h>
#include <signal.h>

#include "../x86info.h"
#include "bench.h"

void display_MHz(struct cpudata *cpu)
{
	if (cpu->MHz < 1000)
		printf("%uMHz", cpu->MHz);
	else {
		int a = (cpu->MHz / 1000);
		int b = ((cpu->MHz % 1000)/100);
		int c = (a*1000)+(b*100);
		printf("%u.%u%uGHz", a, b, (cpu->MHz - c)/10);
	}
}

static volatile int nosignal = 0;

static void sighandler(int sig __attribute__((unused)))
{
	nosignal = 1;
}

void estimate_MHz(struct cpudata *cpu)
{
	unsigned long long int cycles[2]; /* gotta be 64 bit */
	unsigned int eax, ebx, ecx, edx;
	unsigned long r;

	if (cpu->number != 0) {
		if (all_cpus == 0) {
			cpu->MHz = firstcpu->MHz;
			return;
		}
	}

	/* Make sure we have a TSC (and hence RDTSC) */
	cpuid(cpu->number, 1, &eax, &ebx, &ecx, &edx);
	if ((edx & (1<<4)) == 0) {
		printf("No TSC, MHz calculation cannot be performed.\n");
		cpu->MHz = 0;
		return;
	}

	if (signal(SIGALRM, sighandler) == SIG_ERR) {
		printf("Some kind of signal failure.\n");
		return;
	}

	cycles[0] = rdtsc();

	alarm(1);
	while (!nosignal)
		r = r * rand();

	nosignal = 0;

	cycles[1] = rdtsc();

	/* Check to see if rdtsc wrapped */
	if (cycles[1] < cycles[0])
		/* yes we did. */
		cpu->MHz = ((-1ULL - cycles[0]) + cycles[1]) / 1000000;
	else
		cpu->MHz = (cycles[1] - cycles[0]) / 1000000;

	if ((cpu->MHz % 50) > 15)
		cpu->MHz = ((cpu->MHz / 50) * 50) + 50;
	else
		cpu->MHz = ((cpu->MHz / 50) * 50);
}