Linux Daemon Writing HOWTO

[不指定 2010/05/06 23:02 | by ipaddr ]
                   

Linux Daemon Writing HOWTO

Devin Watson

v1.0, May 2004


This document shows how to write a daemon in Linux using GCC. Knowledge of Linux and a familiarity with C are necessary to use this document. This HOWTO is Copyright by Devin Watson, under the terms of the BSD License.


1. Introduction: What is a Daemon?

2. Getting Started

3. Planning Your Daemon

  • 3.1   What Is It Going To Do?  

     
  • 3.2 How Much Interaction?  

4. Basic Daemon Structure

  • 4.1   Forking The Parent Process  

     
  • 4.2   Changing The File Mode Mask (Umask)  

     
  • 4.3   Opening Logs For Writing  

     
  • 4.4   Creating a Unique Session ID (SID)  

     
  • 4.5   Changing The Working Directory  

     
  • 4.6 Closing Standard File   Descriptors  

5. Writing the Daemon Code

  • 5.1   Initialization  

     
  • 5.2 The Big Loop  

6. Putting It All Together

  • 6.1 Complete Sample  


1. Introduction: What is a Daemon?

A daemon (or service) is a background process that is designed to run autonomously,with little or not user intervention. The Apache web server http daemon (httpd) is one such example of a daemon. It waits in the background listening on specific ports, and serves up pages or processes scripts, based on the type of request.

Creating a daemon in Linux uses a specific set of rules in a given order. Knowing how they work will help you understand how daemons operate in userland Linux, but can operate with calls to the kernel also. In fact, a few daemons interface with kernel modules that work with hardware devices, such as external controller boards, printers,and PDAs. They are one of the fundamental building blocks in Linux that give it incredible flexibility and power.

Throughout this HOWTO, a very simple daemon will be built in C. As we go along, more code will be added, showing the proper order of execution required to get a daemon up and running.

2. Getting Started

First off, you'll need the following packages installed on your Linux machine to develop daemons, specifically:

  • GCC   3.2.2 or higher  

     
  • Linux   Development headers and libraries  

If your system does not already have these installed (not likely, but check anyway), you'll need them to develop the examples in this HOWTO. To find out what version of GCC you have installed, use:

        gcc --version

3. Planning Your Daemon

3.1 What Is It Going To Do?

A daemon should do one thing, and do it well. That one thing may be as complex as managing hundreds of mailboxes on multiple domains, or as simple as writing a report and calling sendmail to mail it out to an admin.

In any case, you should have a good plan going in what the daemon should do. If it is going to interoperate with some other daemons that you may or may not be writing, this is something else to consider as well.

3.2 How Much Interaction?

Daemons should never have direct communication with a user through a terminal. In fact, a daemon shouldn't communicate directly with a user at all. All communication should pass through some sort of interface (which you may or may not have to write), which can be as complex as a GTK+ GUI, or as simple as a signal set.

4. Basic Daemon Structure

When a daemon starts up, it has to do some low-level housework to get itself ready for its real job. This involves a few steps:

  • Fork   off the parent process  

     
  • Change   file mode mask (umask)  

     
  • Open   any logs for writing  

     
  • Create   a unique Session ID (SID)  

     
  • Change   the current working directory to a safe place  

     
  • Close   standard file descriptors  

     
  • Enter actual daemon code  

4.1 Forking The Parent Process

A daemon is started either by the system itself or a user in a terminal or script. When it does start, the process is just like any other executable on the system. To make it truly autonomous, a child process must be created where the actual code is executed. This is known as forking, and it uses the fork() function:

        pid_t pid;

        /* Fork off the parent process */      
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

Notice the error check right after the call to fork(). When writing a daemon, you will have to code as defensively as possible. In fact, a good percentage of the total code in a daemon consists of nothing but error checking.

The fork() function returns either the process id (PID) of the child process (not equal to zero), or -1 on failure. If the process cannot fork a child, then the daemon should terminate right here.

If the PID returned from fork() did succeed, the parent process must exit gracefully. This may seem strange to anyone who hasn't seen it, but by forking, the child process continues the execution from here on out in the code.

4.2 Changing The File Mode Mask (Umask)

In order to write to any files (including logs) created by the daemon, the file mode mask (umask) must be changed to ensure that they can be written to or read from properly. This is similar to running umask from the command line, but we do it programmatically here. We can use the umask() function to accomplish this:

        pid_t pid, sid;
       
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                /* Log failure (use syslog if possible) */
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);

By setting the umask to 0, we will have full access to the files generated by the daemon. Even if you aren't planning on using any files, it is a good idea to set the umask here anyway, just in case you will be accessing files on the filesystem.

4.3 Opening Logs For Writing

This part is optional, but it is recommended that you open a log file somewhere in the system for writing. This may be the only place you can look for debug information about your daemon.

4.4 Creating a Unique Session ID (SID)

From here, the child process must get a unique SID from the kernel in order to operate. Otherwise, the child process becomes an orphan in the system. The pid_t type, declared in the previous section, is also used to create a new SID for the child process:

        pid_t pid, sid;
       
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }
       
        /* Change the file mode mask */
        umask(0);
       
        /* Open any logs here */
       
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log any failure */
                exit(EXIT_FAILURE);
        }

Again, the setsid() function has the same return type as fork(). We can apply the same error-checking routine here to see if the function created the SID for the child process.

4.5 Changing The Working Directory

The current working directory should be changed to some place that is guaranteed to always be there. Since many Linux distributions do not completely follow the Linux Filesystem Hierarchy standard, the only directory that is guaranteed to be there is the root (/). We can do this using the chdir() function:

        pid_t pid, sid;
       
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);      
       
        /* Open any logs here */       
               
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log any failure here */
                exit(EXIT_FAILURE);
        }
       
        /* Change the current working directory */
        if ((chdir("/")) < 0) {
                /* Log any failure here */
                exit(EXIT_FAILURE);
        }

Once again, you can see the defensive coding taking place. The chdir() function returns -1 on failure, so be sure to check for that after changing to the root directory within the daemon.

4.6 Closing Standard File Descriptors

One of the last steps in setting up a daemon is closing out the standard file descriptors (STDIN, STDOUT, STDERR). Since a daemon cannot use the terminal, these file descriptors are redundant and a potential security hazard.

The close() function can handle this for us:

          pid_t pid, sid;
       
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }
       
        /* Change the file mode mask */
        umask(0);      
       
        /* Open any logs here */
       
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log any failure here */
                exit(EXIT_FAILURE);
        }
       
        /* Change the current working directory */
        if ((chdir("/")) < 0) {
                /* Log any failure here */
                exit(EXIT_FAILURE);
        }
       
       
        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

It's a good idea to stick with the constants defined for the file descriptors, for the greatest portability between system versions.

5. Writing the Daemon Code

5.1 Initialization

At this point, you have basically told Linux that you're a daemon, so now it's time to write the actual daemon code. Initialization is the first step here. Since there can be a multitude of different functions that can be called here to set up your daemon's task, I won't go too deep into here.

The big point here is that, when initializing anything in a daemon, the same defensive coding guidelines apply here. Be as verbose as possible when writing either to the syslog or your own logs. Debugging a daemon can be quite difficult when there isn't enough information available as to the status of the daemon.

5.2 The Big Loop

A daemon's main code is typically inside of an infinite loop. Technically, it isn't an infinite loop, but it is structured as one:

        pid_t pid, sid;
       
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);      
       
        /* Open any logs here */
       
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log any failures here */
                exit(EXIT_FAILURE);
        }
       
       
        /* Change the current working directory */
        if ((chdir("/")) < 0) {
                /* Log any failures here */
                exit(EXIT_FAILURE);
        }
       
        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
       
        /* Daemon-specific initialization goes here */
       
        /* The Big Loop */
        while (1) {
           /* Do some task here ... */
           sleep(30); /* wait 30 seconds */
        }

This typical loop is usually a while loop that has an infinite terminating condition, with a call to sleep in there to make it run at specified intervals.

Think of it like a heartbeat: when your heart beats, it performs a few tasks, then waits until the next beat takes place. Many daemons follow this same methodology.

6. Putting It All Together

6.1 Complete Sample

Listed below is a complete sample daemon that shows all of the steps necessary for setup and execution. To run this, simply compile using gcc, and start execution from the command line. To terminate, use the kill command after finding its PID.

I've also put in the correct include statements for interfacing with the syslog, which is recommended at the very least for sending start/stop/pause/die log statements, in addition to using your own logs with the fopen()/fwrite()/fclose() function calls.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>

int main(void) {
       
        /* Our process ID and Session ID */
        pid_t pid, sid;
       
        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
                exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);
               
        /* Open any logs here */       
               
        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }
       

       
        /* Change the current working directory */
        if ((chdir("/")) < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }
       
        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
       
        /* Daemon-specific initialization goes here */
       
        /* The Big Loop */
        while (1) {
           /* Do some task here ... */
          
           sleep(30); /* wait 30 seconds */
        }
   exit(EXIT_SUCCESS);
}

From here, you can use this skeleton to write your own daemons. Be sure to add in your own logging (or use the syslog facility), and code defensively, code defensively, code defensively!



以下为转载:

     昨天ie8正式发布了,偶也去下载了一个,感觉很爽, 还在美的时候,突然发现很多网页都出问题,更可气的是自己的网站编辑器也不管用了,所有的按钮都不管用,即使是别的浏览器也都不管用,我这叫一个汗。于是就去360论坛找人问,因为我用的360浏览器以为是360浏览器的问题。问了半天也没有人回答我于是我就去网上找,由于今天才出的ie8所以网上根本也没有这方面的解决办法,于是到了第二天,网上终于有了这样的贴子,所以我也找了找解决办法。

     在一个哥们的贴子中是这样写的,打开include下面的editor.js文件,有这样的段代码:

     if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");

    说是因为ie8屏蔽了anonymous方法 所以要改成click方法,于是就改成这样,但是我又有了一个惊人的发现,那就是改成这样之后在ie7下编辑器就不管用了,痛苦之余我又去网上查,网上果然高水如云,果然在csdn上找到了答案:

if(navigator.appVersion.match(/8./i)=='8.')
    {
      if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "onclick(event)");   
   }
else

   {
     if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");
}

把代码改成这样之后在ie7和ie8下就都管用了。

以下为鱼漂优化后的代码:
上面的代码,在类似遨游之类的IE内核浏览器就无法工作了,因为遨游修改了UserAgent。为简化浏览器的判断,鱼漂优化了一下这个代码,分别执行这两条语句,不过用try catch捕获一下异常,以免中断执行:
 try {
  if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "onclick(event)");
 } catch (err) {
 }
 try {
  if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");
 } catch (err) {
 }

除此之外,ewebeditor的浏览器兼容性做得比较差,个人强烈推荐FCKEditor。


近日某同事在用java发布一个API时,使用Json进行数据编码,发现JSON官方的java扩展在处理汉字时未进行ascii转义,如下所示:
Java源码:

AppObject ao=new AppObject(20,"鱼漂");
try {
JSONObject jo=new JSONObject(ao);
System.out.println(jo.toString());
} catch (Exception e) {
System.out.println("json encode error.");
}

输出为:
{"name":"鱼漂","age":10}

其中的name仍然显示中文,未进行ascii转义,通常情况下是可以正常使用的,但在不同的系统互相调用时,可能需要编码转换,比如从GBK转到UTF8。

通过查看源代码发现,在JSONObject.java的1138-1139行,只将\u0080-\u00a0, \u2000-\u2100的字符转成\u的格式。汉字的unicode编码不在这两个范围之内,所以不会转换。

找到原因后,修改就比较简单,我们只需简单的将\u0080以上的所有字符,转成\u格式即可,将1138-1139行改为:
if (c < ' ' || c >= '\u0080') {

再次测试,输出结果为:
{"name":"\u9c7c\u6f02","age":10}

注:
1. JSON官方的java扩展可以在 http://www.json.org/java/ 找到
2. PHP自带的json扩展没此问题
3. 本文原载于 http://www.eit.name/
4. 关于汉字在unicode的位段,可参考前一篇转载的博文.

  因为FontRouter新版本开发的需要,在网上搜索了一下汉字的Unicode范围,普遍给出了“U+4E00..U+9FA5”。但事实上这个范围是不完整的,甚至连基本的全角(中文)标点也未包含在内。为此,我特地查询了Unicode官方的Code Charts数据库,并根据最新的Unicode 5.0版整理如下:

注:在绝大多数应用场合中,我们可以仅用(1)、(2)、(3)、(4)、(5)的集合作为CJK判断的依据。

1)标准CJK文字

http://www.unicode.org/Public/UNIDATA/Unihan.html

Code point rangeBlock nameRelease
U+3400..U+4DB5CJK Unified Ideographs Extension A3.0
U+4E00..U+9FA5CJK Unified Ideographs1.1
U+9FA6..U+9FBBCJK Unified Ideographs4.1
U+F900..U+FA2DCJK Compatibility Ideographs1.1
U+FA30..U+FA6ACJK Compatibility Ideographs3.2
U+FA70..U+FAD9CJK Compatibility Ideographs4.1
U+20000..U+2A6D6CJK Unified Ideographs Extension B3.1
U+2F800..U+2FA1DCJK Compatibility Supplement3.1

2)全角ASCII、全角中英文标点、半宽片假名、半宽平假名、半宽韩文字母:FF00-FFEF

http://www.unicode.org/charts/PDF/UFF00.pdf

3)CJK部首补充:2E80-2EFF

http://www.unicode.org/charts/PDF/U2E80.pdf

4)CJK标点符号:3000-303F

http://www.unicode.org/charts/PDF/U3000.pdf

5)CJK笔划:31C0-31EF

http://www.unicode.org/charts/PDF/U31C0.pdf

6)康熙部首:2F00-2FDF

http://www.unicode.org/charts/PDF/U2F00.pdf

7)汉字结构描述字符:2FF0-2FFF

http://www.unicode.org/charts/PDF/U2FF0.pdf

8)注音符号:3100-312F

http://www.unicode.org/charts/PDF/U3100.pdf

9)注音符号(闽南语、客家语扩展):31A0-31BF

http://www.unicode.org/charts/PDF/U31A0.pdf

10)日文平假名:3040-309F

http://www.unicode.org/charts/PDF/U3040.pdf

11)日文片假名:30A0-30FF

http://www.unicode.org/charts/PDF/U30A0.pdf

12)日文片假名拼音扩展:31F0-31FF

http://www.unicode.org/charts/PDF/U31F0.pdf

13)韩文拼音:AC00-D7AF

http://www.unicode.org/charts/PDF/UAC00.pdf

14)韩文字母:1100-11FF

http://www.unicode.org/charts/PDF/U1100.pdf

15)韩文兼容字母:3130-318F

http://www.unicode.org/charts/PDF/U3130.pdf

16)太玄经符号:1D300-1D35F

http://www.unicode.org/charts/PDF/U1D300.pdf

17)易经六十四卦象:4DC0-4DFF

http://www.unicode.org/charts/PDF/U4DC0.pdf

18)彝文音节:A000-A48F

http://www.unicode.org/charts/PDF/UA000.pdf

19)彝文部首:A490-A4CF

http://www.unicode.org/charts/PDF/UA490.pdf

20)盲文符号:2800-28FF

http://www.unicode.org/charts/PDF/U2800.pdf

21)CJK字母及月份:3200-32FF

http://www.unicode.org/charts/PDF/U3200.pdf

22)CJK特殊符号(日期合并):3300-33FF

http://www.unicode.org/charts/PDF/U3300.pdf

23)装饰符号(非CJK专用):2700-27BF

http://www.unicode.org/charts/PDF/U2700.pdf

24)杂项符号(非CJK专用):2600-26FF

http://www.unicode.org/charts/PDF/U2600.pdf

25)中文竖排标点:FE10-FE1F

http://www.unicode.org/charts/PDF/UFE10.pdf

26)CJK兼容符号(竖排变体、下划线、顿号):FE30-FE4F

http://www.unicode.org/charts/PDF/UFE30.pdf

以上翻译自Unicode官方网站,部分译法可能不够准确,还望大家予以指正!如有疏漏、错误之处也请一并指出,多谢!

JVM调优总结

[不指定 2010/01/12 17:26 | by ipaddr ]
  1. 堆大小设置
    JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制;系统的可用虚拟内存限制;系统的可用物理内存限制。32位系统下,一般限制在1.5G~2G;64为操作系统对内存无限制。我在Windows Server 2003 系统,3.5G物理内存,JDK5.0下测试,最大可设置为1478m。
    典型设置:
    • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k
      -
      Xmx3550m:设置JVM最大可用内存为3550M。
      -Xms3550m
      :设置JVM促使内存为3550m。此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存。
      -Xmn2g
      :设置年轻代大小为2G。整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小。持久代一般固定大小为64m,所以增大年轻代后,将会减小年老代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8。
      -Xss128k
      :设置每个线程的堆栈大小。JDK5.0以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K。更具应用的线程所需内存大小进行调整。在相同物理内存下,减小这个值能生成更多的线程。但是操作系统对一个进程内的线程数还是有限制的,不能无限生成,经验值在3000~5000左右。
    • java -Xmx3550m -Xms3550m -Xss128k -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxPermSize=16m -XX:MaxTenuringThreshold=0
      -XX:NewRatio=4
      :设置年轻代(包括Eden和两个Survivor区)与年老代的比值(除去持久代)。设置为4,则年轻代与年老代所占比值为1:4,年轻代占整个堆栈的1/5
      -XX:SurvivorRatio=4
      :设置年轻代中Eden区与Survivor区的大小比值。设置为4,则两个Survivor区与一个Eden区的比值为2:4,一个Survivor区占整个年轻代的1/6
      -XX:MaxPermSize=16m:设置持久代大小为16m。
      -XX:MaxTenuringThreshold=0:设置垃圾最大年龄。如果设置为0的话,则年轻代对象不经过Survivor区,直接进入年老代。对于年老代比较多的应用,可以提高效率。如果将此值设置为一个较大值,则年轻代对象会在Survivor区进行多次复制,这样可以增加对象再年轻代的存活时间,增加在年轻代即被回收的概论。
  2. 回收器选择
    JVM给了三种选择:串行收集器、并行收集器、并发收集器,但是串行收集器只适用于小数据量的情况,所以这里的选择主要针对并行收集器和并发收集器。默认情况下,JDK5.0以前都是使用串行收集器,如果想使用其他收集器需要在启动时加入相应参数。JDK5.0以后,JVM会根据当前系统配置进行判断。
    1. 吞吐量优先的并行收集器
      如上文所述,并行收集器主要以到达一定的吞吐量为目标,适用于科学技术和后台处理等。
      典型配置
      • java -Xmx3800m -Xms3800m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20
        -XX:+UseParallelGC
        :选择垃圾收集器为并行收集器。此配置仅对年轻代有效。即上述配置下,年轻代使用并发收集,而年老代仍旧使用串行收集。
        -XX:ParallelGCThreads=20:配置并行收集器的线程数,即:同时多少个线程一起进行垃圾回收。此值最好配置与处理器数目相等。
      • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseParallelOldGC
        -XX:+UseParallelOldGC:配置年老代垃圾收集方式为并行收集。JDK6.0支持对年老代并行收集。
      • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:MaxGCPauseMillis=100
        -XX:MaxGCPauseMillis=100:设置每次年轻代垃圾回收的最长时间,如果无法满足此时间,JVM会自动调整年轻代大小,以满足此值。
      • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:MaxGCPauseMillis=100 -XX:+UseAdaptiveSizePolicy
        -XX:+UseAdaptiveSizePolicy
        :设置此选项后,并行收集器会自动选择年轻代区大小和相应的Survivor区比例,以达到目标系统规定的最低相应时间或者收集频率等,此值建议使用并行收集器时,一直打开。
    2. 响应时间优先的并发收集器
      如上文所述,并发收集器主要是保证系统的响应时间,减少垃圾收集时的停顿时间。适用于应用服务器、电信领域等。
      典型配置
      • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC
        -XX:+UseConcMarkSweepGC:设置年老代为并发收集。测试中配置这个以后,-XX:NewRatio=4的配置失效了,原因不明。所以,此时年轻代大小最好用-Xmn设置。
        -XX:+UseParNewGC:设置年轻代为并行收集。可与CMS收集同时使用。JDK5.0以上,JVM会根据系统配置自行设置,所以无需再设置此值。
      • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseConcMarkSweepGC -XX:CMSFullGCsBeforeCompaction=5 -XX:+UseCMSCompactAtFullCollection
        -XX:CMSFullGCsBeforeCompaction:由于并发收集器不对内存空间进行压缩、整理,所以运行一段时间以后会产生“碎片”,使得运行效率降低。此值设置运行多少次GC以后对内存空间进行压缩、整理。
        -XX:+UseCMSCompactAtFullCollection:打开对年老代的压缩。可能会影响性能,但是可以消除碎片
  3. 辅助信息
    JVM提供了大量命令行参数,打印信息,供调试使用。主要有以下一些:
    • -XX:+PrintGC
      输出形式:[GC 118250K->113543K(130112K), 0.0094143 secs]

      [Full GC 121376K->10414K(130112K), 0.0650971 secs]

    • -XX:+PrintGCDetails
      输出形式:[GC [DefNew: 8614K->781K(9088K), 0.0123035 secs] 118250K->113543K(130112K), 0.0124633 secs]

      [GC [DefNew: 8614K->8614K(9088K), 0.0000665 secs][Tenured: 112761K->10414K(121024K), 0.0433488 secs] 121376K->10414K(130112K), 0.0436268 secs]

    • -XX:+PrintGCTimeStamps -XX:+PrintGC:PrintGCTimeStamps可与上面两个混合使用
      输出形式:11.851: [GC 98328K->93620K(130112K), 0.0082960 secs]
    • -XX:+PrintGCApplicationConcurrentTime:打印每次垃圾回收前,程序未中断的执行时间。可与上面混合使用
      输出形式:Application time: 0.5291524 seconds
    • -XX:+PrintGCApplicationStoppedTime:打印垃圾回收期间程序暂停的时间。可与上面混合使用
      输出形式:Total time for which application threads were stopped: 0.0468229 seconds
    • -XX:PrintHeapAtGC:打印GC前后的详细堆栈信息
      输出形式:
      34.702: [GC {Heap before gc invocations=7:
      def new generation total 55296K, used 52568K [0x1ebd0000, 0x227d0000, 0x227d0000)
      eden space 49152K, 99% used [0x1ebd0000, 0x21bce430, 0x21bd0000)
      from space 6144K, 55% used [0x221d0000, 0x22527e10, 0x227d0000)
      to space 6144K, 0% used [0x21bd0000, 0x21bd0000, 0x221d0000)
      tenured generation total 69632K, used 2696K [0x227d0000, 0x26bd0000, 0x26bd0000)
      the space 69632K, 3% used [0x227d0000, 0x22a720f8, 0x22a72200, 0x26bd0000)
      compacting perm gen total 8192K, used 2898K [0x26bd0000, 0x273d0000, 0x2abd0000)
      the space 8192K, 35% used [0x26bd0000, 0x26ea4ba8, 0x26ea4c00, 0x273d0000)
      ro space 8192K, 66% used [0x2abd0000, 0x2b12bcc0, 0x2b12be00, 0x2b3d0000)
      rw space 12288K, 46% used [0x2b3d0000, 0x2b972060, 0x2b972200, 0x2bfd0000)
      34.735: [DefNew: 52568K->3433K(55296K), 0.0072126 secs] 55264K->6615K(124928K)Heap after gc invocations=8:
      def new generation total 55296K, used 3433K [0x1ebd0000, 0x227d0000, 0x227d0000)
      eden space 49152K, 0% used [0x1ebd0000, 0x1ebd0000, 0x21bd0000)
      from space 6144K, 55% used [0x21bd0000, 0x21f2a5e8, 0x221d0000)
      to space 6144K, 0% used [0x221d0000, 0x221d0000, 0x227d0000)
      tenured generation total 69632K, used 3182K [0x227d0000, 0x26bd0000, 0x26bd0000)
      the space 69632K, 4% used [0x227d0000, 0x22aeb958, 0x22aeba00, 0x26bd0000)
      compacting perm gen total 8192K, used 2898K [0x26bd0000, 0x273d0000, 0x2abd0000)
      the space 8192K, 35% used [0x26bd0000, 0x26ea4ba8, 0x26ea4c00, 0x273d0000)
      ro space 8192K, 66% used [0x2abd0000, 0x2b12bcc0, 0x2b12be00, 0x2b3d0000)
      rw space 12288K, 46% used [0x2b3d0000, 0x2b972060, 0x2b972200, 0x2bfd0000)
      }
      , 0.0757599 secs]
    • -Xloggc:filename:与上面几个配合使用,把相关日志信息记录到文件以便分析。
  4. 常见配置汇总
    1. 堆设置
      • -Xms:初始堆大小
      • -Xmx:最大堆大小
      • -XX:NewSize=n:设置年轻代大小
      • -XX:NewRatio=n:设置年轻代和年老代的比值。如:为3,表示年轻代与年老代比值为1:3,年轻代占整个年轻代年老代和的1/4
      • -XX:SurvivorRatio=n:年轻代中Eden区与两个Survivor区的比值。注意Survivor区有两个。如:3,表示Eden:Survivor=3:2,一个Survivor区占整个年轻代的1/5
      • -XX:MaxPermSize=n:设置持久代大小
    2. 收集器设置
      • -XX:+UseSerialGC:设置串行收集器
      • -XX:+UseParallelGC:设置并行收集器
      • -XX:+UseParalledlOldGC:设置并行年老代收集器
      • -XX:+UseConcMarkSweepGC:设置并发收集器
    3. 垃圾回收统计信息
      • -XX:+PrintGC
      • -XX:+PrintGCDetails
      • -XX:+PrintGCTimeStamps
      • -Xloggc:filename
    4. 并行收集器设置
      • -XX:ParallelGCThreads=n:设置并行收集器收集时使用的CPU数。并行收集线程数。
      • -XX:MaxGCPauseMillis=n:设置并行收集最大暂停时间
      • -XX:GCTimeRatio=n:设置垃圾回收时间占程序运行时间的百分比。公式为1/(1+n)
    5. 并发收集器设置
      • -XX:+CMSIncrementalMode:设置为增量模式。适用于单CPU情况。
      • -XX:ParallelGCThreads=n:设置并发收集器年轻代收集方式为并行收集时,使用的CPU数。并行收集线程数。


调优总结

  1. 年轻代大小选择
    • 响应时间优先的应用尽可能设大,直到接近系统的最低响应时间限制(根据实际情况选择)。在此种情况下,年轻代收集发生的频率也是最小的。同时,减少到达年老代的对象。
    • 吞吐量优先的应用:尽可能的设置大,可能到达Gbit的程度。因为对响应时间没有要求,垃圾收集可以并行进行,一般适合8CPU以上的应用。
  2. 年老代大小选择
    • 响应时间优先的应用:年老代使用并发收集器,所以其大小需要小心设置,一般要考虑并发会话率会话持续时间等一些参数。如果堆设置小了,可以会造成内存碎片、高回收频率以及应用暂停而使用传统的标记清除方式;如果堆大了,则需要较长的收集时间。最优化的方案,一般需要参考以下数据获得:
      • 并发垃圾收集信息
      • 持久代并发收集次数
      • 传统GC信息
      • 花在年轻代和年老代回收上的时间比例
      减少年轻代和年老代花费的时间,一般会提高应用的效率
    • 吞吐量优先的应用:一般吞吐量优先的应用都有一个很大的年轻代和一个较小的年老代。原因是,这样可以尽可能回收掉大部分短期对象,减少中期的对象,而年老代尽存放长期存活对象。
  3. 较小堆引起的碎片问题
    因为年老代的并发收集器使用标记、清除算法,所以不会对堆进行压缩。当收集器回收时,他会把相邻的空间进行合并,这样可以分配给较大的对象。但是,当堆空间较小时,运行一段时间以后,就会出现“碎片”,如果并发收集器找不到足够的空间,那么并发收集器将会停止,然后使用传统的标记、清除方式进行回收。如果出现“碎片”,可能需要进行如下配置:
    • -XX:+UseCMSCompactAtFullCollection:使用并发收集器时,开启对年老代的压缩。
    • -XX:CMSFullGCsBeforeCompaction=0:上面配置开启的情况下,这里设置多少次Full GC后,对年老代进行压缩

linux下获取当前目录

[不指定 2010/01/11 23:44 | by ipaddr ]
1. 取得当前工作目录(相当于windows下的GetCurrentDirectory):
  #include "stdio.h"
  #include "stdlib.h"
  #include "string.h"
  #include "unistd.h"
  
  int main()
  {
   char buf[200];
  
   if(getcwd(buf, sizeof(buf)-1) != NULL)
   {
   printf(buf);
   printf("\n");
   }
   else
   {
   printf("error \n");
   }
  
   return 0;
  }

2. 取得实际文件目录(相当于windows下的GetModuleFileName())
每个进程在/proc下都有一个以进程号命名的目录.
在该目录下有exe文件,该文件是一个链接文件,它指向的路径就是该进程的全路径.
用readlink读exe文件返回该进程的全路径
如果不在意可能导致的安全隐患,可以使用procfs,然后readlink,把当前进程的pid对应的目录下面的file指向的位置读出来(注意需要先挂载procfs)
 
  pit_t mypid = getpid();
  sprintf(strsrc, "/proc/%d/file", mypid);
  readlink(strsrc, strdest, LEN);//LEN最好是你的_POSIX_PATH_MAX

所以,如果支持/proc文件系统,下面的代码可以解决你的问题:
 
  #include <stdlib.h>
  #include <unistd.h>
  #ifndef PATH_MAX
  #define PATH_MAX 1024
  #endif
 
  char * get_exe_name() {
  static char buf[PATH_MAX];
  int rslt = readlink("/proc/self/exe", buf, PATH_MAX);
  if ( rslt < 0 || rslt >= PATH_MAX ) {
  return NULL;
  }
  buf[rslt] = '\0';
  return buf;
  }
 
  int main( void)
  {
  char * exe_name = get_exe_name();
  puts( exe_name );
  return 0;
  }
 
 
  如果不支持/proc文件系统,我也没什么好办法了。

推荐十个最好的Java性能故障排除工具:

   1.jconsole是随着JDK 1.5而推出的。这是一个Java监测和管理控制台-JMX兼容的图形工具来监测Java虚拟机。它能够同时监测本地和远程的JVMs。详情可查看:jconsole工具介绍

   2.VisualVM 集成了几个现有的JDK软件工具,轻量级内存和CPU信息概要能力。这个工具被设计为同时在生产和开发时使用,进一步提高监视的能力和Java SE平台的性能分析能力。

   3.HeapAnalyzer  能够通过它采用启发式搜索引擎和分析Java堆栈存储信息发现可能的Java堆栈泄漏区域,它通过解析Java堆栈存储信息,创建定向图表,变换他们成定向树和执行启发式搜索引擎。   

   4.PerfAnal 是在Java 2平台上为了分析应用表现的一个基于GUI的工具。您能使用PerfAnal的辩认性来查找出您需要调整的代码。
  
    5.JAMon    是一个免费,简单,高性能,安全,允许开发者容易地监测生产应用程序的Java API。

    6.Eclipse Memory Analyzer      是帮助您发现内存泄漏和减少记忆消耗量的一台快速和功能丰富的Java堆分析仪。

    7.[url="http://www.eclipse.org/mat "]GCViewer[/url]   一个免费开源工具,使用Java VM属性-verbose:gc 和-Xloggc生成可视化数据。它也计算垃圾收集相关的性能指标(生产量、积累停留、最长的停留等等)。


如果您正在运行您的应用程序在HP - UX平台,看看下面:

    . HPjmeter
        1. 确定和诊断在HP-UX上运行的java 应用程序的问题;
        2. 监察正在运行的java应用程序和分析切面数据;
        3. 捕捉剖面数据与零制备时,使用JDK / JRE的5.0.04或更高;
        4. 在HP - UX , Linux和Windows 系统上运行HPjmeter控制台;
        5. 改善垃圾回收机制的表现。

     .HPjconfig         
是为调整您的HP-UX 11i HPIntegrity Itanium? 和HP 9000 PA-RISC系统核心参量的Java配置工具,匹配您的应用的特征。 HPjconfig为专门制作您的HP-UX硬件工作台提供核心参量推荐。他提供了保持和还原的功能,根据您的客户的需要提供适合的建议,考虑到具体的Java和HP - UX的版本问题,HPjconfig决定所有最新的HP - UX的修补程序所需的Java性能和功能在系统上安装,并提示出所有遗失或取代补丁。

     .Java Out-of-Box Tool
一个独立的捆绑安装时将安装启动( RC )的脚本,修改核心参量,重建核心和重新起动系统。 在启动期间,启动脚本将修改系统tunables,因而为Java提供更好的“Out of The Box”行为。

鱼漂个人用得比较多有的:jconsole, jprofiler, MAT.
此外,可以使用 -XX:+HeapDumpOnOutOfMemoryError这个参数启动Java应用程序(适用于JDK 1.6+) ,所以当出现Out of memory时,在TOMCAT_HOME%/bin/目录下才会出现java_pid3164.hprof这样的文件,文件名中的数字会根据当前JVM所占用的PID不同而不同。

google-code-prettify introduction

[不指定 2009/12/29 09:42 | by ipaddr ]

Main page: http://code.google.com/p/google-code-prettify/

A Javascript module and CSS file that allows syntax highlighting of source code snippets in an html page.

For an example, see the README

Download

Change Log

Features

  • Works on HTML pages
  • Works even if code contains embedded links, line numbers, etc.
  • Simple API : include some JS&CSS and add an onload handler.
  • Lightweights : small download and does not block page from loading while running.
  • Customizable styles via CSS.
  • Supports all C-like, Bash-like, and XML-like languages. No need to specify the language
  • Extensible language handlers for other languages. You can specify the language.
  • Widely used with good cross-browser support. Powers code.google.com

Ubuntu 9.10下安装Eclipse CDT 6.0

[不指定 2009/12/22 22:30 | by ipaddr ]
这几天试了一下在Ubuntu下使用CDT,安装稍有点麻烦,特记录一下。
首先,直接从Eclipse官方网站下载的最新的CDT包好象有点问题,Finish按钮总是点击不了,非常奇怪,不过可以通过Enter按键来替代。使用起来总有点不爽,所以决定使用deb包试一下。
鱼漂(www.eit.name)原创,转载请注明.

安装步骤如下:
1. 安装Jre, Jdk
sudo apt-get install sun-java6-jre
sudo apt-get install sun-java6-jdk

2. 安装Eclipse
sudo apt-get install eclipse
安装完eclipse后,还需要安装以下两个包,否则安装CDT将失败:
sudo apt-get install eclipse-pde
sudo apt-get install eclipse-jdt

3.安装CDT
通过Eclipse的Help -> Install New Software进入安装插件界面;
首先,需要添加一个CDT的下载站点,填写CDT的下载地址:
http://download.eclipse.org/tools/cdt/releases/galileo
(以上下载地址,可以在http://www.eclipse.org/cdt/downloads.php找到)

之后在此站点安装CDT包即可。


Using PHP sessions across subdomains

[不指定 2009/12/03 10:47 | by ipaddr ]
By default, PHP uses the 'PHPSESSID' cookie to propagate session data across multiple pages, and by default it uses the current top-level domain and subdomain in the cookie declaration.

Example: www.domain.com

The downside to this is that the session data can't travel with you to other subdomains. So if you started a session on www.domain.com, the session data would become unavailable on forums.domain.com. The solution is to change the domain PHP uses when it sets the 'PHPSESSID' cookie.

Assuming you have an init file that you include at the top of every PHP page, you can use the ini_set() function. Just add this to the top of your init page:

ini_set('session.cookie_domain', substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],"."),100));

This line of code takes the domain and lops off the subdomain.

Example: forums.domain.com -> .domain.com

Now, every time PHP sets the 'PHPSESSID' cookie, the cookie will be available to all subdomains!

鱼漂提醒:
1. 以下代码可以优化如下:
//sessions across subdomains
if (strpos($_SERVER['SERVER_NAME'],".")!==false) {
 ini_set('session.cookie_domain',substr($_SERVER['SERVER_NAME'],strpos($_SERVER['SERVER_NAME'],".")));
}


2.除了设置session的domain外,还需要在所有的服务器上共享session的存储, 如果session是文件存储,则所有服务器应共享session存储目录,比如通过nfs或san等. 另外一个方法是将所有服务器的session保存到同一个DB;
分页: 4/13 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页 [ 显示模式: 摘要 | 列表 ]