Skip to content

Instantly share code, notes, and snippets.

Created October 16, 2013 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7014604 to your computer and use it in GitHub Desktop.
Save anonymous/7014604 to your computer and use it in GitHub Desktop.
Test direct IO in stap
/* I can be compiled with gcc -o diotest diotest.c -O0 */
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <sysexits.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#define FILENAME "./test_dio.dat"
#define SIZE 1048576
int main() {
int fd = -1;
void *buf = NULL;
/* Open file and allocate some memory */
if ((fd = open(FILENAME, O_RDWR|O_DIRECT|O_CREAT, S_IRUSR|S_IWUSR)) < 0)
err(EX_OSERR, "Cannot open file %s", FILENAME);
if (posix_memalign(&buf, 512, SIZE))
err(EX_OSERR, "Cannot allocate memory");
/* Unlink the file */
if (unlink(FILENAME))
err(EX_OSERR, "Cannot unlink file %s", FILENAME);
memset(buf, 'A', SIZE);
/* Do some work */
while(1) {
printf("Doing some work\n");
if (write(fd, buf, SIZE) != SIZE)
err(EX_OSERR, "Cannot write to file %s", FILENAME);
if (lseek(fd, 0, SEEK_SET))
err(EX_OSERR, "Cannot seek in file %s", FILENAME);
if (read(fd, buf, SIZE) != SIZE)
err(EX_OSERR, "Cannot read file %s", FILENAME);
if (lseek(fd, 0, SEEK_SET))
err(EX_OSERR, "Cannot seek in file %s", FILENAME);
usleep(500000);
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment