Reputation: 684
At Present i am creating 1 cpu spike using stress
software
# stress --cpu 1 --timeout 5
stress: info: [1830] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
stress: info: [1830] successful run completed in 5s
now its utilising 1 cpu is there a way to utilise 100milli cpu or 10% cpu ?
Upvotes: 0
Views: 404
Reputation: 364503
A process is either running (100% of a core) or it's not (0%) at any given moment.
To get fractional CPU usage, you'd have to average over some time, with a process that spends some of its time in a sleep
system call (or something else that blocks).
With a 10% duty cycle, like 1 millisecond running a loop and then calling nanosleep
to sleep for 9 milliseconds, you can achieve 10% CPU usage if you look at the average load over a long enough interval. But at any given time, your task is either sleeping or running (or waiting to get scheduled onto a core if they're all busy when its sleep ends).
If you're writing a load test, you might want to have it use x86 _mm_pause()
in a loop (or portably, Rust's std::hint::spin_loop
) to save power. Otherwise just use an empty loop body.
Have the loop condition be while(now < end_time) { _mm_pause(); }
where you calculate check the current time against an end timestamp you calculated earlier. You can either check the current time after waking from sleep, or increment a counter without checking if your sleep went slightly longer than it should have, or use the clock_gettime()
values you see along the way to try to maintain the right duty cycle if one sleep went longer than you wanted. (It generally won't go shorter unless you get woken by a signal instead.)
Related: How to calculate time for an asm delay loop on x86 linux? for hand-written asm to busy-wait until a deadline using rdtsc
.
https://github.com/GaetanoCarlucci/CPULoadGenerator is a Python program that can generate fractional loads, presumably using sleeps and delay loops.
Upvotes: 1