Sunday, June 17, 2012

Assembly analysis (Part 1)

Recently my friend Yan posted something about static function:



And people start discussing about the performance different between this with the normal switch statement. And some saying that complier should be smart enough to optimize that. A simple tests showing that the runtime different seems negotiable.

To see how complier handle that, the best way to do is going to see the assembly. I rewrite the program in C because it is easier to map C code with assembly. (source code : https://github.com/johnlcf/test/downloads)

# gcc -g -o f1 f1.c
# objdump -d -M intel -S f1

Use static function:

0000000000400514 <foo>:

void foo(int i) {
  400514: 55                    push   rbp
  400515: 48 89 e5              mov    rbp,rsp
  400518: 48 83 ec 10           sub    rsp,0x10
  40051c: 89 7d fc              mov    DWORD PTR [rbp-0x4],edi
    static void (*lookup[])() = {zero, one, two, three, four};
    lookup[i]();
  40051f: 8b 45 fc              mov    eax,DWORD PTR [rbp-0x4]
  400522: 48 98                 cdqe   
  400524: 48 8b 14 c5 20 0a 60  mov    rdx,QWORD PTR [rax*8+0x600a20]
  40052b: 00 
  40052c: b8 00 00 00 00        mov    eax,0x0
  400531: ff d2                 call   rdx
}
  400533: c9                    leave  
  400534: c3                    ret 

# gcc -g -o f2 f2.c
# objdump -d -M intel -S f2
Use switch statement:
0000000000400514 <foo>:

void foo(int i) {
  400514: 55                    push   rbp
  400515: 48 89 e5              mov    rbp,rsp
  400518: 48 83 ec 10           sub    rsp,0x10
  40051c: 89 7d fc              mov    DWORD PTR [rbp-0x4],edi
    switch (i) 
  40051f: 83 7d fc 04           cmp    DWORD PTR [rbp-0x4],0x4
  400523: 77 48                 ja     40056d <foo+0x59>
  400525: 8b 45 fc              mov    eax,DWORD PTR [rbp-0x4]
  400528: 48 8b 04 c5 a8 06 40  mov    rax,QWORD PTR [rax*8+0x4006a8]
  40052f: 00 
  400530: ff e0                 jmp    rax
    {
        case 0:
            zero();
  400532: b8 00 00 00 00        mov    eax,0x0
  400537: e8 88 ff ff ff        call   4004c4 <zero>
            break;
  40053c: eb 2f                 jmp    40056d <foo+0x59>
        case 1:
            one();
  40053e: b8 00 00 00 00        mov    eax,0x0
  400543: e8 8c ff ff ff        call   4004d4 <one>
            break;
  400548: eb 23                 jmp    40056d <foo+0x59>
        case 2:
            two();
  40054a: b8 00 00 00 00        mov    eax,0x0
  40054f: e8 90 ff ff ff        call   4004e4 <two>
            break;
  400554: eb 17                 jmp    40056d <foo+0x59>
        case 3:
            three();
  400556: b8 00 00 00 00        mov    eax,0x0
  40055b: e8 94 ff ff ff        call   4004f4 <three>
            break;
  400560: eb 0b                 jmp    40056d <foo+0x59>
        case 4:
            four();
  400562: b8 00 00 00 00        mov    eax,0x0
  400567: e8 98 ff ff ff        call   400504 <four>
            break;
  40056c: 90                    nop
    }
}
  40056d: c9                    leave  
  40056e: c3                    ret    

It is interesting to see that actually gcc optimize the switch statement with a similar approach, which called "jump tables".

// Check  if i > 4, if yes, jump to the end of switch statement
  40051f: 83 7d fc 04           cmp    DWORD PTR [rbp-0x4],0x4
  400523: 77 48                 ja     40056d <foo+0x59>
// rax = i * (pointer size: 8) + (the start address of "jump table: 0x400678)
  400525: 8b 45 fc              mov    eax,DWORD PTR [rbp-0x4]
  400528: 48 8b 04 c5 a8 06 40  mov    rax,QWORD PTR [rax*8+0x4006a8]
  40052f: 00 
// jump to there
  400530: ff e0                 jmp    rax


And the jump table is already initialized with the address of the "cases": 400532, 40053c, 400546, 400550. (not shown here).

Furthermore, I find a gcc option that can tune the behaviour: -fno-jump-tables. Here is the assembly after using that option with switch statement, which do a little bit more compare (cmp) and jump-if-equal (eq).


# gcc -g -fno-jump-tables -o f2_no_jump_tables f2.c
# objdump -d -M intel -S f2

 
0000000000400514 <foo>:

void foo(int i) {
  400514: 55                    push   rbp
  400515: 48 89 e5              mov    rbp,rsp
  400518: 48 83 ec 10           sub    rsp,0x10
  40051c: 89 7d fc              mov    DWORD PTR [rbp-0x4],edi
    switch (i) 
  40051f: 8b 45 fc              mov    eax,DWORD PTR [rbp-0x4]
  400522: 83 f8 02              cmp    eax,0x2
  400525: 74 34                 je     40055b <foo+0x47>
  400527: 83 f8 02              cmp    eax,0x2
  40052a: 7f 0b                 jg     400537 <foo+0x23>
  40052c: 85 c0                 test   eax,eax
  40052e: 74 13                 je     400543 <foo+0x2f>
  400530: 83 f8 01              cmp    eax,0x1
  400533: 74 1a                 je     40054f <foo+0x3b>
  400535: eb 47                 jmp    40057e <foo+0x6a>
  400537: 83 f8 03              cmp    eax,0x3
  40053a: 74 2b                 je     400567 <foo+0x53>
  40053c: 83 f8 04              cmp    eax,0x4
  40053f: 74 32                 je     400573 <foo+0x5f>
  400541: eb 3b                 jmp    40057e <foo+0x6a>
    {
        case 0:
            zero();
  400543: b8 00 00 00 00        mov    eax,0x0
  400548: e8 77 ff ff ff        call   4004c4 <zero>
            break;
  40054d: eb 2f                 jmp    40057e <foo+0x6a>
        case 1:
            one();
  40054f: b8 00 00 00 00        mov    eax,0x0
  400554: e8 7b ff ff ff        call   4004d4 <one>
            break;
  400559: eb 23                 jmp    40057e <foo+0x6a>
        case 2:
            two();
  40055b: b8 00 00 00 00        mov    eax,0x0
  400560: e8 7f ff ff ff        call   4004e4 <two>
            break;
  400565: eb 17                 jmp    40057e <foo+0x6a>
        case 3:
            three();
  400567: b8 00 00 00 00        mov    eax,0x0
  40056c: e8 83 ff ff ff        call   4004f4 <three>
            break;
  400571: eb 0b                 jmp    40057e <foo+0x6a>
        case 4:
            four();
  400573: b8 00 00 00 00        mov    eax,0x0
  400578: e8 87 ff ff ff        call   400504 <four>
            break;
  40057d: 90                    nop
    }
}
  40057e: c9                    leave  
  40057f: c3                    ret   

But actually it has done a (may be) simpler optimization. It use 2 as a pivot, after compare if it is 2, it will check if it is greater than 2 or less than 2.

After these analysis, I believed that the runtime of f1 is similar to that of f2, and f2_no_jump_tables would be a little bit slower than them. But when I test it:


$ time ./f1 > /dev/null 
real 0m0.349s
user 0m0.344s
sys 0m0.003s

$ time ./f2 > /dev/null 
real 0m0.272s
user 0m0.270s
sys 0m0.001s

$ time ./f2_no_jump_tables > /dev/null 
real 0m0.287s
user 0m0.285s
sys 0m0.001s

I run it a few times and got similar result. It is quite strange that f1 is significantly slower on my machine, while we already saw that it has similar assembly code in f2. I have to dive deeper and find out the truth (as far as I know) behind this. I will write a part 2 for this.

The more you know, the more you realize you don't know...

Tuesday, February 1, 2011

How to copy a 5G file to large number of servers in shortest time?

One day my ex-colleague asked me this question. It seems like a common interview question. I don't know the best answer (if exist) but I would like to share my solution and my view point.

First I share some common answer and their problem:

1. A tree-type algorithm:

First round: Copy the file to 2nd server.
2nd round: Copy the file from 1st server to 3rd server AND from 2nd server to 4th server at the same time.
3rd round: 1->5, 2->6, 3->7, 4->8
And so on.

So log2(N) rounds of file transfer can sync the file on N systems. Assume t is the time to transfer 5G file over network, it will take log2(N) * t.

It is nice from algorithm point of view. But in real system, if the 5G file cannot fit in the system cache, it will be re-read from disk for N times on 1st system, which can be slow. (Ref. Numbers Everyone Should Know)

2. "Install BT and let BT do it" solution

It can be a simple answer for a lazy guy and a very complex real environment. But actually BT would do many thing that is unnecessary for this simple task.

My solution: A pipeline streaming solution

Stream the file from 1st to 2nd system (e.g. use nc). And stream the file from 2nd system to 3rd system at the same time and so on.

In theory, it will take N*(initial transfer delay) + t. (see the graph below)
And the biggest benefit is, the part of the file that the system operate on will be most likely in the system cache when it is writing and reading from disk, which will greatly increase the efficiency.

Here is a graph to illustrate. The x-axis is the time. The upper part is the tree-algorithm and the lower part is the streaming solution:


Moreover, if the program can utilize zero-copy (A good reference from IBM: Efficient data transfer through zero copy), it may be even faster.

But one major problem of this solution is that if one node is broken, the transfer to the node after it will be affected.

But frankly I haven't test it in real environment yet. I would like to know how it would perform in real environment.

Thursday, January 13, 2011

pipe, split, rotate

Recently I have to strace to monitor a long-running daemon. strace will generate a lot of output but it doesn't have a built-in way to split the output into files. At first, I have a few idea to solve the problem:

1. "split" command can split the output to files but it has a pre-set limit. For example, default it names the output file with suffix "aa", "ab"...., "zz" and then it will stop. You can increase suffix length but it will eventually stop.

2. Normal "logrotate" method would not work because strace will not accept SIGHUP (like httpd does) to close and open the log file.

3. Use a cronjob to "stop, rotate file and restart strace" - it will lost the trace between the stop and restart of strace.

I then try to write my own program to read the input and write to file and rotate. But after I finished, I search on web and find an existing program which can solve my problem: "rotatelogs". It comes with httpd package.

For example:

# strace -f -t -p | rotatelogs output_log 86400


It will write the output to output_log and rotate it every 86400s (24hrs). You can also specify the size of each output file.

Monday, June 14, 2010

Fedora 12 + SELinux + Firefox 3.6 + Sun Java plugin

Because of a legacy application, I need to install Sun Java plugin in my Fedora 12 system. There is a few tricks worth notice:

1. After Firefox 3.6, it doesn't support the original Java plugin format OJI and only support the standard NPAPI and NPRuntime interfaces that come with Sun Java 6 update 10 or later.

Ref. www.java.com/en/download/faq/firefox_newplugin.xml

You have to remove all libjavaplugin_oji.so link from firefox plugin directories (e.g. /usr/lib/mozilla/plugins, /usr/lib/firefox/plugins, ~/.mozilla/plugins, etc) and create this new link:

# ln -s /usr/java/jdk1.6.0_18/jre/lib/i386/libnpjp2.so libnpjp2.so


And restart firefox to take effect.

2.If you SELinux is disabled, it is all you need. But if SELinux is enabled, you will get this message when you start firefox in shell:

#./firefox
LoadPlugin: failed to initialize shared library /usr/java/jre1.6.0_20/lib/i386/libnpjp2.so [/usr/java/jre1.6.0_20/lib/i386/libnpjp2.so: cannot enable executable stack as shared object requires: Permission denied]

A quick and dirty way is to clear the "executable stack" flag for the libnpjp2.so:

# execstack -c /usr/java/jdk1.6.0_18/jre/lib/i386/libnpjp2.so


(Don't do execstack on your symbolic link or it will copy the real file to replace the symlink.)

But after further research, I found that it may be related to a policy issue of SELinux. And upgrade selinux-policy would solve the problem. Ref.

https://bugzilla.redhat.com/show_bug.cgi?id=533486

Monday, February 1, 2010

網癮戰爭

引自維基百科:
《網癮戰爭》是中國大陸個人組織製作的網路視訊,整部作品以網路遊戲《魔獸世界》為藍本,由玩家遊戲中人物「出演」,視訊畫面《魔獸世界》里的遊戲場景截取編輯而成。電影主要反映出玩家對於遊戲審批和主流媒體不公正報導的不滿,並調侃電擊治網癮的楊永信。

這裏是Youtube上的版本

看完後,我有很沉重的感覺,上次有這個感覺應該是看six four片段時吧……

雖然主題是惡攪,我也沒有玩過魔獸世界,我一直到很後來才聽說到有關它在中國的風風雨雨,不過這套片所描寫的卻是2009年在社會中發生的種種不平事。片中人物的吶喊,就是他們對這個政府不平的制度下的吶喊。雖然沒有流血,沒有死人,但是這件事和六四是相似的地方是:高官們的權力爭鬥,被害的是小市民,所有的反對聲音被打壓,大家只能在被和諧的環境下默默地生活著。

我建議香港人,都看一看這部電影。雖然可能很多地方不明白(網上有解說),但聽聽內地網民的呼聲。

還有,那些在香港抗議這個抗議那個的人,來大陸吧,這裏才是問題的核心,別浪響時間在香港那個傀儡政府上。孫中山當年也是人在香港,志在中國。各位年青的香港人,放眼中國吧,這可能就是香港的“歴史使命”。

說多了。

Wednesday, October 14, 2009

Convert ogg to mp4

Recently I bought a new G-phone (HTC Hero) which is the actually the first smart phone for me (finally...) I can download a lot of interesting program from Android Market to it.
And one thing I want to do is downloading some training video to the phone so that I can see it in the subway. But it seems that there are no OGG-ready video player in Android Market yet (may be I should write one?). So I have to convert ogg video to mp4.
I search for such application on web but I find the most simple way is using command line:
# ffmpeg -s vga -i video.ogg video.mp4
And the converted video can play on my G-phone seamlessly.
ffmpeg is a famous video libraries for Linux which is available in RPM format in DAG repo.

Friday, July 17, 2009

Google reader public page

I like to use google reader to read blog post and I would share some articles I like. If you are interested, you can have a look:

http://www.google.com/reader/shared/00610968397564927697

Monday, June 8, 2009

ksar: a sar grapher: usage

If you are the first time user of kSar, you can unzip it and start it in Linux by

# unzip kSar-5.0.6.zip
# cd kSar-5.0.6
# sh run.sh

It should able to start the GUI if the java is in your path. If it is not, you can set $JAVA_HOME first. E.g.:

# export JAVA_HOME=/usr/java/jdk1.5.0_08
# sh run.sh

And then you can try everything in the GUI.

But if you are a heavy user of kSar like me, you should learn this:

# sh run.sh -input /var/log/sa/sar13

And there are many others useful command line argument. You should have a look to them by:

# sh run.sh -help

Tuesday, June 2, 2009

ksar: a sar grapher

I use kSar for more then a year in my daily Linux support life. It is really useful (and saved my life for several times). But I haven't see much articles on web that talk about it. So I would like to write a simple introduction by myself.


kSar is a Java-based standalone application, which can read and graph the "sar" file generated by sar from Solaris, Linux, AIX and HP-UX system. Here are some advantage of kSar:

1. Multiple Platform
Because it is Java-based, it can run on many platforms (include Windows, but I have not tested). It is useful for me because I can study the sar files from whatever OS/platfrom on the platform I am using.

2. Dynamic graph + PDF export
It makes use of jfreechart library such that we can zoom-in the graph, while it can also output the graph in pdf, jpg, png and even csv format. It is useful for me to generate reports to customers. Moreover, you can add background (e.g. company logo) in the output graph.

3. Some useful extensions
It is open-source. And I have added several patch to add some common extended statistics on Linux system like "memused with buffer adjusted", stacked memory graph and system restarted marker (add in ksar-5.0.7).

4. Real time statistics
Other then reading existing sar file, it can get the output from command line or ssh command to display real time statistics.

5. Compare two graph
It can load two sar files and view it together for easy comparison, also you can compare two graph in the sar file.


I will talk about the usage of kSar soon.

Thursday, February 19, 2009

“天才”的洞察力

之前有一段時間迷上了魔方(港譯:扭計骰),網上有不少有video的教程,成功完成後很有滿足感。(我現在的最快時間是1分20秒,基本上兩分鐘內可以完成。)在公司更引起年青的同事們一股玩魔方的熱潮。
後來有一個同事順便買了一個兩階(2x2x2)的魔方,但一直放在公司,有人試過要完成它但沒有成功,大家的感覺就是:兩階跟三階不太一樣呀。
今天突然想到要去網上看看兩階的解法,有一個網頁就簡單說了一句:基本上用跟三階類似解法,先對好底層,然後對頂層的角順序。

天!為什麼我沒想到呢?我就把三階魔方用的幾個方法,用在兩階魔方上,很快就完成了。

當我跟別人說這個的時候,他們的反應是:“二階魔方用跟三階類似解法很合理呀。”是的,但是為什麼我就想不到呢?

這讓我想起哥倫布的故事

有時候我會覺得人們眼中的天才,就是常常能洞察到別人還沒有看到,但說出來又很簡單的東西。

Thursday, December 18, 2008

Restart Marker for kSar

I would like to propose a new feature to kSar. I called it "Restart Marker".

Actually the system reboot information is recorded by sar. It would be convenient to show it on kSar and the graph would be much clearer if the graph line is broken when the system restart. The left-end of the gap is the last record before the system restart. And the right-end of the gap is the first record after the system restart. And the time of the marker is the exact time that the system restart.


I have already completed the patch for Linux sar file. As I know, the sar file in other OS should have similar record too but I don't have them in hand. Would anyone please provide me such sar file with system restart record so that I can add this feature for other OS?

You can download the testing ksar here.
The patch for ksar-5.0.6 is here.

Thursday, August 21, 2008

制度與發展

工作了幾年,見過不同公司、不同部門、還有不同地方的管理之後,我開始覺得:一間公司,能有一個相對來說比較能motivate人的制度,就已經很不錯了。因為一間公司的制度,很多時候都會受很多能想到的或意想不到東西影響,例如公司文化,歷史,人事關係,或者只是當權者的好惡等等。受這些因素影響下的制度,很多時不能合理地motivate人,或是把人motivate到不productive的方向去了。

例如:如果一間公司的情況是拍馬屁就能升職加薪,就會把大家motivate去努力拍馬屁,而不是做好工作。

在wikipedia中看到一段有趣的歷史:

鄧小平的另一大成就是恢復了全國大學統一入學考試,即高考。自從1977年恢復高考,31年中,它是中國被最公正公平執行的大學招生制度。高考讓中國大批有能青年有了獲得普遍承認的機會,讓廣大貧困家庭有了另一個改變命運的途徑。當今一些學者認為,鄧小平的理念是,推進科學化的同時推進民主化進程。

(Ref. http://zh.wikipedia.org/w/index.php?title=邓小平&variant=zh-tw)

對香港人來說,考試已經是生命中的一部分,考試中的不公平永遠都是可以批評的話題。但是再看看沒有高考前的中國,就知道為什麼說高考是“中國被最公正公平執行的大學招生制度”:

全國普通高等學校招生入學考試

歷史

高考于1955年設立,在1966年開始的文化大革命期 間被中斷。文革時期,上大學的資格很大程度上由家庭背景而定。全中國實施「自願報名,群眾推薦,領導批准,學校複審」的方法招收具有兩年以上實踐經驗、初 中以上文化程度的工、農、兵上大學。「工農兵學員」上大學的主要任務不是學習,而是「上大學、管大學、用毛澤東思想改造大學」,簡稱「上、管、改」。高考 的廢除破壞了中國十一年的發展教育事業,耽誤了一代人的前程,對中國文明、經濟、生產力與國力的發展造成了嚴重的阻礙。1976年毛澤東去世以後,文化大革命結束,鄧小平上任並恢復了全國高考。

(Ref. http://zh.wikipedia.org/w/index.php?title=全国普通高等学校招生入学考试&variant=zh-tw)

Saturday, August 2, 2008

北京新景象

臨近北京奧運,雖然我覺得北京的奧運氣氛沒有傳媒說得這麼熱烈,但是變化還是有的:

這是北京首都機場的三號航站樓,挺漂亮的,格局有點像香港機場的。(還是所有新建的機場都是一個格局?)

我公司對面的CCTV新大樓,我是看著它起的。有點後悔沒有每天拍一張照片然後合起來成為一個video。
對市民有最大幫助的一定是新建的地鐡線路了。這些照片是在新開的十號線拍的。十號線從西北面的中關村一帶,沿三環往東到三元橋,再往南通過國貿一帶。可以說是連接兩大商業區(中關村一帶和國貿CBD)的重要交通工具。從前兩個區之間的交通非常繁忙,如果你要由北面到CBD,或者由東面到中關村,最少要花一個多小時在車上,現在卻是非常方便。有個同事說從北面的回龍觀到公司,坐十號線省了半個小時,現在可以到KFC吃個早餐才上班。(這讓我想起80年代末香港"搭地下鐵路,話咁快就到"那種廣告。)

還有就是最近的單雙號行車,也對空氣真的有點幫助。照片中那種晴空的感覺,平常只有下雨後才會有一天,第二天又恢復原狀了。但是最近一個星期都能保持這樣的空氣質素。真的讓整個北京都心曠神怡。


Wednesday, July 23, 2008

My favorite comic


PHD Comics is one of my favorite comics because it really showing both the interesting and boring academic life of the graduate students.

Monday, July 21, 2008

kSar Interrupt list

Here is my new patch for kSar: Interrupt list


It was a missing feature of kSar for Linux. And an option is also added to show it in the stacked way.

Sar would capture first 16 interrupts by default. The interrupts are identified by an number, which is known as IRQ. And you can check the mapping between IRQ and the device in /proc/interrupts on the system.

I don't think normal system admin need to investigate IRQ closely. I would like to add this feature mainly because of functional completeness. And it may be useful if you want to monitor the Interrupt rate or binding, for tuning for better system response time (e.g. for a Real time system). For more information about Interrupt binding, you can see here .

Here is the patch for kSar-4.0.14.
Here is the compiled jar with this feature as an option.

Sunday, July 20, 2008

Gift Card

It is interesting that our company have an quite flexible encouraging mechanism "recognition reward". Everyone can nominate any other colleague (usually in another team or in another Geo) to have a reward because of his/her help on their work. The reward includes a paper certificate with our marketing souvenir for a help, or a USD50 gift card (Debt card) for a big help.

Recently I got a USD50 gift card because I help the customer services team to translate some Chinese customer email when their Chinese staff is on a trip.

Then the problem comes, how to spend the USD gift card in China? I think the company who can receive Visa card can use this. And my colleagues, who have received this card before, share that they can use this card in Super Market in China, like Walmart or Carrefour.

Reference:
Utopia de Fai: Reward

中國特色雪糕

生活在中國內地,每天都會見到不少有中國內地特色的事情。

今天在超市看到一款中國有名的雪糕品牌出的“三條裝”雪條:


有沒有發現有什麼特別?三條裝的雪條是兩大一小的。再看看價錢牌上說明這是“家庭裝”。香港的“家庭裝”東西一般是四件包裝的,但大陸的一孩政策下,家庭一般是兩大一小,公司這樣的包裝也真夠體貼。

伸延閱讀:
《家有儿女》的平常幸福已不平常

Thursday, June 26, 2008

Because it's fun

Recently I read an Paper written by Michael Tiemann, it quoted a survey result for the over 490,000 open source developers on sourceforge.com about the reason of involving in open source software:
The three top reasons they list for their involvement is:
1. Because it's fun
2. Because it improves their skills
3. Because it is good for society
It is interesting. Usually we mention that the contribution of open source software to the world and how meaningful it is. But people usually won't do something just because it is meaningful (i.e. only few people work for charity), people usually do something because it is fun. People even willing to pay for fun!

When someone talk about Software Engineering, with troublesome documentation, processes and procedures... one will reply "well, I will do it because I know it is useful, I know I know..." but it is not fun at all. So people would do that as a job, as a part of their earn for living, without passion and motivation to improve it.

That's why open source is different. That's why people would like to spend their spare time to development software for free (Remember, people even willing to pay for fun!). That's why people in open source are full of passion and motivated. Because it's fun!

Friday, June 20, 2008

Stacked memused on kSar

PS. My patch for memused (buffer adjusted) is accepted by kSar and released in kSar-4.0.14. Thank you for the developer Alexandre Cherif and all others support!

Here is my second patch for kSar: Stacked memory graph.



It added a new option to stack the following together into one graph:
  • memused (buffer adjusted)
  • buffers
  • cached
  • free
  • swap used
When all these information are plotted in different graphs, they all have different scale. It is good if you just want to view particular piece of information, but it is difficult for comparison. Sometimes it would be misleading that a large fluctuation on one graph actually mean a change in range of a few kbytes, which is just a small ripple on another graph.

Currently I just implemented this feature on Linux part of kSar. I am not sure if it is useful for other OS such as AIX, HPUX or Solaris. If someone think it is good to have this feature on other OS, please contact me and tell me how to "stack" the memory info of those OS. :-)

Here is the patch for kSar-4.0.14.
Here is the compiled jar with this feature as an option.

Friday, May 23, 2008

kSar and Memory used (buffer adjusted)

Recently I found a sar graphing tool that I dreamed for more then a year: kSar

It is really a good tool that can view the sar output and export it to PNG or PDF. Really useful for me to do system status reporting for my customers.

And I can't help myself to add my dream feature to it. Here is the first one: Memory used (buffer adjusted).



In Linux system, the "memory used" value in sar (and also other tools like free) included the (1) memory used by the applications AND (2) the memory used by buffer and cache. Part 1 is usually sysadmin concern about. Part 2 is handled by the kernel automatically and it will be freed if the Part 1 increase. So in some tools like free, they will calculate a "buffer adjusted" by memused - cached - buffers. (For more information, see here or the man page of free)

This is one of the most common FAQ and I have to do the calculation on the sar data every time I want to the memory used by application. That's why I create such a patch for kSar.

Here is the patch for kSar-4.0.10: http://johnlcf.googlepages.com/kSar-4.0.10_bufferadj.patch
Here is the compiled kSar.jar with this feature as an option: http://johnlcf.googlepages.com/kSar.jar