akaros/tests/mmap.c
<<
>>
Prefs
   1/* Copyright (c) 2014 The Regents of the University of California
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * See LICENSE for details.
   4 *
   5 * mmap_test: dumping ground for various tests, such as PFs on files. */
   6
   7#include <stdio.h>
   8#include <pthread.h>
   9#include <stdlib.h>
  10#include <unistd.h>
  11#include <sys/time.h>
  12
  13#include <sys/mman.h>
  14#include <fcntl.h>
  15#include <parlib/parlib.h>
  16#include <parlib/timing.h>
  17
  18#include <sys/types.h>
  19#include <sys/stat.h>
  20#include <unistd.h>
  21        
  22void *addr = 0;
  23size_t nr_pgs = 1;
  24#define STRIDE 256
  25
  26static void mmap_test(void)
  27{
  28        assert(addr);
  29        for (int *i = addr; (void*)i < addr + nr_pgs * PGSIZE; i += STRIDE) {
  30                *i += 1;
  31        }
  32}
  33
  34void *worker_thread(void* arg)
  35{       
  36        while (1) {
  37                mmap_test();
  38                uthread_sleep(1);
  39        }
  40        return 0;
  41}
  42
  43int main(int argc, char** argv) 
  44{
  45        pthread_t child;
  46        void *child_ret;
  47        int fd;
  48        struct stat statbuf;
  49
  50        if (argc < 2) {
  51                printf("Usage: %s FILENAME [NR_PGS]\n", argv[0]);
  52                exit(-1);
  53        }
  54        /* if you're going to create, you'll need to seek too */
  55        //fd = open(argv[1], O_RDWR | O_CREAT, 0666);
  56        fd = open(argv[1], O_RDWR, 0666);
  57        if (fd < 0) {
  58                perror("Unable to open file");
  59                exit(-1);
  60        }
  61        if (argc < 3)
  62                nr_pgs = 1;
  63        else
  64                nr_pgs = atoi(argv[2]);
  65        if (fstat(fd, &statbuf)) {
  66                perror("Stat failed");
  67                exit(-1);
  68        }
  69        nr_pgs = MIN(nr_pgs, (ROUNDUP(statbuf.st_size, PGSIZE) >> PGSHIFT));
  70        addr = mmap(0, nr_pgs * PGSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
  71                    0);
  72        if (addr == MAP_FAILED) {
  73                perror("mmap failed");
  74                exit(-1);
  75        }
  76        printf("Running as an SCP\n");
  77        mmap_test();
  78        printf("Spawning worker thread, etc...\n");
  79        pthread_create(&child, NULL, &worker_thread, NULL);
  80        pthread_join(child, &child_ret);
  81} 
  82