Get the App
SLTechnology News&Howtos  ›  Development  › 

How to balance the process load into multicore CPU with Shell script

Shulou Source: shulou.com Published: 2022-06-03 08:42:47 09月19日 Update

This article mainly shows you "Shell script how to achieve process load balancing to multi-core CPU", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Shell script how to achieve process load balancing to multi-core CPU" this article.

Sometimes, due to architectural design or other business characteristics, some applications use CPU unevenly, so business processing is concentrated on one CPU, while other CPU is idle to sleep. Here is a simple optimization implementation that binds each thread to multiple CPU to achieve performance improvement.

Although CPU is a good idea, it is not a killer, and its performance improvement depends on whether the performance of each thread is evenly distributed; so the best way is to optimize your program architecture.

A Shell script (script named bindcpu2p.sh) is shared here, through which the process can be loaded evenly on each CPU.

The code is as follows:

#! / bin/sh

Pids= `/ sbin/pidof $1`

Cpunum= `cat / proc/cpuinfo | grep processor | wc-l`

Cpuidx=0

For pid in $pids

Do

/ usr/bin/taskset-cp ${cpuidx} ${pid}

Cpuidx=$ (($cpuidx+1))

Cpuidx=$ (($cpuidx%$cpunum))

Echo $cpuidx

Done

Use method $>. / bindcpu2p.sh progressname

Note: please take a look at the code, it would be best if you can help debug it.

These are all the contents of the article "how Shell scripts balance the process load to multicore CPU". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Scripts balancing processes multicore content performance articles business code best architecture threading learning help good usage brothers methods reasons multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Shulou Tech Info Redmi Shulou Technology