LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-30-2019, 11:53 AM   #1
system001
Member
 
Registered: Nov 2018
Location: Portland Oregon Canada
Distribution: Kubuntu 19.04 - Manjaro 18.0
Posts: 199

Rep: Reputation: 15
How To Limit CPU Usage In Handbreak


i have some video files that are far larger than they need to be and i'm using handbreak to reduce their sizes. i would like to reduce the load they have on my cpu. yes i know in doing so it will take longer to convert the files, but that is fine. please any suggestions and or tutorials on doing this would be greatly appreciated. thanks

amd fx 6300 on kde neon 64 bit
nvidia 750ti on the 430 driver
kernel 5.1
 
Old 05-30-2019, 12:07 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
The cpulimit application could well meet your needs.

Quote:
cpulimit is a simple program that attempts to limit the CPU usage of a process (expressed in percentage, not in CPU time).
 
Old 05-30-2019, 01:32 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
On that note.
Code:
#!/bin/bash

# requires cpulimit

[[ -z $1 ]] || [[ $# -ne '2' ]] && { echo "Usage ./limitcpu command [limit percent]; exit 1 ; }

echo $1
echo $2
count=0

  pidd=$(pidof $1)
  pidd=${pidd%% *}
  
  echo "pid $pidd"
  cpulimit  -p $pidd -l $2 -z -i

while sleep 3;
do
	
   pidd=$(pidof $1) 
   pidd=${pidd%% *} 
   cpulimit  -p $pidd -l $2 -z -i
	
   if [[ $count -eq '1' ]] && [[ -z "$pidd" ]] ; then
   {
     echo "
     Ran dry...
     exiting .....
     "
     exit
    }
    else
    {
      ((count++))
    }
    fi
    [[ -n "$pidd" ]] && count=0
done
this was written with my script to resample mp3 in mind. whenever lame would stop doing one mp3 then pick up another one it catches the new pid for lame, then puts cpulimit back to work on that new pid, when mp3s where finished it's checks 1 times before exiting, to compensate for lag time between mp3's

example
Code:
$ limitcpu qbittorrent 40
qbittorrent
40
pid 25474
Process 25474 found
then you can start top to just get a look see for your own well being, and of course you can use this with your handbrake or handbrake-cli or creating a script yourself that does the same thing , or off the cli for when you are doing only one movie.

Last edited by BW-userx; 05-30-2019 at 01:55 PM.
 
Old 05-31-2019, 05:10 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
i doubt that cpulimit would be of much help here; but surely can't hurt to try.
i think this should be a setting within handbrake; either within handbrake proper, or something passed to the transcoding software.
If ffmpeg does handbrake's transcoding, or you could switch to ffmpeg proper, here's some ideas.
 
Old 05-31-2019, 05:35 AM   #5
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by ondoho View Post
i doubt that cpulimit would be of much help here...
Out of interest, ondoho, why do you think that?
 
Old 05-31-2019, 05:44 AM   #6
system001
Member
 
Registered: Nov 2018
Location: Portland Oregon Canada
Distribution: Kubuntu 19.04 - Manjaro 18.0
Posts: 199

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ondoho View Post
i doubt that cpulimit would be of much help here; but surely can't hurt to try.
i think this should be a setting within handbrake; either within handbrake proper, or something passed to the transcoding software.
If ffmpeg does handbrake's transcoding, or you could switch to ffmpeg proper, here's some ideas.
thanks i'll have a look through some of those. much rather do that than write out a script.
 
Old 05-31-2019, 06:42 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by system001 View Post
i have some video files that are far larger than they need to be and i'm using handbreak to reduce their sizes. i would like to reduce the load they have on my cpu. yes i know in doing so it will take longer to convert the files, but that is fine. please any suggestions and or tutorials on doing this would be greatly appreciated.
The traditional method is nice. It does not guarantee a CPU percentage, but increases the probability that the CPU is relinquished to non-nice processes when they want it.
Code:
$ nice -n 19 handbrake [handbrake options and arguments]
EDIT: Also renice to change the nice value of a running process.

Last edited by berndbausch; 05-31-2019 at 06:45 AM.
 
Old 05-31-2019, 07:14 AM   #8
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
I have this alias in my bashrc
alias nicesh='chrt -i 0 ionice -c 3 ${SHELL:-/bin/sh}'

Anything that runs or is started from within that nicesh will then run with the lowest possible priority for both cpu and I/O.

nice -n 19 isn't as useful as it once was on linux thanks to the introduction of control-groups.
 
Old 05-31-2019, 11:39 AM   #9
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,140

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Just out of curiosity why would you want this? Is it to have more power to use the computer while encoding? Or because you think it will prevent so called wear and tear on the cpu? If the latter then you shouldn't worry about it.

Cpu's don't exactly wear out, at least not in the common sense. You won't be shortening the life of the chip by running full blast, at least not in any measurable sense. Overclocking however is a good way to blow a chip if you don't know what you are doing and don't keep it cool.
 
Old 05-31-2019, 12:00 PM   #10
RickDeckard
Member
 
Registered: Jan 2014
Location: Canton, Georgia, USA
Distribution: Debian 12
Posts: 205

Rep: Reputation: Disabled
Another method to reduce the stress on your CPU would be using the cpufreq-* utilities if they're available on your distribution, with a governor like "powersave."
 
Old 05-31-2019, 12:06 PM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by hydrurga View Post
Out of interest, ondoho, why do you think that?
it's a kludge that mostly just "Limit cpu usage of a process in percentage. Actually sends SIGSTOP/SIGCONT POSIX signals to processes".
I'm not sure it'll work at all with a GUI program. Maybe it'll just limit the GUI. Surely handbrake outsources the transcoding. It would be much better to tell that process (hopefully ffmpeg) directly (via command options) to restrict itself.
And even if cpulimit can be applied, I suspect that it carries significant overhead.
But as I said, can't hurt to try.
 
1 members found this post helpful.
Old 05-31-2019, 12:16 PM   #12
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
Well if you have cooling issues, limiting how much heat you generate can help prolong your broken hardware. I ran a laptop without a working fan for over a year by only using it to look at game guides while I gamed on the other not-broken computer.

I also live in a warm region so having my computer output less heat lets the A/C turn off once in a blue moon. Other quirks like putting an I/O intensive thing on it's own storage device, so other users don't feel the pinch.

Mostly I use nice on browsers so I can kill the browsers when they misbehave. Versus waiting seconds or minutes for HID interfaces to move far enough to express my intent. Without holding a power button for seconds to kill everything.

In the case of the previous fanless, but not by design computer, it did eventually heat up enough to make the USB unstable. Which it was booting off of, so end of life at that point.

There's cpufreq and other tools to have your CPU run slower than it's max. Not exactly a per process method. But one way to not pop a fuse if you have more computers than you should on one circuit. Or otherwise live in 1970s era housing not designed for our modern electronics.
 
Old 05-31-2019, 03:11 PM   #13
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
I won't speak to the OP's intentions, but the reason I use sched_idle is to maintain a responsive interactive system/desktop while big compile jobs are running in the background. sched_idle or nice won't do anything to help manage cpu thermal output; as others have mentioned, you'll need to look towards cpu frequency management if that is your concern.
 
Old 05-31-2019, 03:19 PM   #14
system001
Member
 
Registered: Nov 2018
Location: Portland Oregon Canada
Distribution: Kubuntu 19.04 - Manjaro 18.0
Posts: 199

Original Poster
Rep: Reputation: 15
it certainly didn't take peeps to get off topic and start useless speculation on why i want to limit handbreak's cpu usage. that said i only had about 40 files that needed to be reduced so decided to just let handbreak use what it wanted. thanks for the input and suggestions though.
 
Old 06-01-2019, 12:13 AM   #15
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
I did (and still do) have a use case for cpulimit - it's a script that generates a desktop background every few minutes or so. If I don't limit it, the fans spin up every time, and that's simply disturbing to me. setting nice to the lowest value does NOT help with that.
so i use cpulimit to keep it from happening. however, i don't run the whole script with cpulimit, just the crucial command.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could not install Handbreak through terminal, Returns error u9477100 Linux - Software 18 09-05-2018 04:40 AM
[SOLVED] High CPU load, but low CPU usage (high idle CPU) baffy Linux - Newbie 5 03-13-2013 09:24 AM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
How can I limit the CPU usage of a PID? ideasman Linux - Software 1 04-01-2004 07:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration