Advertisement
Guest User

Untitled

a guest
Mar 9th, 2012
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1.  
  2. //compilation: g++ -O3 -Wall mmap_test.cpp
  3.  
  4. //run:
  5. //      time ./a.out large_file 0 #mmap test
  6. //      time ./a.out large_file 1 #fread test
  7.  
  8. //3Gb file generation:
  9. //    python -c "s = 'x'*(2**27); f = open('large_file', 'wb'); [f.write(s) for i in xrange(3*8)]; f.close()"
  10. //
  11. //
  12.  
  13. #include <sys/types.h>
  14. #include <sys/mman.h>
  15. #include <sys/stat.h>
  16. #include <sys/stat.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19.  
  20.  
  21. void fatal(const char *msg) {
  22.     fprintf(stderr, "%s\n", msg);
  23.     exit(1);
  24. }
  25.  
  26. off_t file_length(FILE *f) {
  27.     struct stat stat_buf;
  28.     if (fstat(fileno(f), &stat_buf) == -1)
  29.         fatal("file_length failed");
  30.     return (stat_buf.st_size);
  31. }
  32.  
  33. const unsigned char *mmapfile1(const char *filename, off_t *n) {
  34.     FILE *f = fopen(filename, "rb");
  35.     if (!f) fatal("can't open file");
  36.     *n = file_length(f);
  37.     void *p = mmap(NULL, *n, PROT_READ, MAP_PRIVATE, fileno(f), 0);
  38.     if (p == MAP_FAILED)
  39.         fatal("mmap failed");
  40.     return (const unsigned char*)p;
  41. }
  42.  
  43. const unsigned char *mmapfile2(const char *filename, off_t *n) {
  44.     FILE *f = fopen(filename, "rb");
  45.     if (!f) fatal("can't open file");
  46.     *n = file_length(f);
  47.     unsigned char *buf = new unsigned char[*n];
  48.     if (*n != (off_t)fread(buf, 1, *n, f))
  49.         fatal("fread failed");
  50.     return buf;
  51. }
  52.  
  53. int main(int argc, char **argv) {
  54.     if(argc != 3)
  55.         fatal("invalid cmd line, expected filename and mmmap-type (0/1)");
  56.  
  57.     long long int s = 0;
  58.     {
  59.         off_t n;
  60.         bool which_mmap = (argv[2][0] == '0');
  61.         const char *filename = argv[1];
  62.         const unsigned char *data = (which_mmap ? mmapfile1 : mmapfile2)(filename, &n);
  63.  
  64.         printf("%lld\n", (long long int)n);
  65.         printf("%p\n", data);
  66.        
  67.         for(off_t i = 0; i<n; i += 1024) {
  68.             s += (unsigned char)data[i];
  69.         }
  70.     }
  71.     printf("s: %lld\n", s); //verify, that all data is really loaded (no lazy optimizations)
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement