#ifndef __MHz__ #define __MHz__ #include #include int MHz = 1; #define NOW (rdtsc()/MHz) static int roundoff_MHz(int e) { int possible[] = { 0, 25, 33, 50, 66, 75 }; int x, y, z = 1000000; for (x = 0; ; x += 100) { for (y = 0; y < sizeof(possible)/sizeof(possible[0]); y++) { int p = x + possible[y], d = e - p; if (p == 0) continue; if (d < 0) d = -d; if (z > d) z = d, MHz = p; else { if (d * 100 > 15 * MHz) return -1; /* printf("CPU %d MHz\n", MHz); */ return MHz; } } } } void determine_MHz() { quad_t r1, r2; struct timeval t1, t2; int e; if (!tsc_is_broken && roundoff_MHz(tsc_freq/1000/1000) >= 0) return; MHz = 1; microtime(&t1); r1 = NOW; DELAY(1000000); microtime(&t2); r2 = NOW; e = (r2 - r1)/((t2.tv_sec-t1.tv_sec)*1000000 + t2.tv_usec-t1.tv_usec); if (roundoff_MHz(e) >= 0) return; panic("unable to determine CPU frequency!"); } #endif