龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 操作系统 > LINUX系统 >

pdflush内核线程池及其中隐含的竞争(2)

时间:2014-06-01 02:11来源:网络整理 作者:网络 点击:
分享到:
上面这段注释是对pdflush线程池的简单解释,大致的意思就是:pdflush线程是为了将脏数据写回的工作线程。比较理想的情况是为每一个活跃的磁盘轴创建一


上面这段注释是对pdflush线程池的简单解释,大致的意思就是:“pdflush线程是为了将脏数据写回的工作线程。比较理想的情况是为每一个活跃的磁盘轴创建一个线程,但是在这个层次上比较难确定磁盘的拓扑结构,因此,我们处处小心,尽量防止对单一文件系统做多个回写操作。pdflush线程可以通过current->flags中PF_FLUSHER标志来协助实现这个。”

可以看出,内核开发者们对于效率还是相当的“吝啬”,考虑的比较周全。但是,对于层次的划分也相当关注,时刻不敢越“雷池”半步,那么的谨小慎微。

 

 43
 44 /*
 45  * All the pdflush threads.  Protected by pdflush_lock
 46  */
 47 static LIST_HEAD(pdflush_list);
 48 static DEFINE_SPINLOCK(pdflush_lock);
 49
 50 /*
 51  * The count of currently-running pdflush threads.  Protected
 52  * by pdflush_lock.
 53  *
 54  * Readable by sysctl, but not writable.  Published to userspace at
 55  * /proc/sys/vm/nr_pdflush_threads.
 56  */
 57 int nr_pdflush_threads = 0;
 58
 59 /*
 60  * The time at which the pdflush thread pool last went empty
 61  */
 62 static unsigned long last_empty_jifs;
 63
 


定义个一些必要的全局变量,为了不污染内核的名字空间,对于不需要导出的变量都采用了static关键字限定了它们的作用域为此编译单元(即当前的pdflush.c文件)。所有的空闲pdflush线程都被串在双向链表pdflush_list里面,并用变量nr_pdflush_threads对当前pdflush的进程(包括活跃的和空闲的)数就行统计,last_empty_jifs用来记录pdflush线程池上次为空(也就是无线程可用)的jiffies时间,线程池中所有需要互斥操作的场合都采用自旋锁pdflush_lock进行加锁保护。

 

 64 /*
 65  * The pdflush thread.
 66  *
 67  * Thread pool management algorithm:
 68  *
 69  * - The minimum and maximum number of pdflush instances are bound
 70  *   by MIN_PDFLUSH_THREADS and MAX_PDFLUSH_THREADS.
 71  *
 72  * - If there have been no idle pdflush instances for 1 second, create
 73  *   a new one.
 74  *
 75  * - If the least-recently-went-to-sleep pdflush thread has been asleep
 76  *   for more than one second, terminate a thread.
 77  */
 78
 
精彩图集

赞助商链接