SHD-CacheAttackLab/Part1-Timing/main.c

118 lines
3.7 KiB
C
Raw Normal View History

2023-01-31 20:19:02 +00:00
#include "utility.h"
2024-02-07 14:23:56 +00:00
#ifndef VISUAL
#define PRINT_FUNC print_results_plaintext
#else
#define PRINT_FUNC print_results_for_visualization
#endif
#define LINE_SIZE 64
// [1.2] TODO: Uncomment the following lines and fill in the correct size
2024-11-04 07:18:33 +00:00
#define L1_SIZE 768*1024
#define L2_SIZE 16*1024*1024
#define L3_SIZE 64*1024*1024
2024-11-04 07:37:39 +00:00
#define REDUNDANCY 4
2024-11-04 07:33:53 +00:00
#define BUFF_SIZE REDUNDANCY*16*1024*1024
#define REPEAT 10
2023-01-31 20:19:02 +00:00
int main (int ac, char **av) {
// create 4 arrays to store the latency numbers
// the arrays are initialized to 0
uint64_t dram_latency[SAMPLES] = {0};
uint64_t l1_latency[SAMPLES] = {0};
uint64_t l2_latency[SAMPLES] = {0};
uint64_t l3_latency[SAMPLES] = {0};
// A temporary variable we can use to load addresses
2024-02-07 14:23:56 +00:00
uint8_t tmp;
2023-01-31 20:19:02 +00:00
2024-02-07 14:23:56 +00:00
// Allocate a buffer of LINE_SIZE Bytes
// The volatile keyword tells the compiler to not put this variable into a
// register -- it should always try to be loaded from memory / cache.
volatile uint8_t *target_buffer = (uint8_t *)malloc(LINE_SIZE);
2023-01-31 20:19:02 +00:00
if (NULL == target_buffer) {
perror("Unable to malloc");
return EXIT_FAILURE;
}
// [1.2] TODO: Uncomment the following line to allocate a buffer of a size
// of your chosing. This will help you measure the latencies at L2 and L3.
2024-11-04 07:18:33 +00:00
volatile uint8_t *eviction_buffer = (uint8_t *)malloc(BUFF_SIZE);
2023-01-31 20:19:02 +00:00
// Example: Measure L1 access latency, store results in l1_latency array
for (int i=0; i<SAMPLES; i++){
2024-02-07 14:23:56 +00:00
// Step 1: bring the target cache line into L1 by simply accessing
// the line
2023-01-31 20:19:02 +00:00
tmp = target_buffer[0];
// Step 2: measure the access latency
l1_latency[i] = measure_one_block_access_time((uint64_t)target_buffer);
}
// ======
// [1.2] TODO: Measure DRAM Latency, store results in dram_latency array
// ======
//
2024-11-04 07:18:33 +00:00
for (int i=0; i<SAMPLES; i++){
// Step 1: bring the target cache line into DRAM
clflush((void *)target_buffer);
// Step 2: measure the access latency
dram_latency[i] = measure_one_block_access_time((uint64_t)target_buffer);
}
2023-01-31 20:19:02 +00:00
// ======
// [1.2] TODO: Measure L2 Latency, store results in l2_latency array
// ======
//
2024-11-04 07:18:33 +00:00
for (int i=0; i<SAMPLES; i++){
// Step 1: bring the target cache line into L1 by simply accessing
// the line
tmp = target_buffer[0];
2024-11-04 07:33:53 +00:00
for(int k=0; k<REPEAT; k++){
for(int j=0; j<REDUNDANCY*L1_SIZE/LINE_SIZE; j++){
2024-11-04 07:18:33 +00:00
tmp = eviction_buffer[j*LINE_SIZE];
2024-11-04 07:33:53 +00:00
}
2024-11-04 07:18:33 +00:00
}
2024-11-04 07:33:53 +00:00
2024-11-04 07:18:33 +00:00
// Step 2: measure the access latency
l2_latency[i] = measure_one_block_access_time((uint64_t)target_buffer);
}
2023-01-31 20:19:02 +00:00
// ======
// [1.2] TODO: Measure L3 Latency, store results in l3_latency array
// ======
//
2024-11-04 07:18:33 +00:00
for (int i=0; i<SAMPLES; i++){
// Step 1: bring the target cache line into L1 by simply accessing
// the line
tmp = target_buffer[0];
2024-11-04 07:33:53 +00:00
for(int k=0; k<REPEAT; k++){
for(int j=0; j<REDUNDANCY*L2_SIZE/LINE_SIZE; j++){
2024-11-04 07:18:33 +00:00
tmp = eviction_buffer[j*LINE_SIZE];
2024-11-04 07:33:53 +00:00
}
2024-11-04 07:18:33 +00:00
}
2023-01-31 20:19:02 +00:00
2024-11-04 07:18:33 +00:00
// Step 2: measure the access latency
l3_latency[i] = measure_one_block_access_time((uint64_t)target_buffer);
}
2023-01-31 20:19:02 +00:00
// Print the results to the screen
2024-02-07 14:23:56 +00:00
// When compile to main and used by `make run`,
// it uses print_results_plaintext
// When compile to main-visual and used by `run.py`,
// it uses print_results_for_visualization
PRINT_FUNC(dram_latency, l1_latency, l2_latency, l3_latency);
2023-01-31 20:19:02 +00:00
2024-02-07 14:23:56 +00:00
free((uint8_t *)target_buffer);
2023-01-31 20:19:02 +00:00
2024-02-07 14:23:56 +00:00
// [1.2] TODO: Uncomment this line once you uncomment the eviction_buffer
// creation line
//free((uint8_t *)eviction_buffer);
2023-01-31 20:19:02 +00:00
return 0;
}