pdflush内核线程池及其中隐含的竞争(8)
虽然开锁判断线程数不会造成数据损坏,但是如果有几个进程并行判断nr_pdflush_threads的值,并都一致认为线程数还有可以增长的余地,然后都调用start_one_pdflush_thread()去产生新的pdflush线程实例,那么线程数就可能超过MAX_PDFLUSH_THREADS,最坏的情况下可能是其两倍。
再来看接下来的行:
152 pdf = list_entry(pdflush_list.prev, struct pdflush_work, list); 153 if (jiffies - pdf->when_i_went_to_sleep > 1 * HZ) { 154 /* Limit exit rate */ 155 pdf->when_i_went_to_sleep = jiffies; 156 break; /* exeunt */ 157 } |
考虑瞬间的迸发请求,然后都在同一时刻停止运行,这时所有进程退出的时候都不会满足153行的判定,然后都会去睡眠,再假设接下来的n秒内都没有新的请求出发,那么pdflush内核线程数最大的情况将持续n秒,不符合当初的设计要求3。
195 /* 196 * Attempt to wake up a pdflush thread, and get it to do some work for you. 197 * Returns zero if it indeed managed to find a worker thread, and passed your 198 * payload to it. 199 */ 200 int pdflush_operation(void (*fn)(unsigned long), unsigned long arg0) 201 { 202 unsigned long flags; 203 int ret = 0; 204 205 if (fn == NULL) 206 BUG(); /* Hard to diagnose if it's deferred */ 207 208 spin_lock_irqsave(&pdflush_lock, flags); 209 if (list_empty(&pdflush_list)) { 210 spin_unlock_irqrestore(&pdflush_lock, flags); 211 ret = -1; 212 } else { 213 struct pdflush_work *pdf; 214 215 pdf = list_entry(pdflush_list.next, struct pdflush_work, list); 216 list_del_init(&pdf->list); 217 if (list_empty(&pdflush_list)) 218 last_empty_jifs = jiffies; 219 pdf->fn = fn; 220 pdf->arg0 = arg0; 221 wake_up_process(pdf->who); 222 spin_unlock_irqrestore(&pdflush_lock, flags); 223 } 224 return ret; 225 } 226 |
- 上一篇:Linux日志文件系统及性能分析
- 下一篇:linux学习日记五 磁盘与文件系统管理