入职Oracle 以后想着把之前写过的《编程之美》中控制CPU使用率曲线的程序再写一边, 可是总是由于入职须要学习的东西太多, 没有时间。

程序早就写好了。 最终有机会贴出来了。o(∩∩)o..

最早的时候我採用C实现的效果并不好。 当时也没有认真调试。 最初的时候採用C#实现的效果反而不错, 由于C#有非常多方便的类库能够获取CPU的占用率, 控制sleep时间等等。事实上在C中也非常easy实现。

整体的算法思想, 这里就不再反复了, 能够參考以下的链接 :

http://blog.csdn.net/watkinsong/article/details/6865775

http://blog.csdn.net/watkinsong/article/details/6867373

http://blog.csdn.net/watkinsong/article/details/6867473

http://blog.csdn.net/watkinsong/article/details/6867666

http://blog.csdn.net/watkinsong/article/details/6870748

http://blog.csdn.net/watkinsong/article/details/6871235

本次算法的实现, 全部的代码都托管到了github, 而且使用了makefile编译文件。

地址: https://github.com/weixsong/aventador

以下简单的给出代码和效果截图。 这里的效果实现比曾经的C#实现的那个sin曲线要好一些。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <unistd.h> extern const double PI = 3.1415926; // setup the cpu set of this program to run on
int set_cpu(int id)
{
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(id, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
{
fprintf(stderr, "warning: could not set CPU affinity/n");
return 0;
}
return 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <unistd.h> #include "CPUtils.h" const int CPU_KERNEL_ID = 0x0003; const int SAMPLE_COUNT = 200;
const int TIME_SLICE = 200; // ms in second
const int TIME_TRANFORM = 1000; // change ms to us
long * busy_span; // init the busy span, this is used to control the cpu busy time for each sample point
int init_busySpan(int sample_count)
{
busy_span = (long *)malloc(sample_count * sizeof(long));
if(busy_span == NULL)
{
return -1;
}
double radian = 0.0;
double radianIncrement = 2.0 / (double)sample_count;
int amplitude = (int)(TIME_SLICE / 2); // amplitude of sin curves, it means half of the time slice because sin() has negative value int i;
for(i = 0; i < sample_count; i++)
{
busy_span[i] = (long)(amplitude + sin(radian * PI) * amplitude);
radian = radian + radianIncrement;
}
return 1;
} int main(void)
{
if(set_cpu(CPU_KERNEL_ID) == 0)
{
printf("cpu affinity set failed\n");
}
else
{
printf("cpu affinity set succeeded\n");
} printf("clock per second:%ld \n", CLOCKS_PER_SEC);
fflush(stdout); if(!(init_busySpan(SAMPLE_COUNT)))
{
printf("init error \n");
return 0;
} int i = 0;
long busy_time; // us
long sleep_time; // us
clock_t begin;
for(i = 0; ; i = (i + 1) % SAMPLE_COUNT)
{
busy_time = busy_span[i] * TIME_TRANFORM;
begin = clock();
while((clock() - begin) < busy_time)
{
// loop
}
sleep_time = (long)((TIME_SLICE - busy_span[i])) * TIME_TRANFORM;
usleep(sleep_time);
} return 1;
}

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2F0a2luc29uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

生成CPU使用率 sin 曲线 控制cpu使用率 编程之美的更多相关文章

  1. Linux 控制CPU使用率

    曾经看过<编程之美>上提到说使 CPU的使用率固定在百分之多少.然后这次刚好要用到这个东西,下面是一个简单的实现.基于多线程: Linux 版本: #include <iostrea ...

  2. second blog编程之美------控制cpu曲线

    先贴程序: 以前看过这个算法, 不过没什么印象,大概记得它利用while循环来控制cpu利用率 #include int main(int argc,char*argv[]) {         wh ...

  3. 编程之美读书笔记1.1——让CPU占用率曲线听你的指挥

    http://blog.csdn.net/pipisorry/article/details/36189155 <strong><span style="font-size ...

  4. 第1章 游戏之乐——让CPU占用率曲线听你指挥

    让CPU占用率曲线听你指挥 写一个程序,让用于来决定Windows任务管理器(Task Manager)的CPU占用率.程序越精简越好,计算机语言不限.例如,可以实现下面三种情况: CPU的占用率固定 ...

  5. 编程之美 1.1 让cpu占用率曲线听你指挥(多核处理器)

    [目录] 不考虑其他进程,cpu画正弦曲线 获取总体cpu利用率 获取多核处理器单个cpu利用率 考虑其他进程,cpu画正弦曲线 下面的程序针对多核处理器,可以设置让任何一个cpu显示相应的曲线(本文 ...

  6. 让cpu占用率曲线听你指挥(多核处理器)

    编程之美 1.1 让cpu占用率曲线听你指挥(多核处理器) [版权声明]转载请注明出处 http://www.cnblogs.com/TenosDoIt/p/3242910.html  [目录] 不考 ...

  7. Linux资源控制-CPU和内存

    主要介绍Linux下, 如果对进程的CPU和内存资源的使用情况进行控制的方法. CPU资源控制 每个进程能够占用CPU多长时间, 什么时候能够占用CPU是和系统的调度密切相关的. Linux系统中有多 ...

  8. Linux资源控制-CPU和内存【转】

    转自:http://www.cnblogs.com/wang_yb/p/3942208.html 主要介绍Linux下, 如果对进程的CPU和内存资源的使用情况进行控制的方法. CPU资源控制 每个进 ...

  9. Cgroups控制cpu,内存,io示例

    Cgroups是control groups的缩写,最初由Google工程师提出,后来编进linux内核. Cgroups是实现IaaS虚拟化(kvm.lxc等),PaaS容器沙箱(Docker等)的 ...

随机推荐

  1. cookie 实现记住用户名演示 通过代码迅速理解cookie

    // 登录页 可直接 tomcat部署 测试 1 package com.itheima.login; import java.io.IOException; import java.io.Print ...

  2. clear---清除当前屏幕

    clear命令用于清除当前屏幕终端上的任何信息.

  3. 删除小脚本 srm

    提示:只能删除当前路径下的目录或文件 #!/bin/bash #将测试好的脚本,拷贝到 $PATH 能够搜索到目录下.并且改名 例如: /usr/local/bin cp /test/srm.sh / ...

  4. java解析json文件(省,市,区)

    [{"code":"11","name":"北京市"},{"code":"12" ...

  5. 【Codeforces Round #462 (Div. 1) A】 A Twisty Movement

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] ans初值值为a[1..n]中1的个数. 接下来考虑以2为结尾的最长上升子序列的个数. 枚举中间点i. 计算1..i-1中1的个数c ...

  6. 常量成员函数的注意事项 & mutable的使用场景

    mutable的使用场景: 可以在一个const的对象里面,解除对部分字段的const限制.也可以用在const成员函数里面. 对于const与否,一般会调用不同版本的函数: 而对于二元操作符,如果用 ...

  7. python的开发工具UliPad安装篇

    之前文章里写过一个搭建windows下搭建Selenium+Eclipse+Python环境,如今认为这个Eclipse太大了,太笨重了,重新启动又慢,像Python脚本轻级语言,不是必需用那么大的工 ...

  8. idea里新建maven项目时,在new module页面,一直显示loading archetype list...

    不知道什么时候开始,在idea里新建maven项目时,在new module页面,一直显示loading archetype list....,导致一直没办法新建.后来我以为是防火墙问题,各种设置还是 ...

  9. BZOJ 1503 treap

    思路: treap (算是基本操作吧-..) 加减的操作数很少 就暴力好啦 每回判断一下最小的数是不是比M小 如果是 就删,继续判断 搞定. //By SiriusRen #include <c ...

  10. ireport 追加新报表

    ireport  追加新报表 /* To change this template, choose Tools | Templates * and open the template in the e ...