Created
March 28, 2022 20:18
-
-
Save ddk50/987c645b0f476e9449eb9786a9bf8c4d to your computer and use it in GitHub Desktop.
a sample for O_DIRECT open
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 <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 * 10ULL; | |
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]); | |
/* if ((buf = malloc(bs)) == NULL) { */ | |
/* printf("could not alloc"); */ | |
/* exit(EXIT_FAILURE); */ | |
/* } */ | |
/* 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