Skip to content

Instantly share code, notes, and snippets.

@GavinRay97
Created December 12, 2022 16:09
Show Gist options
  • Save GavinRay97/5da61ca8bfce9aad5e3c1db1f0c57775 to your computer and use it in GitHub Desktop.
Save GavinRay97/5da61ca8bfce9aad5e3c1db1f0c57775 to your computer and use it in GitHub Desktop.
Linux 6.1 new Direct IO (DIO) "statx" file information example
#include <fcntl.h> // open, O_CREAT, O_RDWR, AT_FDCWD, AT_STATX_SYNC_AS_STAT
#include <stdio.h> // printf
#include <sys/stat.h> // statx, STATX_ALL
#include <unistd.h> // pwrite
#define TMPFILE "test.txt"
int
main()
{
// Create a temporary file called "test.txt"
int fd;
if (-1 == (fd = open(TMPFILE, O_CREAT | O_RDWR, 0666)))
{
perror("open");
return 1;
}
// Write 10 bytes to the file
if (10 != pwrite(fd, "0123456789", 10, 0))
{
perror("pwrite");
return 1;
}
// Get the file descriptor
struct statx statxbuf;
if (0 != statx(AT_FDCWD, TMPFILE, AT_STATX_SYNC_AS_STAT, STATX_ALL, &statxbuf))
{
perror("statx");
return 1;
}
printf("File size: %llu \n", statxbuf.stx_size);
printf("Direct IO: mem-alignment=%u, offset-alignment=%u \n",
statxbuf.stx_dio_mem_align,
statxbuf.stx_dio_offset_align);
return 0;
}
[user@MSI dio-test]$ clang main.cpp -o main
[user@MSI dio-test]$ ./main
File size: 10
Direct IO: mem-alignment=XXX, offset-alignment=XXX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment