c# 获取所有的进程的cpu使用率_macOS限制CPU或进程使用率

Limit CPU usage for an application/process

因为升级macOS Big Sur 带来的Notes耗用CPU严重问题,继续跟进

f972836005776bb904fbc32cca26ab71.png

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

7823868779035120e65a66c2a10ca445.png

现在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

296b215d9e9ac0f420716b323452316a.png

也有人提到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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值