2 Copyright (c) 2020 Peter Hsu. All Rights Reserved. See LICENCE file for details.
12 #include <sys/types.h>
23 struct perfCounters_t perf
;
26 void perf_create(const char* shm_name
)
28 dieif(!shm_name
, "Must include --perf=");
29 int n
= (insnSpace
.bound
- insnSpace
.base
) / 2;
30 long sz
= sizeof(struct perf_header_t
);
31 sz
+= n
* sizeof(struct count_t
);
32 sz
+= n
* sizeof(long) * 3;
33 // sz += insnSpace.bound - insnSpace.base;
34 int fd
= shm_open(shm_name
, O_CREAT
|O_TRUNC
|O_RDWR
, S_IRWXU
);
35 dieif(fd
<0, "shm_open() failed in perf_create");
36 dieif(ftruncate(fd
, sz
)<0, "ftruncate() failed in perf_create");
37 perf
.h
= (struct perf_header_t
*)mmap(0, sz
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, 0);
38 dieif(perf
.h
==0, "mmap() failed in perf_create");
39 memset((char*)perf
.h
, 0, sz
);
40 perf
.h
->base
= insnSpace
.base
;
41 perf
.h
->bound
= insnSpace
.bound
;
43 perf
.count_array
= (struct count_t
*)( (char*)perf
.h
+ sizeof(struct perf_header_t
) );
44 perf
.ib_miss
= (long*)&perf
.count_array
[n
];
45 perf
.ic_miss
= (long*)&perf
.ib_miss
[n
];
46 perf
.dc_miss
= (long*)&perf
.ic_miss
[n
];
47 // perf.text_segment = (char*)&perf.dc_miss[n];
48 // memcpy(perf.text_segment, (char*)insnSpace.base, (insnSpace.bound-insnSpace.base));
49 for (Addr_t pc
=perf
.h
->base
; pc
<perf
.h
->bound
; pc
+=2)
50 decode_instruction(&perf
.count_array
[(pc
-perf
.h
->base
)/2].i
, pc
);
53 void perf_open(const char* shm_name
)
55 int fd
= shm_open(shm_name
, O_RDONLY
, 0);
56 dieif(fd
<0, "shm_open() failed in perf_open");
57 perf
.h
= (struct perf_header_t
*)mmap(0, sizeof(struct perf_header_t
), PROT_READ
, MAP_SHARED
, fd
, 0);
58 dieif(perf
.h
==0, "first mmap() failed in perf_open");
59 long sz
= perf
.h
->size
;
60 dieif(munmap(perf
.h
, sizeof(struct perf_header_t
))<0, "munmap() failed in perf_open");
61 perf
.h
= (struct perf_header_t
*)mmap(0, sz
, PROT_READ
, MAP_SHARED
, fd
, 0);
62 dieif(perf
.h
==0, "second mmap() failed in perf_open");
63 perf
.count_array
= (struct count_t
*)( (char*)perf
.h
+ sizeof(struct perf_header_t
) );
64 long n
= (perf
.h
->bound
- perf
.h
->base
) / 2;
65 perf
.ib_miss
= (long*)&perf
.count_array
[n
];
66 perf
.ic_miss
= (long*)&perf
.ib_miss
[n
];
67 perf
.dc_miss
= (long*)&perf
.ic_miss
[n
];
68 // perf.text_segment = (char*)&perf.ic_miss[n];
73 dieif(munmap(perf
.h
, perf
.h
->size
)<0, "munmap() failed in perf_close");