Skip to content

improved flashing speed on macos#239

Merged
Ayush1325 merged 2 commits intobeagleboard:mainfrom
sengmonkham:fix/macos-slow-flashing
Feb 8, 2026
Merged

improved flashing speed on macos#239
Ayush1325 merged 2 commits intobeagleboard:mainfrom
sengmonkham:fix/macos-slow-flashing

Conversation

@sengmonkham
Copy link
Copy Markdown
Contributor

This PR addresses the slow flashing speed issue observed on macOS by disabling OS-level file caching for the target device and optimizing buffer alignment.

#58

Changes
Enabled F_NOCACHE: Uses fcntl to set the F_NOCACHE flag on the file descriptor obtained via authopen. This forces direct I/O to the SD card, bypassing the unified buffer cache which was causing performance bottlenecks.

Increased Buffer Alignment: Updated DirectIoBuffer alignment from 512 bytes to 4096 bytes (page size) to ensure optimal compatibility with direct I/O operations and strict alignment requirements on macOS.

Flashing speed before changes

kawkoiharu@kawkois-MacBook-Air ~ % cargo run --manifest-path bb-flasher-sd/Cargo.toml --example flash_benchmark --features macos_authopen -- /dev/disk4
error: manifest path `bb-flasher-sd/Cargo.toml` does not exist
kawkoiharu@kawkois-MacBook-Air ~ % cd code/gsoc/bb-imager-rs
kawkoiharu@kawkois-MacBook-Air bb-imager-rs % clear
kawkoiharu@kawkois-MacBook-Air bb-imager-rs % cargo run --manifest-path bb-flasher-sd/Cargo.toml --example flash_benchmark --features macos_authopen -- /dev/disk4
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.39s
     Running `target/debug/examples/flash_benchmark /dev/disk4`
Creating dummy image (100MB)...
Starting flash to "/dev/disk4"...
Progress: 100.0%
Flash completed successfully.
Time taken: 33.98s
Average Speed: 2.94 MB/s

Flashing speed after the changes

Starting flash to "/dev/disk4"...
Progress: 100.0%
Flash completed successfully.
Time taken: 19.48s
Average Speed: 5.13 MB/s
kawkoiharu@kawkois-MacBook-Air bb-imager-rs %```

@sengmonkham sengmonkham marked this pull request as draft February 4, 2026 10:42
@sengmonkham sengmonkham marked this pull request as ready for review February 4, 2026 10:43
@sengmonkham sengmonkham changed the title improved flashing speed in macos improved flashing speed on macos Feb 4, 2026
@sengmonkham
Copy link
Copy Markdown
Contributor Author

@Ayush1325 please review

@Ayush1325
Copy link
Copy Markdown
Member

@sengmonkham What is the non-existent flash_benchmark example?
Also I can see you are using debug build. Please provide results with release build.

@sengmonkham
Copy link
Copy Markdown
Contributor Author

@sengmonkham What is the non-existent flash_benchmark example? Also I can see you are using debug build. Please provide results with release build.

Sorry for the confusion. I originally used a local flash_benchmark script to bypass the CLI's strict SD card detection (since my test drive wasn't being picked up) and to measure the speeds.

Regarding the release build, I have verified the fix in release mode using the main CLI tool (patched to bypass detection for my setup).

kawkoiharu@kawkois-MacBook-Air bb-imager-rs % cargo run --release --manifest-path bb-imager-cli/Cargo.toml -- flash sd ~/micropython.img.xz /dev/disk4
Compiling bb-flasher v0.1.0 ...
Compiling bb-imager-cli v0.0.19 ...
Finished release profile [optimized] target(s) in 8.03s
Running target/release/bb-imager-cli flash sd /Users/kawkoiharu/micropython.img.xz /dev/disk4
[1] Preparing
[2] Flashing [██████████████████████...██████████████████████] [100 %]

@Ayush1325
Copy link
Copy Markdown
Member

@sengmonkham What is the non-existent flash_benchmark example? Also I can see you are using debug build. Please provide results with release build.

Sorry for the confusion. I originally used a local flash_benchmark script to bypass the CLI's strict SD card detection (since my test drive wasn't being picked up) and to measure the speeds.

Regarding the release build, I have verified the fix in release mode using the main CLI tool (patched to bypass detection for my setup).

kawkoiharu@kawkois-MacBook-Air bb-imager-rs % cargo run --release --manifest-path bb-imager-cli/Cargo.toml -- flash sd ~/micropython.img.xz /dev/disk4 Compiling bb-flasher v0.1.0 ... Compiling bb-imager-cli v0.0.19 ... Finished release profile [optimized] target(s) in 8.03s Running target/release/bb-imager-cli flash sd /Users/kawkoiharu/micropython.img.xz /dev/disk4 [1] Preparing [2] Flashing [██████████████████████...██████████████████████] [100 %]

Can you use time or something while running the CLI. I want the PR description to have info regarding how much real performance improvement this PR causes.

I will also need to check if the new alignment works properly on Windows and Linux myself.

@Ayush1325
Copy link
Copy Markdown
Member

confirmed that the latest CI https://github.com/beagleboard/bb-imager-rs/releases/download/continuous-release/BeagleBoard.Imaging.Utility_alpha_aarch64.dmg flashes much faster than https://github.com/beagleboard/bb-imager-rs/releases/download/0.0.19/BeagleBoard.Imaging.Utility_0.0.19_aarch64.dmg

Not sure why that would be. That release does not contain this PR. And I haven't merged any macos flashing performance PR recently.

@sengmonkham
Copy link
Copy Markdown
Contributor Author

2s difference
without the changes

Screen.Recording.2026-02-05.at.00.03.36.mov

with the changes
https://github.com/user-attachments/assets/d37879cf-0110-4e4f-9048-58eec76588a9

@sengmonkham What is the non-existent flash_benchmark example? Also I can see you are using debug build. Please provide results with release build.

Sorry for the confusion. I originally used a local flash_benchmark script to bypass the CLI's strict SD card detection (since my test drive wasn't being picked up) and to measure the speeds.
Regarding the release build, I have verified the fix in release mode using the main CLI tool (patched to bypass detection for my setup).
kawkoiharu@kawkois-MacBook-Air bb-imager-rs % cargo run --release --manifest-path bb-imager-cli/Cargo.toml -- flash sd ~/micropython.img.xz /dev/disk4 Compiling bb-flasher v0.1.0 ... Compiling bb-imager-cli v0.0.19 ... Finished release profile [optimized] target(s) in 8.03s Running target/release/bb-imager-cli flash sd /Users/kawkoiharu/micropython.img.xz /dev/disk4 [1] Preparing [2] Flashing [██████████████████████...██████████████████████] [100 %]

Can you use time or something while running the CLI. I want the PR description to have info regarding how much real performance improvement this PR causes.

I will also need to check if the new alignment works properly on Windows and Linux myself.

@sengmonkham
Copy link
Copy Markdown
Contributor Author

@Ayush1325 as you can see above, my testing shows an improvement of 2s. Testing with a 130MB compressed image.
Without changes: 25s
With changes: 23s

@Ayush1325
Copy link
Copy Markdown
Member

@Ayush1325 as you can see above, my testing shows an improvement of 2s. Testing with a 130MB compressed image. Without changes: 25s With changes: 23s

Can you try with a bigger image. Maybe a BeagleY-AI image?

@sengmonkham sengmonkham force-pushed the fix/macos-slow-flashing branch from 1db1888 to fd9b4a3 Compare February 5, 2026 13:04
@sengmonkham sengmonkham force-pushed the fix/macos-slow-flashing branch 5 times, most recently from bc9ecc6 to 8865ebc Compare February 5, 2026 16:06
@sengmonkham
Copy link
Copy Markdown
Contributor Author

sengmonkham commented Feb 5, 2026

@Ayush1325 I have added an option for the cli users to switch to /dev/rdisk4 when trying to use /dev/disk4 for faster flashing experience. In my experience flashing the BeagleY-AI image it took 16mins using the rdisk4 and 23mins with disk4.

I also added a fix for the "Resource busy" error on macOS. The CLI now ensures the disk is unmounted before opening, which is required for raw device access. (This is essential for the rdisk workflow to work smoothly, so I included it here).

I could not manage to further improve the flashing speed, no mater what i did the speeds did not change.
Screenshot 2026-02-05 at 21 21 04

}

#[repr(align(512))]
#[repr(align(4096))]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this cause a speed increase?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

tested it in isolation and got these results:
512: 16:51 mins
4096: 16:43 mins

@sengmonkham sengmonkham force-pushed the fix/macos-slow-flashing branch from 8865ebc to 6e7e01a Compare February 5, 2026 18:51
"dist/.target/man" = "/usr/share/man/man1"
"dist/.target/shell-comp/_bb-imager-cli" = "/usr/share/zsh/site-functions/_bb-imager-cli"
"dist/.target/shell-comp/bb-imager-cli.bash" = "/usr/share/bash-completion/bb-imager-cli.bash"
"dist/.target/shell-comp/bb-imager-cli.bash" = "/usr/share/bash-completion/bb-imager-cli.bash" No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not sure why this is showing up in diff. Probably some CRLF nonsense. Anyway remove this change.


// Simple stdin read since we don't have dialoguer
let mut input = String::new();
if std::io::stdin().read_line(&mut input).is_ok() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's fine to use expect here.

let wifi = wifi_ssid.map(|x| (x, wifi_password.unwrap()));

#[cfg(target_os = "macos")]
let dst = if dst.to_string_lossy().starts_with("/dev/disk")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please extract this to it's own function. Maybe something like validate. You can define 2 versions, one for macos and other, a no-op for other OS.

let input = input.trim().to_lowercase();
if input.is_empty() || input == "y" || input == "yes" {
let _ = term.write_line(&format!("Switching to {}\n", rdisk));
PathBuf::from(rdisk)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Once you extract this to a separate function, you can just return here and remove all the else blocks with a single default return.

"dist/.target/man" = "/usr/share/man/man1"
"dist/.target/shell-comp/_bb-imager-cli" = "/usr/share/zsh/site-functions/_bb-imager-cli"
"dist/.target/shell-comp/bb-imager-cli.bash" = "/usr/share/bash-completion/bb-imager-cli.bash"
"dist/.target/shell-comp/_bb-imager-cli" = "/usr/share/zsh/site-functions/_bb-imager-cli" No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have absolutely no idea why it is showing as diff, all i did was remove the last change as you requested. should i re add those lines?

.await
.unwrap()
.map_err(|source| Error::FailedToFormat { source })
tokio::task::spawn_blocking(move || {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why change this?

@sengmonkham sengmonkham force-pushed the fix/macos-slow-flashing branch from e507953 to 3b45018 Compare February 7, 2026 09:36
Sen and others added 2 commits February 7, 2026 15:14
- Increase buffer alignment to 4096 bytes for better Direct I/O performance.
- Add interactive prompt in CLI to switch to raw device (/dev/rdisk) on macOS for significantly faster flashing.
- Fix 'Resource busy' error by automatically unmounting the disk before flashing in the CLI.

Signed-off-by: Sen <kawkoi@proton.mail>
@sengmonkham sengmonkham force-pushed the fix/macos-slow-flashing branch from 3b45018 to 1c778c8 Compare February 7, 2026 09:48
@Ayush1325 Ayush1325 merged commit 4cd7adf into beagleboard:main Feb 8, 2026
11 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.

3 participants