You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

How To Limiting CPU Usage of a High-Priority Process

On a multi-core CPU machine, I have a process A that is loaded with root privileges through `sudo launchctl load /Library/LaunchDaemons/com.xxx.A.plist`. This process forks another process B, which has one thread performing file traversal and four threads performing checks. When the CPU usage exceeds a certain value, the checking threads sleep for a few seconds. However, during execution, the CPU usage of process B is very high, ranging from 100% to 200%. Reducing the number of checking threads from four to one improves the CPU usage somewhat, but it still remains high, exceeding 100%.


I have tried using `renice 20`, `APPPolicy`, and `CPULimit`, but none of these measures can limit the CPU usage to a lower value, such as 50% or 30%. Is this because process B has high privileges and is protected by the system, making CPU usage limiting measures ineffective? How can I restrict the CPU usage of process B to 30% or 50%?

MacBook Air 13″, macOS 14.0

Posted on Oct 23, 2024 10:55 PM

Reply
4 replies

Oct 24, 2024 5:25 AM in response to AnBusyAntIsRunning

No.


It is because process A is CPU intensive, and no other process wants the CPU(s), so the macOS scheduler let the process run.


If all the CPUs were busy with other processes, then process A, and its threads, would wait in line. It would still get some CPU, but because of its nice value, it would always be put at the back of the line, and higher nice’ed processes would be able to jump the line.


The nice value you set is the base priority, but the scheduler will dynamically adjust the wait queue priority as a process sits on the queue, so that eventually it will get a chance to run again, even if there are other high demand processes with lower nice base values. The reverse happens as well, a process that is CPU intensive will have their queue priority dynamically lowered if it is always hogging the CPU. And a process that was waiting for I/O to finish will get a temporary boost in queue priority.


But if no other processes are in line, then process A is allowed to run immediately.

Oct 24, 2024 5:35 AM in response to BobHarris

If yo really want process A to use less than 100% CPU, you could get App Tamer, and it will monitor the CPU usage of selected processes, and if the exceed a specific CPU threshold, App Tamer will temporarily suspend the process, then let it run again. It will do this in a regular basis as long as the selected processes are exceeding the specified CPU threshold.


https://stclairsoft.com/AppTamer/index.html

Oct 24, 2024 6:47 PM in response to BobHarris

@BobHarris


Thank you for your reply. change the priority with nice does not effectively reduce CPU usage.

Instead, we can calculate the CPU usage of the process during the statistical period to make it sleep, or refer to CPULimit by continuously sending suspend and resume signals to the process,

using `kill -SIGSTOP $PID` or `kill -SIGCONT $PID`. Currently, we are using the latter method.

Directly using CPULimit has no effect; however, by modifying the code to adjust the detection cycle, we can significantly control CPU usage.

Oct 24, 2024 7:54 PM in response to AnBusyAntIsRunning

If you are writing the code for process A you could put self monitoring system calls to get current CPU (getrusage()) vs clock time (time()), and institute usleep(n) calls to self throttle.


If you are not writing process A, then the kill() function with signal SIGSTOP and SIGCONT is another option.


I strongly suspect App Tamer uses the kill() function and SIGSTOP & SIGCONT.



How To Limiting CPU Usage of a High-Priority Process

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.