按比例吃CPU
前几天测试软件在多核上的性能,需要按照比例吃各个CPU,查了查资料,撸了下面一小段代码;
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h> #define __USE_GNU
#include <pthread.h>
#include <sched.h> //CPU ID号
#define CPU_0 0x0
#define CPU_1 0x01
#define CPU_2 0x02
#define CPU_3 0x03 //总时间和运行时间
#define FULL_TIME 100
#define RUN_TIME 80 //时钟HZ数
static clock_t clktck; //用户参数输入的吃CPU百分比
static int eat_cpu_percent; //线程绑定CPU
int attach_cpu(int cpu_index)
{
int cpu_num = sysconf(_SC_NPROCESSORS_CONF);
if (cpu_index < || cpu_index >= cpu_num)
{
perror("cpu index ERROR!\n");
return -;
} cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu_index, &mask); if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < )
{
perror("set affinity np ERROR!\n");
return -;
} return ;
} //吃CPU线程
void *thread_task(void *param)
{
int *cpuid = (int *)param;
if (attach_cpu(*cpuid) < )
{
pthread_exit(NULL);
} clock_t time_start;
clock_t fulltime = FULL_TIME;
clock_t runtime = eat_cpu_percent;
while()
{
time_start = times(NULL);
while((times(NULL) - time_start) < runtime);
usleep((fulltime-runtime) * * / clktck);
} pthread_exit(NULL);
} int main(int argc, char *argv[])
{
if (argc < )
{
printf("Please run with 0-100! Example:./eat_cpu 80\n");
return -;
} eat_cpu_percent = (atoi)(argv[]);
if (eat_cpu_percent < || eat_cpu_percent > )
{
printf("eat cpu percent must in range:0-100!\n");
return -;
} int ret = ; clktck = sysconf(_SC_CLK_TCK); pthread_t t0;
int cpuid0 = CPU_0;
if (pthread_create(&t0, NULL, thread_task, &cpuid0) < )
{
perror("create thread 0 ERROR!\n");
ret = -;
goto _OUT;
} pthread_t t1;
int cpuid1 = CPU_1;
if (pthread_create(&t1, NULL, thread_task, &cpuid1) < )
{
perror("create thread 1 ERROR!\n");
ret = -;
goto _CANCEL_0;
} pthread_t t2;
int cpuid2 = CPU_2;
if (pthread_create(&t2, NULL, thread_task, &cpuid2) < )
{
perror("create thread 2 ERROR!\n");
ret = -;
goto _CANCEL_1;
} pthread_t t3;
int cpuid3 = CPU_3;
if (pthread_create(&t3, NULL, thread_task, &cpuid3) < )
{
perror("create thread 3 ERROR!\n");
ret = -;
goto _CANCEL_2;
} //直接停这里好了
while()
{
sleep();
} _CANCEL_3:
pthread_cancel(t3);
pthread_join(t3, NULL); _CANCEL_2:
pthread_cancel(t2);
pthread_join(t2, NULL); _CANCEL_1:
pthread_cancel(t1);
pthread_join(t1, NULL); _CANCEL_0:
pthread_cancel(t0);
pthread_join(t0, NULL); _OUT:
return ret;
}
按比例吃CPU的更多相关文章
- 吃CPU的openmp 程序
g++ -o eat -fopenmp eat.cpp #include "stdio.h" int main(int argc, char *argv[]) { #pragma ...
- .NET(C#):获取进程的CPU使用状况
第一个是通过手动的方法来计算CPU使用比例:CPU使用比例 = 在间隔时间内进程的CPU使用时间 除以 计算机逻辑CPU数量. 使用Process类的UserProcessorTime和Privile ...
- 【转】一文掌握 Linux 性能分析之 CPU 篇
[转]一文掌握 Linux 性能分析之 CPU 篇 平常工作会涉及到一些 Linux 性能分析的问题,因此决定总结一下常用的一些性能分析手段,仅供参考. 说到性能分析,基本上就是 CPU.内存.磁盘 ...
- 一文掌握 Linux 性能分析之 CPU 篇
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 平常工作会涉及 ...
- [转帖]Docker容器CPU、memory资源限制
Docker容器CPU.memory资源限制 https://www.cnblogs.com/zhuochong/p/9728383.html 处理事项内容等 这一块内容感觉 不清楚.. 背景 在使用 ...
- Docker 容器CPU设置
CPU使用率其实就是你运行的程序占用的CPU资源,表示你的机器在某个时间点的运行程序的情况.使用率越高,说明你的机器在这个时间上运行了很多程序,反之较少.CPU使用率的高低与你的CPU强弱有直接关系. ...
- 系统性能--CPU
对于cpu,目前比较关心的是cpu的利用率还有cpu的load,或者还有cpu运行队列. cpu利用率 cpu利用率分为sys,us.分别为操作系统和用户进程所占用的cpu利用率.sys的占用,一般是 ...
- Docker(二十)-Docker容器CPU、memory资源限制
背景 在使用 docker 运行容器时,默认的情况下,docker没有对容器进行硬件资源的限制,当一台主机上运行几百个容器,这些容器虽然互相隔离,但是底层却使用着相同的 CPU.内存和磁盘资源.如果不 ...
- docker cgroup技术之cpu和cpuset
在centos7的/sys/fs/cgroup下面可以看到与cpu相关的有cpu,cpuacct和cpuset 3个subsystem.cpu用于对cpu使用率的划分:cpuset用于设置cpu的亲和 ...
随机推荐
- 【DRF框架】restfull规范
零:核心思想: 1.面对资源编程 2.根据HTTP请求方式的不同对资源进行不同的操作 一.协议 API与用户的通信协议,总是使用HTTPs协议. 二.域名 应该尽量将API部署在专用域名之下. htt ...
- 原生Ajax代码实现
Ajax Asynchronous JavaScript And XML 异步: 指一段程序执行时不会阻塞其他程序执行,其表现形式为程序的执行顺序不依赖程序本身的书写顺序 ,相反的则为同步, 自己 ...
- 如何使用Feign构造多参数的请求
原文:http://www.itmuch.com/spring-cloud-sum/feign-multiple-params/ 本节来探讨如何使用Feign构造多参数的请求.笔者以GET及POST请 ...
- k8s的认证授权
一.ServiceAccount Service account是为了方便Pod里面的进程调用Kubernetes API或其他外部服务而设计的.它与User account不同 User acc ...
- flask 杂记2
添加属性 @property def password(self): return self._password @password.setter def password(self, raw): s ...
- Django REST framework+Vue 打造生鲜电商项目(笔记十)
(from:https://www.cnblogs.com/derek1184405959/p/8877643.html 有修改) 十三.首页.商品数量.缓存和限速功能开发 首先把pycharm环境 ...
- CF277B Set of Points——构造题
题意 构造 $n$ 个点使其凸度(顶点数最多的凸多边形的顶点数)恰好为 $m$,且任意三点不能共线. (题意有点绕,建议看英文原文 分析 首先,题目并不是求凸包上的顶点数,而是求能形成的凸多边形的最大 ...
- VS2010 insert Oracle数据库
背景:批量插入上万条数据到Oracle数据库的一张表里. 工具:VS2010. 因为是访问远程数据库,所以需要先装一个oracle client. 使用oracle客户端的方式访问数据库,需要添加对其 ...
- [bzoj 2393] Cirno的完美算数教室 (容斥原理+dfs剪枝)
题目描述 CirnoCirnoCirno发现了一种bakabakabaka数,这种数呢只含有222和999两种数字 现在CirnoCirnoCirno想知道[L,R][L,R][L,R]中有多少个数能 ...
- 40、扩展原理-BeanDefinitionRegistryPostProcessor
40.扩展原理-BeanDefinitionRegistryPostProcessor BeanDefinitionRegistryPostProcessor extends BeanFactoryP ...