-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_trashing.c
44 lines (32 loc) · 1.07 KB
/
cache_trashing.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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
// my CPU has 32K of L1 cache
//#define L1_CACHE_CAPACITY (32768 / sizeof(int))
#define L1_CACHE_CAPACITY (3331457 / sizeof(int))
//int array[L1_CACHE_CAPACITY][L1_CACHE_CAPACITY];
struct timespec time_diff(struct timespec start, struct timespec end) {
struct timespec temp;
if (end.tv_nsec - start.tv_nsec < 0) {
temp.tv_sec = end.tv_sec - start.tv_sec - 1;
temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec - start.tv_sec;
temp.tv_nsec = end.tv_nsec - start.tv_nsec;
}
return temp;
}
int main(void) {
struct timespec start, end;
int i, j;
int **array;
array = (int **)malloc(L1_CACHE_CAPACITY*L1_CACHE_CAPACITY);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
for (i=0; i<L1_CACHE_CAPACITY; i++)
for (j=0; j<L1_CACHE_CAPACITY; j++)
array[j][i] = i*j;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
struct timespec delta = time_diff(start, end);
printf("Delta is %ld:%6ld\n", delta.tv_sec, delta.tv_nsec);
return EXIT_SUCCESS;
}