OpenBSD 比 Linux 快 10 倍吗?
OpenBSD is so fast, I had to modify the program slightly to measure itself

原始链接: https://flak.tedunangst.com/post/is-OpenBSD-10x-faster-than-Linux

一个简单的基准测试,涉及线程创建和套接字打开,揭示了Linux和OpenBSD之间令人惊讶的性能差异。该测试创建一个线程,然后让两个线程各自打开256个套接字。 在Linux上,测试完成大约需要0.018-0.026秒。然而,OpenBSD完成相同的任务*快得多*——最初达到文件描述符限制,但在增加限制后,运行时间在0.002-0.006秒左右。 作者觉得这个结果很有趣,因为OpenBSD在基准测试中通常表现较慢。这种差异并非由于网络代码,而是实现的一个微妙方面(详见链接的帖子),在特定情况下有利于OpenBSD。作者挑战任何人来证明Linux在这个基准测试中胜过OpenBSD。

## OpenBSD 性能与文件描述符分配 最近的一篇文章指出,某个程序在 OpenBSD 上的性能出乎意料地快,引发了对其原因的调查。核心问题在于多线程应用程序中的文件描述符 (fd) 表分配。Linux 使用读-复制-更新 (RCU) 来扩展 fd 表,这可能会由于同步要求而引入延迟。然而,OpenBSD 使用更简单的基于互斥锁的方法,避免了这种延迟。 原始程序的性能优势是通过预先分配更大的 fd 表(通过 `dup2`),从而完全绕过扩展过程而放大的。讨论表明,性能差异不一定在于整体速度,而在于在特定场景下最大限度地减少延迟。 该讨论还涉及更广泛的基准测试考虑因素,例如硬件一致性以及测试实际工作负载的重要性。最后,许多评论者指出作者网站上一个令人分心的交互元素(射击小行星)妨碍了可读性。
相关文章

原文

Here’s a little benchmark complements of Jann Horn. It’s unexpectedly slow on Linux.

OpenBSD is so fast, I had to modify the program slightly to measure itself, as the time utility is missing sufficient precision to even record nonzero.

All it does is create one extra thread, then both existing threads create 256 sockets. What’s so hard about that?

#include <pthread.h>
#include <unistd.h>
#include <err.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/socket.h>

static void open_sockets(void) {
    for (int i=0; i<256; i++) {
        int sock = socket(AF_INET, SOCK_STREAM, 0);
        if (sock == -1)
            err(1, "socket");
    }
}

static void *thread_fn(void *dummy) {
    open_sockets();
    return NULL;
}

int main(int argc) {
    struct timeval one, two;
    gettimeofday(&one, NULL);
    if (argc > 1)
        dup2(0, 666);
    pthread_t thread;
    if (pthread_create(&thread, NULL, thread_fn, NULL))
        errx(1, "pthread_create");
    open_sockets();
    if (pthread_join(thread, NULL))
        errx(1, "pthread_join");
    gettimeofday(&two, NULL);
    timersub(&two, &one, &one);
    printf("elapsed: %lld.%06lds\n", one.tv_sec, one.tv_usec);
    return 0;
}

On Linux, I get results approximately as so:

tedu@penguin:~$ ./a.out 
elapsed: 0.017770s
tedu@penguin:~$ ./a.out 
elapsed: 0.026309s
tedu@penguin:~$ ./a.out 
elapsed: 0.018414s

On OpenBSD, here we go, choo choo:

ox$ ./a.out                                                                               
a.out: a.out: socketsocket: : Too many open files
Too many open files
ox$ ulimit -n 1024
ox$ ./a.out                                                                               
elapsed: 0.006096s
ox$ ./a.out  
elapsed: 0.002508s
ox$ ./a.out  
elapsed: 0.002326s

These aren’t identical machines, but roughly comparable.

There’s a hint in the code (nothing to do with networking code, if that was your first guess), with more explanation in the linked thread, which is worth reading and some thinking. I’d love to see the system and benchmark where Linux outperforms here.

Really, I just found it a little funny. Usually it’s the weirdo benchmark that shows OpenBSD being 10x slower, so this one is definitely going in the collection.

Posted 15 Aug 2025 16:33 by tedu Updated: 15 Aug 2025 16:33
Tagged: openbsd
联系我们 contact @ memedata.com