許多遊戲和遊戲引擎都使用多執行緒,將 CPU 工作劃分為邏輯任務,而這些任務可能會獨立執行。其中一種常見設定包括輸入和遊戲邏輯的遊戲執行緒、準備和提交要繪製的物件的轉譯執行緒,以及用於其他子工作的背景工作執行緒 (例如動畫或音訊)。
建議您平行處理執行緒,充分運用多執行緒的效能優勢。例如遊戲和轉譯執行緒在不同核心上執行部分或完整並行執行的情況。不過,在共用資料依附元件的情況下,系統不一定每次都能達成此目的;不過,如果可能的話,這麼做可能會降低 CPU 作業時間,進而增加影格速率。
圖 1 具有高度平行處理的主執行緒和轉譯執行緒,以及背景工作執行緒和音訊執行緒的遊戲
CPU 核心相依性
對 CPU 工作負載效能的其中一個因素,是排定在核心上的排程方式。並可分為兩個部分:
遊戲執行緒是否在工作負載適用的最佳核心上執行。
遊戲執行緒是否經常在核心之間切換。
現代裝置通常會使用名為「異質運算」的架構,當中的核心效能不同:
一或多個核心提供最佳效能,但耗電量更高。有時也稱為「大」核心。
其他核心的峰值效能較低,但節能效率較高。有時也稱為「小」核心。
選用:一或多個核心可在效能與功率之間取得平衡。有時也稱為「中等」核心。
擷取追蹤記錄時,您可以在設定檔設定中啟用 CPU,調查 CPU 用量下的 CPU 執行緒行為。將追蹤記錄部分放大至 200 毫秒以內,即可查看在裝置 CPU 核心上執行的個別程序。一般來說,較小的核心會對應到較小的索引 (例如 CPU 的「0」至「3」),而較大型的核心會對應較高的索引 (例如 CPU 的「6」至「7」),而中間核心 (例如 CPU) 會佔用中間核心的索引 (例如 CPU 的「5」至「6」)。這種做法是常見的慣例,但不能保證。
如果您發現某些執行緒的排程在 CPU 上不符合其效能或效能的需求,請考慮為這些執行緒手動設定 CPU 相依性。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Analyze thread scheduling\n\nThere are a few things to consider in order to determine if your game process threads are appropriately utilized and scheduled for the best performance.\n\n- Frame pacing\n- Multithreading and thread parallelization\n- CPU core affinity\n\nMultithreading\n--------------\n\nMany games and game engines use multithreading to divide CPU work into logical tasks, which may be run somewhat independently. One typical configuration is a game thread for input and game logic, a render thread for preparing and submitting objects to be drawn, and worker threads for other subtasks such as animations or audio.\n\nWe recommend parallelizing threads to take advantage of performance gains of\nmultithreading. An example of this is a scenario where the game and render\nthreads are running partially or fully concurrently on different cores. This\nwon't always be possible, such as in cases with shared data dependencies;\nhowever, when possible, this may result in lower CPU times and thus potentially\nhigher frame rates.\n**Figure 1.**Game with a well-parallelized main and render thread, as well as a worker thread and audio thread\n\nCPU core affinity\n-----------------\n\n| **Important:** CPU core affinity has been superseded by the [Performance Hint API](/games/optimize/adpf/performance-hint-api). Use this API when your app is on a device running Android 12 and later.\n\nOne factor that significantly affects the performance of your CPU workloads is how they are scheduled on the cores. This may be split into two components:\n\n- Whether your game threads are running on the most suitable core for their workload.\n- Whether your game threads switch between cores frequently.\n\nModern devices often use an architecture called *heterogeneous computing*, where the cores have different levels of performance:\n\n- One or a few cores offer top peak performance, but consume more power. These are sometimes called \"big\" cores.\n- Other cores have lower peak performance, but are more power-efficient. These are sometimes called \"little\" cores.\n- Optionally: one or more cores offer a balance between performance and power. These are sometimes called \"mid\" cores.\n\nYou may investigate CPU thread behavior under **CPU Usage** by enabling the\n**CPU** in the profile config when taking a trace. By zooming into a section of\nyour trace \\\u003c200 ms, you can view the individual processes running on your device's CPU cores. Typically, smaller cores correspond to smaller indexes (for example, CPUs '0'-'3')\nwhereas larger cores correspond to higher indexes (for example, CPUs '6'-'7')\nand middle cores if present will occupy indexes in between (for example, CPUs '5'-'6').\nThis is by common convention, but it's not a guarantee.\n\nIf you find that certain threads are being scheduled on CPUs that don't meet their needs for performance or power,\nconsider manually setting the CPU affinity for those threads.\n**Figure 2.**Game with main and render thread primarily running on the large cores (CPU 6-7), shown in light blue\n\nYou may also observe whether your threads switch between cores.\nSuch core switches incur some overhead from the context switch and the loss of state with a core's cache/registers.\n**Figure 3.**Game with main (Thread-7) and render thread (Thread-8) that switch between cores, shown in purple\n\nSetting CPU affinity for a thread instructs the system to schedule it on the given core when your game is in the foreground.\nThere are several factors to consider when doing this:\n\n- The platform software can't dynamically adjust task placement for runtime factors such as load and thermal throttling.\n- Performance testing on different devices may yield very different\n performance characteristics, especially if the devices vary considerably by\n price point or by release date.\n\n A newer or more expensive device might run a given workload comfortably on\n a little core, but an older or more affordable device might require a\n bigger core to meet deadlines for that same workload.\n- By forcing affinities to big cores, you may unnecessarily increase battery\n drain and thermal load.\n\nFor these reasons, it's generally best to avoid manually setting CPU affinities."]]