Skip to content

Instantly share code, notes, and snippets.

Created October 16, 2013 20:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7014284 to your computer and use it in GitHub Desktop.
Save anonymous/7014284 to your computer and use it in GitHub Desktop.
Test vfs measurement activity with stap script.
/* I can be built with gcc -o mmaptest mmaptest.c -O0 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <sysexits.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#define FILENAME "./test_map.bin"
#define MAP_SIZE 1048576
int main(const int argc, const char **argv) {
int fd = -1, i=0;
int shareflag = 0;
char *map = NULL;
char r=0, w=0;
if (argc < 2)
errx(EX_SOFTWARE, "Must supply the argument 'private' or 'shared' to determine map mode");
if (strncmp(argv[1], "shared", 8) == 0)
shareflag = MAP_SHARED;
else
if (strncmp(argv[1], "private", 8) == 0)
shareflag = MAP_PRIVATE;
else
errx(EX_SOFTWARE, "Must supply the argument 'private' or 'shared' to determine map mode");
/* Create the file */
if ((fd = open(FILENAME, O_CLOEXEC|O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0)
err(EX_OSERR, "Cannot create file %s", FILENAME);
/* Make this file the right size */
if (ftruncate(fd, MAP_SIZE))
err(EX_OSERR, "Cannot truncate file %s", FILENAME);
printf("File has been created.\n");
/* Map the file, close the fd, unlink the file */
if ((map = mmap(NULL, MAP_SIZE, PROT_READ|PROT_WRITE, shareflag, fd, 0)) == MAP_FAILED)
err(EX_OSERR, "Could not create memory map");
close(fd);
fd = -1;
if (unlink(FILENAME))
errx(EX_OSERR, "Could not delete the filename %s", FILENAME);
printf("Map has been generated\n");
/* Do it */
while (1) {
printf("Doing map work..\n");
for (i=0; i < MAP_SIZE; i++) {
r = map[i];
map[i] = w++ % 256;
}
usleep(500000);
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment