-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlb.c
56 lines (50 loc) · 1.82 KB
/
tlb.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
//Homework answers//
//
// #1) CLOCK_PROCESS_CPUTIME_ID has resolution of 1 nanosecond on Linux
//
// #5) Using gcc's optimize option gcc -O0 to disable optimization. This is the default setting.
//
// #6) Use sched_setaffinity(2), pthread_setaffinity_np(3), taskset(1) or sudo systemd-run -p AllowedCPUs=0 ./tlb.out on Linux, cpuset_setaffinity(2) or cpuset(1) on FreeBSD.
// Or use hwloc-bind package:0.pu:0 -- ./tlb.out.
//
// #7) Use calloc(3) to initialize array then measure time.
////////////////////
#define handle_error(msg) \
do { \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage: %s pages trials\n", argv[0]);
exit(EXIT_FAILURE);
}
long PAGESIZE = sysconf(_SC_PAGESIZE); // 4096
long jump = PAGESIZE / sizeof(int); // 1024
int pages = atoi(argv[1]);
int trials = atoi(argv[2]);
if (pages <= 0 || trials <= 0) {
fprintf(stderr, "Invalid input\n");
exit(EXIT_FAILURE);
}
int *a = calloc(pages, PAGESIZE);
struct timespec start, end;
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start) == -1)
handle_error("clock_gettime");
for (int j = 0; j < trials; j++) {
for (int i = 0; i < pages * jump; i += jump)
a[i] += 1;
}
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) == -1)
handle_error("clock_gettime");
// nanoseconds
printf("%f\n",
((end.tv_sec - start.tv_sec) * 1E9 + end.tv_nsec - start.tv_nsec) /
(trials * pages));
free(a);
return 0;
}