Call readahead in batches before calling readint* fds = (int*)malloc(sizeof(int)*1000);int* fd_size = (int*)malloc(sizeof(int)*1000);for (int i = 0; i < 1000; ++i){ int fd = open_file(); int size = stat_file_size(); readahead(fd, 0, size); fds[i] = fd; fd_size[i] = size;}char* buf = (char*)malloc(10*1024*1024);for (int i = 0; i < 1000; ++i){ read(fds[i], buf, fd_size[i]); // do something with buf close(fds[i]);}
If you are interested, you can write the code for actual testing. Note that you must write the dirty page and clear the page cache before testing. Execute the following command:
sync&&sudosysctl-wvm.drop_caches=3
Check the Cached and Dirty items in/proc/meminfo to check whether the configuration takes effect.
The test shows that the second method is about 10%-20% faster than the first method. In this scenario, the read operation is performed immediately after the readahead is executed in batches, and the optimization space is limited, if you can call readahead some time before the read operation, the read speed of the read operation will be greatly improved.
This solution actually uses the page cache of the operating system, that is, it triggers the operating system to read files to the page cache in advance, in addition, the operating system provides a complete set of mechanisms for page missing processing, cache hit, and cache elimination. Although users can also perform Cache Management for their own data, however, it is no different from using page cache directly, and it will increase the maintenance cost.
MmapMmap is a memory ing file method that maps a file or other objects to the address space of a process, implements a one-to-one ing between the file disk address and a segment of virtual address in the virtual address space of the process. The function prototype is as follows:
void*mmap(void*addr,size_tlength,intprot,intflags,intfd,off_toffset);
After such a ing relationship is implemented, the process can read and write the memory segment using a pointer, and the system will automatically write back the dirty page to the corresponding file disk, that is, the operation on the file is completed without calling the read, write, and other system call functions. As shown in
In addition to system calls such as read and write, mmap can also reduce the number of copies in the memory. For example, during read calls, a complete process is for the operating system to read disk files to the page cache, copy data from the page cache to the buffer passed by read. If mmap is used, the operating system only needs to read the disk to the page cache, then, the user can directly operate the memory mapped to mmap through the pointer, reducing the data copying from the kernel state to the user State.Mmap is suitable for frequent reads and writes to the same region. For example, if a 64 M file stores some index information, we need to modify it frequently and make it persistent to the disk, in this way, the file can be mapped to the user's virtual memory through mmap, and then the memory area can be modified through the pointer. The operating system will automatically fl the modified part back to the disk, you can also manually call msync to manually fl the disk.
From: http:// OS .51cto.com/art/201609/517642.htm
URL: www.linuxprobe.com/linux-read-write-tuning.html