#include #include #include #include #include #include #include #include #include #include struct event { uintptr_t addr; int nb_access; } __attribute__((packed)); int fd; int n; struct event* events; int (*nb_access_at)(int n) = NULL; double timestamp() { struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec + 1e-6*tv.tv_usec; } int nb_access_at_mmap(int n) { return events[n].nb_access; } int nb_access_at_read(int n) { struct event tmp; lseek(fd, n * sizeof(tmp), SEEK_SET); if(read(fd, &tmp, sizeof(tmp)) == -1) { fprintf(stderr, "read(): %s\n", strerror(errno)); exit(2); } return tmp.nb_access; } void usage(const char* self) { fprintf(stderr, "Usage: %s filename [mmap or read]\n", self); exit(1); } int main(int argc, char** argv) { if(argc != 3) usage(argv[0]); if(strcmp(argv[2], "mmap") == 0) nb_access_at = nb_access_at_mmap; else if(strcmp(argv[2], "read") == 0) nb_access_at = nb_access_at_read; else usage(argv[0]); fd = open(argv[1], O_RDONLY); if(fd == -1) { fprintf(stderr, "open(%s): %s\n", argv[1], strerror(errno)); exit(2); } struct stat st; fstat(fd, &st); n = st.st_size / sizeof(struct event); printf("The trace file contains %d events\n", n); events = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if(events == MAP_FAILED) { fprintf(stderr, "mmap(): %s\n", strerror(errno)); exit(2); } double start = timestamp(); int total = 0; for(int i=0; i