【发布时间】:2012-03-26 15:44:03
【问题描述】:
是否可以通过某些 API 或函数获取此类信息,而不是解析 /proc/cpuinfo?
【问题讨论】:
-
重复问题。查看答案:stackoverflow.com/a/55304841/236062
标签: c linux operating-system cpu
是否可以通过某些 API 或函数获取此类信息,而不是解析 /proc/cpuinfo?
【问题讨论】:
标签: c linux operating-system cpu
来自man 5 proc:
/proc/cpuinfo This is a collection of CPU and system architecture dependent items, for each supported architecture a different list. Two common entries are processor which gives CPU number and bogomips; a system constant that is calculated during kernel initialization. SMP machines have information for each CPU.
这是读取信息并将其打印到控制台的示例代码,stolen from forums - 它实际上只是一个专门的cat 命令。
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *cpuinfo = fopen("/proc/cpuinfo", "rb");
char *arg = 0;
size_t size = 0;
while(getdelim(&arg, &size, 0, cpuinfo) != -1)
{
puts(arg);
}
free(arg);
fclose(cpuinfo);
return 0;
}
请注意,如果您真的关心 CPU 与 CPU 内核的数量,您需要解析和比较 physical id、core id 和 cpu cores 以获得准确的结果。另请注意,如果flags 中有htt,则表示您运行的是超线程CPU,这意味着您的里程可能会有所不同。
还请注意,如果您在虚拟机中运行内核,您只能看到专用于 VM 来宾的 CPU 内核。
【讨论】:
你可以将它用于几乎所有类型的 linux 发行版
对于 C 代码
num_cpus = sysconf( _SC_NPROCESSORS_ONLN );
(在QNX系统中,可以使用num_cpus = sysinfo_numcpu())
对于shell脚本,你可以使用cat /proc/cpuinfo
或者在linux中使用lscpu或者nproc命令
【讨论】:
libcpuid 提供了一个简单的 API,它将直接返回所有 CPU 功能,包括内核数量。要在运行时获取内核数,您可以执行以下操作:
#include <stdio.h>
#include <libcpuid.h>
int main(void)
{
if (!cpuid_present()) {
printf("Sorry, your CPU doesn't support CPUID!\n");
return -1;
}
struct cpu_raw_data_t raw;
struct cpu_id_t data;
if (cpuid_get_raw_data(&raw) < 0) {
printf("Sorry, cannot get the CPUID raw data.\n");
printf("Error: %s\n", cpuid_error());
return -2;
}
if (cpu_identify(&raw, &data) < 0) {
printf("Sorrry, CPU identification failed.\n");
printf("Error: %s\n", cpuid_error());
return -3;
}
printf("Processor has %d physical cores\n", data.num_cores);
return 0;
}
【讨论】:
configure && make && sudo make install 并且失败了,可能真的很简单:)
阅读/proc/cpuinfo
样本输出
processor : 0
model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz
cache size : 6144 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 4
processor : 1
model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz
cache size : 6144 KB
physical id : 0
siblings : 4
core id : 1
cpu cores : 4
processor : 2
model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz
cache size : 6144 KB
physical id : 0
siblings : 4
core id : 2
cpu cores : 4
processor : 3
model name : Intel(R) Xeon(R) CPU E5410 @ 2.33GHz
cache size : 6144 KB
physical id : 0
siblings : 4
core id : 3
cpu cores : 4
show_cpuinfo 是实际实现/proc/cpuinfo 功能的函数
【讨论】:
在您的源代码中添加以下行..
system("cat /proc/cpuinfo | grep processor | wc -l");
这将打印系统中的 CPU 数量。 如果你想在你的程序中使用这个系统调用的输出而不是使用 popen 系统调用。
【讨论】:
解析文件 /proc/cpuinfo.这将为您提供有关 CPU 的许多详细信息。将相关字段提取到您的 C/C++ 文件中。
【讨论】:
不,不是。要么你必须解析 cpuinfo 文件,要么某个库会为你解析。
【讨论】:
根据您的 Linux 风格,您将从 /proc/cpuid 获得不同的结果。
这对我在 CentOS 上用于获取内核总数有用。
cat /proc/cpuinfo | grep -w cores | sed -e 's/\t//g' | awk '{print $3}' | xargs | sed -e 's/\ /+/g' | bc
同样的方法在 Ubuntu 中不起作用。对于 Ubuntu,您可以使用以下命令。
nproc
【讨论】:
cat 的无用用法。以下将正常工作:grep -m1 "cores" /proc/cpuinfo | tr -d '[a-z]:[:space:]'
你见过这个shell命令“cat /proc/cpuinfo”的输出吗?我认为在那里你可以得到你需要的所有信息。 要读取 C 程序中的信息,我更喜欢 fopen、fgets 等文件操作函数。
【讨论】: