Linux内核中BogoMips是如何计算的?

发布于 2024-11-17 23:12:48 字数 44 浏览 0 评论 0原文

Linux内核中bogomips是如何计算的?究竟做了什么才能获得这个值?

How bogomips is calculated in linux kernel? What exactly is done to get this value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随遇而安 2024-11-24 23:12:48
/*
 *                Standalone BogoMips program
 *
 * Based on code Linux kernel code in init/main.c and
 * include/linux/delay.h
 *
 * For more information on interpreting the results, see the BogoMIPS
 * Mini-HOWTO document.
 *
 * version: 1.3 
 *  author: Jeff Tranter ([email protected])
 */

#include <stdio.h>
#include <time.h>

#ifdef CLASSIC_BOGOMIPS
/* the original code from the Linux kernel */
static __inline__ void delay(int loops)
{
  __asm__(".align 2,0x90\n1:\tdecl %0\n\tjns 1b": :"a" (loops):"ax");
}
#endif

#ifdef QNX_BOGOMIPS
/* version for QNX C compiler */
void delay(int loops);
#pragma aux delay = \
     "l1:"       \
     "dec eax"   \
     "jns l1"    \
     parm nomemory [eax] modify exact nomemory [eax];
#endif

#ifdef PORTABLE_BOGOMIPS
/* portable version */
static void delay(int loops)
{
  long i;
  for (i = loops; i >= 0 ; i--)
    ;
}
#endif

int
main(void)
{
  unsigned long loops_per_sec = 1;
  unsigned long ticks;

  printf("Calibrating delay loop.. ");
  fflush(stdout);

  while ((loops_per_sec <<= 1)) {
    ticks = clock();
    delay(loops_per_sec);
    ticks = clock() - ticks;
    if (ticks >= CLOCKS_PER_SEC) {
      loops_per_sec = (loops_per_sec / ticks) * CLOCKS_PER_SEC;
      printf("ok - %lu.%02lu BogoMips\n",
         loops_per_sec/500000,
         (loops_per_sec/5000) % 100
         );
      return 0;
    }
  }
  printf("failed\n");
  return -1;
}
/*
 *                Standalone BogoMips program
 *
 * Based on code Linux kernel code in init/main.c and
 * include/linux/delay.h
 *
 * For more information on interpreting the results, see the BogoMIPS
 * Mini-HOWTO document.
 *
 * version: 1.3 
 *  author: Jeff Tranter ([email protected])
 */

#include <stdio.h>
#include <time.h>

#ifdef CLASSIC_BOGOMIPS
/* the original code from the Linux kernel */
static __inline__ void delay(int loops)
{
  __asm__(".align 2,0x90\n1:\tdecl %0\n\tjns 1b": :"a" (loops):"ax");
}
#endif

#ifdef QNX_BOGOMIPS
/* version for QNX C compiler */
void delay(int loops);
#pragma aux delay = \
     "l1:"       \
     "dec eax"   \
     "jns l1"    \
     parm nomemory [eax] modify exact nomemory [eax];
#endif

#ifdef PORTABLE_BOGOMIPS
/* portable version */
static void delay(int loops)
{
  long i;
  for (i = loops; i >= 0 ; i--)
    ;
}
#endif

int
main(void)
{
  unsigned long loops_per_sec = 1;
  unsigned long ticks;

  printf("Calibrating delay loop.. ");
  fflush(stdout);

  while ((loops_per_sec <<= 1)) {
    ticks = clock();
    delay(loops_per_sec);
    ticks = clock() - ticks;
    if (ticks >= CLOCKS_PER_SEC) {
      loops_per_sec = (loops_per_sec / ticks) * CLOCKS_PER_SEC;
      printf("ok - %lu.%02lu BogoMips\n",
         loops_per_sec/500000,
         (loops_per_sec/5000) % 100
         );
      return 0;
    }
  }
  printf("failed\n");
  return -1;
}
如梦亦如幻 2024-11-24 23:12:48

太糟糕了,考虑到这些

CFLAGS="-Wall -O2 -fomit-frame-pointer -finline-functions -s -std=gnu99"

独立的 bogomips 程序总是失败。从 -O2 更改为 -O0 使其实际工作,尽管报告的值远低于 /proc/cpuinfo 报告的值。有趣的是,我已经尝试了 gcc (4.4.6) 手册页中列出的所有 -f 优化器标志(对于 -O1、-O2 甚至 -O3),但结果没有改变。添加 -O1 后失败。我想知道应该启用/禁用延迟循环的严格优化器标志集是什么,以免变成单个标量表达式(导致经过的刻度始终保持为 0)。为什么-O1会导致失败?

Too bad that given these

CFLAGS="-Wall -O2 -fomit-frame-pointer -finline-functions -s -std=gnu99"

the standalone bogomips program always fails. Changing from -O2 to -O0 makes it actually work, although reported values are much lower than reported by /proc/cpuinfo. Interestingly, I have tried all the -f optimizer flags that are listed in the gcc (4.4.6) man page (for -O1, -O2 and even -O3), but the results don't change. Upon adding -O1 it fails. I'm wondering what is the strict set of optimizer flags that should be enabled/disabled for the delay loop to not get turned into a single scalar expression (causing the elapsed ticks to always remain 0). Why does -O1 make it fail?

魔法唧唧 2024-11-24 23:12:48

我没有定义任何延迟版本,只是提供了这个原型:

extern void Delay(int Loops);

并从 QNX 版本中获取提示,使用“nasm -felf64 delay.asm”

部分 .text
组装以下内容
;原型 extern void 延迟(int 循环);
全局延迟
延迟:
dec edi ;rdi 是 64 位 C 程序中传递的第一个参数
jnz 延迟
ret

和最后

gcc -s -O2 -Wall bogomips.c delay.o -o bogomips

这是在 64 位 Linux 中。顺便说一句,结果比 /proc/cpuinfo 中的值更快,并且更改 gcc 选项中的 -O(n) 值几乎没有什么区别

I didn't define any of the delay versions, just provided this prototype:

extern void delay(int loops);

and taking a cue from the QNX version, assembled the following with "nasm -felf64 delay.asm"

section .text
; prototype extern void delay(int loops);
global delay
delay:
dec edi ;rdi is the first parameter passed in 64 bit C programs
jnz delay
ret

and finally

gcc -s -O2 -Wall bogomips.c delay.o -o bogomips

This is in 64 bit Linux. BTW, the result was faster than the value in /proc/cpuinfo, and changing the -O(n) values in the gcc options hardly made a difference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文