Question

I am trying create cpu load generator tool. In which my input will be a number which is in between 0-100. I m using time function for calculating the load and i check the load. If load exceeds a desired value i try to sleep the process. The issue i m facing i that i get a desired load for but only for time period of 500ms or more. Not less than that. I m checking the load on a tool called as 'top'. When i try to decrease the time period in top. I observe the load fluctuations. Is there any ways to make a process sleep for 1ms or less.

Was it helpful?

Solution

Since you tagged this question linux-kernel, I will answer with specific details about the Linux scheduler...

Short answer: no you can't sleep for less that 1ms, because that's the resolution of the scheduler. In other words, the kernel is waking up every millisecond to decide which process to schedule, so sleeping for less than that is not possible.

Long answer: designing a cpu load generator for Linux was a subject complicated enough to deserve multiple publications. You can try the terms cpu burner and cpu load generator on Google Scholar to find more info. One of the most efficient way now is not to do it: there is cpu bandwith limitation in the cgroups feature of any recent kernel, meaning you don't have to execute code to limit the amount of cpu time available to other processes.

And no, nanosleep does not provide any guaranty that your process will be stopped, only that the clock advanced at least the amount of time you requested.

OTHER TIPS

There's nanosleep(2) but there's no guarantee the process will sleep the exact requested time, due to scheduling. You could also use that in conjunction with renice(8) to set the process to a higher priority.

Use the nanosleep function. It requests the suspension of the calling thread for at least the given nanosecond-resolution duration.

The timer in OS is always not reliable and accurate as you want. Normally the OS timer is quite suitable for most usage. But if you want to obtain some high resolution tick, you have to do these: 1, check the system hardware, that is there any timer generator can provide a hardware timer interrupt; 2, check the OS system, that there is no other process/thread or device will mask the hardware timer interrupt; also if possible register the timer device interrupt and tune the timer value into your desired time; 3, at the same time, the cputime is also used as a reference when you get the designed interrupt to check the time floating and accuracy. It is prefer to use some technical to do it. Nanosleep somehow did not work as your wish.

I don't think it would be possible to generate a fixed CPU load on a time window as short as 500ms. Consider that CPU is a dynamic system which is hard to model from a mathematical perspective. The thing you can do is to generate a CPU load that, on average, matches the target you want to reach.

For example this simple script https://github.com/GaetanoCarlucci/CPULoadGenerator, by means of a PID regulator, controls the CPU core "sleep time" in order to achieve, on average, the desired CPU load.

Here you can see a dynamics example in which 50% load is generated on CPU core 0:

enter image description here

You can notice that the transient time is larger than 500ms.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top