怎么控制cpu
•编程使cpu占用率达到100%•
#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++;
}
}