File: mmapbench.c

package info (click to toggle)
gatling 0.13-6
  • links: PTS
  • area: main
  • in suites: sid, stretch
  • size: 1,196 kB
  • ctags: 1,115
  • sloc: ansic: 23,805; makefile: 143; sh: 71; perl: 30
file content (134 lines) | stat: -rw-r--r-- 2,989 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "byte.h"
#include "buffer.h"
#include "scan.h"
#include "str.h"
#include "io.h"
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>

#ifdef __i386__
#define rdtscl(low) \
     __asm__ __volatile__ ("rdtsc" : "=A" (low))
#endif

int main(int argc,char* argv[]) {
  unsigned long count=25000;
  int64 fd;
#ifdef __i386__
  unsigned long long a,b,c;
#else
  struct timeval a,b,c;
  unsigned long d;
#endif

  for (;;) {
    int i;
    int c=getopt(argc,argv,"hc:");
    if (c==-1) break;
    switch (c) {
    case 'c':
      i=scan_ulong(optarg,&count);
      if (i==0 || optarg[i]) {
	buffer_puts(buffer_2,"mmapbench: warning: could not parse count: ");
	buffer_puts(buffer_2,optarg+i+1);
	buffer_putsflush(buffer_2,"\n");
      }
      break;
    case '?':
usage:
      buffer_putsflush(buffer_2,
		  "usage: mmapbench [-h] [-c count] filename\n"
		  "\n"
		  "\t-h\tprint this help\n"
		  "\t-c n\tmmap n 4k pages (default: 25000)\n");
      return 0;
    }
  }

  if (!argv[optind]) goto usage;
  if (!io_readfile(&fd,argv[optind])) {
    buffer_puts(buffer_2,"could not open ");
    buffer_puts(buffer_2,argv[optind]);
    buffer_puts(buffer_2,": ");
    buffer_puterror(buffer_2);
    buffer_putnlflush(buffer_2);
    exit(1);
  }

  buffer_puts(buffer_2,"cache priming: reading ");
  buffer_putulong(buffer_2,count*2);
  buffer_puts(buffer_2," pages (");
  buffer_putulong(buffer_2,count*8);
  buffer_putsflush(buffer_2," KB)...\n");
  {
    unsigned long i;
    char* p;
    volatile char c;
    p = mmap(NULL, count*8192, PROT_READ, MAP_SHARED, fd, 0);
    if (p==MAP_FAILED) {
      buffer_puts(buffer_2,"mmap failed: ");
      buffer_puterror(buffer_2);
      buffer_putnlflush(buffer_2);
      return 111;
    }
    for (i=0; i<count; ++i)
      c += p[i*8192];
    munmap(p,count*8192);
  }

  {
    unsigned long i;
    char **p=malloc(count*sizeof(char*));
    if (!p) {
      buffer_puts(buffer_2,"out of memory!\n");
      exit(1);
    }
    for (i=0; i<count; ++i) {
      volatile char ch;
#ifdef __i386__
      rdtscl(a);
#else
      gettimeofday(&a,0);
#endif
      p[i]=mmap(0,4096,PROT_READ,MAP_SHARED,fd,((off_t)i)*8192);
      if (p[i]==MAP_FAILED) {
	buffer_puts(buffer_2,"mmap failed: ");
	buffer_puterror(buffer_2);
	buffer_putnlflush(buffer_2);
	return 111;
      }
#ifdef __i386__
      rdtscl(b);
#else
      gettimeofday(&b,0);
#endif
      ch=*p[i];
#ifdef __i386__
      rdtscl(c);
      buffer_putulong(buffer_1,b-a);
#else
      gettimeofday(&c,0);
      d=(b.tv_sec-a.tv_sec)*1000000;
      d=d+b.tv_usec-a.tv_usec;
      buffer_putulong(buffer_1,d);
#endif
      buffer_putspace(buffer_1);
#ifdef __i386__
      buffer_putulong(buffer_1,c-b);
#else
      d=(c.tv_sec-b.tv_sec)*1000000;
      d=d+c.tv_usec-b.tv_usec;
      buffer_putulong(buffer_1,d);
#endif
      buffer_puts(buffer_1,"\n");
    }
  }

  buffer_flush(buffer_1);
  return 0;
}