perfr, from Orbit · Post 30

Four Cores

A capture of Godot's shooter demo on a 32-thread machine showed every one of its 84 threads running on CPUs 4 to 7 and nowhere else. Godot had not done that. It had asked for it, by a path three programs long, and its own thread pool had been sized as if it had not. This post follows the mask from the core lanes in perfr back to the daemon that set it, measures what the four cores cost, and finds that the answer is nothing you can see, until something else wants the same cores.

4of 32 hardware threads carried every Godot slice in the capture
32worker threads Godot started, one per hardware thread it could not use
300 msbefore taskset was undone, by a daemon Godot had enrolled itself with
3.3×the CPU time once the pin was lifted, for the same 50 frames a second

The scheduling lanes in a perfr capture are per core: one row for each hardware thread, showing which process ran on it and when. Pointed at the Godot demo from Post 28, which had been left running in its level for a day, a player and four robots idling in it, the view had 28 empty rows. Everything the process did happened on four of them. The numbers below are the scheduling slices of the capture summed by core, over a window of 9.2 seconds in which the rest of the machine was mostly idle.

cores used by godot: [4, 5, 6, 7]
  core 4: running 13.4% of window, 44 distinct threads
  core 5: running 14.4% of window, 43 distinct threads
  core 6: running 13.5% of window, 43 distinct threads
  core 7: running 15.7% of window, 43 distinct threads
godot sched slices 130288 = 14176 context switches a second

Forty-four distinct threads on each of four cores, and fourteen thousand context switches a second, is a process that has more runnable threads than places to run them. It was also a process that could have had 32 places. /proc/1458583/status gave the first explanation: Cpus_allowed_list: 4-7, on all 84 threads. The shell that launched it had 0-31. Godot's source has no call to sched_setaffinity. Something else had set the mask after the process started.

One capture, pinned then released, with its story. Press Play, or step through the list. For the first five seconds GameMode held the process on CPUs 4–7; at +5.0 s a helper called gamemode_request_end_for and the daemon restored the full mask. The scheduler track is one lane per core, and the colour is the Godot process; other processes draw grey. The story's readings, from this capture: pinned, 7 of 32 lanes at or above 5% of the window, cores 4–7 at 23–29% and every Godot slice on them; released, 32 of 32 lanes, the busiest at 20%. The first Godot slice off cores 4–7 comes 35 ms after the request. The 32 workers go from runnable 75% and running 30% to running 145% and runnable 18%; the samples go from 2,860 with Embree's intersector at 10.5% self to 11,107 with __lll_lock_wait at 23.7% and __lll_lock_wake at 22.2%. render viewport ran 250 times in 0.81 s pinned and 285 times in 1.44 s released, with the same 16.7 ms median iteration. The mask itself is not in the capture; it was read from /proc and GameMode's status.

A mask that fights back

The obvious experiment is to clear it. taskset -a -cp 0-31 on the pid reported success, and polling the 84 thread masks afterwards gave this:

t+100ms:  84 Cpus_allowed_list: 0-31
t+200ms:  84 Cpus_allowed_list: 0-31
t+300ms:  61 Cpus_allowed_list: 0-31; 23 Cpus_allowed_list: 4-7
t+400ms:  84 Cpus_allowed_list: 4-7

Something was walking the thread list and putting the mask back, thread by thread. The process list had gamemoded on it, and gamemoded -s said "gamemode is active". GameMode is Feral Interactive's daemon for Linux gaming: a game registers with it over D-Bus and the daemon adjusts the system while the game runs, originally the CPU governor, later I/O priority, niceness, GPU clocks, and since 1.8, which cores the game may use. Its journal had the registration, three seconds after Godot started, in the form of a complaint about each thread's I/O priority. The governor and split-lock changes had failed on a pkexec prompt nobody answered. Core pinning needs no privilege, so it was the one optimisation that took.

Nobody had run gamemoderun. Godot had registered itself. Since August 2026 the Linux platform layer asks GameMode to start for its own pid at the top of the main loop, and the project setting that controls it defaults to on:

// platform/linuxbsd/os_linuxbsd.cpp
bool use_gamemode = GLOBAL_GET("application/run/use_game_mode");
if (!e->is_editor_hint() && !e->is_project_manager_hint()) {
    if (use_gamemode) {
        gamemode_status = GameMode::gamemode_request_start_for(OS::get_singleton()->get_process_id());

// core/config/project_settings.cpp
GLOBAL_DEF("application/run/use_game_mode", true);

The setting's documentation says GameMode allows "optimizing system performance during gameplay, e.g. by using a specific CPU governor or forcing a high GPU power state". It does not mention cores, because when the integration was written the daemon's shipped configuration did not pin them. GameMode 1.8 changed the default. The [cpu] section of the installed gamemode.ini has pin_cores=yes as a comment, with the note that "the default if uncommented is to disable parking but enable pinning", and that the autodetection "will currently only properly autodetect Ryzen 7900x3d, 7950x3d and Intel CPU:s with E- and P-cores".

The detection, in daemon/gamemode-cpu.c, reads two files per CPU: the L3 size and cpuinfo_max_freq. If L3 sizes differ it keeps the cores with the largest, which is the X3D case. If they are uniform it keeps the cores whose maximum frequency is within 5% of the highest. This machine's L3 is one 36 MB slice for everything, so the frequency rule decided, and the frequencies look like this:

CPUsPhysical coresKindcpuinfo_max_freqWithin 5% of 5.8 GHz
4–72, with SMTP-core, favoured5.8 GHzyes
0–3, 8–156, with SMTP-core5.5 GHzno, by 5.2%
16–3116E-core4.3 GHzno

Intel marks two of the eight P-cores as able to boost 300 MHz higher than the rest. That 300 MHz is 5.2% of 5.8 GHz, so the other six P-cores miss the cutoff, and the game is pinned to the two favoured cores and their SMT siblings. Four hardware threads. The daemon refuses to pin when "logic or config would result in less than 4 active cores", and four is not less than four, so it proceeded. On a machine with one favoured core, or a cutoff of 6%, this post would not exist.

The pin is applied to every entry of /proc/pid/task when the game registers, and again for every registered game on each pass of the daemon's reaper thread, which the configuration runs every five seconds. That is the walk the polling caught. A thread created between passes runs where it likes until the next one; a taskset lasts until the next one too.

Thirty-two workers, four hardware threads

Godot sizes its WorkerThreadPool at startup. The project setting threading/worker_pool/max_threads defaults to -1, documented as "all logical CPU cores available", and the implementation is std::thread::hardware_concurrency(). On glibc that call reads the count of online processors, not the calling thread's affinity mask. A nine-line test under taskset -c 4-7:

unpinned:    hardware_concurrency=32 sched_getaffinity=32
taskset 4-7: hardware_concurrency=32 sched_getaffinity=4

The pool therefore has 32 worker threads, named WorkerThread 0 to 31, and the pin arrives three seconds later and squeezes them, the main thread, the audio thread and some fifty helpers onto four hardware threads on two physical cores. Here is what the thread-state lanes recorded in the quiet capture, as a share of the window, for the busiest threads:

     tid name              run%  runnable%  switches
 1458583 main              27.0        1.0      5577
 1458596 WorkerThread 9     2.3        3.0      3133
 1458595 WorkerThread 8     1.8        2.9      3079
 1458597 WorkerThread 10    1.3        3.3      4227
 1458598 WorkerThread 11    1.2        3.1      4212
 1459269 PulseAudio         2.2        2.0      7279

WorkerThread x32:   running 30.8%   runnable 73.5%   (thread-% of window, summed)

A worker thread was runnable, meaning woken and waiting for a core, for more than twice as long as it ran. Across the pool that is 0.31 cores of work and 0.74 cores of queueing. The main thread, which does the real-time work, waited 1% of the window. Those two numbers together are the mechanism: when the main thread dispatches a parallel task, the pool wakes 32 workers for four slots, they take turns at a few hundred microseconds each, and the main thread, which the scheduler prefers because it has been sleeping least, mostly gets its core back when it wants it.

Letting go

Whether that costs a frame needs the same process without the pin, and taskset cannot provide that for longer than five seconds. The daemon can. GameMode's client library has gamemode_request_end_for(pid), which unregisters a game and restores its affinity to all online cores, and gamemode_request_start_for(pid), which registers it again. A dozen lines of C against the header Godot vendors, and the process could be released for a capture and re-pinned afterwards without being touched otherwise. Two captures, eight seconds each, a minute apart, in the same scene:

GameMode on (pinned to 4–7)GameMode released (0–31)
Cores with Godot slices432
Godot CPU time in 9.2 s5.2 s (0.57 cores)17.3 s (1.87 cores)
Thread time running / runnable5.7 s / 7.1 s18.4 s / 2.3 s
Context switches a second14,20019,500
Main thread running27% of the window42% of the window
Workers running / runnable, summed0.31 / 0.74 cores1.51 / 0.23 cores
Frames a second50.650.2
Frame interval p90 / p99 / max17.5 / 33.5 / 36.7 ms17.9 / 32.7 / 34.0 ms
render viewport, per frame2.76 ms3.77 ms
Physics Step, per tick1.62 ms2.29 ms
blit_render_targets_to_screen, per frame10.6 ms7.9 ms

The frame rate did not move. The p99 frame interval did not move. Released, the process used three and a third times the CPU to produce the same frames, and the extra time went to the workers, which ran five times as much and waited a third as long. The sampled stacks say what they ran. Pinned, the commonest worker leaves below _process_task were RaycastOcclusionCull::Scenario::_raycast and Embree's rtcIntersect16: the work. Released, they were __lll_lock_wait, __lll_lock_wake and the mutex lock and unlock around them: 32 threads on 32 cores contending for the pool's queue lock and the group counters, and finishing the same culling later. The two parallel sections of the frame, viewport rendering and the physics step, took about 40% longer per call with 32 cores than with four. The blit, which is where the frame waits for the swapchain, shrank by the same amount and vsync absorbed the difference.

That is the sense in which GameMode's pin was right by accident. The demo in this scene needs about half a core and gets no benefit from parallelism beyond the two physical cores it was given, and spreading a wake-everything thread pool across sixteen P-core threads and sixteen E-cores makes each parallel section slower, not faster. The cost of the pin, the queueing of the workers, is paid by threads whose results the frame does not wait on long.

When it bites

The first capture of the day was taken without knowing any of this, while a build was running in another terminal. The build had no affinity mask, so the kernel spread it over all 32 hardware threads, including the four Godot was confined to. Other processes occupied 33% of cores 4 to 7 in that window, and Godot's thread states show it: 3.3 seconds running, 6.4 seconds runnable, two thirds of every wish for a core spent waiting. The frame rate fell to 47, and the frame interval's p90 went from 17.5 ms to 30.1 ms. A vsynced game that misses one frame in ten is a game that stutters.

The build is a confound, since it loaded the whole machine and not only those cores. But the asymmetry is the point: the build could go anywhere, and Godot could go to four places, and the scheduler balances load across the cores a task is allowed to use, not away from the cores another task is stuck on. A process pinned to the two fastest cores on the machine is also pinned to the two cores every unpinned burst on the machine will find most attractive.

What to do about it

Three programs each did something reasonable. Godot asked GameMode for help because a game should. GameMode kept the game on the fastest cores because on a hybrid or chiplet CPU that is often the right call, and it had a rule that stopped it going below four. glibc answered the question it was asked. The mismatch is between the second and the third: a pool sized for the machine running on a mask sized for the game. Two small changes would close it.

Godot could size the pool from sched_getaffinity when that is smaller than hardware_concurrency, on Linux at least; a 4-thread pool on a 4-thread mask would have had no queueing to measure, and the parallel sections would have been no slower than they were. It could also delay the GameMode request until after the pool exists, which it already does, and then ask the question again; but a pool that re-sizes is a larger change than a pool that asks once and correctly. For a project that does not want any of this, application/run/use_game_mode=false is in the project settings today, and so is threading/worker_pool/max_threads.

GameMode's rule could consider how many cores it is leaving. Four hardware threads on two physical cores is a small place to put a modern engine, and the 5% cutoff that excludes six P-cores at 5.5 GHz in favour of two at 5.8 GHz trades a 5% clock advantage for a 75% reduction in cores. A cutoff that keeps all cores of the fastest kind would have left Godot sixteen hardware threads here, and would still exclude the E-cores, which is the case the feature exists for.

For perfr the lesson is about what a capture should show first. The core lanes made the pin visible in one look; the thread-state lanes made the cost legible; the sampled stacks said what the workers were doing with the cores they got. None of that needed a hook or a scope. The remaining question, which program set the mask, was answered by polling /proc and reading a journal, and a capture that records affinity changes as events, with the pid that made them, would have answered it in the same look. That is on the list.

The short version. Godot on Linux asks GameMode to start for its own pid, GameMode 1.8 pins registered games to the fastest cores by default, and on an i9-13900KF the fastest cores within its 5% cutoff are two P-cores and their SMT siblings: four hardware threads of 32. Godot's worker pool had already sized itself to 32 from hardware_concurrency, which ignores the mask, so 32 workers queued for four slots at 14,000 context switches a second, runnable twice as long as they ran. Released from the pin for a capture, the process used 3.3× the CPU and rendered the same 50 frames a second, its workers now contending for the pool lock instead of waiting for a core. The pin cost nothing visible until a build landed on the same four cores and the p90 frame interval doubled. A pool sized from the affinity mask would have removed the queueing; a cutoff that keeps all cores of the fastest kind would have left sixteen.