Nov 24, 2010

Python Cheat Sheet

剛剛逛 Tsung's Blog 看到一篇分享 Python Cheat Sheet 的文章就想起,以前也曾經找過相關的東西,個人記性不是那麼好,只要過一陣子沒寫 Code 語法很快就會忘記,所以看 Cheat Sheet or Quick Reference 是最好的 recall 方式。

Quick Python Script Expalanation for Programmers from http://coffeeghost.net



中文版 from http://wiki.woodpecker.org.cn/

Python 2.6 Quick Reference

Nov 20, 2010

Kernel Shared Memory (KSM)

Kernel Shared Memory[1] 有時也被稱為 Kernel Samepage Merging 是在 linux 2.6.32[2] 被加入 main tree 裡,簡單的話,它的目地如 XEN的 Memory CoW[3] 與 VmWare 的 Transparent Page Sharing[4] 一樣,把相同內容的 page merge 在一起就是做 de-duplication 的動作讓記憶體空間更有效的被利用,當然相對地,需要花更多的 CPU 效能來做這些事,不過相對上來說,因該還是值得的。

動手玩看看前,先確定 linux kernel > 2.6.32 and Enable KSM
$ cat /boot/config-`uname -r` | grep KSM
CONFIG_KSM=y

KSM 只能使用在利用 madvise syscall 將記憶體區塊設為 MADV_MERGEABLE,另外如要取消改為 MADV_UNMERGEABLE。所以需要確認 KVM 的版本 > v0.12.0-rc0。

linux 有 Enable KSM 的話,/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
run Whether the KSM process is running.
sleep_millisecs how many milliseconds ksmd should sleep before performing another page scan.

開始使用
1. 開啟 KSM 功能
echo 1 > /sys/kernel/mm/ksm/run

2. 開二個相似的 VM image
/opt/bin/qemu -hda xp.raw -m 1024 &
/opt/bin/qemu -hda xp2.raw -m 1024 &

下面的二張圖是有開KSM跟沒關的差別,可明顯的看出,開了KSM的記憶體使用量少了一半左右


[1] http://lwn.net/Articles/306704/
[2] http://kernelnewbies.org/Linux_2_6_32
[3] http://www.xen.org/files/xensummit_fall07/18_GregorMilos.pdf
[4] http://kb.vmware.com/kb/1021095

Nov 11, 2010

讓你的封包,送出時自動加上 VLAN ID

在 Linux 下,使用 VLAN 的方法

1. insert 802.1q kernel module
sudo modprobe 8021q 


2. create a vlan id on NIC by vconfig

sudo vconfig add eth0 111
Added VLAN with VID == 111 to IF -:eth0:-
3. start up eth0.111
sudo ifconfig eth0.111 192.168.1.2 up

4. 測試前的準備:
sudo ifconfig eth0.111 192.168.1.2

eth0.111  Link encap:Ethernet  HWaddr 00:21:70:ff:d7:88  
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::221:70ff:feff:d788/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:238 (238.0 B)
5. 利用 wireshark 抓出來的封包


此例中,在eth0上加上 vlanid 111,成功之後,會多出一個 eth0.111 的network device,只要是從這個 device 出去的封包,會自動加上 802.1Q 的 header