LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-25-2007, 12:58 AM   #1
expeliarmus
LQ Newbie
 
Registered: Jul 2007
Posts: 17

Rep: Reputation: 0
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
 
Old 07-25-2007, 02:45 PM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Rep: Reputation: 66
Posting a sample of code might be helpful in getting your problem solved.
 
Old 07-25-2007, 08:36 PM   #3
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,378

Rep: Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108Reputation: 1108
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.

Last edited by sundialsvcs; 07-25-2007 at 08:38 PM.
 
Old 07-25-2007, 09:44 PM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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".



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
 
Old 07-26-2007, 08:41 AM   #5
expeliarmus
LQ Newbie
 
Registered: Jul 2007
Posts: 17

Original Poster
Rep: Reputation: 0
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);
}
 
Old 07-26-2007, 08:45 AM   #6
expeliarmus
LQ Newbie
 
Registered: Jul 2007
Posts: 17

Original Poster
Rep: Reputation: 0
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!!
 
  


Reply

Tags
generation, load


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Code Craft: The Practice of Writing Excellent Code LXer Syndicated Linux News 0 01-09-2007 04:03 AM
C++ Generating code balgaltz Programming 3 12-28-2006 10:21 PM
LXer: Democratization, Writing and Writing Code LXer Syndicated Linux News 0 11-22-2006 05:03 AM
generating mips assembly from c code with gcc? lilili Programming 4 05-11-2006 08:37 PM
code for generating random samples! aru_04 Programming 2 07-21-2005 08:35 AM


All times are GMT -5. The time now is 11:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration