许多游戏和游戏引擎使用多线程将 CPU 工作划分为可以稍微独立运行的逻辑任务。一种典型的配置是:用于输入和游戏逻辑的游戏线程、用于准备和提交要绘制对象的渲染线程,以及用于其他子任务(例如动画或音频)的工作线程。
我们建议并行处理线程,以充分利用多线程的性能优势。例如,游戏和渲染线程在不同的核心上部分或完全并发运行。这并非总能实现,例如在具有共享数据依赖项的情况下;但是,如果可能,这可能会导致 CPU 时间缩短,从而可能提高帧速率。
图 1. 具有高度并行化的主线程和渲染线程以及工作器线程和音频线程的游戏
CPU 核心亲和性
对 CPU 工作负载性能有显著影响的一个因素是它们在核心上的调度方式。具体可分为两部分:
游戏线程是否在最适合其工作负载的核心上运行。
游戏线程是否经常在核心之间切换。
现代设备通常使用一种称为“异构计算”的架构,在该架构中,核心具有不同级别的性能:
一个或多个核心可提供最高的峰值性能,但会消耗更多电量。这些核心有时称为“大”核心。
其他核心的峰值性能较低,但节能较高。这些核心有时称为“小”核心。
可选:一个或多个核心可在性能和功耗之间取得平衡。这些核心有时称为“中等”核心。
您可以在获取跟踪记录时,在配置文件配置中启用 CPU,从而调查 CPU 使用率下的 CPU 线程行为。通过将轨迹的某个部分放大(小于 200 毫秒),您可以查看在设备的 CPU 核心上运行的各个进程。通常,较小的核心对应于较小的索引(例如 CPU“0”-“3”),而较大的核心对应于较高的索引(例如 CPU “6”-“7”),而中间核心(如果存在)将占据中间的索引(例如 CPU “5”-“6”)。这是惯例,但不保证一定如此。
如果您发现某些线程被调度到无法满足其性能或功耗需求的 CPU 上,请考虑手动为这些线程设置 CPU 亲和性。
[null,null,["最后更新时间 (UTC):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."]]