Page 1 of 1
CPU Throttling
Posted: Mon Dec 01, 2014 5:16 pm
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?
Re: CPU Throttling
Posted: Mon Dec 01, 2014 9:07 pm
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
Re: CPU Throttling
Posted: Mon Dec 01, 2014 11:12 pm
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.
Re: CPU Throttling
Posted: Mon Dec 01, 2014 11:41 pm
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.
Re: CPU Throttling
Posted: Tue Dec 02, 2014 5:39 pm
by davido
@idle,
Can you explain what Delay(0) is actually doing? Seems such an odd thing to do.
Re: CPU Throttling
Posted: Tue Dec 02, 2014 7:17 pm
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.
Re: CPU Throttling
Posted: Tue Dec 02, 2014 7:33 pm
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.
Re: CPU Throttling
Posted: Tue Dec 02, 2014 11:35 pm
by davido
@
idle,
Thank you very much for taking the time to explain this.
