Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.text.DecimalFormat;
- import java.util.Hashtable;
- import java.util.Scanner;
- public class Memory {
- /* Run using input file as argument via command line
- Rewrite from scratch, original skeleton was bloated and confusing
- Sam Mazarei
- Comp262
- */
- private static Hashtable<String, Long> cache = new Hashtable<>();
- private final static int CSIZE = 12;
- private final static int BLOCK = 1000;
- private final static int BYTE = 1;
- private final static int RAM = 0;
- private final static int SSD = 1;
- // Rates are in GB/s, latency is in nanoseconds
- private final static double RAM_R_RATE = 20;
- private final static double RAM_W_RATE = 10;
- private final static double RAM_LAT = 100;
- private final static double SSD_R_RATE = .200;
- private final static double SSD_W_RATE = .50;
- private final static double SSD_LAT = 15000;
- // Converting rates in GB/s to ns/Byte
- private final static double ram_r_t = (1 / RAM_R_RATE);
- private final static double ram_w_t = (1 / RAM_W_RATE);
- private final static double ram_lat = RAM_LAT;
- private final static double ssd_r_t = (1 / SSD_R_RATE);
- private final static double ssd_w_t = (1 / SSD_W_RATE);
- private final static double ssd_lat = SSD_LAT;
- private final static double CACHE_R_RATE = 4000;
- private final static double CACHE_W_RATE = 3000;
- private final static double CACHE_LAT = 1;
- private final static double cache_r_t = (1 / CACHE_R_RATE);
- private final static double cache_w_t = (1 / CACHE_W_RATE);
- private final static double cache_lat = CACHE_LAT;
- // Running totals initialized to 0
- private double ram_r_tot = 0;
- private double ram_w_tot = 0;
- private double ram_lat_tot = 0;
- private double ram_t_tot = 0;
- private double ssd_r_tot = 0;
- private double ssd_w_tot = 0;
- private double ssd_lat_tot = 0;
- private double ssd_t_tot = 0;
- private double cache_r_tot = 0;
- private double cache_w_tot = 0;
- private double cache_lat_tot = 0;
- private double cache_t_tot = 0;
- // Used to keep track of when cache is accessed
- private Long time = Long.MIN_VALUE;
- // For updating cache and keeping track of LRU
- public Long timeStamp() {
- Long timeNow = time;
- time++;
- return time;
- }
- // This inserts a block into cache when it is not
- // already there, and updates its time stamp if it is
- // Cache latency penalty
- private void accessCache() {
- cache_lat_tot += cache_lat;
- cache_t_tot += cache_lat;
- }
- // SSD latency penalty
- private void accessSSD() {
- ssd_lat_tot += ssd_lat;
- ssd_t_tot += ssd_lat;
- }
- // RAM latency penalty
- private void accessRAM() {
- ram_lat_tot += ram_lat;
- ram_t_tot += ram_lat;
- }
- // Modifies the time stats for a specific number of
- // bytes read from cache, accounting for latency
- public void readCache(int bytes) {
- double reads = bytes * cache_r_t;
- accessCache();
- cache_r_tot += reads;
- cache_t_tot += reads;
- }
- // Modifies the time stats for a specific number of
- // bytes read from SSD, accounting for latency
- private void readSSD(int bytes) {
- double reads = bytes * ssd_r_t;
- accessSSD();
- ssd_r_tot += reads;
- ssd_t_tot += reads;
- }
- // Modifies the time stats for a specific number of
- // bytes read from RAM, accounting for latency
- private void readRAM(int bytes) {
- double reads = bytes * ram_r_t;
- accessRAM();
- ram_r_tot += reads;
- ram_t_tot += reads;
- }
- // Modifies the time stats for a specific number of
- // bytes written to cache, accounting for latency
- public void writeCache(int bytes) {
- double writes = bytes * cache_w_t;
- accessCache();
- cache_w_tot += writes;
- cache_t_tot += writes;
- }
- // Modifies the time stats for a specific number of
- // bytes written to SSD, accounting for latency
- private void writeSSD(int bytes) {
- double writes = bytes * ssd_w_t;
- accessSSD();
- ssd_w_tot += writes;
- ssd_t_tot += writes;
- }
- // Modifies the time stats for a specific number of
- // bytes written to RAM, accounting for latency
- private void writeRAM(int bytes) {
- double writes = bytes * ram_w_t;
- accessRAM();
- ram_w_tot += writes;
- ram_t_tot += writes;
- }
- // Helper function to determine which device write to call
- public void writeDev(int type, int bytes) {
- switch (type) {
- case RAM:
- writeRAM(bytes);
- break;
- case SSD:
- writeSSD(bytes);
- break;
- }
- }
- // Helper function to determine which device write to call
- public void readDev(int type, int bytes) {
- switch (type) {
- case RAM:
- readRAM(bytes);
- break;
- case SSD:
- readSSD(bytes);
- break;
- }
- }
- // Returns the LRU block in cache by searching for
- // lowest timestamp value in each key,value pair
- public String getLRU() {
- String minKey = "";
- Long min = Long.MAX_VALUE;
- for (String key : cache.keySet()) {
- if (cache.get(key) <= min) {
- min = cache.get(key);
- minKey = key;
- }
- }
- return minKey;
- }
- // Inserts new block into cache, or updates the timestamp
- // on an existing block
- public void updateCache(int type, int address) {
- String key = Integer.toString(type) + " " + Integer.toString(address / BLOCK);
- cache.put(key, timeStamp());
- }
- // Check to see if block is in cache
- public boolean inCache(int type, int address) {
- String key = Integer.toString(type) + " " + Integer.toString(address / BLOCK);
- return cache.containsKey(key);
- }
- // Updates latency and makes appropriate function
- // calls to update time stats, modify cache, and
- // read/write appropriate devices
- public void checkCache(int type, int add) {
- accessCache();
- if (!(inCache(type, add))) {
- if (cache.size() > 11) {
- cache.remove(getLRU());
- }
- updateCache(type, add);
- readDev(type, BLOCK);
- writeCache(BLOCK);
- }
- }
- // Load makes all appropriate calls to load data into
- // cache if not present, and read data into cpu register
- public void load(int stype, int saddress) {
- checkCache(stype, saddress);
- readCache(BYTE);
- }
- public void copy(int stype, int saddress, int dtype, int daddress) {
- load(stype, saddress);
- readCache(BLOCK);
- writeDev(dtype, BLOCK);
- }
- public static void main(String[] args) {
- Memory myMem = new Memory();
- File input_file = new File(args[0]);
- myMem.parseFile(input_file);
- }
- public void parseFile(File input_file) {
- // For use in parsing instructions
- final int opcode_pos = 0;
- final int source_type_pos = 1;
- final int source_add_pos = 2;
- final int dest_type_pos = 3;
- final int dest_add_pos = 4;
- final String COPY = "COPY";
- final String LOAD = "LOAD";
- final String STOP = "STOP";
- final String split = " ";
- try {
- Scanner scanner = new Scanner(input_file);
- while (scanner.hasNextLine()) {
- String raw_line = scanner.nextLine();
- if (raw_line.charAt(0) == '/') {
- continue;
- }
- String[] instruction = raw_line.split(split);
- switch (instruction[opcode_pos]) {
- case LOAD:
- load(Integer.parseInt(instruction[source_type_pos]),
- Integer.parseInt(instruction[source_add_pos]));
- break;
- case COPY:
- copy(Integer.parseInt(instruction[source_type_pos]),
- Integer.parseInt(instruction[source_add_pos]),
- Integer.parseInt(instruction[dest_type_pos]),
- Integer.parseInt(instruction[dest_add_pos]));
- break;
- case STOP:
- displayTotals();
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public void displayTotals() {
- double totalTime = cache_t_tot + ram_t_tot + ssd_t_tot;
- DecimalFormat d3 = new DecimalFormat("#.###");
- DecimalFormat perc = new DecimalFormat("##.#");
- String sFormat = "%-8s %-10s %12s %12s %12s %n";
- System.out.printf("%-8s %-10s %12s %12s %12s %n", "Device", "Operation", "Time (ns)", " Device%", "Total%");
- System.out.println("-----------------------------------------------------------");
- if (cache_t_tot > 0) {
- double cacheReadPercent = cache_r_tot / cache_t_tot;
- double cacheReadTotalPercent = cache_r_tot / totalTime;
- double cacheWritePercent = cache_w_tot / cache_t_tot;
- double cacheWriteTotalPercent = cache_w_tot / totalTime;
- double cacheLatencyPercent = cache_lat_tot / cache_t_tot;
- double cacheLatencyTotalPercent = cache_lat_tot / totalTime;
- double cacheTotalPercent = cache_t_tot / totalTime;
- System.out.printf(sFormat, "CACHE", "READ", d3.format(cache_r_tot), perc.format(cacheReadPercent * 100), perc.format(cacheReadTotalPercent * 100));
- System.out.printf(sFormat, "CACHE", "WRITE", d3.format(cache_w_tot), perc.format(cacheWritePercent * 100), perc.format(cacheWriteTotalPercent * 100));
- System.out.printf(sFormat, "CACHE", "LATENCY", d3.format(cache_lat_tot), perc.format(cacheLatencyPercent * 100), perc.format(cacheLatencyTotalPercent * 100));
- System.out.println("-----------------------------------------------------------");
- System.out.printf(sFormat, "CACHE", "TOTAL", d3.format(cache_t_tot), "", perc.format(cacheTotalPercent * 100));
- System.out.println("-----------------------------------------------------------");
- }
- if (ram_t_tot > 0) {
- double ramReadPercent = ram_r_tot / ram_t_tot;
- double ramReadTotalPercent = ram_r_tot / totalTime;
- double ramWritePercent = ram_w_tot / ram_t_tot;
- double ramWriteTotalPercent = ram_w_tot / totalTime;
- double ramLatencyPercent = ram_lat_tot / ram_t_tot;
- double ramLatencyTotalPercent = ram_lat_tot / totalTime;
- double ramTotalPercent = ram_t_tot / totalTime;
- System.out.printf(sFormat, "RAM", "READ", d3.format(ram_r_tot),
- perc.format(ramReadPercent * 100), perc.format(ramReadTotalPercent * 100));
- System.out.printf(sFormat, "RAM", "WRITE", d3.format(ram_w_tot),
- perc.format(ramWritePercent * 100), perc.format(ramWriteTotalPercent * 100));
- System.out.printf(sFormat, "RAM", "LATENCY", d3.format(ram_lat_tot),
- perc.format(ramLatencyPercent * 100), perc.format(ramLatencyTotalPercent * 100));
- System.out.println("-----------------------------------------------------------");
- System.out.printf(sFormat, "RAM", "TOTAL", d3.format(ram_t_tot),
- "", perc.format(ramTotalPercent * 100));
- System.out.println("-----------------------------------------------------------");
- }
- if (ssd_t_tot > 0) {
- double ssdReadPercent = ssd_r_tot / ssd_t_tot;
- double ssdReadTotalPercent = ssd_r_tot / totalTime;
- double ssdWritePercent = ssd_w_tot / ssd_t_tot;
- double ssdWriteTotalPercent = ssd_w_tot / totalTime;
- double ssdLatencyPercent = ssd_lat_tot / ssd_t_tot;
- double ssdLatencyTotalPercent = ssd_lat_tot / totalTime;
- double ssdTotalPercent = ssd_t_tot / totalTime;
- System.out.printf(sFormat, "SSD", "READ", d3.format(ssd_r_tot), perc.format(ssdReadPercent * 100), perc.format(ssdReadTotalPercent * 100));
- System.out.printf(sFormat, "SSD", "WRITE", d3.format(ssd_w_tot), perc.format(ssdWritePercent * 100), perc.format(ssdWriteTotalPercent * 100));
- System.out.printf(sFormat, "SSD", "LATENCY", d3.format(ssd_lat_tot), perc.format(ssdLatencyPercent * 100), perc.format(ssdLatencyTotalPercent * 100));
- System.out.println("-----------------------------------------------------------");
- System.out.printf(sFormat, "SSD", "(TOTAL)", d3.format(ssd_t_tot), "", perc.format(ssdTotalPercent * 100));
- System.out.println("-----------------------------------------------------------");
- }
- double totalRead = ram_r_tot + ssd_r_tot;
- double totalReadPercent = totalRead / totalTime;
- double totalWrite = ram_w_tot + ssd_w_tot;
- double totalWritePercent = totalWrite / totalTime;
- double totalLatency = ram_lat_tot + ssd_lat_tot;
- double totalLatencyPercent = totalLatency / totalTime;
- System.out.printf(sFormat, "TOTAL", "READ", d3.format(totalRead), "", perc.format(totalReadPercent * 100));
- System.out.printf(sFormat, "TOTAL", "WRITE", d3.format(totalWrite), "", perc.format(totalWritePercent * 100));
- System.out.printf(sFormat, "TOTAL", "LATENCY", d3.format(totalLatency), "", perc.format(totalLatencyPercent * 100));
- System.out.println("-----------------------------------------------------------");
- System.out.printf(sFormat, "TOTAL", "TIME", d3.format(totalTime), "", "");
- System.out.println("-----------------------------------------------------------");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement