Tre*_*iño 56 c linux cpu multithreading processor
是否有一个API来获取Linux中可用的CPU数量?我的意思是,不使用/ proc/cpuinfo或任何其他sys-node文件......
我已经使用sched.h找到了这个实现:
int GetCPUCount()
{
cpu_set_t cs;
CPU_ZERO(&cs);
sched_getaffinity(0, sizeof(cs), &cs);
int count = 0;
for (int i = 0; i < 8; i++)
{
if (CPU_ISSET(i, &cs))
count++;
}
return count;
}
Run Code Online (Sandbox Code Playgroud)
但是,使用公共库是不是更高级别?
chr*_*ock 75
#include <unistd.h>
long number_of_processors = sysconf(_SC_NPROCESSORS_ONLN);
Run Code Online (Sandbox Code Playgroud)
Vik*_*exe 20
此代码(从此处绘制)应适用于Windows和*NIX平台.
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main() {
long nprocs = -1;
long nprocs_max = -1;
#ifdef _WIN32
#ifndef _SC_NPROCESSORS_ONLN
SYSTEM_INFO info;
GetSystemInfo(&info);
#define sysconf(a) info.dwNumberOfProcessors
#define _SC_NPROCESSORS_ONLN
#endif
#endif
#ifdef _SC_NPROCESSORS_ONLN
nprocs = sysconf(_SC_NPROCESSORS_ONLN);
if (nprocs < 1)
{
fprintf(stderr, "Could not determine number of CPUs online:\n%s\n",
strerror (errno));
exit (EXIT_FAILURE);
}
nprocs_max = sysconf(_SC_NPROCESSORS_CONF);
if (nprocs_max < 1)
{
fprintf(stderr, "Could not determine number of CPUs configured:\n%s\n",
strerror (errno));
exit (EXIT_FAILURE);
}
printf ("%ld of %ld processors online\n",nprocs, nprocs_max);
exit (EXIT_SUCCESS);
#else
fprintf(stderr, "Could not determine number of CPUs");
exit (EXIT_FAILURE);
#endif
}
Run Code Online (Sandbox Code Playgroud)
小智 20
#include <stdio.h>
#include <sys/sysinfo.h>
int main(int argc, char *argv[])
{
printf("This system has %d processors configured and "
"%d processors available.\n",
get_nprocs_conf(), get_nprocs());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
https://linux.die.net/man/3/get_nprocs
R..*_*R.. 13
使用/proc/cpuinfo是最干净,最便携的解决方案.如果打开失败,你可以简单地假设1个cpu或2个cpu.代码依赖于为微观优化以外的目的而知道cpus的数量(例如,选择要运行的理想线程数)几乎肯定会做一些愚蠢的事情.
该_SC_NPROCESSORS_ONLN解决方案依赖于非标准(特定于glibc)的sysconf扩展,这是一个比/proc(所有Linux系统都具有的更大依赖性/proc,但有些具有非glibc libcs或缺少glibc的旧版本_SC_NPROCESSORS_ONLN).
RCL*_*RCL 11
sched_affinity()你在开头提到的版本仍然比/proc/cpuinfo和/或更好,_SC_NPROCESSORS_ONLN因为它只计算给定进程可用的CPU(有些可能被sched_setaffinity()外部进程调用禁用).唯一的变化是使用CPU_COUNT()而不是CPU_ISSET循环.
小智 7
没有一个答案涉及sysconf(...)或get_nprocs()不正确地遵守由 cpu 关联性限制的任务的处理器数量。
您需要这样的方法来获取任务可用的处理器数量:
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
int nprocs()
{
cpu_set_t cs;
CPU_ZERO(&cs);
sched_getaffinity(0, sizeof(cs), &cs);
return CPU_COUNT(&cs);
}
int main()
{
printf("procs=%d\n", nprocs());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我个人对于最近的英特尔 CPU 使用这个:
int main()
{
unsigned int eax=11,ebx=0,ecx=1,edx=0;
asm volatile("cpuid"
: "=a" (eax),
"=b" (ebx),
"=c" (ecx),
"=d" (edx)
: "0" (eax), "2" (ecx)
: );
printf("Cores: %d\nThreads: %d\nActual thread: %d\n",eax,ebx,edx);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Cores: 4
Threads: 8
Actual thread: 1
Run Code Online (Sandbox Code Playgroud)
或者,更简洁地说:
#include <stdio.h>
int main()
{
unsigned int ncores=0,nthreads=0,ht=0;
asm volatile("cpuid": "=a" (ncores), "=b" (nthreads) : "a" (0xb), "c" (0x1) : );
ht=(ncores!=nthreads);
printf("Cores: %d\nThreads: %d\nHyperThreading: %s\n",ncores,nthreads,ht?"Yes":"No");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Cores: 4
Threads: 8
HyperThreading: Yes
Run Code Online (Sandbox Code Playgroud)