/* * Example for efficient kernel block buffer usage when writing large files * * Copyright (C) 2011 RIEGL Research ForschungsGmbH * Copyright (C) 2011 Clifford Wolf * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * BTW: When you are interested in this one you might also find the system * call mincore() in combination with mmap() and mlock() fascinating. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include "check.h" #define MAXBLOCK (1024*256) char header[] = "This is the dump2disk output file\n"; char block[1024*1024*4 + 12345]; /* * Write data to a file and instruct the kernel to flush it from the block * buffer right after it has been written. */ ssize_t write_and_forget(int fd, const void *buf, size_t count) { size_t pos = 0; off_t file_pos = lseek(fd, 0, SEEK_CUR); while (pos < count) { ssize_t rc = write(fd, buf+pos, count-pos < MAXBLOCK ? count-pos : MAXBLOCK); if (rc <= 0) return rc < 0 ? rc : (ssize_t)pos; posix_fadvise(fd, file_pos, rc, POSIX_FADV_DONTNEED); file_pos += rc, pos += rc; } return count; } int main() { int fd, i, j; // open file and write header fd = CHECK(open("dump2disk.out", O_CREAT|O_TRUNC|O_WRONLY, 0666), >= 0); CHECK(write_and_forget(fd, header, sizeof(header)-1), == sizeof(header)-1); // generate test data for (i = 0, j = 0; i < (int)sizeof(block); i++, j = (j+1) % (2 + 'z' - '!')) block[i] = (j == 1 + 'z' - '!') ? '\n' : '!' + j; block[sizeof(block)-1] = '\n'; // write approx. 50MB of test data for (i = 0; i < 10; i++) CHECK(write_and_forget(fd, block, sizeof(block)), == sizeof(block)); // close output file close(fd); return 0; }