数据库杂谈(3)

由巨内核和微内核之争联想到的

前篇我们聊到了分布式系统及分布式数据库的定义,其中也谈到分布式系统定义中的一个很重要的概念:

  • 消息传递

这不由的让我想起了历史上一场著名的争论:巨内核和微内核。关于这场争论,感兴趣的可以读下当时的邮件讨论( https://yarchive.net/comp/microkernels.html ) 。

这里我想引用Linus在1999年发表的一篇文章《The Linux Edge》( https://www.oreilly.com/openbook/opensources/book/linus.html )中的关于微内核的一段论述,虽然时隔20多年,还是非常值得我们借鉴和思考的:

Microkernels

When I began to write the Linux kernel, there was an accepted school of thought about how to write a portable system. The conventional wisdom was that you had to use a microkernel-style architecture.
当我开始编写Linux内核时,学术界对和如何编写可移植系统有一个共识,他们通常认为明智的选择就是必须用微内核的架构。

With a monolithic kernel such as the Linux kernel, memory is divided into user space and kernel space. Kernel space is where the actual kernel code is loaded, and where memory is allocated for kernel-level operations. Kernel operations include scheduling, process management, signaling, device I/O, paging, and swapping: the core operations that other programs rely on to be taken care of. Because the kernel code includes low-level interaction with the hardware, monolithic kernels appear to be specific to a particular architecture.
在如Llnu调内核这样的巨内核 中,内存被分为用户空间和内核空间。内核空间是用以存放内核代码以及内核级别操作的内存。内核操作包括调度、进程管理、信号、设备输入/输出、分页和转换等。这些都是其他程序赖以运行的关键操作。由于内核代码含有与低层硬件的交互作用功能, 所以巨内核看起来似乎只能局限于某一特定的体系。

A microkernel performs a much smaller set of operations, and in more limited form: interprocess communication, limited process management and scheduling, and some low-level I/O. Microkernels appear to be less hardware-specific because many of the system specifics are pushed into user space. A microkernel architecture is basically a way of abstracting the details of process control, memory allocation, and resource allocation so that a port to another chipset would require minimal changes.
微内核是只在较有限的形式下完成较少操作的一个集合,如进程间通信、有限的进程管理与调度,以及一些低级别的输入/输出等。由于许多系统特性被划分给了用户空间,因此微内核似乎与硬件关系不大。微内核体系基本上只是对进程控制、内存分配、资源分配等细节的一种抽象方式,要将它移入另一类芯片只需做 很小的改动。

So at the time I started work on Linux in 1991, people assumed portability would come from a microkernel approach. You see, this was sort of the research darling at the time for computer scientists. However, I am a pragmatic person, and at the time I felt that microkernels (a) were experimental, (b) were obviously more complex than monolithic Kernels, and (c) executed notably slower than monolithic kernels. Speed matters a lot in a real-world operating system, and so a lot of the research dollars at the time were spent on examining optimization for microkernels to make it so they could run as fast as a normal kernel. The funny thing is if you actually read those papers, you find that, while the researchers were applying their optimizational tricks on a microkernel, in fact those same tricks could just as easily be applied to traditional kernels to accelerate their execution.
因此,1991年当我开始编写Linux时,人们都认为只有微内核方法可以解决可移植性。因为它在当时是计算机科学家研究的宠儿。 然而,我是个固执、讲求实际的人,当时我对微内核的看法是:
1.仍处于实验阶段;
2.比巨内核复杂得多;
3.运行速度明显地比巨内核慢。
由于运算速度对于现实世界中的操作系统意味着一切,所以,当时许许多多的科研经费都花在了对微内核的最优化检测上,看它是否运行得如同正常内核一样快。令人好笑的是,如果你真正读一读那些论文的话,你就会发现,研究人员应用在微内核上的最优化技巧,事实上可以很容易地应用到传统内核上以加快其运行速度。

In fact, this made me think that the microkernel approach was essentially a dishonest approach aimed at receiving more dollars for research. I don’t necessarily think these researchers were knowingly dishonest. Perhaps they were simply stupid. Or deluded. I mean this in a very real sense. The dishonesty comes from the intense pressure in the research community at that time to pursue the microkernel topic. In a computer science research lab, you were studying microkernels or you weren’t studying kernels at all. So everyone was pressured into this dishonesty, even the people designing Windows NT. While the NT team knew the final result wouldn’t approach a microkernel, they knew they had to pay lip service to the idea.
实际上,这使我觉得微内核方案就像是一个自欺欺人的方案,目的是获取更多的研究经费。我并不是说这些研究人员诚心要欺骗,也许他们只是愚昧,或者是搞错了。我说的这些都是我的真实感受。这种不诚实来自了当时笼罩在研究领域的强大压力,迫使人们致力于微内核方面的研究。在计算机科学实验室里,你要么在研究微内核,要么就根本不是在研究内核。所以大家都在被迫表现出这种不诚实,甚至包括设计Windows NT的人们。即使NT工作小组的人员知道最终的结果不是采用微内核方案,他们也不得不迎合这一思想。

Fortunately I never felt much pressure to pursue microkernels. The University of Helsinki had been doing operating system research from the late 60s on, and people there didn’t see the operating system kernel as much of a research topic anymore. In a way they were right: the basics of operating systems, and by extension the Linux kernel, were well understood by the early 70s; anything after that has been to some degree an exercise in self-gratification.
值得庆幸的是,我没有受到追随微内核的压力。芬兰赫尔辛基大学从60年代未起一直从事操作系统的研究,他们不再把操作系统内核视为一个研究焦点。从某些方面说这是正确的。操作系统的基础,也就是Linux内核的扩展的基础。早在70年代初就已被人们了解得很清楚,这之后的任何事都是某种程度上的自我满足的练习。

If you want code to be portable, you shouldn’t necessarily create an abstraction layer to achieve portability. Instead you should just program intelligently. Essentially, trying to make microkernels portable is a waste of time. It’s like building an exceptionally fast car and putting square tires on it. The idea of abstracting away the one thing that must be blindingly fast—the kernel—is inherently counter-productive.
如果你想编写出具有可移植性的代码,那么你不必非要靠建立一个抽象层才能实现代码的可移植。相反,你只需要编程时聪明一点就可以到达目的。基本上可以这样说,想方设法使微内核可移植是浪费时间。这好比使用方型轮胎来研制一种超高速的汽车。一味地将内核中的一件事单独提取出来,盲目地追求其速度,其相应的效果只能是适得其反。

Of course there’s a bit more to microkernel research than that. But a big part of the problem is a difference in goals. The aim of much of the microkernel research was to design for a theoretical ideal, to come up with a design that would be as portable as possible across any conceivable architecture. With Linux I didn’t have to aim for such a lofty goal. I was interested in portability between real world systems, not theoretical systems.
当然,微内核的研究并不局限于上述努力,但问题的大部分就是目的上的不同。很多微内核研究的目的是针对某个理想化的理论而设计的,以求这种设计能够跨跃任何可能的体系,从而实现可移植性。我井没有想在Linux上实现这么虚幻的目标。我感兴趣的是在现实的系统上实现可移植性,而不是在理论的系统上。

从现实结果来看,巨内核架构的Linux/Unix已经支配了世界。当然,Linux/Unix也并不是固执己见,一成不变,其也借鉴了一些微内核的理念,但是是在一些特定的认为适合使用的场合。

以史为鉴,一味的鼓吹分布式数据库是否真的恰当?我不否认分布式数据库的好处,但是需要有场景。根据分布式的理论,我认为分布式数据库是为解决全球部署,或者多地部署(不是对裸光纤距离有严格限制的伪多地)而生的,脱离这个前提,而大谈特谈分布式数据库,就有点耍流氓的味道了。

请使用浏览器的分享功能分享到微信等