On Tue, Apr 12, 2011 at 9:29 PM, John Homer H Alvero
<[email protected]> wrote:
> Hello Fooler,
>
> Sorry for the late reply. It's for an integration project. There's a
> trigger on CPU usage and CPU usage alone. I need to simulate simulate
> that CPU usage.
>
hi john,
ok i understand.. i made a simple C program for you (as im not used to
perl programming) to simulate CPU usage... it will waste or use the
CPU approximately with a given percentange as a parameter...
just take note that cpu percentage is different from cpu load... cpu
percentage is the time spent by cpu on a given process... so i guess
this is what you are looking for to waste CPU cycles in user space...
take note also if you have N processors... run this program N times
also so that each proccessor will be wasting X percentage of cpu
cycles... so that your average will be (X*N) / N or X percentage...
linux scheduler will take care fo assigning the program to its free
cpu..
name the program below let say to waste.c and compile with gcc
$ gcc waste.c -o waste
run the program with a given percentage let say 85 percent...
$ ./waste 85
run "top" command and press 1 to see per CPU utilization as well as
per process utilization... you can see there the dynamics of linux
scheduler... just increase SAMPLING value not greater than signed
integer value (around 2 giga plus) if you need better accuracy or
decrease it for faster sampling times but at the expense of its
accuracy... or you can convert signed interger to unsigned integer for
bigger value... hope this helps..
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/select.h>
#define SAMPLING 2000000000
void WasteCPU(int waste, int percent) {
int i;
struct timeval sleep;
for(i = waste; i; i--);
sleep.tv_sec = 0;
sleep.tv_usec = (100 - percent) * 10000;
select(1, NULL, NULL, NULL, &sleep);
}
int main(int argc, char *argv[]) {
float cps;
int waste, percent;
clock_t time1, time2;
printf("Start to sampling clock speed...\n");
percent = 100;
waste = SAMPLING;
time1 = clock();
WasteCPU(waste, percent);
time2 = clock();
cps = (time2 - time1) / (float) CLOCKS_PER_SEC;
percent = atoi(argv[1]);
waste = (int) (SAMPLING / cps / (100.0 / percent));
printf("Sampling done. Wasting CPU approximately %d
percent...\n", percent);
while (1) {
WasteCPU(waste, percent);
}
return (0);
}
fooler.
_________________________________________________
Philippine Linux Users' Group (PLUG) Mailing List
http://lists.linux.org.ph/mailman/listinfo/plug
Searchable Archives: http://archives.free.net.ph