我正在尝试将多个文件写入USB闪存可移动驱动器FAT32格式
我不是Windows内部的专家,所以我要求你的经验和建议。
虽然大文件的写入速度受USB和USB设备的限制,但小文件的写入速度却非常慢
使用DiskMon我可以看到Win7每个文件写入USB驱动器扇区的内容
一些事实:
写入目录由8x 512字节扇区块完成,块大小为4KB,边界为4KB
写入FAT由1-8x 512字节扇区块完成,最大4KB,边界为4KB
写入FAT扇区数量取决于文件大小,簇大小,扇区大小和FAT扇区边界的交叉
一些方程式:
filetotalclusters =(filesize + clustersize-1)/ clustersize
clusterspersector = sectorsize / 4
fileFATsectors = filetotalclusters / clusterpersector,当FAT扇区边界交叉时,可选择+1扇区
因此对于32KB集群和512字节扇区,1个FAT扇区覆盖4MB的文件大小,8个扇区(4KB)覆盖32MB文件。
not big file single block write (<32MB and FAT chain doesnt cross sector boundary):
create(),write(),close() 6 writes per file
1) create() write dir 4KB (name of file, default attributes and current times)
2) write() write file data
3) write dir 4KB (first cluster of chain)
4) write FAT1 up to 4KB (chain of clusters)
5) write FAT2 up to 4KB (chain of clusters)
6) close() write dir 4KB (I guess it writes new file size)
bigger file or cross of FAT sector boundary and single block write:
create(),write(),close() 4+2x(ceiling of filesize/32MB) writes per file
1) create() write dir 4KB (name of file, default attributes and current times)
2) write() write file data
3) write dir 4KB (first cluster of chain)
4) write FAT1 up to 4KB (chain of clusters)
5) write FAT2 up to 4KB (chain of clusters)
6) write FAT1 up to 4KB (continue chain of clusters)
7) write FAT2 up to 4KB (continue chain of clusters)
8) close() write dir 4KB (I guess it writes new file size)
So for long files added 2x(filesize/32MB) writes to the FAT.
with file times set
create(),setfiletime(),write(),close() 7 writes per file
1) create() write dir 4KB (name of file, default attributes and current times)
2) setfiletime() write dir 4KB (new file times: creation,lastaccess,lastwrite)
3) write() write file data
4) write dir 4KB (first cluster of chain)
5) write FAT1 up to 4KB (chain of clusters)
6) write FAT2 up to 4KB (chain of clusters)
7) close() write dir 4KB (I guess it writes new file size)
with file length set before write to avoid fragmentation.
create(),setfiletime(),setlength(),write(),close() 7 writes per file
1) create() write dir 4KB (name of file, default attributes and current times)
2) setfiletime() write dir 4KB (new file times: creation,lastaccess,lastwrite)
3) setlength() write dir 4KB (first cluster of chain and file size)
4) write FAT1 up to 4KB (chain of clusters)
5) write FAT2 up to 4KB (chain of clusters)
6) write() write file data
7) close() write dir 4KB (I dont know what it writes as file times, file length and cluster already set)
if multiple block writes, 13 writes for 2 blocks write (<32MB and no FAT sector crossed) and more.
create(),setfiletime(),setlength(),write1(),write2(),writeX(),close()
1) create() write dir 4KB (name of file, default attributes and current times)
2) setfiletime() write dir 4KB (new file times: creation,lastaccess,lastwrite)
3) setlength() write dir 4KB (first cluster of chain)
4) write FAT1 up to 4KB (chain of clusters)
5) write FAT2 up to 4KB (chain of clusters)
6) write1() write file data block1
7) write2() write file data block2 (and it triggers write of buffered FAT and Dir sectors)
8-9..) write multiple FAT1 and FAT2 depending on file size which should be already written on step 4 and 5.
10) write dir 4KB which should be already written on step 3.
11) writeX() write file data blockX (and it triggers write of buffered FAT and Dir sectors)
12) write dir 4KB which should be already written on step 3.
13-14..) write multiple FAT1 and FAT2 depending on file size which should be already written on step 4 and 5
15) close() write dir 4KB (I dont know what it writes as file times, file length and cluster already set)
16-17..) write multiple FAT1 and FAT2 depending on file size which should be already written on step 4 and 5
我猜多个块中的文件以某种方式使得Windows缓存中的Dir和FAT扇区变脏 因此,无论其内容如何变化,它都会一次又一次地重写它们 如何避免这种行为?
因此,为了减少每个文件的写入量,似乎需要使用:
1)簇大小尽可能大,32KB或64KB,但小文件会占用太多空间
2)在单个数据块写入时写入文件或尽可能少的块来减少触发过多的FAT和Dir写入。
但很难分配大的contiguos内存块,因此可能是64MB的上限。
如果我在多个线程中写文件,我会看到FAT和Dir的写入量减少两倍 似乎Windows Cache允许在它急速刷新磁盘之前修改FAT和Dir缓冲区两次 无论并行线程数量多少,只需进行两次修改 我不知道是否可以增加USB可移动驱动器的刷新时间 编写许多小文件的速度很慢与FAT和Dir的许多重复写入有关 如果文件大小为1KB,则由1024个文件共享1个FAT 4KB块,由128个文件共享1个Dir 4KB块 如果每个文件操作系统多次写入Dir和FAT,您可以看到它如何浪费资源:时间和闪存生命。
我可以在USB可移动驱动器上强制执行写缓存,它确实加快了第一个文件的写入速度,但总时间更差。
我不知道它的原因,可能是多次写入“毒药”FAT和Dir缓存扇区,所以他们开始一次又一次地与数据混合写入。