improved flashing speed on macos#239
Conversation
|
@Ayush1325 please review |
|
@sengmonkham What is the non-existent |
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 |
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. |
Not sure why that would be. That release does not contain this PR. And I haven't merged any macos flashing performance PR recently. |
|
2s difference Screen.Recording.2026-02-05.at.00.03.36.movwith the changes
|
|
@Ayush1325 as you can see above, my testing shows an improvement of 2s. Testing with a 130MB compressed image. |
Can you try with a bigger image. Maybe a BeagleY-AI image? |
1db1888 to
fd9b4a3
Compare
bc9ecc6 to
8865ebc
Compare
|
@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. |
| } | ||
|
|
||
| #[repr(align(512))] | ||
| #[repr(align(4096))] |
There was a problem hiding this comment.
Does this cause a speed increase?
There was a problem hiding this comment.
tested it in isolation and got these results:
512: 16:51 mins
4096: 16:43 mins
8865ebc to
6e7e01a
Compare
bb-imager-cli/Cargo.toml
Outdated
| "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 |
There was a problem hiding this comment.
Not sure why this is showing up in diff. Probably some CRLF nonsense. Anyway remove this change.
bb-imager-cli/src/main.rs
Outdated
|
|
||
| // Simple stdin read since we don't have dialoguer | ||
| let mut input = String::new(); | ||
| if std::io::stdin().read_line(&mut input).is_ok() { |
bb-imager-cli/src/main.rs
Outdated
| 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") |
There was a problem hiding this comment.
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.
bb-imager-cli/src/main.rs
Outdated
| 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) |
There was a problem hiding this comment.
Once you extract this to a separate function, you can just return here and remove all the else blocks with a single default return.
bb-imager-cli/Cargo.toml
Outdated
| "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 |
There was a problem hiding this comment.
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?
bb-flasher-sd/src/pal/macos.rs
Outdated
| .await | ||
| .unwrap() | ||
| .map_err(|source| Error::FailedToFormat { source }) | ||
| tokio::task::spawn_blocking(move || { |
e507953 to
3b45018
Compare
- 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>
Signed-off-by: sen <kawkoi@proton.me>
3b45018 to
1c778c8
Compare

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
Flashing speed after the changes