Last active
December 11, 2019 08:48
-
-
Save tkhai/5b788651cdb74c1dbff3500745878856 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _GNU_SOURCE | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <errno.h> | |
#define BLOCK_SIZE 4096 | |
#define STEP (BLOCK_SIZE * 16) | |
#define SIZE (4ULL * 1024 * 1024 * 1024) | |
int main(int argc) | |
{ | |
int fd, step, ret = 0; | |
unsigned long i; | |
void *buf; | |
if (posix_memalign(&buf, BLOCK_SIZE, SIZE)) { | |
perror("alloc"); | |
exit(1); | |
} | |
fd = open("file2.img", O_RDWR|O_CREAT|O_DIRECT); | |
if (fd < 0) { | |
perror("open"); | |
exit(1); | |
} | |
if (ftruncate(fd, SIZE)) { | |
perror("ftruncate"); | |
exit(1); | |
} | |
ret = fallocate(fd, 0, 0, SIZE); | |
if (ret) { | |
perror("fallocate"); | |
exit(1); | |
} | |
for (step = STEP - BLOCK_SIZE; step >= 0; step -= BLOCK_SIZE) { | |
printf("step=%u\n", step); | |
for (i = step; i < SIZE; i += STEP) { | |
errno = 0; | |
if (pwrite(fd, buf, BLOCK_SIZE, i) != BLOCK_SIZE) { | |
perror("pwrite"); | |
exit(1); | |
} | |
} | |
if (fsync(fd)) { | |
perror("fsync"); | |
exit(1); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment