'Writing to a file, confused about speed results

First, this is done on a unix-based system. I was asked to simulate writes to a 128MB file, using different methods. First writing to an aligned random location (lseek to a location that is a multiple of the write size) in the file, with changing write sizes (1MB, 256KB,64KB,16KB,4KB), and keep writing until 128MB are written. Then to perform the same operation but with the O_DIRECT flag, and lastly perform it again with unaligned location without the flag.

The results I got were that for the aligned + O_DIRECT writes, decreasing the write size drastically reduces performance, which is understandable because with O_DIRECT it will access the disk directly more with smaller write sizes.

With the unaligned writes, reducing write sizes again reduces performance which makes sense because we have more writes done to different locations in the file, and then when the cache is flushed, during the physical writing, the disk might go back and forth between sectors to write the data. More writes, more going back and forth.

The results that confused me were for the aligned writes without the O_DIRECT flag. This time for smaller write sizes, the performance drastically increased, with 32,678 4KB writes being a lot faster than 128 1MB writes. And I can't figure out what would cause such an increase. I should also add the unix computer i'm working with is a remote one, and I do not know its specs, i.e what hdd it has, whats its sector size and so on. I guess I should also add that the code to write unaligned and aligned is exactly the same, the only change is the location that lseek receives.

One thing I did think about, if the physical sector size is say, 4KB, and the PC has enough RAM, all the write operations can be cached in pages and then flushed. Then if the disk goes from the smallest address to the biggest, it can go sector by sector forward because its aligned, which might mean it can write everything in one go.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source