forked from suztomo/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 53
/
used-mem
executable file
·109 lines (89 loc) · 3.86 KB
/
used-mem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/sh
## debug mode
#DEBUG="yes"
if [ "$DEBUG" = "yes" ]; then
set -x
fi
#
# メモリ使用率(%)を求める
# 精度は小数点第1位までとする
#
calculate_used_mem() {
# vm_stat
# page size of 4096 bytes
VM_STAT=$(vm_stat)
PAGES_FREE=$(echo "$VM_STAT" | awk '/Pages free/ {print $3}' | tr -d '.')
PAGES_ACTIVE=$(echo "$VM_STAT" | awk '/Pages active/ {print $3}' | tr -d '.')
PAGES_INACTIVE=$(echo "$VM_STAT" | awk '/Pages inactive/ {print $3}' | tr -d '.')
PAGES_SPECULATIVE=$(echo "$VM_STAT" | awk '/Pages speculative/ {print $3}' | tr -d '.')
PAGES_WIRED=$(echo "$VM_STAT" | awk '/Pages wired down/ {print $4}' | tr -d '.')
# 空きメモリ
FREE_MEM=$(($PAGES_FREE + $PAGES_SPECULATIVE))
# 使用中メモリ
USED_MEM=$(($PAGES_ACTIVE + $PAGES_INACTIVE + $PAGES_WIRED))
ACTUAL_USED_MEM=$((PAGES_ACTIVE + $PAGES_WIRED))
ACTUAL_USED_MEM_GB=$(echo "$(($ACTUAL_USED_MEM * 4096 / 1024 / 1024 ))" | sed -e 's/\(.*\)\([0-9]\{3\}\)/\1.\2/' -e 's/\(.*\)\.\([0-9]\{1\}\).*/\1.\2/' -e 's/^\./0./' )
# 合計
TOTAL_MEM=$(($FREE_MEM + $USED_MEM))
TOTAL_MEM_GB=$(echo "$(($TOTAL_MEM * 4096 / 1024 / 1024 ))" | sed -e 's/\(.*\)\([0-9]\{3\}\)/\1.\2/' -e 's/\(.*\)\.\([0-9]\{1\}\).*/\1.\2/' -e 's/^\./0./' )
# 使用中メモリ(%)
# 小数点第1位まで求めて後から小数点文字(ドット)を挿入
USED_MEM_PERCENT=$(echo "$(($ACTUAL_USED_MEM * 1000 / $TOTAL_MEM))" | sed -e 's/\(.*\)\([0-9]\)/\1.\2/' -e 's/^\./0./')
echo "${USED_MEM_PERCENT}%(${ACTUAL_USED_MEM_GB}G/${TOTAL_MEM_GB}G)"
# RET
return 0
}
# Debug
debug() {
if [ "$DEBUG" = "yes" ]; then
echo "PAGES_FREE: $PAGES_FREE pages"
echo "PAGES_FREE: $(($PAGES_FREE * 4096 / 1024 / 1024 )) MB"
echo "PAGES_ACTIVE: $PAGES_ACTIVE pages"
echo "PAGES_ACTIVE: $(($PAGES_ACTIVE * 4096 / 1024 / 1024)) MB"
echo "PAGES_INACTIVE: $PAGES_INACTIVE pages"
echo "PAGES_INACTIVE: $(($PAGES_INACTIVE * 4096 / 1024 / 1024)) MB"
echo "PAGES_SPECULATIVE: $PAGES_SPECULATIVE pages"
echo "PAGES_SPECULATIVE: $(($PAGES_SPECULATIVE * 4096 / 1024 / 1024 )) MB"
echo "PAGES_WIRED: $PAGES_WIRED pages"
echo "PAGES_WIRED: $(($PAGES_WIRED * 4096 / 1024 / 1024)) MB"
echo "FREE_MEM: $FREE_MEM pages"
echo "FREE_MEM: $(($FREE_MEM * 4096 / 1024 / 1024)) MB"
echo "USED_MEM: $USED_MEM pages"
echo "USED_MEM: $(($USED_MEM * 4096 / 1024 / 1024)) MB"
echo "TOTAL_MEM: $TOTAL_MEM pages"
echo "TOTAL_MEM: $(($TOTAL_MEM * 4096 / 1024 / 1024)) MB"
echo "---------------"
# 搭載メモリ(理論値): 8GBの場合
echo "TOTAL_MEM: $((8 * 1024)) MB (Theoretical value)"
echo "---------------"
echo "USED_MEM: $(($USED_MEM * 1000 / $TOTAL_MEM)) %"
fi
}
if type vm_stat > /dev/null 2>&1; then
calculate_used_mem
RET=$?
exit $RET
fi
calculate_used_mem_free() {
FREE=$(free)
# 空きメモリ
FREE_MEM=$(echo "$FREE" | awk '/-\/\+ buffers\/cache/ {print $4}')
# 使用中メモリ
USED_MEM=$(echo "$FREE" | awk '/-\/\+ buffers\/cache/ {print $3}')
USED_MEM_GB=$(echo "$(($USED_MEM / 1024 ))" | sed -e 's/\(.*\)\([0-9]\{3\}\)/\1.\2/' -e 's/\(.*\)\.\([0-9]\{1\}\).*/\1.\2/' -e 's/^\./0./' )
# 合計
TOTAL_MEM=$(($FREE_MEM + $USED_MEM))
TOTAL_MEM_GB=$(echo "$(($TOTAL_MEM / 1024 ))" | sed -e 's/\(.*\)\([0-9]\{3\}\)/\1.\2/' -e 's/\(.*\)\.\([0-9]\{1\}\).*/\1.\2/' -e 's/^\./0./' )
# 使用中メモリ(%)
# 小数点第1位まで求めて後から小数点文字(ドット)を挿入
USED_MEM_PERCENT=$(echo "$(($USED_MEM * 1000 / $TOTAL_MEM))" | sed -e 's/\(.*\)\([0-9]\)/\1.\2/' -e 's/^\./0./')
echo "${USED_MEM_PERCENT}%(${USED_MEM_GB}G/${TOTAL_MEM_GB}G)"
# RET
return 0
}
if type free > /dev/null 2>&1; then
calculate_used_mem_free
RET=$?
exit $RET
fi
exit 1