LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   writing C code for generating load variations (http://www.linuxquestions.org/questions/programming-9/writing-c-code-for-generating-load-variations-572073/)

expeliarmus 07-25-2007 12:58 AM

writing C code for generating load variations
 
Hi,
am new to programming.I am currently trying out a C program that can scale up/down child processes in order to achieve a load average as desired by the user.The input can be something like this:
"i want to generate a CPU load average of 15, and sustain it for 5 minutes."
can someone help me out with this interesting C problem?I am using an exponential program as a sample.

yours gratefully,
expeliarmus

exvor 07-25-2007 02:45 PM

Posting a sample of code might be helpful in getting your problem solved.

sundialsvcs 07-25-2007 08:36 PM

Usually, "load simulation" problems are approached by techniques that simulate the effect of delays, not actual loads.

Y'know... "what did this poor system ever do to you?" :)

Stress-testing libraries will subject child processes to random suspend-and-resume delays designed to flush-out timing problems.

paulsm4 07-25-2007 09:44 PM

Hi -

In Linux, the CPU load can only go up to "10". But there's a special patch you can install to make it go up to "11".

:D

All kidding aside, Sundialsvcs is correct. Here's a link that explains in a bit more detail:
http://www.teamquest.com/resources/g...ay/5/index.htm
... or ...
http://agiletesting.blogspot.com/200...d-testing.html

The author, Dr. Gunther, wrote my very favorite book on "performance":
The Practical Performance Analyst
http://www.amazon.com/Practical-Perf...qid=1185417429

And finally, you might want to look through Sourceforge or Freshmeat.com for sample code for "stress tests" and "benchmarks". For example:

http://www.opensourcetesting.org/performance.php
http://drupal.org/node/79237
http://opensta.org/

'Hope that helps .. PSM

expeliarmus 07-26-2007 08:41 AM

thanks you paulsm4 and sundialsvcs.I hv included my code here,but The following problem has arisen:
Though load generation does take place,the program often generates loads larger than that desired by the user.Mind you the load's only for the one minute LA Triplet and the load only increases,I have not included code to decrease load.would br grateful if you guys suggest modifications.
#include<stdio.h> /*a program to find out p^p,ptimes,that is if the input is 2,output gives,2^2^2.if
input is 3,output is 3^3^3,and so on */
#include<stdlib.h>
#include<math.h>
#include<unistd.h>
main()
{double load[3];
if (getloadavg(load, 3) != -1) /*getloadavg is the function used to calculate and obtain the load average*/
{
printf("load average : %f , %f , %f\n", load[0],load[1],load[2]);
}
float m;
int n=2;
long double g,h,i,pid,j,p,x=1,a=1;
printf("enter the number whose exponential's is to be found\n");
scanf("%Lf",&p);
printf("enter the load average to be generated for the 1st minute\n");
scanf("%f",&m); /*reading the input*/
printf("finding (p^p^p^p....ptimes)\n");
h=p;
while(h>0) /*calculating the final exponent value*/
{x=x*p;
h--;
}
for(i=x;i>0;i--) /*calculating the actual answer*/
{a=a*p;
}
printf("the value is:%Lf\n",a);
getloadavg(load,3);

while(load[0]<m)
{
n=n*2; /*increasing the number of child process*/
goto x;
}

/*
printf("enter the number of child processes to be spwaned\n");
scanf("%Lf",&n);
*/
x:
while(load[0]<m)
{
for(j=n;j>0;j--)
{
pid=fork(); /*creating the child process*/
if(pid>0)
wait(); /*making the parent process wait*/
if(pid <0)
{printf("error:cannot fork!!");
exit(1);
}
if (pid==0) /*child process*/
{ long double f=1;
getloadavg(load,3);
if(load[0]>m)
goto y;
printf("finding (p^p^p^p....ptimes)\n");
h=p;
while(h>0)
{x=x*p;
h--;
}
for(i=x;i>0;i--)
{f=f*p;
}
printf("the value is:%Lf\n",a);
if (getloadavg(load, 3) != -1)
{
printf("load average: %f, %f,%f\n",load[0],load[1],load[2]);
}
break;
}

}
getloadavg(load,3);
if(load[0]<m)
n=n*2; /*increasing the forked processes*/
continue;
}
y:
printf("load average: %f, %f,%f\n",load[0],load[1],load[2]);
return(1);
}

expeliarmus 07-26-2007 08:45 AM

the sample code
 
Quote:

Originally Posted by exvor
Posting a sample of code might be helpful in getting your problem solved.

hello exvor,
I have posted the sample code.would be grateful if you could go through it and suggest modifications and solutions to my problem.
thanks!!


All times are GMT -5. The time now is 07:02 PM.