为什么 syslog 比文件 IO 慢这么多?

cev*_*ing 9 linux debian-squeeze syslog

我写了一个简单的测试程序来测量 syslog 功能的性能。这是我的测试系统的结果:(Debian 6.0.2 with Linux 2.6.32-5-amd64)

测试用例调用有效负载持续时间思想 
                      [] [MB] [s] [MB/s]    
------------- ---------- ---------- ---------- ----------
系统日志 200000 10.00 7.81 1.28      
系统日志 %s 200000 10.00 9.94 1.01      
写/dev/null 200000 10.00 0.03 343.93    
printf %s 200000 10.00 0.13 76.29     

测试程序进行了 200000 次系统调用,每次调用时写入 50 字节的数据。

为什么 Syslog 比文件 IO 慢十倍以上?

这是我用来执行测试的程序:

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

const int  iter  = 200000;
const char msg[] = "123456789 123456789 123456789 123456789 123456789";

struct timeval t0;
struct timeval t1;

void start ()
{
    gettimeofday (&t0, (void*)0);
}

void stop ()
{
    gettimeofday (&t1, (void*)0);
}

void report (char *action)
{
    double dt = (double)t1.tv_sec - (double)t0.tv_sec +
        1e-6 * ((double)t1.tv_usec - (double)t0.tv_usec);
    double mb = 1e-6 * sizeof (msg) * iter;

    if (action == NULL)
        printf ("Test Case             Calls       Payload     Duration    Thoughput \n"
                "                      []          [MB]        [s]         [MB/s]    \n"
                "--------------------  ----------  ----------  ----------  ----------\n");
    else {
        if (strlen (action) > 20) action[20] = 0;
        printf ("%-20s  %-10d  %-10.2f  %-10.2f  %-10.2f\n",
                action, iter, mb, dt, mb / dt);
    }
}

void test_syslog ()
{
    int i;

    openlog ("test_syslog", LOG_PID | LOG_NDELAY, LOG_LOCAL0);
    start ();
    for (i = 0; i < iter; i++)
        syslog (LOG_DEBUG, msg);
    stop ();
    closelog ();
    report ("syslog");
}

void test_syslog_format ()
{
    int i;

    openlog ("test_syslog", LOG_PID | LOG_NDELAY, LOG_LOCAL0);
    start ();
    for (i = 0; i < iter; i++)
        syslog (LOG_DEBUG, "%s", msg);
    stop ();
    closelog ();
    report ("syslog %s");
}

void test_write_devnull ()
{
    int i, fd;

    fd = open ("/dev/null", O_WRONLY);
    start ();
    for (i = 0; i < iter; i++)
        write (fd, msg, sizeof(msg));
    stop ();
    close (fd);
    report ("write /dev/null");
}

void test_printf ()
{
    int i;
    FILE *fp;

    fp = fopen ("/tmp/test_printf", "w");
    start ();
    for (i = 0; i < iter; i++)
        fprintf (fp, "%s", msg);
    stop ();
    fclose (fp);
    report ("printf %s");
}

int main (int argc, char **argv)
{
    report (NULL);
    test_syslog ();
    test_syslog_format ();
    test_write_devnull ();
    test_printf ();
}
Run Code Online (Sandbox Code Playgroud)

小智 11

系统日志调用每次调用都会向 AF_UNIX 套接字发出一个 send()。即使 syslogd 丢弃了数据,它仍然必须先读取它。这一切都需要时间。

对 /dev/null 的写入每次调用也会发出一个 write() 但由于数据被丢弃,因此内核可以非常快速地处理它。

fprintf() 调用仅为传输的每 4096 个字节生成一个 write(),即大约每八十个 printf 调用生成一个。每个只涉及将数据从 libc 的缓冲区传输到内核的缓冲区。对磁盘的提交将(至少相比之下)非常慢,但在没有任何显式同步调用的情况下,稍后可能会发生(甚至可能在进程终止之后)。

简而言之:syslog 比 /dev/null 慢,因为它做了很多工作,并且由于缓冲而比 ​​printf 慢。