perlAffen has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know of a program written in perl strictly to use a certain amount of cpu and memory ? I have something I have written in bash, but it doesn't behave randomly enough and appears in top as sh. I have an application that really needs resources at certain times, if it doesn't have it, it breaks. The Virt Machine gestapo have stated that they are subtracting CPU and memory resources based on average use numbers and not peak. So they have already decimated my Dev boxes and are starting to baseline my UAT (user acceptance test) boxes. I need to dial up something that looks real to utilize more resources. Any ideas ? Thanks

Replies are listed 'Best First'.
Re: Configurable CPU/Memory Use Script
by SuicideJunkie (Vicar) on Jun 30, 2011 at 20:44 UTC

    This sounds like a political problem, rather than a technical problem.

    I expect the actual IT folks know better than that, but the policy is probably coming down from on high. You should try getting your boss involved so you can stop the problem at its true source, or at least get an exception for your group.

    That said, are you shutting down your virtual machines when they're not in use? If you power them off after you're done and thus don't leave them idling at low resource use, that might bump the average up closer to reality. Plus it frees up more resources and lessens the need for aggressive resource policies in the first place.

      That is definitely a political problem, and one that probably should be elevated to a very high level within the company.   IT departments sometimes become so protective of “their” resources that they forget (or simply, don’t know ...) what is the business impact of those resources.   If they are “throttling down” your resources such that they are impacting the mission of your particular department, well, what is the business impact of your particular department?   (And, if you can’t say for sure, maybe you’re working for the wrong department!   There are executioners of many kinds within any large company, including those with the power to eliminate a department altogether... and they often do not really know the impact of what they are doing.)

      This is not a battle that you can, or should fight, but your boss(es) can.   Rack-mounted computers are very cheap and getting cheaper by the day ... yes, even though they cost thousands of dollars apiece.   They pay for themselves, many times over, by what they do, such that misguided attempts to “economize” on their direct cost can have precisely the opposite effect on the company as a whole.   That’s not to say that you should be intentionally wasteful of those resources, but anyone who is merely looking at a meter in his hands and closing a valve, without being accountable for the total impact of closing that valve, probably should be dismissed.

Re: Configurable CPU/Memory Use Script
by Khen1950fx (Canon) on Jun 30, 2011 at 21:11 UTC
Re: Configurable CPU/Memory Use Script
by zek152 (Pilgrim) on Jun 30, 2011 at 20:37 UTC

    Just do a mix of spinwait and sleep(). Note that you will have to finetune what the ratio should be. Also in the while(1) you should put the logic to decide when the program should wake up.

    Seems like a pretty big waste of CPU though. Might there be a way of running the process with a higher priority?

    $sec = 0; while(1) { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( +time); if($sec%2==0) { sleep(1); } }