云计算百科
云计算领域专业知识百科平台

提升服务器CPU和memory(内存)利用率

一、让cpu使用率高起来

1、将下面代码复制粘贴到目标服务器。保存为cpu_up.sh

#!/usr/bin/env bash

# 获取脚本所在目录
base_dir=$(cd `dirname $0`; pwd)

# 获取当前CPU使用率(平均值)
cpu_using=$(top -bn 1 | grep '%Cpu(s)' | awk '{print $2}' | awk -F '.' '{print $1}')

# 需要达到的CPU使用率,从脚本参数获取
cpu_used=$1

# 如果脚本执行没有参数传入,返回脚本使用方法并退出脚本
if [ $# -ne 1 ]; then
printf "\\e[0;34mUSAGE: bash $0 30\\e[0m\\n"
exit 1
fi

# 如果需要达到的CPU使用率小于等于当前使用率则退出脚本
if [[ "${cpu_used}" -le "${cpu_using}" ]]; then
echo "Current CPU usage (${cpu_using}%) is already greater than or equal to the target (${cpu_used}%). Exiting."
exit 0
fi

# 获取CPU核心数
cpu_proc=$(grep -c ^processor /proc/cpuinfo)

# 计算需要增加的CPU使用率
cpu_status=$(( cpu_used – cpu_using ))

# 计算当前使用的线程数量
cpu_using_count=$(awk "BEGIN {printf \\"%d\\", ${cpu_proc} * ${cpu_using} / 100}")

# 计算需要达到的线程数量
cpu_used_count=$(awk "BEGIN {printf \\"%d\\", ${cpu_proc} * ${cpu_used} / 100}")

# 计算需要增加的线程数量
cpu_num=$(( cpu_used_count – cpu_using_count ))

# 清空日志文件
> ${base_dir}/kill_cpu_up.log

for i in $(seq ${cpu_num})
do
(
while true
do
: # 这里可以用一个无操作命令占位,或者添加实际需要执行的命令
done
) &

echo "kill $!" >> ${base_dir}/kill_cpu_up.log 2>&1
done

echo "Started ${cpu_num} processes to reach the target CPU usage of ${cpu_used}%."

2、假设要求cpu使用率不能低于30%,执行: sh cpu_up.sh 30

3、关闭脚本,在运行目录下会生成kill_cpu_up.log。kill 掉里面的进程就ok

二、让内存使用率高起来(会占用一些cpu,如果也要升CPU使用率的话,可以先把内存搞起来,不然会影响cpu的计算) 首先安装 stress-ng 工具,安装方法自行搜索:(使用stress-ng工具来分配指定大小的内存。stress-ng是一个更现代、功能更强大的压力测试工具,相比stress提供了更多的选项和更好的性能。并且现在离线安装包只能下载到stress-ng工具,离线安装包可能只用执行命令make install进行安装)

1、使用方法和cpu一样。将脚本保存为memory_up.sh

注意:1.脚本里需要自行设置stress-ng运行时间

           2.脚本里未计算当前已使用内存,输入的百分比数据,是将要提升的内存使用量

#!/bin/bash

# 检查输入参数
if [ $# -ne 1 ]; then
echo "Usage: $0 <memory_percentage>"
exit 1
fi

# 获取输入参数
memory_percentage=$1

# 验证输入参数是否为有效的数字
if ! [[ "$memory_percentage" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
echo "Error: Memory percentage must be a number."
exit 1
fi

# 计算内存使用量
total_memory=$(free -m | awk '/^Mem:/{print $2}')
target_memory=$((total_memory * memory_percentage / 100))

# 检查目标内存是否合理
if [ "$target_memory" -le 0 ] || [ "$target_memory" -gt "$total_memory" ]; then
echo "Error: Target memory (${target_memory}MB) is out of range (0-${total_memory}MB)."
exit 1
fi

# 分配内存
echo "Allocating approximately ${target_memory}MB of memory…"

# 使用 stress-ng 工具来分配内存–1个线程,持续时间4800小时(时间自行设置)
stress-ng –vm-bytes "${target_memory}M" –vm-keep -vm 1 –timeout 4800h &

# 记录后台进程的PID
stress_pid=$!

# 输出提示信息
echo "Press Ctrl+C to stop memory consumption."

# 持续运行,直到手动中断脚本
trap "kill $stress_pid" SIGINT SIGTERM
wait $stress_pid

2、假设要求内存使用率提升30%,则执行:(后台运行) nohup sh memory_up.sh 30 &

内存使用率直接提升30%。

关闭方法:(top后按1和C按键,能看到很多信息) top 找到memory_up.sh的pid(或者在nohup.out日志里找到pid) kill pid

三、linux上执行脚本报错解决方法

$‘\\r‘: command not found的解决方法

出现这样的错误,是因为Shell脚本在Windows系统编写时,每行结尾是\\r\\n,而在Linux系统中行每行结尾是\\n,所以在Linux系统中运行脚本时,会认为\\r是一个字符,导致运行错误。

去除Shell脚本的\\r字符:

方法1 sed -i 's/\\r//' one-more.sh

方法2 dos2unix one-more.sh

输出如下:

dos2unix: converting file one-more.sh to Unix format …

赞(0)
未经允许不得转载:网硕互联帮助中心 » 提升服务器CPU和memory(内存)利用率
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!