You can't. You either have 100% of CPU time, or 0%, assuming one processor. That is, the CPU is either running your program, or it isn't.
However, over a given time period, you can have had N%, 0 <= N <= 100, of the available CPU time. To archieve that, just before finishing the program, measure how much CPU time the program has had, how much time has passed, and calculate how long you have to sleep before doing the exit. But remember, you cannot sleep negative time. Instead of sleeping, you may have to doing a lot of work to increase the average CPU time (if the machine is busy, this may require the process get root permission and kill other processes, and/or decrease its niceness, so it can get more or longer time slots).
Calculations can get more hairy if you have more than 1 processor, and more threads. And ambiguous. Does 100% CPU time mean using the full power of all CPUs, or just the equivalent of one? | [reply] |
You need a method to sleep in the subsecond range. 'perldoc sleep' mentions two methods for doing that
Then you have two possibilities:
1) Do a feedback loop. You need fine-grained measurement of cpu usage, on linux you could check out whether the /proc directory offers suitable information (ps gets its usage information from there AFAIK). Sleep a millisecond if you are above 80%, or do a loop for some time (doesn't really matter how long) if you are below. Loop over that
2) sleep and work in a fixed ratio.You need a method to loop for a fixed subsecond time (perldoc alarm tells you of a few methods, basically the same as for sleep). Then just work and sleep in a ratio of 80 to 20, if you want 80% utilization
| [reply] |
this seems to work although with some error (~3%):
#!/usr/bin/perl
use Time::HiRes qw(time sleep);
my $load = shift @ARGV;
my $sleep = 0.01;
$| = 1;
my @t = times;
my @last = [time, $t[0] + $t[1]];
while (1) {
@t = times;
unshift @last, [time, $t[0] + $t[1]];
pop @last if @last > 100;
my $current = 100 * ($last[0][1]-$last[-1][1])/ (0.00001 + $last[0
+][0]-$last[-1][0]);
if ($current > $load) {
sleep $sleep;
}
else {
while (time < $last[0][0] + $sleep) {
for (1..10000) { 1; }
}
}
}
| [reply] [d/l] |
This is impossible to do for anything other than a completely quiescent individual system.
Let's say you write a program that alternates between a tight loop and a short sleep and you adjust the timings so that when run it consumes exactly 70% cpu. What happens when you run a second copy concurrently? (Or play an MP3 or stream a movie; or check your email)
Another approach might be to query the current cpu usage and then increase or decrease your loop constant to compensate. But the figure you queried was for the previous time period. So now both copies of your program discover that the usage was 90%, so they both step back. But now they get 50% so they ramp up. And now they hit 100% so they step back again. This problem is called hysteresis. And if you succeed in getting 2 copies to balance over some time period, start a third copy (or play an mp3 ... etc. )
Now, assuming your purpose for this is to test the responsiveness of some other program when the system is under load. Perform your test when you are consuming 100% cpu. Adjust the priorities of your tasks (cpu bound lower; IO responsive higher), until you meet the needed response time needs of the IO process. If it will respond in a timely fashion when a (correctly prioritized) cpu-bound application is running flat out, it will definitely continue to do so when the system is under less load.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |