A Comparison of Solaris, Linux, and FreeBSD Kernels
by Max Bruning
October 14, 2005
這篇文章不是效能比較, 而是單純的 kernel 實作比較, 作者就 scheduling, memory management, 與 file
system architecture 在這三種作業系統中運作的模式與資料結構做簡單的解說 (講簡單是跟文章裡提到的書籍內容做比較,
雖然簡單但不失其精確性)
btw, 裡面提到的三本書都相當的不錯, 如果有興趣研究這三種作業系統的核心, 一定要買回來 (當然不是當枕頭用..)
- Linux Kernel Development, 2/e by Robert Love.
- The Design and Implementation of the FreeBSD Operating SystemMarshall Kirk McKusick, George V. Neville-Neil by
- Solaris Internals: Core Kernel Architecture by Jim Mauro, Richard McDougall
I spend most of my time teaching classes on Solaris internals, device drivers, and kernel crash dump analysis and debugging. When explaining to classes how various subsystems are implemented in Solaris, students often ask, "How does it work in Linux?" or, "In FreeBSD, it works like this, how about Solaris?" This article examines three of the basic subsystems of the kernel and compares implementation between Solaris 10, Linux 2.6, and FreeBSD 5.3.
The three subsystems examined are scheduling, memory management, and file system architecture. I chose these subsystems because they are common to any operating system (not just Unix and Unix-like systems), and they tend to be the most well-understood components of the operating system.
This article does not go into in-depth details on any of the subsystems described. For that, refer to the source code, various websites, and books on the subject. For specific books, see:
- Solaris Internals: Core Kernel Architecture by McDougall and Mauro (visit Solaris Internals)
- The Design and Implementation of the FreeBSD Operating System by McKusick and Neville-Neil (visit The Design and Implementation of the FreeBSD Operating System)
- Linux Kernel Development by Love (visit Linux Kernel Development, 2nd Edition ) and Understanding the Linux Kernel by Bovet and Cesati (visit Understanding the Linux Kernel, 2nd Edition)
If you search the Web for Linux, FreeBSD, and Solaris comparisons, most of the hits discuss old (in some cases, Solaris 2.5, Linux 2.2, etc.) versions of the OSes. Many of the "facts" are incorrect for the newest releases, and some were incorrect for the releases they intended to describe. Of course, most of them also make value judgments on the merits of the OSes in question, and there is little information comparing the kernels themselves. The following sites seem more or less up to date:
- "Solaris Vs. Linux" is pretty one-sided for Solaris 10 over Linux.
- " Comparing MySQL Performance" on Solaris 10, Linux, FreeBSD, and others.
- " Fast Track to Solaris 10 Adoption" has some comparisons between Linux and Solaris.
- " Solaris 10 Heads for Linux Territory" is not really a comparison, but reviews Solaris 10.
One of the more interesting aspects of the three OSes is the amount of similarities between them. Once you get past the different naming conventions, each OS takes fairly similar paths toward implementing the different concepts. Each OS supports time-shared scheduling of threads, demand paging with a not-recently-used page replacement algorithm, and a virtual file system layer to allow the implementation of different file system architectures. Ideas that originate in one OS often find their way into others. For instance, Linux also uses the concepts behind Solaris's slab memory allocator. Much of the terminology seen in the FreeBSD source is also present in Solaris. With Sun's move to open source Solaris, I expect to see much more cross-fertilization of features. Currently, the LXR project provides a source cross-reference browser for FreeBSD, Linux, and other Unix-related OSes, available at fxr.watson.org. It would be great to see OpenSolaris source added to that site.
Scheduling and Schedulers
The basic unit of scheduling in Solaris is the kthread_t
; in
FreeBSD, the thread
; and in Linux, the task_struct
.
Solaris represents each process as a proc_t
, and each thread
within the process has a kthread_t
. Linux represents processes
(and threads) by task_struct
structures. A single-threaded process
in Linux has a single task_struct
. A single-threaded process in
Solaris has a proc_t
, a single kthread_t
, and a
klwp_t
. The klwp_t
provides a save area for threads
switching between user and kernel modes. A single-threaded process in FreeBSD
has a proc
struct, a thread
struct, and a
ksegrp
struct. The ksegrp
is a "kernel scheduling
entity group." Effectively, all three OSes schedule threads, where a thread is
a kthread_t
in Solaris, a thread
structure in
FreeBSD, and a task_struct
in Linux.
Scheduling decisions are based on priority. In Linux and FreeBSD, the lower the priority value, the better. This is an inversion; a value closer to 0 represents a higher priority. In Solaris, the higher the value, the higher the priority. Table 1 shows the priority values of the different OSes.
Table 1. Scheduling Priorities in Solaris, Linux, and FreeBSD | ||
Solaris | ||
Priorities | Scheduling Class | |
---|---|---|
0-59 | Time Shared, Interactive, Fixed, Fair Share Scheduler | |
60-99 | System Class | |
100-159 | Real-Time (note real-time higher than system threads) | |
160-169 | Low level Interrupts | |
Linux | Priorities | Scheduling Class |
0-99 | System Threads, Real time (SCHED_FIFO ,
SCHED_RR ) |
|
100-139 | User priorities (SCHED_NORMAL ) |
|
FreeBSD | Priorities | Scheduling Class |
0-63 | Interrupt | |
64-127 | Top-half Kernel | |
128-159 | Real-time user (system threads are better priority) | |
160-223 | Time-share user | |
224-255 | Idle user |
All three OSes favor interactive threads/processes. Interactive threads run at better priority than compute-bound threads, but tend to run for shorter time slices. Solaris, FreeBSD, and Linux all use a per-CPU "runqueue." FreeBSD and Linux use an "active" queue and an "expired" queue. Threads are scheduled in priority from the active queue. A thread moves from the active queue to the expired queue when it uses up its time slice (and possibly at other times to avoid starvation). When the active queue is empty, the kernel swaps the active and expired queues. FreeBSD has a third queue for "idle" threads. Threads run on this queue only when the other two queues are empty. Solaris uses a "dispatch queue" per CPU. If a thread uses up its time slice, the kernel gives it a new priority and returns it to the dispatch queue. The "runqueues" for all three OSes have separate linked lists of runnable threads for different priorities. (Though FreeBSD uses one list per four priorities, both Solaris and Linux use a separate list for each priority.)
Linux and FreeBSD use an arithmetic calculation based on run time versus sleep time of a thread (as a measure of "interactive-ness") to arrive at a priority for the thread. Solaris performs a table lookup. None of the three OSes support "gang scheduling." Rather than schedule n threads, each OS schedules, in effect, the next thread to run. All three OSes have mechanisms to take advantage of caching (warm affinity) and load balancing. For hyperthreaded CPUs, FreeBSD has a mechanism to help keep threads on the same CPU node (though possibly a different hyperthread). Solaris has a similar mechanism, but it is under control of the user and application, and is not restricted to hyperthreads (called "processor sets" in Solaris and "processor groups" in FreeBSD).
One of the big differences between Solaris and the other two OSes is the
capability to support multiple "scheduling classes" on the system at the same
time. All three OSes support Posix SCHED_FIFO
,
SCHED_RR
, and SCHED_OTHER
(or
SCHED_NORMAL
). SCHED_FIFO
and SCHED_RR
typically result in "realtime" threads. (Note that Solaris and Linux support
kernel preemption in support of realtime threads.) Solaris has support for a
"fixed priority" class, a "system class" for system threads (such as page-out
threads), an "interactive" class used for threads running in a windowing
environment under control of the X server, and the Fair Share Scheduler in
support of resource management. See priocntl(1)
for information
about using the classes, as well as an overview of the features of each class.
See FSS(7)
for an overview specific to the Fair Share Scheduler.
The scheduler on FreeBSD is chosen at compile time, and on Linux the scheduler
depends on the version of Linux.
The ability to add new scheduling classes to the system comes with a price. Everywhere in the kernel that a scheduling decision can be made (except for the actual act of choosing the thread to run) involves an indirect function call into scheduling class-specific code. For instance, when a thread is going to sleep, it calls scheduling-class-dependent code that does whatever is necessary for sleeping in the class. On Linux and FreeBSD, the scheduling code simply does the needed action. There is no need for an indirect call. The extra layer means there is slightly more overhead for scheduling on Solaris (but more features).
Memory Management and Paging
In Solaris, every process has an "address space" made up of logical section
divisions called "segments." The segments of a process address space are
viewable via pmap(1)
. Solaris divides the memory management code
and data structures into platform-independent and platform-specific parts. The
platform-specific portions of memory management is in the HAT, or hardware
address translation, layer. FreeBSD describes its process address space by a
vmspace, divided into logical sections called regions. Hardware-dependent
portions are in the "pmap" (physical map) module and "vmap" routines handle
hardware-independent portions and data structures. Linux uses a memory
descriptor to divides the process address space into logical sections called
"memory areas" to describe process address space. Linux also has a
pmap
command to examine process address space.
Linux divides machine-dependent layers from machine-independent layers at a much higher level in the software. On Solaris and FreeBSD, much of the code dealing with, for instance, page fault handling is machine-independent. On Linux, the code to handle page faults is pretty much machine-dependent from the beginning of the fault handling. A consequence of this is that Linux can handle much of the paging code more quickly because there is less data abstraction (layering) in the code. However, the cost is that a change in the underlying hardware or model requires more changes to the code. Solaris and FreeBSD isolate such changes to the HAT and pmap layers respectively.
Segments, regions, and memory areas are delimited by:
- Virtual address of the start of the area.
- Their location within an object/file that the segment/region/memory area maps.
- Permissions.
- Size of the mapping.
For instance, the text of a program is in a segment/region/memory area. The mechanisms in the three OSes to manage address spaces are very similar, but the names of data structures are completely different. Again, more of the Linux code is machine-dependent than is true of the other two OSes.
Paging
All three operating systems use a variation of a least recently used
algorithm for page stealing/replacement. All three have a daemon process/thread
to do page replacement. On FreeBSD, the vm_pageout
daemon wakes up
periodically and when free memory becomes low. When available memory goes below
some thresholds, vm_pageout
runs a routine
(vm_pageout_scan
) to scan memory to try to free some pages. The
vm_pageout_scan
routine may need to write modified pages
asynchronously to disk before freeing them. There is one of these daemons
regardless of number of CPUs. Solaris also has a pageout
daemon
that also runs periodically and in response to low-free-memory situations.
Paging thresholds in Solaris are automatically calibrated at system startup so
that the daemon does not overuse the CPU or flood the disk with page-out
requests. The FreeBSD daemon uses values that, for the most part, are
hard-coded or tunable in order to determine paging thresholds. Linux also uses
an LRU algorithm that is dynamically tuned while it runs. On Linux, there can
be multiple kswapd
daemons, as many as one per CPU. All three OSes
use a global working set policy (as opposed to per process working set).
FreeBSD has several page lists for keeping track of recently used pages.
These track "active," "inactive," "cached," and "free" pages. Pages move
between these linked lists depending on their uses. Frequently accessed pages
will tend to stay on the active list. Data pages of a process that exits can be
immediately placed on the free list. FreeBSD may swap entire processes out if
vm_pageout_scan
cannot keep up with load (for example, if the
system is low on memory). If the memory shortage is severe enough,
vm_pageout_scan
will kill the largest process on the system.
Linux also uses different linked lists of pages to facilitate an LRU-style algorithm. Linux divides physical memory into (possibly multiple sets of) three "zones:" one for DMA pages, one for normal pages, and one for dynamically allocated memory. These zones seem to be very much an implementation detail caused by x86 architectural constraints. Pages move between "hot," "cold," and "free" lists. Movement between the lists is very similar to the mechanism on FreeBSD. Frequently accessed pages will be on the "hot" list. Free pages will be on the "cold" or "free" list.
Solaris uses a free list, hashed list, and vnode page list to maintain its variation of an LRU replacement algorithm. Instead of scanning the vnode or hash page lists (more or less the equivalent of the "active"/"hot" lists in the FreeBSD/Linux implementations), Solaris scans all pages uses a "two-handed clock" algorithm as described in Solaris Internals and elsewhere. The two hands stay a fixed distance apart. The front hand ages the page by clearing reference bit(s) for the page. If no process has referenced the page since the front hand visited the page, the back hand will free the page (first asynchronously writing the page to disk if it is modified).
All three operating systems take NUMA locality into account during paging.
The I/O buffer cache and the virtual memory page cache is merged into one
system page cache on all three OSes. The system page cache is used for
reads/writes of files as well as mmap
ped files and text and data
of applications.
File Systems
All three operating systems use a data abstraction layer to hide file system
implementation details from applications. In all three OSes, you use
open
, close
, read
, write
,
stat
, etc. system calls to access files, regardless of the
underlying implementation and organization of file data. Solaris and FreeBSD
call this mechanism VFS ("virtual file system") and the principle data structure
is the vnode
, or "virtual node." Every file being accessed in
Solaris or FreeBSD has a vnode
assigned to it. In addition to
generic file information, the vnode
contains pointers to
file-system-specific information. Linux also uses a similar mechanism, also
called VFS (for "virtual file switch"). In Linux, the file-system-independent
data structure is an inode
. This structure is similar to the
vnode
on Solaris/FreeBSD. (Note that there is an
inode
structure in Solaris/FreeBSD, but this is
file-system-dependent data for UFS file systems). Linux has two
different structures, one
for file operations and the other for inode operations. Solaris and
FreeBSD
combine these as "vnode operations."
VFS allows the implementation of many file system types on the system. This means that there is no reason that one of these operating systems could not access the file systems of the other OSes. Of course, this requires the relevant file system routines and data structures to be ported to the VFS of the OS in question. All three OSes allow the stacking of file systems. Table 2 lists file system types implemented in each OS, but it does not show all file system types.
Table 2. Partial List of File System Types | ||
Solaris | ufs | Default local file system (based on BSD Fast Filesystem) |
nfs | Remote Files | |
proc | /proc files; see proc(4) |
|
namefs | Name file system; allows opening of doors/streams as files | |
ctfs | Contract file system used with Service Management Facility | |
tmpfs | Uses anonymous space (memory/swap) for temporary files | |
swapfs | Keeps track of anonymous space (data, heap, stack, etc.) | |
objfs | Keeps track of kernel modules, see objfs(7FS) |
|
devfs | Keeps track of /devices files; see devfs(7FS) |
|
FreeBSD | ufs | Default local file system (ufs2, based on BSD Fast Filesystem) |
defvs | Keeps track of /dev files | |
ext2 | Linux ext2 file system (GNU-based) | |
nfs | Remote files | |
ntfs | Windows NT file system | |
smbfs | Samba file system | |
portalfs | Mount a process onto a directory | |
kernfs | Files containing various system information | |
Linux | ext3 | Journaling, extent-based file system from ext2 |
ext2 | Extent-based file system | |
afs | AFS client support for remote file sharing | |
nfs | Remote files | |
coda | Another networked file system | |
procfs | Processes, processors, buses, platform specifics | |
reiserfs | Journaling file system |
Conclusions
Solaris, FreeBSD, and Linux are obviously benefiting from each other. With Solaris going open source, I expect this to continue at a faster rate. My impression is that change is most rapid in Linux. The benefits of this are that new technology has a quick incorporation into the system. Unfortunately, the documentation (and possibly some robustness) sometimes lags behind. Linux has many developers, and sometimes it shows. FreeBSD has been around (in some sense) the longest of the three systems. Solaris has its basis in a combination of BSD Unix and AT&T Bell Labs Unix. Solaris uses more data abstraction layering, and generally could support additional features quite easily because of this. However, most of the layering in the kernel is undocumented. Probably, source code access will change this.
A brief example to highlight differences is page fault handling. In
Solaris, when a page fault occurs, the code starts in a platform-specific trap
handler, then calls a generic as_fault()
routine. This routine
determines the segment where the fault occurred and calls a "segment driver" to
handle the fault. The segment driver calls into file system code. The file
system code calls into the device driver to bring in the page. When the page-in
is complete, the segment driver calls the HAT layer to update page table
entries (or their equivalent). On Linux, when a page fault occurs, the kernel
calls the code to handle the fault. You are immediately into platform-specific
code. This means the fault handling code can be quicker in Linux, but the Linux
code may not be as easily extensible or ported.
Kernel visibility and debugging tools are critical to get a correct
understanding of system behavior. Yes, you can read the source code, but I
maintain that you can easily misread the code. Having tools available to test
your hypothesis about how the code works is invaluable. In this respect, I see
Solaris with kmdb
, mdb
, and DTrace as a clear winner. I have been "reverse
engineering" Solaris for years. I find that I can usually answer a question by
using the tools faster than I can answer the same question by reading source
code. With Linux, I don't have as much choice for this. FreeBSD allows use of
gdb
on kernel crash dumps. gdb
can set breakpoints,
single step, and examine and modify data and code. On Linux, this is also
possible once you download and install the tools.
Max Bruning currently teaches and consults on Solaris internals, device drivers, kernel (as well as application) crash analysis and debugging, networking internals, and specialized topics. Contact him at max@bruningsystems.com or http://mbruning.blogspot.com/.
References :
http://its.downreg.com/data
http://glibc-bsd.alioth.debian
http://www.osnews.com/story
http://fanqiang.chinaunix.net/
http://speed.cis.nctu.edu.tw/~ydlin/miscpub/hands-on_SOC_integration.pdf
http://people.freebsd.org/~murray/bsd_flier.html
http://www.over-yonder.net/~fullermd/rants/bsd4linux/bsd4linux1.php
http://blog.xuite.net/jackie.xie/bluelove/5553829?st=m&w=2005-04&re=list&p=1&ctype=List
FreeBSD vs. Linux vs. Windows 2000
FreeBSD |
Linux |
Windows 2000 |
|
---|---|---|---|
Reliability |
![]() |
![]() |
![]() |
Performance |
![]() |
![]() |
![]() For their own "Hotmail" Internet servers, Microsoft used FreeBSD for many years. |
Security |
![]() FreeBSD has been the subject of a massive auditing project for several years. All of the critical system components have been checked and rechecked for security-related errors. The entire system is open source so the security of the system can and has been verified by third parties. A default FreeBSD installation has yet to be affected by a single CERT security advisory in 2000.3 FreeBSD also has the notion of kernel security levels. These are much more powerful than simple run-levels since they allow the administrator to completely deny access to certain operating system functions such as reading /dev/mem, changing file system flags, or writing to disks without mounting a filesystem. FreeBSD includes a very robust packet filtering firewall system and many intrusion detection tools. |
![]() The open source nature of Linux allows anyone to inspect the security of the code and make changes, but in reality the Linux codebase is modified too rapidly by inexperienced programmers. There is no formal code review policy and for this reason Linux has been suceptible to nearly every Unix-based CERT advisory of the year. This problem is compounded by the fact that distributions like Red Hat tend to turn on notoriously insecure services by default. However, Linux does include a very robust packet filtering firewall system and many intrusion detection tools. |
![]() Microsoft claims that their products are secure. But they offer no guarantee, and their software is not available for inspection or peer review. Since Windows is closed source there is no way for users to fix or diagnose any of the security compromises that are regularly published about Microsoft systems. |
Filesystem |
![]() FreeBSD uses the UFS (Unix File System), which is a little more complex than Linux's ext2. It offers a better way to insure filesystem data integrity, mainly with the "sofupdates" option. This option decreases synchronous I/O and increases asynchronous I/O because writes to a UFS filesystem aren't synced on a sector basis but according to the filesystem structure. This ensures that the filesystem is always coherent between two updates. The FreeBSD filesystem also supports file flags, which can stop a would-be intruder dead in his or her tracks. There are several flags that you can add to a file such as the immutable flag. The immutable (schg) flag won't allow any alteration to the file or directory unless you remove it. Other very handy flags are append only (sappnd), cannot delete (sunlnk), and archive (arch). When you combine these with the kernel security level option, you have a very impenetrable system. |
![]() The Linux ext2 filesystem gets its performance from having an asynchronous mount. You can mount FreeBSD UFS filesystems as asynchronous but this is very dangerous and no seasoned Unix admin would do this. It's amazing that Linux is designed this way by default. Often a hard carsh permanently damages a mount. FreeBSD or Solaris can sustain a very hard crash with only minor data loss, and the filesystem will be remountable with few problems. There are several new journaling filesystems in development for Linux that will fix some of these issues, but these will not be ready for the 2.4 release of Linux. |
![]() The Microsoft FAT filesystem and the newer NTFS are both plagued by over 15 years of backwards compatability with the earliest of PC-based filesystems. These filesystems were not designed for today's demanding server applications, they weren't even designed with a multi-user OS or networking in mind! |
Device Drivers |
![]() |
![]() |
![]() |
Commercial Applications |
![]() |
![]() |
![]() |
Free Applications |
![]() |
![]() |
![]() |
Development environment |
![]() |
![]() |
![]() |
Development infrastructure |
![]() |
![]() |
![]() |
Support |
![]() |
![]() |
![]() |
Price, and Total Cost of Ownership |
![]() |
![]() |
![]() The server edition of Windows 2000 costs nearly $700. Even basic applications cost extra. Users often spend many thousands of dollars for programs that are included for free with Linux or FreeBSD. Documentation is expensive, and very little on-line documentation is provided. A license is required for every computer, which means delays and administrative overhead. The initial learning curve for simple administration tasks is smaller than with Unix, but it also requires a lot more work to keep the system running with any significant work load. |
Total |
FreeBSD![]() ![]() ![]() |
Linux![]() ![]() ![]() |
Windows 2000![]() ![]() ![]() |
1
http://www.ece.cmu.edu/~ganger/papers/CSE-TR-254-95/
2
http://advisor.gartner.com/n_inbox/hotcontent/hc_2121999_3.html#h8
3
Recent CERT Advisaries affecting Linux :
- CA-2000-22 - Input Validation Problems in LPRng
- CA-2000-21 - Denial-of-Service Vulnerabilities in TCP/IP Stacks
- CA-2000-20 - Multiple Denial-of-Service Problems in ISC BIND
- CA-2000-17 - Input Validation Problem in rpc.statd
- CA-2000-13 - Two Input Validation Problems In FTPD
- CA-2000-06 - Multiple Buffer Overflows in Kerberos Authenticated Services
- CA-2000-03 - Continuing Compromises of DNS servers
Recent CERT Advisaries affecting Windows :
- CA-2000-16 - Microsoft 'IE Script'/Access/OBJECT Tag Vulnerability
- CA-2000-14 - Microsoft Outlook and Outlook Express Cache Bypass Vulnerability
- CA-2000-12 - HHCtrl ActiveX Control Allows Local Files to be Executed
- CA-2000-10 - Inconsistent Warning Messages in Internet Explorer
- CA-2000-07 - Microsoft Office 2000 UA ActiveX Control Incorrectly Marked "Safe for Scripting"
- CA-2000-04 - Love Letter Worm
Additional Information
- Is FreeBSD a Superior Server Platform to Linux?
- Microsoft Windows NT vs Unix by John Kirch, MCP
This document was prepared by Bob Bruce and Murray Stokely, with
input from Matt Dillon, Nathan dude, and many others.