0

As far as I know, in linux systems, when modifying the content of a file, the pages containing the content of the file in page cache will be marked dirty, and will be flushed to disk eventually.

What I'm wondering is that, when these pages are flushed to disk, are they flushed in blocks?

For example, if the block size is 4kB and I need to flush some content of 1024kB, is the disk written 1024 / 4 = 256 times?

2

That's a pretty complex topic, and depends on the disk, the disk controller and kernel settings.

In general, the kernel will attempt to be as efficient as it can. For example, if you update the same block multiple times within an adjustable time window (typically 30 seconds or so), and don't explicitly force syncing all the way to the disk each time, most of your write operations will only update the data in the cache and only the ultimate result will actually go to the disk.

If you write a long series of consecutive blocks, the kernel will certainly attempt to execute it in as few and as large chunks as the storage controller and the disk itself will allow.

The kernel's I/O scheduler may also optimize the ordering of disk operations to achieve most efficient disk access. This optimization can be mostly irrelevant in virtual machines and on SSDs, and so it can be switched off. (SSDs are plenty fast even if you access random blocks in a shotgun fashion; on virtual machines, the hypervisor will usually redo the optimization based on the entire set of VMs and all their disk operations anyway, so trying to micro-optimize on the level of a single VM is wasted effort.)

Some disks may have restrictions or recommendations on I/O operation sizes:

# fdisk -l /dev/sdb
Disk /dev/sdb: 1.8 TiB, 2000398934016 bytes, 3907029168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

For example, this HDD internally uses 4k sector size, although it emulates traditional 512-byte disk sectors. As a result, a minimum I/O size of 4k is specified.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.