Skip to content

Optimize reboots with Android emulator initialization#5280

Open
jardondiego wants to merge 14 commits into
masterfrom
optimize-android-reboots
Open

Optimize reboots with Android emulator initialization#5280
jardondiego wants to merge 14 commits into
masterfrom
optimize-android-reboots

Conversation

@jardondiego
Copy link
Copy Markdown
Collaborator

@jardondiego jardondiego commented May 16, 2026

Goal

Significantly improve the startup efficiency and fuzzing throughput of Android bots by eliminating redundant, time-consuming device reboots during the initialization and sanitizer setup phases.

Background

Historically, Android bots suffered from excessive hard reboots (~60s penalty each) during task execution:

  1. Unconditional Final Reboots: initialize_device() unconditionally triggered a hard reboot at the end of its sequence to ensure a "good state," even if setup_asan_if_needed() had just performed a clean shell restart seconds prior.
  2. Sanitizer Option Reboots: Setting ASan options (sanitizer.set_options) writes to the /system partition. The helper function adb.write_data_to_file() contained a hardcoded rule forcing a reboot anytime a file in /system was modified.

@jardondiego jardondiego marked this pull request as ready for review May 17, 2026 00:44
@jardondiego jardondiego requested a review from a team as a code owner May 17, 2026 00:44
Comment thread src/clusterfuzz/_internal/platforms/android/device.py
Comment thread src/clusterfuzz/_internal/platforms/android/device.py
Comment thread src/clusterfuzz/_internal/platforms/android/sanitizer.py Outdated
Copy link
Copy Markdown
Collaborator

@IvanBM18 IvanBM18 left a comment

Choose a reason for hiding this comment

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

LGTM, minus some NITs

@jardondiego jardondiego requested a review from letitz May 18, 2026 19:27
Copy link
Copy Markdown
Collaborator

@fernandofloresg fernandofloresg left a comment

Choose a reason for hiding this comment

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

lgtm, would be nice to add those docstrings

Comment thread src/clusterfuzz/_internal/platforms/android/device.py
Copy link
Copy Markdown
Collaborator

@letitz letitz left a comment

Choose a reason for hiding this comment

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

I'm a bit confused about the main logic change in initialize_device(), see comment below.

Comment thread src/clusterfuzz/_internal/platforms/android/adb.py Outdated
Comment thread src/clusterfuzz/_internal/platforms/android/device.py Outdated
Comment thread src/clusterfuzz/_internal/platforms/android/device.py Outdated
Comment thread src/clusterfuzz/_internal/platforms/android/device.py Outdated
Comment thread src/clusterfuzz/_internal/platforms/android/sanitizer.py
Comment thread src/clusterfuzz/_internal/tests/core/platforms/android/device_test.py Outdated
Comment thread src/clusterfuzz/_internal/tests/core/platforms/android/device_test.py Outdated
Comment thread src/clusterfuzz/_internal/tests/core/platforms/android/device_test.py Outdated
Comment thread src/clusterfuzz/_internal/tests/core/platforms/android/device_test.py Outdated
Comment thread src/clusterfuzz/_internal/tests/core/platforms/android/device_test.py Outdated
@jardondiego jardondiego changed the title Optimize Android emulator initialization by batching reboots Optimize reboots with Android emulator initialization May 20, 2026
Diego Jardon added 14 commits May 21, 2026 01:32
This change reduces the number of reboots during Android device setup by:
- Adding a wait_for_reboot parameter to adb.write_data_to_file.
- Tracking reboot status in initialize_device to skip the final reboot if one already occurred.
- Disabling reboots when setting sanitizer options since the app restart is sufficient.

These optimizations improve bot startup efficiency and overall fuzzing throughput.
Adds `-> bool` return type hints and explanatory docstrings to:
- `configure_system_build_properties`
- `setup_asan_if_needed`

These updates clarify that the returned boolean indicates whether a device reboot occurred during the setup step.
- Revert configure_system_build_properties to not return a bool.
- Simplify initialize_device to only reboot if ASan setup did not reboot the device. This fixes the bug where it would reboot even if no properties changed.
- Refactor device_test.py to use helpers.patch instead of manual mock setups, matching the codebase style.
@jardondiego jardondiego force-pushed the optimize-android-reboots branch from bff881e to 2888eab Compare May 21, 2026 02:09
@jardondiego jardondiego requested a review from letitz May 21, 2026 02:28
Copy link
Copy Markdown
Collaborator

@letitz letitz left a comment

Choose a reason for hiding this comment

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

One substantive comment around skipping reboots after writing ASan options, otherwise just two small nits.

Comment on lines +889 to +890
should_reboot: Whether to reboot the device if a system file is modified.
Defaults to True.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's explain what a system file is, and no need to repeat the default argument value since that's self-documenting:

Suggested change
should_reboot: Whether to reboot the device if a system file is modified.
Defaults to True.
should_reboot: Whether to reboot the device after writing the file.
Only applies if `file_path` is under `/system`, since we need to
remount that partition as read-write first.

Comment thread src/clusterfuzz/_internal/platforms/android/sanitizer.py
"""Tests the reboot batching logic in initialize_device."""

def setUp(self):
from clusterfuzz._internal.tests.test_libs import helpers
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's move this to the top level, no need to make this import conditional on the test being run?

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.

4 participants