首頁 > 運動

macOS限制CPU或程序使用率

由 Mac樂 發表于 運動2021-12-11

簡介= 0 ]thenecho “the process is running,Now we seen the cpu%”cpu=$(ps aux|grep Notes|grep -v “grep”|aw

怎麼設定cpu使用率

Limit CPU usage for an application/process

因為升級macOS Big Sur 帶來的Notes耗用CPU嚴重問題,繼續跟進

macOS限制CPU或程序使用率

https://www。toutiao。com/i6894855986931040780/

重製SMC也不能解決問題。所以在上篇文章中透過殺程序來臨時解決。總感覺這個辦法太過粗魯且不夠優雅。

測試使用nice也沒有效果:

nice和renice並不限制給定應用程式本身可用的CPU百分比,但是它們允許改變排程優先順序,或者換句話說,一個程序將獲得多少CPU時間。這都是相對於你的系統的CPU負載而言的,所以如果系統的利用率不高,你很可能不會看到任何區別。

因此想著如何,透過簡單的命令列或者軟體來

限制應用或者程序的CPU使用率

https://github。com/maumi/cputhrottlehttp://www。willnolan。com/cputhrottle/cputhrottle。html已經不支援現在的macOS。

我這裡測試可行的OS: macOS Big Sur 10。16 20B29 x86_64

https://github。com/denji/cpulimithttps://github。com/denji/cpulimit/blob/master/README。md

直接clone程式碼make && make install

➜ git clone https://github。com/denji/cpulimitCloning into ‘cpulimit’。。。remote: Enumerating objects: 698, done。remote: Total 698 (delta 0), reused 0 (delta 0), pack-reused 698Receiving objects: 100% (698/698), 197。36 KiB | 8。00 KiB/s, done。Resolving deltas: 100% (420/420), done。➜ cd cpulimit➜ cpulimit git:(master) makecd src && /Applications/Xcode。app/Contents/Developer/usr/bin/make allcc -c list。c -Os -Wall -g -D_GNU_SOURCEcc -c process_iterator。c -Os -Wall -g -D_GNU_SOURCEcc -c process_group。c -Os -Wall -g -D_GNU_SOURCEcc -o cpulimit cpulimit。c list。o process_iterator。o process_group。o -Os -Wall -g -D_GNU_SOURCEn “CPU Utilization Limiter” -s 1 -s https://github。com/denji/cpulimit -N 。/cpulimit > cpulimit。1/bin/sh: n: command not foundmake[1]: [cpulimit。1] Error 127 (ignored)cd test && /Applications/Xcode。app/Contents/Developer/usr/bin/make allcc -o busy busy。c -lpthread -Wall -gcc -I。。/src -o process_iterator_test process_iterator_test。c 。。/src/list。o 。。/src/process_iterator。o 。。/src/process_group。o -lpthread -Wall -g➜ cpulimit git:(master) ✗ sudo make installPassword:cd src && /Applications/Xcode。app/Contents/Developer/usr/bin/make install/usr/bin/sudo install -D cpulimit /usr/local/bin/cpulimit/usr/bin/sudo install -D cpulimit。1 /usr/local/share/man/man1/cpulimit。1touch installcd test && /Applications/Xcode。app/Contents/Developer/usr/bin/make installtouch install

然後簡單的指令碼如下:

#!/bin/bashprocessid=$(pgrep Notes)echo “limit 40 ” >> /tmp/kill。log/usr/local/opt/coreutils/libexec/gnubin/date >> /tmp/kill。log/usr/local/bin/cpulimit -l 40 -p $processid -z現在要做的就是放到後臺就可以了。當然這指令碼的前提是需要運行了Notes

現在可以看下效果activity monitor

macOS限制CPU或程序使用率

現在CPU使用率被限制,且不影響正常使用

稍微修改下:

cat /Users/lex/shell/cpulimitNotes。sh#!/bin/bash pgrep Note if [ $? = 0 ] then echo “the process is running,Now we seen the cpu%” cpu=$(ps aux|grep Notes|grep -v “grep”|awk ‘{print$3}’|awk ‘{print int($0)}’) #Notespid=$(ps aux|grep Notes|grep -v “grep”|awk ‘{print$2}’) #Notespid=$(pgrep Notes) if [ “${cpu}” -gt “60” ] then echo “the cpu is $cpu,so we decide to kill this pid” killall -9 Notes #or kill -9 $Notepid echo “kill process id finished” /usr/local/opt/coreutils/libexec/gnubin/date >> /tmp/kill。log else echo “cpu ok less 60%” exit fi else echo “now Notes is not running” /usr/bin/open -a “/System/Applications/Notes。app” processid=$(pgrep Notes) echo “limit 40 ” >> /tmp/kill。log /usr/local/opt/coreutils/libexec/gnubin/date >> /tmp/kill。log /usr/local/bin/cpulimit -l 40 -p $processid -z exitfi

另外測試用軟體也可以做到:

App Tamer

macOS限制CPU或程序使用率

也有人提到AppPolice這個我沒有測試哈。

參考:

https://github。com/maumi/cputhrottle

http://hints。macworld。com/article。php?story=20110131001708255

https://apple。stackexchange。com/questions/24998/can-i-manually-limit-the-cpu-used-by-a-process

https://forums。macrumors。com/threads/limiting-cpu-usage-how。437347/

http://www。willnolan。com/cputhrottle/cputhrottle。html

https://github。com/AppPolice/AppPolice

https://github。com/rwanyoike/cputhrottle

https://github。com/denji/cpulimit

Tags:cpulimitcomusrbinprocess