Thanks Fooler, I'm gonna need some time to digest the information and probably more time to perl-port it. :) I'll get back to this list when it's done. I think it's just right to buy you Tiger Beer next time.
On Wed, Apr 13, 2011 at 11:42 AM, fooler mail <[email protected]> wrote: > because you wanted by percentage, the formula i used to clock > WasteCPU() function call in one second time is... > > cpu_time * percentage + sleep_time * (1 - percentage) > > where: > > cpu_time is where you need to keep the cpu busy.. in this case i used > the for loop (eg. "for(i = waste; i; i--);").. the "waste" variable > value is computed based on 1 second time... i used clock() function > call and divided by CLOCKS_PER_SEC to get processor time per second... > but you can use gettimeofday() function call also... > > sleep_time is where you need the cpu to sleep.. in this case i used > select() function call (eg. "select(1, NULL, NULL, NULL, &sleep);").. > but you can use usleep() and nanosleep() function calls for micro and > nano seconds precision respectively... select() is using microsecond > precision... > > based on the formula above.. let say if you need 100% cpu to > utilize... sleep_time is zero and cpu_time is 100%... if you need 80% > cpu to utilize... sleep_time is 20% and cpu_time is 80% in 1 second > time... > > this formula is the same formula used for morphing an object... > remember black and white mtv of michael jackson that it morphs from > girl to boy? > > face = girl * (1 - percent) + boy * percent > > starting from 0 percent.. the face looks like a girl.. as the loop > reaches to 100 percent.. the face looks like now a boy... when i saw > that mtv.. i got an idea how it works and taught this in computer > science department in our computer graphics subject right away aside > from 2d and 3d modelling when i was still in the philippines... > > fooler. > > On Wed, Apr 13, 2011 at 8:30 AM, John Homer H Alvero > <[email protected]> wrote: >> This works! Thanks. Mind explaining the general idea / concept? Maybe >> I can port it to perl. >> >> >> >> On Wed, Apr 13, 2011 at 6:21 AM, fooler mail <[email protected]> wrote: >>> 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 >>> >> _________________________________________________ >> Philippine Linux Users' Group (PLUG) Mailing List >> http://lists.linux.org.ph/mailman/listinfo/plug >> Searchable Archives: http://archives.free.net.ph >> > _________________________________________________ > Philippine Linux Users' Group (PLUG) Mailing List > http://lists.linux.org.ph/mailman/listinfo/plug > Searchable Archives: http://archives.free.net.ph > _________________________________________________ Philippine Linux Users' Group (PLUG) Mailing List http://lists.linux.org.ph/mailman/listinfo/plug Searchable Archives: http://archives.free.net.ph

