Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 多 CPU 負載均衡與執行緒遷移機制:load_balance、migration_thread、pull_task 深度解析

編輯:Linux 教學

核心負載均衡透過 move_tasks() 遷移任務,主要調用路徑:

  1. load_balance():每個時鐘節拍由 scheduler_tick() → rebalance_tick() 觸發,沿調度域層級從本地運行隊列向上遍歷,嘗試從最忙碌組遷移任務到本地 CPU。
  2. migrate_thread() (migration_thread 核心執行緒):當 load_balance 遷移失敗時喚醒,沿調度域鏈從最忙隊列向上搜尋閒置 CPU。亦在 do_exec、sched_setaffinity、CPU 熱插拔、load_balance_newidle 等場景被喚醒。

遷移核心邏輯 (move_task → pull_task)

  1. 優先掃描最忙運行隊列的 過期行程,再掃描 活動行程。
  2. 發現可遷移任務調用 pull_task():dequeue_task() 從遠端移除 → enqueue_task() 插入本地。
  3. 若新任務優先級高於當前任務,觸發 reschedule_task() 搶佔本地 CPU。
  4. max_nr_move 限制單次最大遷移任務數。

遷移條件

參考《Linux Kernel Development》第 3 版第 289 頁:任務必須可遷移(非綁定 CPU)、非正在運行、優先級適合等。

CPU 親和性管理

sched_getaffinity()  /  sched_setaffinity()

以位元遮罩設定任務允許運行的 CPU 集合。

Copyright © Windows教程網 All Rights Reserved