buddyinfo 内存碎片数据采集

本文介绍了一个名为buddyinfo.sh的Shell脚本,该脚本用于监控和展示Linux系统的内存分配情况,特别是不同层级的伙伴系统内存块状态。通过定时收集/proc/buddyinfo中的数据并进行计算,输出了各个层级的内存使用情况,有助于理解系统内存管理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不说了,上工具

#cat    buddyinfo.sh
#!/bin/sh
#****************************************************************#
# ScriptName: buddyinfo.sh
# Author: $SHTERM_REAL_USER@alibaba-inc.com
# Create Date: 2017-04-26 15:47
# Modify Author: $SHTERM_REAL_USER@alibaba-inc.com
# Modify Date: 2017-04-26 15:47
# Function:
#***************************************************************#

BC_MUTI(){
    #subtraction
    if [[ -n $1 && -n $2 ]];then
        res1=$(printf "%.2f" `echo "scale=2;($1*$2)"|bc`)
        echo $res1
    fi
}

MESG(){
  local GROUP="$1"
  local KEY=$2
  local VALUE=$3
  local MEASURE=$4

  if [[ -n $VALUE ]];then
      echo -e "$(date "+%F %H:%M:%S"),$GROUP,$KEY,$VALUE,$MEASURE"  >> $REAL_LOG
      return 0
  fi
}

BC_BUDDY(){
    aa=$1
    bb=$2
    let "res1=$aa*2**$bb"
    echo $res1
}

CONVERT_BUDDY_VALUE(){
    start=$1
    MAX=`cat /proc/buddyinfo  | grep "Normal"| awk -F"Normal" '{print $2}'|awk '{print NF}'`
    MAX=`echo "$MAX - 1" | bc`
    buddy_values=`cat /proc/buddyinfo  | grep "Normal"| awk -F"Normal" '{print $2}'`
    array2=(
        $buddy_values
    )
  bb=`echo ${array2[@]:$start:$MAX}`

    count=0
    final_vvv=0
    for value in ${bb[@]};do
        vvv=`BC_BUDDY $value $count`
        count=`echo "$count +1 "|bc`
        final_vvv=`echo "$final_vvv + $vvv"|bc`
    done

    echo  $final_vvv
}

LOOP_VMSTAT(){
    while [[ 0 == 0 ]] ;do
        sleep 1
        CALCULATE_VMSTAT
    done
}

CALCULATE_VMSTAT(){
    timestamp=$(date "+%Y-%m-%d-%H:%M:%S")
    order1=`CONVERT_BUDDY_VALUE 1`
    order2=`CONVERT_BUDDY_VALUE 2`
    order3=`CONVERT_BUDDY_VALUE 3`
    order4=`CONVERT_BUDDY_VALUE 4`
    order5=`CONVERT_BUDDY_VALUE 5`
    order6=`CONVERT_BUDDY_VALUE 6`
    order7=`CONVERT_BUDDY_VALUE 7`
    order8=`CONVERT_BUDDY_VALUE 8`
    order9=`CONVERT_BUDDY_VALUE 9`
    order10=`CONVERT_BUDDY_VALUE 10`
    printf "%-20s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n" $timestamp $order1 $order2 $order3 $order4 $order5 $order6 $order7 $order8 $order9 $order10
}
main(){
     printf "%-20s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s %-7s\n" "timestamp" "order1" "order2" "order3" "order4" "order5" "order6" "order7" "order8" "order9" "order10"
     LOOP_VMSTAT
}
main
[root@jiangyi01.sqa.zmf /home/ahao.mah]
#sh   buddyinfo.sh
timestamp            order1  order2  order3  order4  order5  order6  order7  order8  order9  order10

2017-04-26-20:53:20  9952240 4975948 2487866 1243711 621496  309892  154207  76867   38313   19124
2017-04-26-20:53:22  9952309 4975928 2487861 1243712 621496  309892  154207  76867   38313   19124
2017-04-26-20:53:23  9952392 4975970 2487869 1243713 621496  309892  154207  76867   38313   19124
2017-04-26-20:53:24  9952429 4975992 2487877 1243713 621496  309892  154207  76867   38313   19124
2017-04-26-20:53:26  9952449 4975993 2487879 1243713 621496  309892  154207  76867   38313   19124
2017-04-26-20:53:27  9952485 4975998 2487880 1243713 621496  309892  154207  76867   38313   19124
2017-04-26-20:53:28  9952490 4976003 2487883 1243715 621497  309892  154207  76867   38313   19124
2017-04-26-20:53:29  9952475 4975996 2487882 1243716 621498  309892  154207  76867   38313   19124
2017-04-26-20:53:31  9952404 4975956 2487881 1243716 621498  309892  154207  76867   38313   19124
2017-04-26-20:53:32  9952494 4975998 2487886 1243716 621498  309892  154207  76867   38313   19124

970272-20170426205416350-517314710.png

转载于:https://www.cnblogs.com/muahao/p/6770014.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值