-
Cpu Load
want to control the cpu load to test the power consume at a specified load.
Exemple:
-Fix the load at 65% and check the power consume of the computer using a Voltage and Current meter
I was thinking in something like this:
sleep(x);
/*X time (X float and inside [0;1]) */
while(1);
/* during 1-X time */
This doesn't work because the time unit is to large (sleep only accept seconds), i want something like miliseconds ou even microseconds.
Any idea?
Thanks
-
When using Dev-C++ with gcc on win2k, sleep() wil sleep for a number of milliseconds? But I guess that's a windows thing or something... Anyway, this is what I think you could use:
usleep()
-
usleep() works fine but i have another problem:
Code:
int x=6; /* It will load the cpu with an average of 60% */
while(1){
usleep(x);
fullload(10-x); /* This doen't exist, i have to create it */
}
I need a function that do the inverse of sleep, something that full load's the cpu for a defined time.
Any idea about a funtion that does this?
I was thinking in something like this:
Code:
void fullload(int Delay){
int timeStamp;
timeStamp=currTimeInUSecond(); /* dont know if this exist, need something like this */
/* Pooling until the Delay have pass */
while(currTimeInUSecond()<timeStamp+Delay);
return;
}
Where can i get a similar funcion currTimeInUSecond(); ?
-
-
Code:
#include <stdio.h>
#include <sys/time.h>
#define LOAD 6
void fullload(int Delay){
int timeStamp;
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
timeStamp=tv.tv_usec;
while(1){
gettimeofday(&tv,&tz);
if(timeStamp+Delay>tv.tv_usec)
break;
}
return;
}
int main(){
while(1){
usleep(LOAD);
fullload(10-LOAD);
}
}
Doesn't work... dont know why...
The load's goes to 150% consulting /proc/loadavg :(
Any idea?
-
Er...you're sleeping for 6 microseconds? There's 1 million microseconds in a second. I don't think the kernel's timer is even accurate enough to account for 6 microseconds. You'd be better off using something much larger like sleep for 1000 microseconds, fullload for 400 microseconds.
EDIT: Also, if you don't care about the timezone parameter to gettimeofday() then just pass NULL.
-
Isn't it Milliseconds and not Microsseconds usleep is sleeping for?
Anway I'm not sure this aproach will ever work. Its a multitaking OS so when you sleep other processes are using the processor.
-
Code:
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
/* Valor de 0 a 100 corresponde ao load de 0 a 100 */
#define LOAD 7000
void fullload(int Delay){
long timeStampSeg;
long timeStampUSeg;
struct timeval tv;
gettimeofday(&tv,NULL);
timeStampUSeg=tv.tv_usec;
timeStampSeg=tv.tv_sec;
/* Espera activa ate o tempo ter passado */
while(timeStampUSeg<(tv.tv_usec+tv.tv_sec*1000000+Delay)){
gettimeofday(&tv,NULL);
}
return;
}
int main(){
while(1){
usleep(10000-LOAD);
fullload(LOAD);
}
}
Using this like u sugested it doen't load anything, "top" program indicate 0% cpu consume.
If i comment usleep(); the load get's to 100% :(
-
Code:
]Isn't it Milliseconds and not Microsseconds usleep is sleeping for?
Anway I'm not sure this aproach will ever work. Its a multitaking OS so when you sleep other processes are using the processor.
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
Its micro for sure.
Thanks
-
I founded a soulution:
Code:
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#define LOAD 20
int main(){
int pid;
/* Create a Son and a Father process */
pid=fork();
/* If its the son consume 100% of resources */
if(pid==0){
while(1);
}
/* Father */
else{
while(1){
/* 100% Load */
usleep(LOAD*10000);
/* Stops child processes */
kill(pid,SIGSTOP);
/* 0% Load */
usleep(999999-LOAD*10000);
/* Resume child process */
kill(pid,SIGCONT);
}
}
return 0;
}
Whis load's exact the cpu for the amount of % that i put int the top #define
Thanks a lot to all :D