如何实现emmc分区的最佳对齐?

如何实现emmc分区的最佳对齐?

我们有基于 bbb 的定制板,其中包含 eMMC。
我们创建了如下分区,

parted --script -a minimal /dev/mmcblk0 \
  mklabel gpt \
  mkpart primary 131072B 262143B \
  mkpart primary 262144B 393215B \
  mkpart primary 393216B 524287B \
  mkpart primary 524288B 1572863B \
  mkpart primary 1572864B 2621439B \ 
  mkpart primary 2621440B 3145727B \
  mkpart primary 3145728B 3276799B \ 
  mkpart primary 3276800B 8519679B \
  mkpart primary 8519680B 13762559B \
  mkpart primary 13762560B 19005439B \
  mkpart primary 19005440B 19267583B \
  mkpart primary 19267584B 19529727B \
  mkpart primary 19529728B 19791871B \
  mkpart primary 20MiB 31MiB \
  mkpart primary ext4 32MiB 232MiB \
  mkpart primary ext4 232MiB 432MiB \
  mkpart primary ext4 432MiB 532MiB \
  mkpart primary ext4 532MiB 592MiB \
  mkpart primary ext4 592MiB 792MiB \
  mkpart primary ext4 792MiB 827MiB \
  mkpart primary ext4 827MiB 3650MiB \

通过上述命令,我们可以对 eMMC 进行分区,但对齐方式设置为最小。我们想检查如何才能实现最佳对齐?实现最佳对齐有什么优势吗?我提到链接,但parted -a opt /dev/sdb mkpart primary 0% 100%不能使用类似的解决方案,因为我们需要以下结构。

# parted --list
Model: MMC MMC04G (sd/mmc)
Disk /dev/mmcblk0: 3842MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name        Flags
 1      131kB   262kB   131kB                
 2      262kB   393kB   131kB                
 3      393kB   524kB   131kB                
 4      524kB   1573kB  1049kB               
 5      1573kB  2621kB  1049kB               
 6      2621kB  3146kB  524kB                
 7      3146kB  3277kB  131kB                
 8      3277kB  8520kB  5243kB               
 9      8520kB  13.8MB  5243kB               
10      13.8MB  19.0MB  5243kB               
11      19.0MB  19.3MB  262kB                
12      19.3MB  19.5MB  262kB                
13      19.5MB  19.8MB  262kB                
14      21.0MB  32.5MB  11.5MB               
15      33.6MB  243MB   210MB   ext4         
16      243MB   453MB   210MB   ext4         
17      453MB   558MB   105MB   ext4         
18      558MB   621MB   62.9MB  ext4         
19      621MB   830MB   210MB   ext4         
20      830MB   867MB   36.7MB  ext4         
21      867MB   3827MB  2960MB  ext4         

对于当前命令,如果我们替换minimal为,optimal我们会看到来自parted的以下消息,

Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.
Warning: The resulting partition is not properly aligned for best performance.

对于实现最佳对齐有任何指示/建议/更正吗?

如建议的链接我提取了以下值,

# cat /sys/class/mmc_host/mmc1/mmc1\:0001/block/mmcblk0/queue/optimal_io_size
0
# cat /sys/class/mmc_host/mmc1/mmc1\:0001/block/mmcblk0/queue/minimum_io_size
512
# cat /sys/class/mmc_host/mmc1/mmc1\:0001/block/mmcblk0/alignment_offset
0
# cat /sys/class/mmc_host/mmc1/mmc1\:0001/block/mmcblk0/queue/physical_block_siz
e
512

mmc utils 给出以下输出,

# ./mmc extcsd read /dev/mmcblk0
=============================================
  Extended CSD rev 1.5 (MMC 4.41)
=============================================

Card Supported Command sets [S_CMD_SET: 0x01]
HPI Features [HPI_FEATURE: 0x03]: implementation based on CMD12
Background operations support [BKOPS_SUPPORT: 0x01]
Background operations status [BKOPS_STATUS: 0x02]
1st Initialisation Time after programmed sector [INI_TIMEOUT_AP: 0x7a]
Power class for 52MHz, DDR at 3.6V [PWR_CL_DDR_52_360: 0x00]
Power class for 52MHz, DDR at 1.95V [PWR_CL_DDR_52_195: 0x00]
Minimum Performance for 8bit at 52MHz in DDR mode:
 [MIN_PERF_DDR_W_8_52: 0x00]
 [MIN_PERF_DDR_R_8_52: 0x00]
TRIM Multiplier [TRIM_MULT: 0x06]
Secure Feature support [SEC_FEATURE_SUPPORT: 0x15]
Secure Erase Multiplier [SEC_ERASE_MULT: 0x02]
Secure TRIM Multiplier [SEC_TRIM_MULT: 0x03]
Boot Information [BOOT_INFO: 0x07]
 Device supports alternative boot method
 Device supports dual data rate during boot
 Device supports high speed timing during boot
Boot partition size [BOOT_SIZE_MULTI: 0x10]
Access size [ACC_SIZE: 0x06]
High-capacity erase unit size [HC_ERASE_GRP_SIZE: 0x08]
 i.e. 4096 KiB
High-capacity erase timeout [ERASE_TIMEOUT_MULT: 0x01]
Reliable write sector count [REL_WR_SEC_C: 0x01]
High-capacity W protect group size [HC_WP_GRP_SIZE: 0x01]
 i.e. 4096 KiB
Sleep current (VCC) [S_C_VCC: 0x08]
Sleep current (VCCQ) [S_C_VCCQ: 0x08]
Sleep/awake timeout [S_A_TIMEOUT: 0x10]
Sector Count [SEC_COUNT: 0x00728000]
 Device is block-addressed
Minimum Write Performance for 8bit:
 [MIN_PERF_W_8_52: 0x08]
 [MIN_PERF_R_8_52: 0x08]
 [MIN_PERF_W_8_26_4_52: 0x08]
 [MIN_PERF_R_8_26_4_52: 0x08]
Minimum Write Performance for 4bit:
 [MIN_PERF_W_4_26: 0x08]
 [MIN_PERF_R_4_26: 0x08]
Power classes registers:
 [PWR_CL_26_360: 0x00]
 [PWR_CL_52_360: 0x00]
 [PWR_CL_26_195: 0x00]
 [PWR_CL_52_195: 0x00]
Partition switching timing [PARTITION_SWITCH_TIME: 0x01]
Out-of-interrupt busy timing [OUT_OF_INTERRUPT_TIME: 0x02]
Card Type [CARD_TYPE: 0x07]
 HS Dual Data Rate eMMC @52MHz 1.8V or 3VI/O
 HS eMMC @52MHz - at rated device voltage(s)
 HS eMMC @26MHz - at rated device voltage(s)
CSD structure version [CSD_STRUCTURE: 0x02]
Command set [CMD_SET: 0x00]
Command set revision [CMD_SET_REV: 0x00]
Power class [POWER_CLASS: 0x00]
High-speed interface timing [HS_TIMING: 0x01]
Erased memory content [ERASED_MEM_CONT: 0x00]
Boot configuration bytes [PARTITION_CONFIG: 0x00]
 Not boot enable
 No access to boot partition
Boot config protection [BOOT_CONFIG_PROT: 0x00]
Boot bus Conditions [BOOT_BUS_CONDITIONS: 0x00]
High-density erase group definition [ERASE_GROUP_DEF: 0x01]
Boot write protection status registers [BOOT_WP_STATUS]: 0x00
Boot Area Write protection [BOOT_WP]: 0x00
 Power ro locking: possible
 Permanent ro locking: possible
 ro lock status: not locked
User area write protection register [USER_WP]: 0x00
FW configuration [FW_CONFIG]: 0x00
RPMB Size [RPMB_SIZE_MULT]: 0x01
Write reliability setting register [WR_REL_SET]: 0x00
 user area: existing data is at risk if a power failure occurs during a write operation
 partition 1: existing data is at risk if a power failure occurs during a write operation
 partition 2: existing data is at risk if a power failure occurs during a write operation
 partition 3: existing data is at risk if a power failure occurs during a write operation
 partition 4: existing data is at risk if a power failure occurs during a write operation
Write reliability parameter register [WR_REL_PARAM]: 0x05
 Device supports writing EXT_CSD_WR_REL_SET
 Device supports the enhanced def. of reliable write
Enable background operations handshake [BKOPS_EN]: 0x00
H/W reset function [RST_N_FUNCTION]: 0x00
HPI management [HPI_MGMT]: 0x01
Partitioning Support [PARTITIONING_SUPPORT]: 0x03
 Device support partitioning feature
 Device can have enhanced tech.
Max Enhanced Area Size [MAX_ENH_SIZE_MULT]: 0x0001ca
 i.e. 1875968 KiB
Partitions attribute [PARTITIONS_ATTRIBUTE]: 0x00
Partitioning Setting [PARTITION_SETTING_COMPLETED]: 0x00
 Device partition setting NOT complete
General Purpose Partition Size
 [GP_SIZE_MULT_4]: 0x000000
 [GP_SIZE_MULT_3]: 0x000000
 [GP_SIZE_MULT_2]: 0x000000
 [GP_SIZE_MULT_1]: 0x000000
Enhanced User Data Area Size [ENH_SIZE_MULT]: 0x000000
 i.e. 0 KiB
Enhanced User Data Start Address [ENH_START_ADDR]: 0x000000
 i.e. 0 bytes offset
Bad Block Management mode [SEC_BAD_BLK_MGMNT]: 0x00

编辑:按照“Глеб Майоров”的建议实现了最佳对齐,除了前 3 个分区(我无法更改)之外,其他小分区都更改为最小 1MiB 大小。并且除了 3 个分区外,所有其他分区似乎都是对齐的。这是我用于 eMMC 的最新分区脚本。(请注意:在下面的脚本中,我更改了上面脚本的分区大小)

parted --script -a optimal /dev/mmcblk0 \
  mklabel gpt \
  mkpart primary 128KiB 255KiB \
  mkpart primary 256KiB 383KiB \
  mkpart primary 384KiB 511KiB \
  mkpart primary 1MiB 2MiB \
  mkpart primary 2MiB 3MiB \ 
  mkpart primary 3MiB 4MiB \
  mkpart primary 4MiB 5MiB \ 
  mkpart primary 5MiB 10MiB \
  mkpart primary 10MiB 15MiB \
  mkpart primary 15MiB 20MiB \
  mkpart primary 20MiB 21MiB \
  mkpart primary 21MiB 22MiB \
  mkpart primary 22MiB 23MiB \
  mkpart primary 23MiB 30MiB \
  mkpart primary ext4 30MiB 530MiB \
  mkpart primary ext4 530MiB 1030MiB \
  mkpart primary ext4 1030MiB 1130MiB \
  mkpart primary ext4 1130MiB 1190MiB \
  mkpart primary ext4 1190MiB 1720MiB \
  mkpart primary ext4 1720MiB 1755MiB \
  mkpart primary ext4 1755MiB 100%

答案1

尝试与 eMMC 擦除块大小保持一致。它通常等于 0.5、1、2、4、8 MiB,具体取决于 eMMC 数据表。如果您发现块大小对齐浪费太多内存,请坚持页面大小,通常在 4..16 KiB 范围内。

尝试使分区大小和边界为擦除块大小的倍数,这样当文件系统写入第一个或最后一个FS块时,存储卡不必擦除和重写下一个/上一个分区的开头/结尾。

不要依赖parted的功能来对齐,只需使用计算器、笔和一张纸,并以扇区或字节为单位计算出正确的边界。

就我个人而言,我更喜欢对齐 8 MiB 边界,因为它不会浪费太多内存,并且无论特定擦除块大小如何,任何分区都在擦除块边界上开始和结束,因此我不需要搜索存储卡文档。

最佳对齐可降低写入放大系数,因此您的内存可以持续更长时间。

答案2

答案取决于磁盘。parted显示错误消息

not aligned: 2048s % 65535s != 0s

当我尝试对金士顿 SA400M8120G 进行分区时。然而,三星 980 PRO 250GB 可以在没有警告的情况下任意分区。dmesg插入金士顿驱动器时(通过 USB → NVMe 适配器),日志中会出现提示:

[81602.009178] scsi 6:0:0:0: Direct-Access     ASMT     2115             0    PQ: 0 ANSI: 6
[81602.010206] sd 6:0:0:0: Attached scsi generic sg1 type 0
[81602.021292] sd 6:0:0:0: [sda] 234441648 512-byte logical blocks: (120 GB/112 GiB)
[81602.021390] sd 6:0:0:0: [sda] Write Protect is off
[81602.021395] sd 6:0:0:0: [sda] Mode Sense: 43 00 00 00
[81602.021619] sd 6:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[81602.021903] sd 6:0:0:0: [sda] Optimal transfer size 33553920 bytes
[81602.061285] sd 6:0:0:0: [sda] Attached SCSI disk

最重要的信息是Optimal transfer size 33553920 bytes.即,为了“最佳对齐”,分区应从 33553920 (= 65535 × 512) 字节的整数倍开始。

基于如何使用parted对齐分区以获得最佳性能我通过查询文件系统解决了对齐问题sys。要分区的磁盘是/dev/sda,系统是当前(2020-12-22)Archlinux,内核 5.9.14-arch1-1,parted修订版 3.3:

DISK="sda"
OPTIMAL_IO_SIZE=$(cat /sys/block/${DISK}/queue/optimal_io_size)
ALIGNMENT_OFFSET=$(cat /sys/block/${DISK}/alignment_offset)
PHYSICAL_BLOCK_SIZE=$(cat /sys/block/${DISK}/queue/physical_block_size)

通过这些变量,我可以计算分区的起始扇区。知道 的值OPTIMAL_IO_SIZEPHYSICAL_BLOCK_SIZE调整魔法11 和 128 之间的数字:

START_EFI=$(((OPTIMAL_IO_SIZE+ALIGNMENT_OFFSET)/PHYSICAL_BLOCK_SIZE))
START_SWAP=$((START_EFI+11*OPTIMAL_IO_SIZE/PHYSICAL_BLOCK_SIZE))
START_ROOT=$((START_SWAP+128*OPTIMAL_IO_SIZE/PHYSICAL_BLOCK_SIZE))

有了这些知识,调用parted就很简单了:

parted -s -a optimal /dev/${DISK} mklabel gpt \
 mkpart "Efi"  fat32      ${START_EFI}s  $((START_SWAP-1))s \
 mkpart "Swap" linux-swap ${START_SWAP}s $((START_ROOT-1))s \
 mkpart "root" ext4       ${START_ROOT}s  100% \
 set 1 esp on \
 print

验证对齐是否正确:

parted /dev/sda align-check optimal 1 \
 align-check optimal 2 \
 align-check optimal 3

输出如下:

1 aligned
2 aligned
3 aligned

如前所述,上面的答案适用于“Kingston SA400M8120G”。我必须承认,我还没有发现任何其他设备(SATA 驱动器、SDCard、NVMe 驱动器、USB 记忆棒)报告的“最佳传输大小”不同于 0。由此我可以得出结论,后面的驱动器不需要特殊对齐。并且parted不报告它们的对齐警告。

相关内容