Modern smartphones have to strike
an on balance between performance, thermal output,
and battery consumption.
And normally when a user's purchasing a device,
these are the last things on their minds
My name is Colt McAnlis.
And unbeknownst to game devs, these physical properties
of a phone can manifest into some really weird performance
issues.
You see all modern mobile CPUs have
a process known as "frequency scaling."
That is, in order to conserve battery,
the hardware will reduce the amount of voltage supplied
to the CPU, depending on a variety of factors.
This dip in supplied energy helps
reduce battery consumption as well as thermal output
but has the downside of slowing down your CPU in the process.
Now there's a variety of factors that can cause this behavior,
but there's two of them that you need to know about.
The first type of frequency throttling
has to do with the load capacity of the CPU.
If your CPU is chugging along and then
suddenly hits a consistent run of low compute frames,
the CPU governor-- which is the thing that
controls the frequency-- can reduce the frequency
supplied to the CPU, reducing its computational power
and thus saving energy.
Subsequent frames, of course, will
run a bit slower since there's less power being given
to the CPU, which means you'll see your frame time go up.
This creates a problem, however, once your CPU load returns back
to normal.
Since the frequency has been downscaled,
you could end up with large performance spikes
since your CPU has been running slower.
This performance hit will continue until the CPU governor
decides to upscale your frequency back at which case
you should be back in your original state.
The good news is that this type of throttling
is entirely under your control, and you
can address it before things go badly.
By timing your CPU frame, you can easily
detect when consistent lulls in CPU load occur;
and for the next few frames, add some work
to ensure that the CPU is getting artificially
pegged at the right frame rate.
This will keep your CPU from scaling
and reduce your spikes in your frame time.
By the way, things like spin loops
are particularly nice solutions that help chew up frame time.
Or you can do something more awesome,
like path finding for 200 zombie entities.
But that's really another episode.
Thankfully your Android device reports the amount
of frequency scaling that's occurring on your device.
So you can determine if this is actually a problem.
To get access to this information
simply read a few files from an oddly cryptic system path,
and you can determine if you're getting artificially scaled
or not.
Now the second type of throttling
is a little bit more difficult to deal
with since it has everything to do
with the thermal properties of the client device.
See one of the dominant trends in mobile computing
has to do with heat distribution.
Typically on desktop CPUs, they have
this obnoxiously large heat sink fan attached to it specifically
to get rid of heat and keep your computer from melting.
Now in contrast, mobile devices don't
have the amount of physical room for such a powerful heat
sink and have to find different ways to distribute heat.
And more so, each manufacturer likes
taking into account different thermal properties
and use cases for their designs.
For example, some Android devices
are built to handle long-term gaming.
While others expect more moderate levels of usage
and may not provide enough airflow or sink space to get
rid of heat effectively.
As such, once the thermal letoff becomes a problem,
the CPU will become throttled to help reduce the amount of heat
being generated by the hardware.
The end game here is that eventually,
due to form factor or running duration,
your CPU can get overheated and then throttled and remain
that way until the heat properties of the device return
back to normal operating values.
And once in this state, your CPU frame time is going to suffer.
Now generally, there's no good way
to be reactive about this type of throttling as it has much
less to do with your code and more
to do with the thermal properties of the device--
unless you're doing something really crazy,
like trying to do protein folding on someone's handheld.
In which case, shame on you.
Proactive developers will collect performance
characteristics about the devices
that their game is run on and can
reduce computational complexity, features,
or quality for those devices.
But even then, that's a lot work to do
to avoid this thermal problem.
Of course the first step in fighting
any type of thermal throttling is aggressively
optimizing the performance of your game
to ensure that you're not wasting battery, frame
time, or thermal letoff on your consumer's device.
So as always, keep calm.
Profile your code.
And remember, perf matters.