多核 CPU 频率

multicore CPU frequency

下面是我使用 rdtsc.

测试计算机 CPU 频率和计时功能的代码
/* Code only works on x86 machine compiling with GCC */

/* Keep track of most recent reading of cycle counter */
static unsigned cyc_hi = 0;
static unsigned cyc_lo = 0;

void access_counter(unsigned *hi, unsigned *lo)
{
  /* Get cycle counter */
  asm("rdtsc; movl %%edx,%0; movl %%eax,%1" 
      : "=r" (*hi), "=r" (*lo)
      : /* No input */ 
      : "%edx", "%eax");
}

double get_counter()
{
  unsigned ncyc_hi, ncyc_lo;
  unsigned hi, lo, borrow;
  double result;
  /* Get cycle counter */
  access_counter(&ncyc_hi, &ncyc_lo);
  /* Do double precision subtraction */
  lo = ncyc_lo - cyc_lo;
  borrow = lo > ncyc_lo;
  hi = ncyc_hi - cyc_hi - borrow;
  return (double) hi * (1 << 30) * 4 + lo;
}

void start_counter()
{
  access_counter(&cyc_hi, &cyc_lo);
}

void p()
{
  sleep(1);
}

int main(int argc, char const *argv[])
{
    /* Determine Clock Rate of Processor */
    double MHZ;
    int sleep_time = 10;
    start_counter();
    sleep(sleep_time);
    MHZ = get_counter() / (sleep_time * 1e6);
    printf("Processor Clock Rate ~= %.1f MHz\n", MHZ);
    /* cat /proc/cpuinfo */

    /* Time Function P */
    double tsecs;
    start_counter();
    p();
    tsecs = get_counter() / (MHZ * 1e6);
  printf("%.1f seconds\n", tsecs);
    return 0;
}

在运行ning这个程序之后,它打印出处理器时钟频率大约是3591.8MHz,然后我运行cat /proc/cpuinfo,它显示有8个处理器(0~7) ,有些处理器的cpu MHz是不一样的,但是没有一个是3591.8Mhz。我想知道如何解释这个值 3591.8MHz?非常感谢。

CPU每秒可以重新计时数千次。他们旨在做到这一点。您的程序可能会正常工作并显示当前频率的快照,而您之后手动执行 cat 时未观察到该频率?此外,您的程序会产生一些工作负载,因此它可能会激增一个 CPU 以暂时从某种深度休眠状态中出来以快速完成工作,然后返回睡眠状态。这是新的英特尔酷睿 i 处理器的行为。你的 CPU 是什么?如果你 cat /proc/cpuinfo 多次,你会得到相同的结果吗?

如果您使用 /proc/cpuinfobogomips 场是稳定的并且是 CPU 频率的 2 倍。

如果您有最新的处理器,它将具有固定的 TSC [最大] 时钟速率,在 /proc/cpuinfoflags 字段中用 constant_tsc 表示。这意味着它不会改变,即使处理器有速度步长。

所以,你要的是最大CPU频率,你可以通过阅读得到:

/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq

详情请看我的回答:Getting TSC rate in x86 kernel