利用taskset有效控制cpu資源

經常感受系統資源不夠用,一臺機子上跑了不下3個比較重要的服務,可是天天咱們還要在上面進行個備份壓縮等處理,網絡長時間傳輸,這在就很影響本就不夠用的系統資源;linux

這個時候咱們就能夠把一些不過重要的好比copy/備份/同步等工做限定在一顆cpu上,或者是多核的cpu的一顆核心上進行處理,雖然這不必定是最有效的方法,但能夠最大程度上利用了有效資源,下降那些不過重要的進程佔用cpu資源;網絡

查看系統下cpu信息:ide

#cat /proc/cpuinfo工具

taskset就能夠幫咱們完成這項工做,並且操做很是簡單;ui

該工具系統默認安裝,rpm包名util-linuxthis

#taskset --help
taskset (util-linux 2.13-pre7)
usage: taskset [options] [mask | cpu-list] [pid | cmd [args...]]
set or get the affinity of a processorm

-p, –pid operate on existing given pid
-c, –cpu-list display and specify cpus in list format
-h, –help display this help
-v, –version output version information進程

舉例:
一、開啓一個只用0標記的cpu核心的新進程(job.sh是你的工做腳本)ci

#taskset -c 0 sh job.sh資源

二、查找現有的進程號,調整該進程cpu核心使用狀況(23328舉例用的進程號)

#taskset -pc 0 23328
pid 23328's current affinity list: 0-3 #0-3表示使用全部4核進行處理
pid 23328's new affinity list: 0 #調整後改成僅適用0標記單核處理

三、可在top中進行負載check

最後你能夠在你的工做腳本中加入該指令來合理利用現有的cpu資源;

相關文章
相關標籤/搜索