pdflush内核线程池及其中隐含的竞争
pdflush内核线程池是Linux为了回写文件系统数据而创建的进程上下文工作环境。它的实现比较精巧,全部代码只有不到250行。
1 /* 2 * mm/pdflush.c - worker threads for writing back filesystem data 3 * 4 * Copyright (C) 2002, Linus Torvalds. 5 * 6 * 09Apr2002 akpm@zip.com.au 7 * Initial version 8 * 29Feb2004 kaos@sgi.com 9 * Move worker thread creation to kthread to avoid chewing 10 * up stack space with nested calls to kernel_thread. 11 */ |
文件头部的说明,主要包含版权信息和主要的更改记录(Changlog)。kaos@sgi.com将内核工作线程的创建工作移交给了kthread,主要是为了防止过多的内核线程消耗太多的父工作线程的堆栈空间。关于这个改变我们也能够通过ps的结果看出:
root 5 1 5 0 1 21:31 ? 00:00:00 [kthread] root 114 5 114 0 1 21:31 ? 00:00:00 [pdflush] root 115 5 115 0 1 21:31 ? 00:00:00 [pdflush] |
所有pdflush内核线程的父进程都是kthread进程(pid为5)。
12 13 #include <linux/sched.h> 14 #include <linux/list.h> 15 #include <linux/signal.h> 16 #include <linux/spinlock.h> 17 #include <linux/gfp.h> 18 #include <linux/init.h> 19 #include <linux/module.h> 20 #include <linux/fs.h> // Needed by writeback.h 21 #include <linux/writeback.h> // Prototypes pdflush_operation() 22 #include <linux/kthread.h> 23 #include <linux/cpuset.h> 24 25 |
包含一些比要的头文件。不过有一点不怎么好,虽然C++的行注释已经迁移到了C,可在内核的代码里面看到,还是一样的不舒服,可能是我太挑剔了,本身也没啥不好,我可能需要与时俱进。
26 /* 27 * Minimum and maximum number of pdflush instances 28 */ 29 #define MIN_PDFLUSH_THREADS 2 30 #define MAX_PDFLUSH_THREADS 8 31 32 static void start_one_pdflush_thread(void); 33 34 |
29和30行分别定义了pdflush内核线程实例的最小和最大数量,分别是2和8。最小线程数是为了减少操作的延时,最大线程数是为了防止过多的线程降低系统性能。不过,这里的最大线程数有些问题,下面我们分析其中的竞争条件时会再次提及它。
35 /* 36 * The pdflush threads are worker threads for writing back dirty data. 37 * Ideally, we'd like one thread per active disk spindle. But the disk 38 * topology is very hard to divine at this level. Instead, we take 39 * care in various places to prevent more than one pdflush thread from 40 * performing writeback against a single filesystem. pdflush threads 41 * have the PF_FLUSHER flag set in current->flags to aid in this. 42 */ 43 |
- 上一篇:Linux日志文件系统及性能分析
- 下一篇:linux学习日记五 磁盘与文件系统管理