Created
October 16, 2013 20:49
-
-
Save anonymous/7014604 to your computer and use it in GitHub Desktop.
Test direct IO in stap
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
/* 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