Skip to content

Instantly share code, notes, and snippets.

@ddk50
Created March 28, 2022 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddk50/c9232bac56b557cfdf3ac537a119e730 to your computer and use it in GitHub Desktop.
Save ddk50/c9232bac56b557cfdf3ac537a119e730 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
int main (int argc, char *argv[])
{
int file;
unsigned int bs = 1024 * 1024 * 1; // 1MB
uint64_t total_size = 1024 * 1024 * 1024 * 1ULL;
int use_reopen = 1;
int first_time = 1;
uint64_t pos = 0;
uint8_t *buf = NULL;
int i;
int ret;
printf("close benchmark\n");
if (argc < 2) {
printf("lack of target path\n");
exit(EXIT_FAILURE);
}
printf("target %s\n", argv[1]);
/* must be align in O_DIRECT */
if (posix_memalign((void **)&buf, 512, bs)) {
perror("posix_memalign");
exit(EXIT_FAILURE);
}
printf("trying to open...\n");
if ((file = open(argv[1], O_WRONLY | O_DIRECT)) < 0) {
perror("open\n");
exit(EXIT_FAILURE);
}
printf("done\n");
printf("writing buffer...\n");
for (pos = 0 ; pos < total_size ; ) {
ret = lseek(file, pos, SEEK_SET);
if (ret == -1) {
perror("lseek\n");
exit(EXIT_FAILURE);
}
ret = write(file, buf, bs);
if (ret < 0) {
perror("write\n");
exit(EXIT_FAILURE);
}
pos += ret;
}
printf("total pos was wrote: 0x%lx\n", pos);
close(file);
printf("done\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment