前几天测试软件在多核上的性能,需要按照比例吃各个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的更多相关文章

  1. 吃CPU的openmp 程序

    g++ -o eat -fopenmp eat.cpp #include "stdio.h" int main(int argc, char *argv[]) { #pragma ...

  2. .NET(C#):获取进程的CPU使用状况

    第一个是通过手动的方法来计算CPU使用比例:CPU使用比例 = 在间隔时间内进程的CPU使用时间 除以 计算机逻辑CPU数量. 使用Process类的UserProcessorTime和Privile ...

  3. 【转】一文掌握 Linux 性能分析之 CPU 篇

    [转]一文掌握 Linux 性能分析之 CPU 篇 平常工作会涉及到一些 Linux 性能分析的问题,因此决定总结一下常用的一些性能分析手段,仅供参考. 说到性能分析,基本上就是 CPU.内存.磁盘 ...

  4. 一文掌握 Linux 性能分析之 CPU 篇

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 平常工作会涉及 ...

  5. [转帖]Docker容器CPU、memory资源限制

    Docker容器CPU.memory资源限制 https://www.cnblogs.com/zhuochong/p/9728383.html 处理事项内容等 这一块内容感觉 不清楚.. 背景 在使用 ...

  6. Docker 容器CPU设置

    CPU使用率其实就是你运行的程序占用的CPU资源,表示你的机器在某个时间点的运行程序的情况.使用率越高,说明你的机器在这个时间上运行了很多程序,反之较少.CPU使用率的高低与你的CPU强弱有直接关系. ...

  7. 系统性能--CPU

    对于cpu,目前比较关心的是cpu的利用率还有cpu的load,或者还有cpu运行队列. cpu利用率 cpu利用率分为sys,us.分别为操作系统和用户进程所占用的cpu利用率.sys的占用,一般是 ...

  8. Docker(二十)-Docker容器CPU、memory资源限制

    背景 在使用 docker 运行容器时,默认的情况下,docker没有对容器进行硬件资源的限制,当一台主机上运行几百个容器,这些容器虽然互相隔离,但是底层却使用着相同的 CPU.内存和磁盘资源.如果不 ...

  9. docker cgroup技术之cpu和cpuset

    在centos7的/sys/fs/cgroup下面可以看到与cpu相关的有cpu,cpuacct和cpuset 3个subsystem.cpu用于对cpu使用率的划分:cpuset用于设置cpu的亲和 ...

随机推荐

  1. BASIS小问题汇总1

    try to start SAP system but failed 2019-04-04 Symptom: when i tried to start SAP system, using the c ...

  2. STM8 LED

    时钟分频寄存器(CLK_CKDIVR) 举例 int main() { CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //高速内部时钟1分频 GPIO_ ...

  3. List集合和Set集合互转

    List集合转成Set集合(如果List集合的元素有重复,转成Set集合就会去掉重复的数据,每条数据只保留一条) //List转化成Set List<String> list = new ...

  4. linux uniq命令用法

    uniq命令: 对指定的ASCII文件或标准输入进行唯一性检查,以判断文本文件中重复出现的行,常用于分析日志:查看tcp各个状态连接数,ip或域名连接数排名等等场景,一般与 sort 命令结合使用. ...

  5. 如何在github上提交pr

    如何在github上提交pr 1.fork开源的代码到自己的远程仓库 2.clone自己的仓库到本地电脑 3.与源代码的github仓库建立新的连接 git remote add upstream h ...

  6. oracle concepts学习

    祭图一张!!!

  7. CIP 协议安全扫盲

  8. Redis数据缓存淘汰策略【FIFO 、LRU、LFU】

    FIFO.LFU.LRU FIFO:先进先出算法 FIFO(First in First out),先进先出.在FIFO Cache设计中,核心原则就是:如果一个数据最先进入缓存中,则应该最早淘汰掉. ...

  9. cookie、session与用户认证组件

    1.cookie def login(request): if request.method == "GET": return render(request,"login ...

  10. unittest单元测试笔记

    单元测试 unittest单元测试是基于java的JUnit思想框架开发出来的测试框架 import unittest import HTMLTestRunner class Mytest1(unit ...