Created
September 9, 2011 18:12
-
-
Save wankdanker/1206923 to your computer and use it in GitHub Desktop.
bash script to calculate ksm stats in kb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ "$1" != "" ]; then | |
echo " | |
---------------------------------------------------------------------------- | |
http://www.kernel.org/doc/Documentation/vm/ksm.txt : | |
The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/: | |
pages_shared - how many shared pages are being used | |
pages_sharing - how many more sites are sharing them i.e. how much saved | |
pages_unshared - how many pages unique but repeatedly checked for merging | |
pages_volatile - how many pages changing too fast to be placed in a tree | |
full_scans - how many times all mergeable areas have been scanned | |
A high ratio of pages_sharing to pages_shared indicates good sharing, but | |
a high ratio of pages_unshared to pages_sharing indicates wasted effort. | |
pages_volatile embraces several different kinds of activity, but a high | |
proportion there would also indicate poor use of madvise MADV_MERGEABLE. | |
---------------------------------------------------------------------------- | |
" | |
fi; | |
pages_shared=`cat /sys/kernel/mm/ksm/pages_shared`; | |
pages_sharing=`cat /sys/kernel/mm/ksm/pages_sharing`; | |
pages_unshared=`cat /sys/kernel/mm/ksm/pages_unshared`; | |
pages_volatile=`cat /sys/kernel/mm/ksm/pages_volatile`; | |
page_size=`getconf PAGESIZE`; | |
ratio_sharing_to_shared=$(echo "scale=2;$pages_sharing / $pages_shared"|bc); | |
ratio_unshared_to_sharing=$(echo "scale=2;$pages_unshared / $pages_sharing"|bc); | |
saved=$(echo "scale=0;$pages_sharing * $page_size"|bc); | |
saved=$(expr $saved / 1048576); | |
printf "Shared\tSharing\tUnshared\tVolatile\tSharing:Shared\tUnshared:Sharing\tSaved\n"; | |
printf "%'d\t%'d\t%'d\t\t%'d\t\t%'f:1\t%'f:1\t\t%'dM\n" $pages_shared $pages_sharing $pages_unshared $pages_volatile $ratio_sharing_to_shared $ratio_unshared_to_sharing $saved; |
Also getting the error:
++ cat /sys/kernel/mm/uksm/pages_shared
+ pages_shared=3629
++ cat /sys/kernel/mm/uksm/pages_sharing
+ pages_sharing=55277
++ cat /sys/kernel/mm/uksm/pages_unshared
+ pages_unshared=168436
++ cat /sys/kernel/mm/uksm/pages_volatile
cat: /sys/kernel/mm/uksm/pages_volatile: No such file or directory
+ pages_volatile=
++ getconf PAGESIZE
+ page_size=4096
++ echo 'scale=2;55277 / 3629'
++ bc
+ ratio_sharing_to_shared=15.23
++ echo 'scale=2;168436 / 55277'
++ bc
+ ratio_unshared_to_sharing=3.04
++ echo 'scale=0;55277 * 4096'
++ bc
+ saved=226414592
++ expr 226414592 / 1048576
+ saved=215
+ printf 'Shared\tSharing\tUnshared\tVolatile\tSharing:Shared\tUnshared:Sharing\tSaved\n'
Shared Sharing Unshared Volatile Sharing:Shared Unshared:Sharing Saved
+ printf '%'\''d\t%'\''d\t%'\''d\t\t%'\''d\t\t%'\''f:1\t%'\''f:1\t\t%'\''dM\n' 3629 55277 168436 15.23 3.04 215
ksm.sh: line 40: printf: 15.23: invalid number
ksm.sh: line 40: printf: 3.04: invalid number
3.629 55.277 168.436 15 0,000000:1 215,000000:1 0M
So I wrote this:
#!/bin/bash
[ -e /sys/kernel/mm/uksm/ ] && ksm=uksm
[ -e /sys/kernel/mm/ksm/ ] && ksm=ksm
pagesize=$(getconf PAGESIZE)
echo $(($pagesize*$(cat /sys/kernel/mm/$ksm/pages_sharing)/1024))kb scanned
echo $(($pagesize*$(cat /sys/kernel/mm/$ksm/pages_shared)/1024))kb shared=saved
echo $(($pagesize*$(cat /sys/kernel/mm/$ksm/pages_unshared)/1024))kb not shared
To fix "printf: ...: invalid number" errors add this line to start of script:
LC_NUMERIC="en_US.UTF-8"
Nice!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Whenever I run the script my output looks like this:
`Shared Sharing Unshared Volatile Sharing:Shared Unshared:Sharing Saved
/opt/ksmstat.sh: Zeile 40: printf: 26.38: Ungültige Zahl.
/opt/ksmstat.sh: Zeile 40: printf: .26: Ungültige Zahl.
30.263 798.389 212.168 1.063.240 0,000000:1 0,000000:1 3.118M
`
Zeile = line
Ungültigle Zahl = invalid number