Memory Caches
事先要申请,事后要销毁
kmem_cache_create
kmem_cache_destroy
再分配和退还
kmem_cache_alloc
kmem_cache_free
Reference Counts
每个模块到处 xxx_hold xxx_release函数用于增加和减少引用计数
Garbage Collection
两种方式, 同步 异步
Function Pointers and Virtual Function Tables (VFTs)
Vector Definitions
a placeholder
struct abc
{
int a;
char placeholder[0];
};
sizeof(struct abc) 是4
Compile-Time Optimization for Condition Checks
使用 likely和unlikely两个宏 可以优化代码
likely表示 经常会发生
unlikely表示 很少发生
Mutual Exclusion
Spin locks
This is a lock that can be held by only one thread of execution at a time. An attempt to acquire the lock by another thread of execution makes the latter loop until the lock is released. Because of the waste cause by looping, spin locks are used only on multiprocessor systems, and generally are used only when the developer expects the lock to be held for short intervals. Also because of the waste caused to other threads, a thread of execution must not sleep while holding a spin lock.
Read-write spin locks
When the uses of a given lock can be clearly classified as read-only and read-write, the use of read-write spin locks is preferred. The difference between spin locks and read-write spin locks is that in the latter, multiple readers can hold the lock at the same time. However, only one writer at a time can hold the lock, and no reader can acquire it when it is already held by a writer. Because readers are given higher priority over writers, this type of lock performs well when the number of readers (or the number of read-only lock acquisitions) is a good deal bigger than the number of writers (or the number or read-write lock acquisitions).
When the lock is acquired in read-only mode, it cannot be promoted to read-write mode directly: the lock must be released and reacquired in read-write mode.
Read-Copy-Update (RCU)
RCU is one of the latest mechanisms made available in Linux to provide mutual exclusion. It performs quite well under the following specific conditions:
-
Read-write lock requests are rare compared to read-only lock requests.
-
The code that holds the lock is executed atomically and does not sleep.
-
The data structures protected by the lock are accessed via pointers.
Catching Bugs
使用宏 BUG_ON 和 BUG_TRAP来捕捉bug
Statistics
做好 数据统计
Measuring Time
使用jiffies 全局变量