2

I want to lower the priority of a thread.

The default policy of my thread is SCHED_OTHER, and the range of priority under my system(Ubuntu) is [0,0] (I get the range by sched_get_priority_min(SCHED_OTHER) and sched_get_priority_max(SCHED_OTHER)) which means all threads with SCHED_OTHER will have the same priority.

Is there any approach to lower the priority with SCHED_OTHER? I've been searching for a while and I found the nice value system, but not sure if it's the correct way to do this, since the man page said nice value is for process, instead of thread, I'm confused...

Could anyone give the correct solution to do this, and maybe with a short code snippet? Thanks!

Added:

why I want to lower the priority of thread:

I have a worker thread, which is doing some intensive computation periodically (say, a few seconds every minute, thus will cause some CPU usage peak), and my whole system will experience periodically downgrade in performance. But the priority of this worker thread is low, as long as it can finish computation before next minute, it should be fine. so I want to amortize the computation of this task over this time window smoothly.

5
  • pthread_setschedparam Commented Jul 9, 2019 at 18:02
  • 5
    This sounds like an XY problem. For what problem have you decided that lowering thread priority is the solution? Commented Jul 9, 2019 at 18:03
  • Note that in Linux (and most other Unix OS's), the default scheduler (aka SCHED_OTHER) will automatically reduce a thread's priority when it sees that the thread is using up lots of CPU cycles. Therefore, if your concern is that a CPU-heavy computational thread might hold off a mostly-idle e.g. GUI thread that requires low latency, then the OS may have already dealt with that problem for you automatically. Commented Jul 9, 2019 at 18:45
  • @JeremyFriesner I've updated some description about my situation, the default system scheduling might not be able to handle my problem, so I want to manually lower the priority of the thread. Any advice based on my situation? Commented Jul 9, 2019 at 19:39
  • @AndrewHenle thanks for the advice, I've updated my question to clarify my situation, any advice based on that? Commented Jul 9, 2019 at 19:41

2 Answers 2

1

Assuming you are running a fairly recent version of the Linux kernel, you can try setting your thread to SCHED_IDLE as shown at this link, i.e.:

void set_idle_priority() { struct sched_param param; param.sched_priority = 0; if (pthread_setschedparam(pthread_self(), SCHED_IDLE, &param) != 0) perror("pthread_setschedparam"); } 

In that mode, your thread will only run when nothing else in the system wants to run.

... that said, I'm not confident that doing so will actually solve your problem, since from your description you shouldn't be having that problem in the first place. In particular, the presence of a CPU-hogging thread running at normal/default priority should not significantly slow down your system, since the scheduler should automatically detect its CPU-hogging nature and implicitly deprioritize it, without you having to take any special steps. That makes me think that your problem probably isn't the thread's CPU usage, but rather something else, like maybe your thread is using up all of the system's available RAM capacity, causing the system to have to page memory to disk. That would definitely cause the system to slow down considerably. Another possibility would be if your thread is doing a lot of disk I/O (although that seems less likely, since in that case it would probably not be pinning a CPU core).

You might try temporarily replacing your thread's computations with a trivial CPU-burning loop, e.g.:

void my_thread_entry_func() { while(1) {/* empty */} } 

... and run that just to see if it also provokes the slowdown. If not, then it's not the CPU-usage itself that is causing the slowdown, but rather something else your thread is doing, and you'll want to do further testing to narrow down exactly which part(s) of your thread's execution-path are the culprits.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the solution! Set idle partially relieve the periodic performance downgrade, I think that's one potential solution to my problem, since I simply want that thread not making CPU burst periodically, but amortize its computation smoothly, so it won't take too much computing resource from other worker threads when it burst. And also thanks to your advice about memory issue, I bench the memory usage of my whole program and I think memory is not the bottleneck, I'm still investigate other potential approach to smooth the CPU burst of this thread.
I don't think there is any advantage in ramping a thread's CPU usage up and down gradually over time. CPUs aren't like cars that can travel at different speeds; rather a CPU is either in use at its normal speed, or it is idle; there is no "running at half speed"; if you see 50% indicated on a CPU graph, that meant the CPU was in in use for half of the sampling period and was idle for the other half. Also, a low-priority thread will never "steal" CPU cycles from a higher priority thread; as soon as the higher-priority thread wants to run, the lower-priority one gets kicked off the CPU.
Thank you for the explanation! Then I think my program might have some other problems that will cause slowing down periodically, I'll investigate into it, thanks!
What's variable &m?
&m was a typo; it should have been &param. I’ve fixed it.
1

Indeed, the situation with scheduling priorities on Linux is a huge mess of confusion over what applies to processes vs threads. At the specification level, nice and setpriority apply to processes, but Linux doesn't actually support doing that, so it interprets the argument as a kernel-level thread id instead (not same as pthread_t, and there's no standard userspace API to request the kernel-level tid of a thread!).

You might be able to achieve what you want with SCHED_IDLE or SCHED_BATCH, but they don't really work right either.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.