| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1010 人关注过本帖
标题:怎么控制cpu
只看楼主 加入收藏
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
结帖率:40%
收藏
 问题点数:0 回复次数:14 
怎么控制cpu
•编程使cpu占用率达到100%•
2013-06-09 13:00
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
#include <sys/time.h>  
#include <unistd.h>  
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<math.h>
#include<sys/resource.h>

#define DWORD unsigned long  
#define UINT64 unsigned long long

struct occupy     
{
    char name[20];     
    unsigned int user;  
    unsigned int nice;
    unsigned int system;
    unsigned int idle;  
};

const double SPLIT = 0.01;  
const int COUNT = 200;  
const double PI = 3.14159265;  
const int INTERVAL = 100;
float g_cpu_used;
int cpu_num;
               
float cal_occupy(struct occupy *, struct occupy *);
void get_occupy(struct occupy *);
void *usecpu(void* arg);
float * cpu_used;

void calcu_cpu_usage(float * cpuused)
{
    struct occupy ocpu[10];
    struct occupy ncpu[10];

    //printf("In calcu_cpu_usage: The share memory address is %ld \n", cpuused);

     sleep(1);        
    get_occupy(ocpu);
    sleep(1);
    get_occupy(ncpu);

    int i;
    for (i=0; i<cpu_num; i++)
    {
        g_cpu_used = cal_occupy(&ocpu[i], &ncpu[i]);
        float oldcpuused = cpuused[i];

        if(g_cpu_used < oldcpuused)
            g_cpu_used = oldcpuused;

        if((g_cpu_used - oldcpuused) < 95)
            cpuused[i]=95 - (g_cpu_used - oldcpuused);
        else
            cpuused[i]=0;
        //printf("The core %d, g_cpu_used = %f, oldcpuused = %f, need consume rate %f \n", i, g_cpu_used, oldcpuused, cpu_used[i]);
    }
}

void *schedule_calcu(void* arg)
{
    //printf("Upgrade thread runing...\n");
    float* p = (float*)arg;

    //printf("In schedule_calcu: The share memory address is %ld \n", p);
    while(1)
    {
        calcu_cpu_usage(p);
        sleep(1);
    }
}

int main()                 
{
    cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
    cpu_used = (float *) malloc( cpu_num * sizeof(float) );
    //printf("In main: The share memory address is %ld \n", cpu_used);

    int i;
    for (i=0; i<cpu_num; i++)
        {
        cpu_used[i] = 0;
        }

    calcu_cpu_usage(cpu_used);
    /*   
    pthread_t idn;
    pthread_t id[cpu_num];   
    int retn=pthread_create(&idn,NULL,schedule_calcu,cpu_used);
    pthread_join(idn,NULL);      
    for(i=0;i<cpu_num;i++){
      int ret=pthread_create(&id[i],NULL,usecpu,&cpu_used[i]);
    }
    for(i=0;i<cpu_num;i++){
        pthread_join(id[i],NULL);
    }
    */
    if(cpu_num==1){
        pthread_t id,idn;
        int ret=pthread_create(&id,NULL,usecpu,&cpu_used[0]);
        int retn=pthread_create(&idn,NULL,schedule_calcu,cpu_used);
        pthread_join(id,NULL);
        pthread_join(idn,NULL);
    }
    else if(cpu_num==2){
        pthread_t id,id1,idn;
        int ret=pthread_create(&id,NULL,usecpu,&cpu_used[0]);
        int ret1=pthread_create(&id1,NULL,usecpu,&cpu_used[1]);
        int retn=pthread_create(&idn,NULL,schedule_calcu,cpu_used);
        pthread_join(id,NULL);
        pthread_join(id1,NULL);
        pthread_join(idn,NULL);
    }
    else if(cpu_num==3){
        pthread_t id,id1,id2,idn;
        int ret=pthread_create(&id,NULL,usecpu,&cpu_used[0]);
        int ret1=pthread_create(&id1,NULL,usecpu,&cpu_used[1]);
        int ret2=pthread_create(&id2,NULL,usecpu,&cpu_used[2]);
        int retn=pthread_create(&idn,NULL,schedule_calcu,cpu_used);
        pthread_join(id,NULL);
        pthread_join(id1,NULL);
        pthread_join(id2,NULL);
        pthread_join(idn,NULL);
    }
    else if(cpu_num==4){
        pthread_t id,id1,id2,id3,idn;
        int ret=pthread_create(&id,NULL,usecpu,&cpu_used[0]);
        int ret1=pthread_create(&id1,NULL,usecpu,&cpu_used[1]);
        int ret2=pthread_create(&id2,NULL,usecpu,&cpu_used[2]);
        int ret3=pthread_create(&id3,NULL,usecpu,&cpu_used[3]);
        int retn=pthread_create(&idn,NULL,schedule_calcu,cpu_used);
        pthread_join(id,NULL);
        pthread_join(id1,NULL);
        pthread_join(id2,NULL);
        pthread_join(id3,NULL);
        pthread_join(idn,NULL);
    }
    else if(cpu_num==5){
        pthread_t id,id1,id2,id3,id4,idn;
        int ret=pthread_create(&id,NULL,usecpu,&cpu_used[0]);
        int ret1=pthread_create(&id1,NULL,usecpu,&cpu_used[1]);
        int ret2=pthread_create(&id2,NULL,usecpu,&cpu_used[2]);
        int ret3=pthread_create(&id3,NULL,usecpu,&cpu_used[3]);
        int ret4=pthread_create(&id4,NULL,usecpu,&cpu_used[4]);
        int retn=pthread_create(&idn,NULL,schedule_calcu,cpu_used);
        pthread_join(id,NULL);
        pthread_join(id1,NULL);
        pthread_join(id2,NULL);
        pthread_join(id3,NULL);
        pthread_join(id4,NULL);
        pthread_join(idn,NULL);
    }
    else {
        pthread_t id,id1,id2,id3,id4,idn;
        int ret=pthread_create(&id,NULL,usecpu,&cpu_used[0]);
        int ret1=pthread_create(&id1,NULL,usecpu,&cpu_used[1]);
        int ret2=pthread_create(&id2,NULL,usecpu,&cpu_used[2]);
        int ret3=pthread_create(&id3,NULL,usecpu,&cpu_used[3]);
        int ret4=pthread_create(&id4,NULL,usecpu,&cpu_used[4]);
        int retn=pthread_create(&idn,NULL,schedule_calcu,cpu_used);
        pthread_join(id,NULL);
        pthread_join(id1,NULL);
        pthread_join(id2,NULL);
        pthread_join(id3,NULL);
        pthread_join(id4,NULL);
        pthread_join(idn,NULL);
    }
      
}


float cal_occupy (struct occupy *o, struct occupy *n)
 {
    double od, nd;
    double id, sd;
    double scale;   
    od = (double) (o->user + o->nice + o->system +o->idle);
    nd = (double) (n->user + n->nice + n->system +n->idle);
    scale = 100.0 / (float)(nd-od);      
    id = (double) (n->user - o->user);  
    sd = (double) (n->system - o->system);
    return ((sd+id)*100.0)/(nd-od);
}
void get_occupy (struct occupy *o)
{
    FILE *fd;        
    int n;           
    char buff[1024];

    fd = fopen ("/proc/stat", "r");
    fgets (buff, sizeof(buff), fd);
    for(n=0;n<cpu_num;n++)         
    {
        fgets (buff, sizeof(buff),fd);
        sscanf (buff, "%s %u %u %u %u", &o[n].name, &o[n].user, &o[n].nice,&o[n].system, &o[n].idle);
        //fprintf (stderr, "%s %u %u %u %u\n", o[n].name, o[n].user, o[n].nice,o[n].system, o[n].idle);
    }
    fclose(fd);
}

void *usecpu(void* arg){
    float *p=(float*)arg;
    printf("%f \n",*p);
    struct timeval tms;  
    DWORD busySpan[COUNT];  
    DWORD idleSpan[COUNT];  
    int i;
    clock_t startTime = 0;  
    int j = 0;
    //printf("cpu busy thread started\n");

    while(1)  
    {
        j = j % COUNT;
                   busySpan[j] = (DWORD)(*p);
                   idleSpan[j] = INTERVAL - busySpan[j];

        timerclear(&tms);  
        gettimeofday(&tms,NULL);  
        UINT64 startTime = tms.tv_usec;  
        while(1)
        {  
            timerclear(&tms);  
            gettimeofday(&tms,NULL);  
            UINT64 nowTime = tms.tv_usec;  
            if((nowTime - startTime)/1000 > busySpan[j])
                break;  
        }
        usleep(idleSpan[j]*1000);

        //printf(">>>>>>>>>>>>>>>>>>>>>>>>>busySpan[%d] = %d>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n",j,busySpan[j]);
        j++;
    }  
}
2013-06-09 13:01
Han_FlyB
Rank: 6Rank: 6
等 级:侠之大者
帖 子:143
专家分:424
注 册:2013-3-25
收藏
得分:0 
话说你已经相当牛X了
2013-06-09 13:54
YJ_Hao
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:215
专家分:609
注 册:2013-3-22
收藏
得分:0 
牛是牛,不过瞄了一眼,最后是个死循环!
    while(1)  
    {
        j = j % COUNT;
                   busySpan[j] = (DWORD)(*p);
                   idleSpan[j] = INTERVAL - busySpan[j];

        timerclear(&tms);  
        gettimeofday(&tms,NULL);  
        UINT64 startTime = tms.tv_usec;  
        while(1)
        {  
            timerclear(&tms);  
            gettimeofday(&tms,NULL);  
            UINT64 nowTime = tms.tv_usec;  
            if((nowTime - startTime)/1000 > busySpan[j])
                break;  
        }
        usleep(idleSpan[j]*1000);

        //printf(">>>>>>>>>>>>>>>>>>>>>>>>>busySpan[%d] = %d>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n",j,busySpan[j]);
        j++;
    }
外层出不来的!

[ 本帖最后由 YJ_Hao 于 2013-6-9 14:06 编辑 ]
2013-06-09 14:04
蚕头燕尾
Rank: 10Rank: 10Rank: 10
来 自:Gryffindo
等 级:贵宾
威 望:12
帖 子:734
专家分:1546
注 册:2013-3-24
收藏
得分:0 
膜拜了

学习编程,为的是表达自己的思想,而不是被别人的思想所禁锢。要先明白自己想干嘛,而不要先问别人让你干嘛。               

                                                                                                                    Black Cat      Hello Tomorrow~
2013-06-10 23:33
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
#include <sys/time.h>  
#include <unistd.h>  
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<math.h>
#include<sys/resource.h>

#define DWORD unsigned long  
#define UINT64 unsigned long long

struct occupy     
{
    char name[20];     
    unsigned int user;  
    unsigned int nice;
    unsigned int system;
    unsigned int idle;  
};

const double SPLIT = 0.01;  
const int COUNT = 200;  
const double PI = 3.14159265;  
const int INTERVAL = 100;
float g_cpu_used;
int cpu_num;
               
float cal_occupy(struct occupy *, struct occupy *);
void get_occupy(struct occupy *);
void *usecpu(void* arg);
float * cpu_used;

void calcu_cpu_usage(float * cpuused)
{
    struct occupy ocpu[10];
    struct occupy ncpu[10];

     sleep(1);        
    get_occupy(ocpu);
    sleep(1);
    get_occupy(ncpu);

    int i;
    for (i=0; i<cpu_num; i++)
    {
        g_cpu_used = cal_occupy(&ocpu[i], &ncpu[i]);
        float oldcpuused = cpuused[i];

        if(g_cpu_used < oldcpuused)
        {
            g_cpu_used = oldcpuused;
        }

        if((g_cpu_used - oldcpuused) < 95)
        {
            cpuused[i]=95 - (g_cpu_used - oldcpuused);
        }
        else
        {
            cpuused[i]=0;
        }

    }
}

void *schedule_calcu(void* arg)
{
    float* p = (float*)arg;

    while(1)
    {
        calcu_cpu_usage(p);
        sleep(1);
    }
}

int main()                 
{
    cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
    cpu_used = (float *) malloc( cpu_num * sizeof(float) );

    int i;
    for (i=0; i<cpu_num; i++)
        {
        cpu_used[i] = 0;
        }

    calcu_cpu_usage(cpu_used);
 
    pthread_t idn;
    pthread_t id[cpu_num];   
        
    for(i=0;i<cpu_num;i++)
    {
        int ret=pthread_create(&id[i],NULL,usecpu,&cpu_used[i]);
    }

    int retn=pthread_create(&idn,NULL,schedule_calcu,cpu_used);

    for(i=0;i<cpu_num;i++)
    {
            pthread_join(id[i],NULL);
    }
    pthread_join(idn,NULL);
}


float cal_occupy (struct occupy *o, struct occupy *n)
 {
    double od, nd;
    double id, sd;
    double scale;   
    od = (double) (o->user + o->nice + o->system +o->idle);
    nd = (double) (n->user + n->nice + n->system +n->idle);
    scale = 100.0 / (float)(nd-od);      
    id = (double) (n->user - o->user);  
    sd = (double) (n->system - o->system);
    return ((sd+id)*100.0)/(nd-od);
}
void get_occupy (struct occupy *o)
{
    FILE *fd;        
    int n;           
    char buff[1024];

    fd = fopen ("/proc/stat", "r");
    fgets (buff, sizeof(buff), fd);
    for(n=0;n<cpu_num;n++)         
    {
        fgets (buff, sizeof(buff),fd);
        sscanf (buff, "%s %u %u %u %u", &o[n].name, &o[n].user, &o[n].nice,&o[n].system, &o[n].idle);
    }
    fclose(fd);
}

void *usecpu(void* arg){
    float *p=(float*)arg;
    struct timeval tms;  
    DWORD busySpan[COUNT];  
    DWORD idleSpan[COUNT];  
    int i;
    clock_t startTime = 0;  
    int j = 0;

    while(1)  
    {
        j = j % COUNT;
                   busySpan[j] = (DWORD)(*p);
                   idleSpan[j] = INTERVAL - busySpan[j];

        timerclear(&tms);  
        gettimeofday(&tms,NULL);  
        UINT64 startTime = tms.tv_usec;  
        while(1)
        {  
            timerclear(&tms);  
            gettimeofday(&tms,NULL);  
            UINT64 nowTime = tms.tv_usec;  
            if((nowTime - startTime)/1000 > busySpan[j])
                break;  
        }
        usleep(idleSpan[j]*1000);

        j++;
    }  
}
2013-07-04 14:48
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
package com.samsung.disaster.cpubusy;

/**
 * JAVA控制CPU的使用率,完美曲线
 *
 */
public class CpuBusy {
    public static void main(String[] args) throws Exception {
        // System.out.println(args[0]);
        // System.out.println(args[1]);
        if (args.length == 0) {
            Runtime runtime = Runtime.getRuntime();
            int nrOfProcessors = runtime.availableProcessors();
            for (int i = 0; i < nrOfProcessors; i++) {
                MyThreador myt = new MyThreador(90, 0);
                Thread t = new Thread(myt);
                t.start();
            }
        } else if (args.length == 1) {
            double rate = Double.parseDouble(args[0]);
            Runtime runtime = Runtime.getRuntime();
            int nrOfProcessors = runtime.availableProcessors();
            for (int i = 0; i < nrOfProcessors; i++) {
                MyThreador myt = new MyThreador(rate, 0);
                Thread t = new Thread(myt);
                t.start();
            }
            
        } else{
            double rate = Double.parseDouble(args[0]);
            double duration = Double.parseDouble(args[1]);
            Runtime runtime = Runtime.getRuntime();
            int nrOfProcessors = runtime.availableProcessors();
            for (int i = 0; i < nrOfProcessors; i++) {
                MyThreador myt = new MyThreador(rate, duration);
                // MyThreador myt = new MyThreador(40,10);
                Thread t = new Thread(myt);
                t.start();
            }
        }

    }
}

class MyThreador extends Thread {
    private double rate;
    private double duration;
    public MyThreador(){
               }
    public MyThreador(double rate,double duration) {
        this.rate = rate;
        this.duration =duration ;   
    }
   
    public void run() {
        // 角度的分割
        final double SPLIT = 0.01;
        // 2PI分割的次数,也就是2/0.01个,正好是一周
        final int COUNT = (int) (2 / SPLIT);

        final double PI = Math.PI;
        // 时间间隔
        final int INTERVAL = 100;
        long[] busySpan = new long[COUNT];
        long[] idleSpan = new long[COUNT];

        double radian = 0.0;
        for (int i = 0; i < COUNT; i++) {
            busySpan[i] = (long) (rate + (Math.sin(PI * radian) * duration));
            idleSpan[i] = INTERVAL - busySpan[i];
            radian += SPLIT;
        }

        long startTime = 0;
        int j = 0;
        while (true) {
            j = j % COUNT;
            startTime = System.currentTimeMillis();
            while (System.currentTimeMillis() - startTime < busySpan[j])
                ;
            try {
                Thread.sleep(idleSpan[j]);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            j++;
        }
    }
}
2013-07-04 15:50
会飞的蛋
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2013-6-10
收藏
得分:0 
顿时觉得自己酱油了
2013-07-04 17:40
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
回复 8楼 会飞的蛋
hehe
2013-07-05 11:53
NothingHelp
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2013-7-6
收藏
得分:0 
酱油。
2013-07-06 11:11
快速回复:怎么控制cpu
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028366 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved