CPU Throttling

Linux specific forum
danielheth
New User
New User
Posts: 9
Joined: Mon Dec 01, 2014 5:12 pm
Location: Arkansas
Contact:

CPU Throttling

Post by danielheth »

I need to develop throttling for my application under non-windows OS's. We have CPU throttling implemented on windows, but now we need to add this non-windows feature.
Anyone have ideas on how to accomplish CPU throttling on non-windows operating systems?
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: CPU Throttling

Post by idle »

good question but it's not quite straight forward to find the relevent info.
if your process is scheduled with the default scheduling algorithm you can lower your own processes priority with setpriority_()
but only the superuser can raise it higher than the default. the range is -20 to 19 negative values higher priority, positive lower.

Code: Select all

#SCHED_OTHER	=0
#SCHED_FIFO	=1
#SCHED_RR	=2
#SCHED_BATCH =	3
;#SCHED_IDLE = 4   didn't find this constants definition 

#PRIO_PROCESS = 0;,		/* WHO is a process ID.  */
#PRIO_PGRP = 1;  
#PRIO_USER = 2		

#PRIO_MIN	=-20	;/* Minimum priority a process can have.  */
#PRIO_MAX	=20	;/* Maximum priority a process can have.  */


sched = sched_getscheduler_(0)
Select sched
  Case #SCHED_OTHER, #SCHED_BATCH;, #SCHED_IDLE   
    priority = getpriority_(#PRIO_PROCESS,0)
    ;lower number makes it faster but only superuser can lower priorities  
    setpriority_(#PRIO_PROCESS,0,#PRIO_MAX) ;make it lower priority 
    Debug getpriority_(#PRIO_PROCESS,0)
  Case #SCHED_FIFO, #SCHED_RR  
     min = sched_get_priority_min_(sched) 
     max = sched_get_priority_max_(sched) 
     Debug min 
     Debug max
     
EndSelect  

Windows 11, Manjaro, Raspberry Pi OS
Image
danielheth
New User
New User
Posts: 9
Joined: Mon Dec 01, 2014 5:12 pm
Location: Arkansas
Contact:

Re: CPU Throttling

Post by danielheth »

I've been playing with running a ps and finding my pid/process to see what it is reporting as my cpu usage. Then if beyond my "limit", i run a "delay" which should pause my app long enough to drop the cpu usage. Wondering if there is something better than that.
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: CPU Throttling

Post by idle »

If you're process is cpu intensive you could use a delay(0) in the busy part so it can relinquish it's resources
it will still utilize all the cpu available in it's timeslice and it shouldn't be a problem assuming you're not blocking the event queue

if it's only supposed to be busy in bursts use a processing thread and signal a semaphores to wake up a thread to do the processing
and then let it go back to the wait once it's done and add a delay(0) in the thread.
Windows 11, Manjaro, Raspberry Pi OS
Image
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: CPU Throttling

Post by davido »

@idle,

Can you explain what Delay(0) is actually doing? Seems such an odd thing to do.
DE AA EB
danielheth
New User
New User
Posts: 9
Joined: Mon Dec 01, 2014 5:12 pm
Location: Arkansas
Contact:

Re: CPU Throttling

Post by danielheth »

yes please. When using it on the mac version of my app it appears in the activity monitor as a non-responsive app... although the cpu and memory are clearly being used and my log continues to work.
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: CPU Throttling

Post by idle »

davido wrote:@idle,

Can you explain what Delay(0) is actually doing? Seems such an odd thing to do.
In linux I guess it would be more correct to use schedyield rather than delay(0) but I'm not sure.
As far as using a delay(0) goes it will resolve to whatever the minimum time resolution is on the system (like on win32 it's 16ms)
using a delay essentially takes the burden off the process scheduler kicking the process if it's used it timeslice up
and can also enable a process main thread to empty it's event queue should it have a worker thread generating events
faster than the main thread can process them.
Essentially it allows you to use all available cpu without bogging down the system since your process or thread yields
itself long before it's timeslice is up.
Windows 11, Manjaro, Raspberry Pi OS
Image
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: CPU Throttling

Post by davido »

@idle,

Thank you very much for taking the time to explain this. :D
DE AA EB
Post Reply