如何检测运行时正在使用的CPU? [英] How to detect what CPU is being used during runtime?

查看:163
本文介绍了如何检测运行时正在使用的CPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测运行时正在使用哪个CPU? C ++代码需要区分AMD / Intel架构?使用gcc 4.2。

解决方案

如果您在Linux上(或在Cygwin下运行的Windows上),可以通过阅读特殊文件 / proc / cpuinfo 并寻找以 vendor_id 开头的行。如果字符串是 GenuineIntel ,那么您正在使用英特尔芯片。如果您获得 AuthenticAMD ,则您正在使用AMD芯片。

  void get_vendor_id(char * vendor_id)//必须至少有13个字节
{
FILE * cpuinfo = fopen(/ proc / cpuinfo,r);
if(cpuinfo == NULL)
; //处理错误
char line [256];
while(fgets(line,256,cpuinfo))
{
if(strncmp(line,vendor_id,9)== 0)
{
char *冒号= strchr(行,':');
if(colon == NULL || colon [1] == 0)
; //处理错误
strncpy(vendor_id,12,冒号+ 2);
fclose(cpuinfo);
return;
}
}

//如果我们在这里,处理错误
fclose(cpuinfo);



$ b如果你知道你在x86架构上运行,将使用CPUID指令:

$ $ p $ $ $ $ $ $ $ get_vendor_id(char * vendor_id)//必须至少有13个字节
{
// GCC内联汇编程序
__asm__ __volatile__
(movl $ 0,%% eax\\\
\t
cpuid\\\
\t
movl %% ebx,%0 \\\
\t
movl %% edx,%1 \\\
\t
movl %% ecx,%2\\ \\ n \ t
:= m(vendor_id),= m(vendor_id + 4),= m(vendor_id + 8)//输出
://无输入
:%eax,%ebx,%edx,%ecx,内存); // clobbered寄存器
vendor_id [12] = 0;
}

int main(void)
{
char vendor_id [13];
get_vendor_id(vendor_id);

if(strcmp(vendor_id,GenuineIntel)== 0)
; //它是英特尔
else if(strcmp(vendor_id,AuthenticAMD)== 0)
; //它是AMD
else
; //其他
返回0;
}


How can I detect which CPU is being used at runtime ? The c++ code needs to differentiate between AMD / Intel architectures ? Using gcc 4.2.

解决方案

If you're on Linux (or on Windows running under Cygwin), you can figure that out by reading the special file /proc/cpuinfo and looking for the line beginning with vendor_id. If the string is GenuineIntel, you're running on an Intel chip. If you get AuthenticAMD, you're running on an AMD chip.

void get_vendor_id(char *vendor_id)  // must be at least 13 bytes
{
    FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
    if(cpuinfo == NULL)
        ;  // handle error
    char line[256];
    while(fgets(line, 256, cpuinfo))
    {
        if(strncmp(line, "vendor_id", 9) == 0)
        {
            char *colon = strchr(line, ':');
            if(colon == NULL || colon[1] == 0)
                ;  // handle error
            strncpy(vendor_id, 12, colon + 2);
            fclose(cpuinfo);
            return;
        }
    }

    // if we got here, handle error
    fclose(cpuinfo);
}

If you know you're running on an x86 architecture, a less portable method would be to use the CPUID instruction:

void get_vendor_id(char *vendor_id)  // must be at least 13 bytes
{
    // GCC inline assembler
    __asm__ __volatile__
        ("movl $0, %%eax\n\t"
         "cpuid\n\t"
         "movl %%ebx, %0\n\t"
         "movl %%edx, %1\n\t"
         "movl %%ecx, %2\n\t"
         : "=m"(vendor_id), "=m"(vendor_id + 4), "=m"(vendor_id + 8)  // outputs
         : // no inputs
         : "%eax", "%ebx", "%edx", "%ecx", "memory");  // clobbered registers
    vendor_id[12] = 0;
}

int main(void)
{
    char vendor_id[13];
    get_vendor_id(vendor_id);

    if(strcmp(vendor_id, "GenuineIntel") == 0)
        ; // it's Intel
    else if(strcmp(vendor_id, "AuthenticAMD") == 0)
        ; // it's AMD
    else
        ; // other
    return 0;
}

这篇关于如何检测运行时正在使用的CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆