Skip to content

Commit aff4a93

Browse files
committed
2 parents 2d3f388 + f69d34a commit aff4a93

File tree

6 files changed

+88
-6
lines changed

6 files changed

+88
-6
lines changed

CacheSim/Memory.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* This file contains the implementation for the memory simulator
3+
*
4+
* Authors :-
5+
* Kshitiz Dange <kdange@andrew.cmu.edu>
6+
* Yash Tibrewal <ytibrewa@andrew.cmu.edu>
7+
*/
8+
19
#include <iostream>
210
#include "Memory.h"
311
#include "Protocol.h"
@@ -8,20 +16,29 @@ std::list<MemRequest *> Memory::req_table;
816
pthread_cond_t Memory::threads_cv;
917
pthread_cond_t Memory::req_cv;
1018

19+
/**
20+
* Adds latency to the memory operation
21+
*/
1122
//#pragma optimize( "", off )
1223
void dummy_instructions() {
1324

1425
usleep(2000); // 2 ms
1526
}
1627
//#pragma optimize( "", on )
1728

29+
/**
30+
* Constructor
31+
*/
1832
MemRequest::MemRequest(unsigned long a) {
1933
addr = a;
2034
pthread_cond_init(&cv, NULL);
2135
done = false;
2236
waiters = 0;
2337
}
2438

39+
/**
40+
* Initializes the memory worker and the synchronization primitives
41+
*/
2542
void Memory::initialize() {
2643
pthread_mutex_init(&lock, NULL);
2744
pthread_cond_init(&threads_cv, NULL);
@@ -31,6 +48,9 @@ void Memory::initialize() {
3148
pthread_create(&tid, NULL, memory_worker, NULL);
3249
}
3350

51+
/**
52+
* The worker function for the memory thread
53+
*/
3454
void *Memory::memory_worker(void *arg) {
3555
pthread_mutex_lock(&lock);
3656
while(true) {
@@ -57,6 +77,10 @@ void *Memory::memory_worker(void *arg) {
5777
return NULL;
5878
}
5979

80+
/**
81+
* This function is called by the cache workers to request a cacheline from
82+
* memory
83+
*/
6084
void Memory::request(unsigned long addr) {
6185
pthread_mutex_lock(&lock);
6286

CacheSim/Memory.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
/**
2+
* The interface for the memory simulator
3+
*
4+
* Authors :-
5+
* Kshitiz Dange <kdange@andrew.cmu.edu>
6+
* Yash Tibrewal <ytibrewa@andrew.cmu.edu>
7+
*/
8+
19
#ifndef _MEMORY_H_
210
#define _MEMORY_H_
311

412
#include <pthread.h>
513
#include <list>
614

15+
/**
16+
* Helper class to denote a single memory request
17+
*/
718
class MemRequest {
819
public:
920
unsigned long addr;
@@ -15,10 +26,13 @@ class MemRequest {
1526
};
1627

1728

29+
/**
30+
* The class for the memory simulator
31+
*/
1832
class Memory {
1933
private:
2034
static pthread_mutex_t lock;
21-
static std::list<MemRequest *> req_table;
35+
static std::list<MemRequest *> req_table; /* The request table */
2236
static pthread_cond_t threads_cv;
2337
static pthread_cond_t req_cv;
2438

CacheSim/Protocol.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/**
2+
* The implementation for the Protocol class. Holds the important state for
3+
* the cache simulator to work
4+
*
5+
* Authors :-
6+
* Kshitiz Dange <kdange@andrew.cmu.edu>
7+
* Yash Tibrewal <ytibrewa@andrew.cmu.edu>
8+
*/
9+
110
#include <iostream>
211
#include <pthread.h>
312
#include <vector>
@@ -59,7 +68,7 @@ void Protocol::initialize(std::string protocol, int num_cores, int cache_size,
5968
}
6069

6170
/**
62-
*
71+
* Called by the trace worker to forward a request to proper processor
6372
*/
6473
void Protocol::process_mem_access(int thread_id, std::string op,
6574
unsigned long addr) {

CacheSim/Protocol.h

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/**
2+
* The Protocol class holds the state for the metrics and the next operation
3+
* to be performed
4+
*
5+
* Authors :-
6+
* Kshitiz Dange <kdange@andrew.cmu.edu>
7+
* Yash Tibrewal <ytibrewa@andrew.cmu.edu>
8+
*/
9+
110
#include <string>
211
#include <vector>
312
#include "Cache.h"
@@ -12,11 +21,15 @@ class Protocol {
1221
static pthread_mutex_t lock;
1322
static pthread_cond_t trace_cv; /* Signal mem acc processor to continue */
1423
static pthread_cond_t worker_cv; /* Signal workers for a new access */
15-
static bool ready;
16-
static int request_id;
17-
static std::string request_op;
18-
static unsigned long request_addr;
24+
static bool ready; /* Indicates that work from the trace is ready */
25+
static int request_id; /* The processor for whom the work is */
26+
static std::string request_op; /* Read / Write */
27+
static unsigned long request_addr; /* The address for the operation */
1928
static int num_cores;
29+
30+
/*
31+
* The metrics for comparison
32+
*/
2033
static std::atomic <long> bus_transactions;
2134
static std::atomic <long> mem_reqs;
2235
static std::atomic <long> mem_write_backs;

CacheSim/SnoopingCache.h

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* The interface for the base class of all the protocols
3+
*
4+
* Authors :-
5+
* Kshitiz Dange <kdange@andrew.cmu.edu>
6+
* Yash Tibrewal <ytibrewa@andrew.cmu.edu>
7+
*/
8+
19
#ifndef _SNOOPING_CACHE_H_
210
#define _SNOOPING_CACHE_H_
311

CacheSim/main.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* The main file for the Cache simulator
3+
*
4+
* Authors :-
5+
* Kshitiz Dange <kdange@andrew.cmu.edu>
6+
* Yash Tibrewal <ytibrewa@andrew.cmu.edu>
7+
*/
8+
19
#include <iostream>
210
#include <fstream>
311
#include <vector>
@@ -17,6 +25,9 @@ void print_usage() {
1725
" [-p <Cache Coherence Protocol>] [-t <Trace File>]\n";
1826
}
1927

28+
/**
29+
* Processes the input trace file line by line
30+
*/
2031
void process_trace_file(std::string trace_filename) {
2132
std::ifstream tracefile(trace_filename.c_str());
2233
if(!tracefile) {
@@ -41,6 +52,9 @@ void process_trace_file(std::string trace_filename) {
4152
while(local_count != Protocol::trace_count);
4253
}
4354

55+
/**
56+
* Displays the results
57+
*/
4458
void print_mem_metrics(){
4559

4660
std::cout << "\nTotal Acceses = " << Protocol::trace_count;

0 commit comments

Comments
 (0)