Skip to content

thermal_monitor: Add thermal monitor binary - #55

Merged
jimblacklercorp merged 6 commits into
android-graphics:mainfrom
jimblacklercorp:add-thermal-monitor
Nov 6, 2026
Merged

jimblacklercorp merged 6 commits into
android-graphics:mainfrom
jimblacklercorp:add-thermal-monitor

Conversation

@jimblacklercorp

Copy link
Copy Markdown

Add thermal_monitor, a standalone Android executable that samples the framework thermal state via the NDK AThermal API while a capture is running, and writes it into the trace as atrace counters (ATrace_setCounter):

Comment thread thermal_monitor/main.cpp Resolved
Comment thread thermal_monitor/test/CMakeLists.txt Resolved
Add thermal_monitor, a standalone Android executable that samples the
framework thermal state via the NDK AThermal API while a capture is
running, and writes it into the trace as atrace counters
(ATrace_setCounter):

- sherlock.thermal.status: AThermal_getCurrentThermalStatus() (Android
  11+).
- sherlock.thermal.headroom: AThermal_getThermalHeadroom(0) (Android
  12+), multiplied by 1000 because atrace counters are integers.
- sherlock.thermal.headroom.forecast: a 10-second headroom forecast
  (overridable via --forecast-seconds), sampled on alternate ticks
  because AThermal_getThermalHeadroom() is rate-limited to about 1 Hz
  whatever the horizon. A failed forecast tick emits no headroom
  rather than retrying the current headroom, and the forecast is
  dropped after 3 failures in a row, because a single NaN can just
  mean the call was rate-limited.
- sherlock.thermal.headroom.threshold.<status>: the device-reported
  thresholds from AThermal_getThermalHeadroomThresholds() (Android
  15+), re-emitted with every headroom sample because the monitor
  starts before tracing does.

The monitor polls once a second (--poll-interval-ms can slow it down
but not speed it up, because of the rate limit). Unknown arguments
and malformed values are logged and ignored. The Android build now
targets API 31 (Sherlock's minimum), so the AThermal calls link
directly; AThermal_getThermalHeadroomThresholds() (API 35) is weakly
linked and guarded by __builtin_available.

Android Performance Analyzer ships the binary in its companion APK
next to agi_launch_producer, and reads these counters in its trace
viewer. It is built and installed to bin/ like agi_launch_producer.

The sampling policy (ThermalCollector) has no Android dependencies, so
it is built on the host too and unit tested with GoogleTest
(test_thermal_collector).

@emrekultursay emrekultursay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick pass. I could not finish reviewing it.

Comment thread thermal_monitor/thermal_collector.h Resolved
Comment thread thermal_monitor/main.cpp Resolved
Comment thread thermal_monitor/thermal_collector.h Resolved
Comment thread thermal_monitor/main.cpp Resolved

@emrekultursay emrekultursay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick pass. I could not finish reviewing it.

Comment thread thermal_monitor/thermal_collector.cpp Resolved
Comment thread thermal_monitor/main.cpp Resolved

@emrekultursay emrekultursay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to push? (I don't see Perfetto SDK changes)

Comment thread thermal_monitor/main.cpp Resolved
- Return a ThermalStatus enum from ThermalSource::CurrentThermalStatus()
  instead of int. It mirrors AThermalStatus, which the host test build
  cannot include, and main.cpp static_asserts that the values match.
  Threshold maps and ThresholdCounterName() are keyed by it too.
- Carry headroom as float, as the NDK returns it, instead of double.
- Document the headroom range of ThermalSource::ThermalHeadroom().
- Reword the comment on AThermal_getThermalHeadroomThresholds() so it
  only describes what the monitor does on older Android versions.
- Rename ThermalCollector's emit / emit_ to emitter / emitter_.
Write the thermal counters as Perfetto track event counters in a
"SherlockThermal" category, in the same way as the
device_memory_report layer, instead of with ATrace_setCounter():

- Counters are doubles, so headroom is written as the API's value
  rather than multiplied by 1000 and rounded. The float is written as
  the double nearest to its shortest decimal form, so that 0.8f shows
  as 0.8 rather than 0.800000011920929.
- The trace no longer depends on atrace app tags being enabled for the
  monitor's process.
- The headroom thresholds are written once at the start of each
  tracing session (from a TrackEventSessionObserver) instead of with
  every current headroom sample, so ThermalCollector reads them at
  construction and EmitThresholds() writes them.

thermal_monitor links the Perfetto SDK from layersvt/perfetto, built as
a separate static library so that the availability checks only apply
to main.cpp. The stripped arm64 binary is now about 1.3 MB.
Use the NDK's AThermalStatus directly in ThermalSource,
ThresholdCounterName() and the threshold maps, instead of the
ThermalStatus enum that mirrored it. This drops the enum and the
static_asserts in main.cpp that kept it in sync.

The host build, which unit tests ThermalCollector, has no NDK, so it
puts host_stubs/ on the include path. host_stubs/android/thermal.h
declares only the AThermalStatus enum, with its NDK ABI values. Android
builds use the real header.
Comment thread thermal_monitor/main.cpp Resolved
Comment thread thermal_monitor/main.cpp Resolved
Comment thread thermal_monitor/main.cpp Resolved
Comment thread thermal_monitor/main.cpp Resolved
Comment thread thermal_monitor/thermal_collector.cpp Resolved
@jimblacklercorp

Copy link
Copy Markdown
Author

Forgot to push? (I don't see Perfetto SDK changes)

I didn't forget to push but what seems to happen is that GitHub sends the messages as soon as you write them (unlike Gerrit where they are set as drafts and everything sent together). I did see a way around this which is to click 'Start Review' in the Files Changed tab (even though I am replying not reviewing) but currently this is showing Submit Review, so IDK.

…d-graphics#55

- Rename the Perfetto track event category from SherlockThermal to
  Thermals and reference ThermalConstants::kTrackEventCategory
  directly in Perfetto macros.
- Log an error and exit with status 1 on unknown CLI flags or missing
  or malformed integer option values.
- Call collector.EmitThresholds() unconditionally at startup and let
  Perfetto drop the events when the category is disabled.
- Remove ToCounterValue() and pass float headroom values directly to
  the counter emitter.
@jimblacklercorp

Copy link
Copy Markdown
Author

PTAL

Comment thread thermal_monitor/main.cpp Resolved
@jimblacklercorp
jimblacklercorp merged commit c973b2c into android-graphics:main Nov 6, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants