Linux cpu亲和力
2020-05-01 07:15
871 查看
最近在对项目进行性能优化,由于在多核平台上,所以了解了些进程、线程绑定cpu核的问题,在这里将所学记录一下。
不管是线程还是进程,都是通过设置亲和性(affinity)来达到目的。对于[进程]的情况,一般是使用sched_setaffinity这个函数来实现,网上讲的也比较多,这里主要讲一下[线程]的情况。
与[进程]的情况相似,[线程]亲和性的设置和获取主要通过下面两个函数来实现:
int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,const cpu_set_t *cpuset); int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset);从函数名以及参数名都很明了,唯一需要点解释下的可能就是cpu_set_t这个结构体了。这个结构体的理解类似于select中的fd_set, 可以理解为cpu集,也是通过约定好的宏来进行清除、设置以及判断:
void CPU_ZERO (cpu_set_t *set); //初始化,设为空 void CPU_SET (int cpu, cpu_set_t *set); //将某个cpu加入cpu集中 void CPU_CLR (int cpu, cpu_set_t *set); //将某个cpu从cpu集中移出 int CPU_ISSET (int cpu, const cpu_set_t *set); //判断某个cpu是否已在cpu集中设置了cpu集可以认为是一个掩码,每个设置的位都对应一个可以合法调度的 cpu,而未设置的位则对应一个不可调度的 CPU。换而言之,线程都被绑定了,只能在那些对应位被设置了的处理器上运行。通常,掩码中的所有位都被置位了,也就是可以在所有的cpu中调度。
以下为测试代码:
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include <sched.h> void *myfun(void *arg) { cpu_set_t mask; cpu_set_t get; char buf[256]; int i; int j; int num = sysconf(_SC_NPROCESSORS_CONF); printf("system has %d processor(s)\n", num); for (i = 0; i < num; i++) { CPU_ZERO(&mask); CPU_SET(i, &mask); if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) { fprintf(stderr, "set thread affinity failed\n"); } CPU_ZERO(&get); if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) { fprintf(stderr, "get thread affinity failed\n"); } for (j = 0; j < num; j++) { if (CPU_ISSET(j, &get)) { printf("thread %d is running in processor %d\n", (int)pthread_self(), j); } } j = 0; while (j++ < 100000000) { memset(buf, 0, sizeof(buf)); } } pthread_exit(NULL); } int main(int argc, char *argv[]) { pthread_t tid; if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0) { fprintf(stderr, "thread create failed\n"); return -1; } pthread_join(tid, NULL); return 0; }这段代码将使myfun线程在所有cpu中,依次执行一段时间,在我的四核cpu上,执行结果为 :
system has 4 processor(s) thread 1095604544 is running in processor 0 thread 1095604544 is running in processor 1 thread 1095604544 is running in processor 2 thread 1095604544 is running in processor 3在一些嵌入式设备中,运行的进程线程比较单一,如果指定进程线程运行于特定的cpu核,减少进程、线程的核间切换,有可能可以获得更高的性能。 回复「 篮球的大肚子」进入技术群聊
回复「1024」获取1000G学习资料 写代码的篮球球痴 博客专家 原创文章 435获赞 1136访问量 90万+ 关注 他的留言板
相关文章推荐
- linux进程、线程与cpu的亲和性(affinity)
- linux CPU Load负载详解,lscpu详解
- Linux查看内核、CPU等信息的命令
- Linux cpufreq驱动框架
- linux CPU Load负载详解,lscpu详解
- linux查看cpu 内存
- Linux性能优化和监控系列(二)――分析CPU性能
- Linux下分析某个进程CPU占用率高的原因
- linux性能问题(CPU,内存,磁盘I/O,网络)
- Linux下如何查看CPU信息, 包括位数和多核信息
- linux下查看最消耗CPU、内存的进程
- Linux下查看操作系统信息、内存情况及cpu信息:cpu个数、核心数、线程数
- 018、Linux性能监控:CPU
- 查看Linux进程CPU过高具体的线程堆栈(不中断程序)
- linux查看物理cpu、逻辑cpu个数
- Linux中线程与CPU核的绑定
- Linux压力测试中CPU个数、核数、逻辑CPU个数关系,配置
- Linux cpuinfo 详解
- Linux下计算进程的CPU占用和内存占用的编程方法[转]
- linux 下查看机器是cpu是几核的