23,116
社区成员
发帖
与我相关
我的任务
分享
unsigned long user_j, nice_j, sys_j, other_j; /* jiffies (clock ticks) */
double up_1, up_2, seconds;
unsigned long jiffies, h;
char buf[80];
int uptime_fd = -1;
int stat_fd = -1;
long smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
if(smp_num_cpus<1) smp_num_cpus=1;
do {
int local_n;
FILE_TO_BUF("uptime", uptime_fd);
up_1 = strtod(buf, 0);
FILE_TO_BUF("stat", stat_fd);
sscanf(buf, "cpu %lu %lu %lu %lu", &user_j, &nice_j, &sys_j, &other_j);
FILE_TO_BUF2("uptime", uptime_fd);
up_2 = strtod(buf, 0);
} while((long)( (up_2-up_1)*1000.0/up_1 )); /* want under 0.1% error */
close(uptime_fd);
close(stat_fd);
jiffies = user_j + nice_j + sys_j + other_j;
seconds = (up_1 + up_2) / 2;
h = (unsigned long)( (double)jiffies/seconds/smp_num_cpus );/* actual values used by 2.4 kernels: 32 64 100 128 1000 1024 1200 */
switch(h){
case 30 ... 34 : Hertz = 32; break; /* ia64 emulator */
case 48 ... 52 : Hertz = 50; break;
case 58 ... 62 : Hertz = 60; break;
case 63 ... 65 : Hertz = 64; break; /* StrongARM /Shark */
case 95 ... 105 : Hertz = 100; break; /* normal Linux */
case 124 ... 132 : Hertz = 128; break; /* MIPS, ARM */
case 195 ... 204 : Hertz = 200; break; /* normal << 1 */
case 253 ... 260 : Hertz = 256; break;
case 295 ... 304 : Hertz = 300; break; /* 3 cpus */
case 393 ... 408 : Hertz = 400; break; /* normal << 2 */
case 495 ... 504 : Hertz = 500; break; /* 5 cpus */
case 595 ... 604 : Hertz = 600; break; /* 6 cpus */
case 695 ... 704 : Hertz = 700; break; /* 7 cpus */
case 790 ... 808 : Hertz = 800; break; /* normal << 3 */
case 895 ... 904 : Hertz = 900; break; /* 9 cpus */
case 990 ... 1010 : Hertz = 1000; break; /* ARM */
case 1015 ... 1035 : Hertz = 1024; break; /* Alpha, ia64 */
case 1095 ... 1104 : Hertz = 1100; break; /* 11 cpus */
case 1180 ... 1220 : Hertz = 1200; break; /* Alpha */
default:
/* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
Hertz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;