Dec 11, 2010

git object

git 裡,所有的檔案、文件與資料匝都是以 hash 來表示"身份" (object),而 hash 是使用 SAH1,這樣每一個 object 幾呼是唯一性的。git 中所謂的 object 指的是,content 本身加上 hash,而 content 分為下面四種 blob, tree, commit, and tag.
blob : the data is the contents of a file
tree : To store a directory, Git writes out an object describing the directory's contents

實際測試看看 :
$ echo "hello" > hello
$ git add .
$ git commit -am 'hello'
[master (root-commit) 21039e0] hello
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello

$ mkdir foo
$ echo "bar" > foo/bar
$ echo "world" >> hello
$ git add .
$ git commit -am 'foo/bar and hello world'
[master ab4c151] foo/bar and hello world
2 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 foo/bar

$ git log
commit ab4c151820a51da73eb00381472a4774475bcf66
Author: Yao-Po Wang
Date: Sat Dec 11 16:07:27 2010 +0800

foo/bar and hello world

commit 21039e0f12ebf539275346566982566c3bcd9217
Author: Yao-Po Wang
Date: Sat Dec 11 16:06:32 2010 +0800

hello

利用 git cat-file 來得到更多資訊
git-cat-file - Provide content or type and size information for repository objects
Option:
-t : show the object type.
-s : show the object size.
-p : Pretty-print based on its type.


$ git cat-file -t 21039
commit

$ git cat-file -s ab4c1
234

$ git cat-file -s 21039
168

可利用 -p or commit 來提到,此 commit 的 tree object
$ git cat-file commit 21039
$ git cat-file -p 21039
tree b4d01e9b0c4a9356736dfddf8830ba9a54f5271c
author Yao-Po Wang 1292054792 +0800
committer Yao-Po Wang 1292054792 +0800

hello

從上面,我們可以得到 commit 的 tree object 利用 tree object 可得知,在這個 commit 的情況下目錄包含的
git ls-tree - List the contents of a tree object
$ git ls-tree 21039e0
100644 blob ce013625030ba8dba906f756967f9e9ca394464a hello

$ git ls-tree ab4c151
040000 tree ee314a31b622b027c10981acaed7903a3607dbd4 foo
100644 blob 94954abda49de8615a048f8d2e64b5de848e27a1 hello

$ git ls-tree -r ab4c151 <-- recurse 100644 blob 5716ca5987cbf97d6bb54920bea6adde242d87e6 foo/bar 100644 blob 94954abda49de8615a048f8d2e64b5de848e27a1 hello


從 git ls-tree 我們可以知道,tree 下所有的 blob (補 SHA-1 的 content), 利用 cat-file blob 就可得知在些 commit 下的 blob 的內容
$ git cat-file -p ce0136
hello

$ git cat-file blob 94954
hello
world

到這裡,因該可以大約的知道, 放在 git 裡的檔案是如果被儲存與 object 的架構, 而這些 object 是被存放在 .git/objects 下
$ tree .git/objects/
.git/objects/
├── 21
│   └── 039e0f12ebf539275346566982566c3bcd9217
├── 55
│   └── 9dbaeba4c3e2fff4ac4bf4323b13a5ed1616c7
├── 57
│   └── 16ca5987cbf97d6bb54920bea6adde242d87e6
├── 94
│   └── 954abda49de8615a048f8d2e64b5de848e27a1
├── ab
│   └── 4c151820a51da73eb00381472a4774475bcf66
├── b4
│   └── d01e9b0c4a9356736dfddf8830ba9a54f5271c
├── ce
│   └── 013625030ba8dba906f756967f9e9ca394464a
├── ee
│   └── 314a31b622b027c10981acaed7903a3607dbd4
├── info
└── pack

10 directories, 8 files

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

Oct 24, 2010

zenity 的 python wrap : PyZenity

Zenity 的相關範例可以參考 A COMPLETE ZENITY DIALOG EXAMPLES 2
PyZenity 其實一個 wrap 而已,執行時是 call popen(zenity),而使用起來跟 zenity 差不多。

Oct 4, 2010

Install Ubuntu 9.10 Karmic on Kohjinsha SX3 UMPC

去年夏天,天真的想說,買台 UMPC 來當電子書使用,到後來,還是賣掉了,最主要是因為,1.2公斤還是太重,以下是當時的筆記,剛整理時發現的,或許,有人會需要它 :).

1. Poulsbo (GMA500) Support in Karmic (9.10)1)
1)PPA-based (UbuntuMobile & Milone):
wget http://gma500re.altervista.org/scripts/poulsbo_ppa.sh && sh ./poulsbo_ppa.sh
or
2) FTP-based (no ppa repositories):
wget http://gma500re.altervista.org/scripts/poulsbo.sh && sh ./poulsbo.sh
2.  IM
http://vicamo.blogspot.com/2009/07/boshiamy-for-ibus.html
3.  pen screen
工人舍SH8安裝Ubuntu 9.04 netbook remix初步心得 
penmount Driver Download

call graph

前幾天在 chihchun's Channel 看到 $4 之前於 H4 分享的 Debug 技巧,讓我想起,很久以前 Jserv 也於它的 blog 分享 GCC 函式追蹤功能 主要是利用 "finstrument-functions" 的功能,當進出 function 時,執行以下二個函式
void __cyg_profile_func_enter (void *this_fn, void *call_site);
void __cyg_profile_func_exit  (void *this_fn, void *call_site);


先來個 example 試試看:
#include 

void __attribute__((__no_instrument_function__))
__cyg_profile_func_enter(void *this_func, void *call_site)
{
printf("Enter &%s:%p, called by %p \n", __FUNCTION__, this_func, call_site);
}

void __attribute__((__no_instrument_function__))
__cyg_profile_func_exit(void *this_func, void *call_site)
{
printf("Enter &%s:%p, called by %p \n", __FUNCTION__, this_func, call_site);
}

void a(void);
void b(void);

void b()
{
}

void a()
{
b();
}

int main(int argc, const char *argv[])
{
a();
return 0;
}



利用 j 的 Dot 語法,可以簡單的畫出 directed graph 語言感覺很像以前讀研究所時,寫 network simulation的腳本
digraph G {
node1;
node2;
node3;

node1 -> node2 [label="edge_1_2"];
node1 -> node3 [label="edge_1_3"];
node2 -> node3 [label="edge_2_3"];
}

Conver to JPG:
dot -Tjpg graph.dot -o graph.jpg
產生出來的圖



reference:
http://tarot.freeshell.org/leafpad/
http://blog.linux.org.tw/~jserv/archives/001870.html
http://www.ibm.com/developerworks/cn/linux/l-graphvis/
http://blog.linux.org.tw/~jserv/archives/001723.html

booting iso from grub2

1. Format a partition on the USB stick to some filesystem that's supported by GRUB 2 in the way you prefer to do that.
sudo mkfs.vfat /dev/sdd1

2. mount partition on the temp directory
sudo mkdir /media/flash_disk
sudo mount /dev/sdd1 /media/flash_disk/

3. Install GRUB 2 onto the USB stick.
sudo grub-install --no-floppy --root-directory=/media/flash_disk/ /dev/sdd

4. Edit /media/flash_disk/boot/grub/grub.cfg to point to the ISO files.
set timeout=10
set default=0

menuentry "Run Ubuntu Live 10.04" {
 loopback loop /ubuntu-10.04-desktop-i386.iso
 linux (loop)/casper/vmlinuz boot=casper iso-scan/filename=/ubuntu-10.04-desktop-i386.iso splash --
 initrd (loop)/casper/initrd.lz
}

menuentry "Ubuntu 10.4 Server i386" {
loopback loop /ubuntu-10.04-server-i386.iso
linux (loop)/install/vmlinuz --
initrd (loop)/install/initrd.gz
}

5. copy linux cdrom iso to /media/flash_disk
cp ubuntu-10.04-desktop-i386 /media/flash_disk/

6. booting by qemu
sudo /opt/bin/qemu -hda /dev/sdd -boot c -m 256 -localtime

Jun 1, 2010

Doxygen 初試

最近部門開始要求要認真寫 functional spec,唉.. 但是沒有定義出正式的格式是什麼,之前在 maintain 部門的 code 時,發現這份 code 的 comment 是用 doxygen 的格式。
剛在網路上找到一份不錯的文章:簡介Doxygen

1. 先在 project 目錄打 doxygen -g doxy.conf; doxy.conf是產生的 config 檔,使用 doxy 之前先對你的 project 做相關的設定
2. doxygen doxy.con; doxygen 後面直接接你的 config 檔,執行玩會發現你的目錄多出二個檔案匝 html and latex。

May 4, 2010

讓 Chrome 支援 Java

  1. mkdir /opt/google/chrome/plugins
  2. cd /opt/google/chrome/plugins
  3. ln -s `dpkg -L sun-java6-bin | grep libnpjp2.so` .

recover juniper ex-4200's root password

今天跟Lab拿了一台 Juniper EX-4200 24Port L3 Switch,開機之後,發現,需要帳號,問了所有可能模過這台機器的同事,沒想到沒人知道有帳號這回事 = ="....,那不就是買了快一年的機器,都被亮在那裡沒人去動。
其實一拿到 juniper 的機器就有很多地方覺的很有趣,如:它的boot loader是用 u-boot,system是用 freebsd 改的,還有 csh or bash 可以用 @@",裡面有很多設計讓我大開眼境,不像 cisco like般,只有很單純的 cli 可以使用,而是你可以看到它與OS的整合,希望之後還有時間可以讓我慢慢玩這台機器。
recove的方法:

1. 開機之後,出現下列文字,快點按下你的 SPACE
Hit [Enter] to boot immediately, or space bar for command prompt.
Booting [/kernel] in 1 second...

2. 輸入 "boot -s"
3. 出現 "Enter full pathname of shell or 'recovery' for root password recovery or RETURN for /bin/sh" 輸入 recovery
4. 之後就能順立開機,進入 single mode,且只要按照它的指示做就好了
NOTE: Once in the CLI, you will need to enter configuration mode using
NOTE: the 'configure' command to make any required changes. For example,
NOTE: to reset the root password, type:
NOTE: configure
NOTE: set system root-authentication plain-text-password
NOTE: (enter the new password when asked)
NOTE: commit
NOTE: exit
NOTE: exit
NOTE: When you exit the CLI, you will be asked if you want to reboot
NOTE: the system

Apr 29, 2010

hte: 超棒的 Hex's Editor

hte本身可以做到,hex edit 跟 分析 File formats,但目前使用上有一個缺點,它很多 binding-key 會跟 window manage衝到。



Apr 27, 2010

auto-update tags

工作後,大部份的時間都在 windows 上 Coding,問了蠻多人,大多在 Windows 上寫Coding的人,大多是用 Source Insigne、Ultra Edit。自已本身是用Source Insigne一開始使用就有種,無痛上手的感覺,使用起來,完全沒有門檻,當然也試過 Vim + winCtags,光是要想辨法建tags,就搞了好久,後來就直接放棄。
而自已本身大多還是習慣在Linux的環境下,不外呼,vim + ctags + cscope + Tlist + ........ ,在這多種組合下,用的還算順手,但對於 tag 的 auto update,一直到最近才找到如何處理。

可以利用下面的 vim function,當存檔時,會自助幫你執行 append 新的 tag,但沒辨法利用 set tags 的變數來決定我 tags的位置,目前是直接放棄這個方式。
function! UPDATE_TAGS()
let _f_ = expand("%:p")
let _cmd_ = '"ctags -a -f /dvr/tags --c++-kinds=+p --fields=+iaS --extra=+q " ' . '"' . _f_ . '"'
let _resp = system(_cmd_)
unlet _cmd_
unlet _f_
unlet _resp
endfunction
autocmd BufWritePost *.cpp,*.h,*.c call UPDATE_TAGS()

AutoTag主要是提供一支 autotags.vim的plugin,使用方法很簡單,只要將此plugin放入你.vim/plugin內就可以,而它是使用python來implement auto update的功能,這對只會寫 python 看不懂 vim script在做什麼的我,因該也算是一種好處吧 ,哈哈。

今天,為了試看看,autotags.vim用在大型 project 速度上怎樣,我拿 linux kernel 2.6.31 來測試,做出來的 tags 約 85MB (arch/下只剩 powerpc的目錄),我隨便開啟一個檔案,編輯後存檔,大約花了 5 秒做 auto update的動作(CPU是C2D E6600 @ 2.4G),雖然花了5秒,但沒想像中的那麼慢,或許之後,可以思考如何去切而不是整個都包到同一個 tags 內。

在autotags.vim裡,有一個變數 __maxTagsFileSize 用來限定,tags的大小,預設是 7MB,如果你的 tags 超過這個大小,會直接放棄 update 的動作,當然,可以手動把它設大一點 :)。

除了 ctags,cscope也是一個很好用的 source browsing,支援比ctags更多的功能,但它的 cscope.out ,好像不支援 append 的功能,如要更新,要全部重做一次。Automatically create and update cscope database提供了一組 Hot-Key,來幫忙不用退出vim,"一指"完成。
Hot-Key如下:
nmap :!find . -iname '*.c' -o -iname '*.cpp' -o -iname '*.h' -o -iname '*.hpp' > cscope.files
\ :!cscope -b -i cscope.files -f cscope.out
\ :cs reset