-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcalibration.c
83 lines (78 loc) · 1.91 KB
/
calibration.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
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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sched.h>
#include "./cacheutils.h"
size_t array[5*1024];
size_t hit_histogram[80];
size_t miss_histogram[80];
size_t onlyreload(void* addr)
{
size_t time = rdtsc();
maccess(addr);
size_t delta = rdtsc() - time;
return delta;
}
size_t flushandreload(void* addr)
{
size_t time = rdtsc();
maccess(addr);
size_t delta = rdtsc() - time;
flush(addr);
return delta;
}
int main(int argc, char** argv)
{
memset(array,-1,5*1024*sizeof(size_t));
maccess(array + 2*1024);
sched_yield();
for (int i = 0; i < 4*1024*1024; ++i)
{
size_t d = onlyreload(array+2*1024);
hit_histogram[MIN(79,d/5)]++;
sched_yield();
}
flush(array+1024);
for (int i = 0; i < 4*1024*1024; ++i)
{
size_t d = flushandreload(array+2*1024);
miss_histogram[MIN(79,d/5)]++;
sched_yield();
}
printf(".\n");
size_t hit_max = 0;
size_t hit_max_i = 0;
size_t miss_min_i = 0;
for (int i = 0; i < 80; ++i)
{
printf("%3d: %10zu %10zu\n",i*5,hit_histogram[i],miss_histogram[i]);
if (hit_max < hit_histogram[i])
{
hit_max = hit_histogram[i];
hit_max_i = i;
}
if (miss_histogram[i] > 3 && miss_min_i == 0)
miss_min_i = i;
}
if (miss_min_i > hit_max_i+4)
printf("Flush+Reload possible!\n");
else if (miss_min_i > hit_max_i+2)
printf("Flush+Reload probably possible!\n");
else if (miss_min_i < hit_max_i+2)
printf("Flush+Reload maybe not possible!\n");
else
printf("Flush+Reload not possible!\n");
size_t min = -1UL;
size_t min_i = 0;
for (int i = hit_max_i; i < miss_min_i; ++i)
{
if (min > (hit_histogram[i] + miss_histogram[i]))
{
min = hit_histogram[i] + miss_histogram[i];
min_i = i;
}
}
printf("The lower the threshold, the lower the number of false positives.\n");
printf("Suggested cache hit/miss threshold: %zu\n",min_i * 5);
return min_i * 5;
}