Advertisement
Sinux1

Memory.java

Oct 21st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.10 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.text.DecimalFormat;
  4. import java.util.Hashtable;
  5. import java.util.Scanner;
  6.  
  7. public class Memory {
  8.     /*  Run using input file as argument via command line
  9.         Rewrite from scratch, original skeleton was bloated and confusing
  10.         Sam Mazarei
  11.         Comp262
  12.     */
  13.     private static Hashtable<String, Long> cache = new Hashtable<>();
  14.     private final static int CSIZE = 12;
  15.     private final static int BLOCK = 1000;
  16.     private final static int BYTE = 1;
  17.     private final static int RAM = 0;
  18.     private final static int SSD = 1;
  19.  
  20.     // Rates are in GB/s, latency is in nanoseconds
  21.     private final static double RAM_R_RATE = 20;
  22.     private final static double RAM_W_RATE = 10;
  23.     private final static double RAM_LAT = 100;
  24.  
  25.     private final static double SSD_R_RATE = .200;
  26.     private final static double SSD_W_RATE = .50;
  27.     private final static double SSD_LAT = 15000;
  28.  
  29.     // Converting rates in GB/s to ns/Byte
  30.     private final static double ram_r_t = (1 / RAM_R_RATE);
  31.     private final static double ram_w_t = (1 / RAM_W_RATE);
  32.     private final static double ram_lat = RAM_LAT;
  33.  
  34.     private final static double ssd_r_t = (1 / SSD_R_RATE);
  35.     private final static double ssd_w_t = (1 / SSD_W_RATE);
  36.     private final static double ssd_lat = SSD_LAT;
  37.  
  38.     private final static double CACHE_R_RATE = 4000;
  39.     private final static double CACHE_W_RATE = 3000;
  40.     private final static double CACHE_LAT = 1;
  41.  
  42.     private final static double cache_r_t = (1 / CACHE_R_RATE);
  43.     private final static double cache_w_t = (1 / CACHE_W_RATE);
  44.     private final static double cache_lat = CACHE_LAT;
  45.  
  46.     // Running totals initialized to 0
  47.     private double ram_r_tot = 0;
  48.     private double ram_w_tot = 0;
  49.     private double ram_lat_tot = 0;
  50.     private double ram_t_tot = 0;
  51.  
  52.     private double ssd_r_tot = 0;
  53.     private double ssd_w_tot = 0;
  54.     private double ssd_lat_tot = 0;
  55.     private double ssd_t_tot = 0;
  56.  
  57.     private double cache_r_tot = 0;
  58.     private double cache_w_tot = 0;
  59.     private double cache_lat_tot = 0;
  60.     private double cache_t_tot = 0;
  61.  
  62.     // Used to keep track of when cache is accessed
  63.     private Long time = Long.MIN_VALUE;
  64.  
  65.     // For updating cache and keeping track of LRU
  66.     public Long timeStamp() {
  67.         Long timeNow = time;
  68.         time++;
  69.         return time;
  70.     }
  71.  
  72.     // This inserts a block into cache when it is not
  73.     // already there, and updates its time stamp if it is
  74.  
  75.     // Cache latency penalty
  76.     private void accessCache() {
  77.         cache_lat_tot += cache_lat;
  78.         cache_t_tot += cache_lat;
  79.     }
  80.  
  81.     // SSD latency penalty
  82.     private void accessSSD() {
  83.         ssd_lat_tot += ssd_lat;
  84.         ssd_t_tot += ssd_lat;
  85.     }
  86.  
  87.     // RAM latency penalty
  88.     private void accessRAM() {
  89.         ram_lat_tot += ram_lat;
  90.         ram_t_tot += ram_lat;
  91.     }
  92.  
  93.     // Modifies the time stats for a specific number of
  94.     // bytes read from cache, accounting for latency
  95.     public void readCache(int bytes) {
  96.         double reads = bytes * cache_r_t;
  97.         accessCache();
  98.         cache_r_tot += reads;
  99.         cache_t_tot += reads;
  100.     }
  101.  
  102.     // Modifies the time stats for a specific number of
  103.     // bytes read from SSD, accounting for latency
  104.     private void readSSD(int bytes) {
  105.         double reads = bytes * ssd_r_t;
  106.         accessSSD();
  107.         ssd_r_tot += reads;
  108.         ssd_t_tot += reads;
  109.     }
  110.  
  111.     // Modifies the time stats for a specific number of
  112.     // bytes read from RAM, accounting for latency
  113.     private void readRAM(int bytes) {
  114.         double reads = bytes * ram_r_t;
  115.         accessRAM();
  116.         ram_r_tot += reads;
  117.         ram_t_tot += reads;
  118.     }
  119.  
  120.     // Modifies the time stats for a specific number of
  121.     // bytes written to cache, accounting for latency
  122.     public void writeCache(int bytes) {
  123.         double writes = bytes * cache_w_t;
  124.         accessCache();
  125.         cache_w_tot += writes;
  126.         cache_t_tot += writes;
  127.     }
  128.  
  129.     // Modifies the time stats for a specific number of
  130.     // bytes written to SSD, accounting for latency
  131.     private void writeSSD(int bytes) {
  132.         double writes = bytes * ssd_w_t;
  133.         accessSSD();
  134.         ssd_w_tot += writes;
  135.         ssd_t_tot += writes;
  136.     }
  137.  
  138.     // Modifies the time stats for a specific number of
  139.     // bytes written to RAM, accounting for latency
  140.     private void writeRAM(int bytes) {
  141.         double writes = bytes * ram_w_t;
  142.         accessRAM();
  143.         ram_w_tot += writes;
  144.         ram_t_tot += writes;
  145.     }
  146.  
  147.     // Helper function to determine which device write to call
  148.     public void writeDev(int type, int bytes) {
  149.         switch (type) {
  150.             case RAM:
  151.                 writeRAM(bytes);
  152.                 break;
  153.             case SSD:
  154.                 writeSSD(bytes);
  155.                 break;
  156.         }
  157.     }
  158.  
  159.     // Helper function to determine which device write to call
  160.     public void readDev(int type, int bytes) {
  161.         switch (type) {
  162.             case RAM:
  163.                 readRAM(bytes);
  164.                 break;
  165.             case SSD:
  166.                 readSSD(bytes);
  167.                 break;
  168.         }
  169.     }
  170.  
  171.     // Returns the LRU block in cache by searching for
  172.     // lowest timestamp value in each key,value pair
  173.     public String getLRU() {
  174.         String minKey = "";
  175.         Long min = Long.MAX_VALUE;
  176.         for (String key : cache.keySet()) {
  177.             if (cache.get(key) <= min) {
  178.                 min = cache.get(key);
  179.                 minKey = key;
  180.             }
  181.         }
  182.         return minKey;
  183.     }
  184.  
  185.  
  186.     // Inserts new block into cache, or updates the timestamp
  187.     // on an existing block
  188.     public void updateCache(int type, int address) {
  189.         String key = Integer.toString(type) + " " + Integer.toString(address / BLOCK);
  190.         cache.put(key, timeStamp());
  191.     }
  192.  
  193.     // Check to see if block is in cache
  194.     public boolean inCache(int type, int address) {
  195.         String key = Integer.toString(type) + " " + Integer.toString(address / BLOCK);
  196.         return cache.containsKey(key);
  197.     }
  198.  
  199.     // Updates latency and makes appropriate function
  200.     // calls to update time stats, modify cache, and
  201.     // read/write appropriate devices
  202.     public void checkCache(int type, int add) {
  203.         accessCache();
  204.         if (!(inCache(type, add))) {
  205.             if (cache.size() > 11) {
  206.                 cache.remove(getLRU());
  207.             }
  208.             updateCache(type, add);
  209.             readDev(type, BLOCK);
  210.             writeCache(BLOCK);
  211.         }
  212.     }
  213.  
  214.     // Load makes all appropriate calls to load data into
  215.     // cache if not present, and read data into cpu register
  216.     public void load(int stype, int saddress) {
  217.         checkCache(stype, saddress);
  218.         readCache(BYTE);
  219.     }
  220.  
  221.     public void copy(int stype, int saddress, int dtype, int daddress) {
  222.         load(stype, saddress);
  223.         readCache(BLOCK);
  224.         writeDev(dtype, BLOCK);
  225.     }
  226.  
  227.     public static void main(String[] args) {
  228.         Memory myMem = new Memory();
  229.         File input_file = new File(args[0]);
  230.         myMem.parseFile(input_file);
  231.  
  232.  
  233.     }
  234.  
  235.     public void parseFile(File input_file) {
  236.         // For use in parsing instructions
  237.         final int opcode_pos = 0;
  238.         final int source_type_pos = 1;
  239.         final int source_add_pos = 2;
  240.         final int dest_type_pos = 3;
  241.         final int dest_add_pos = 4;
  242.  
  243.         final String COPY = "COPY";
  244.         final String LOAD = "LOAD";
  245.         final String STOP = "STOP";
  246.         final String split = " ";
  247.  
  248.         try {
  249.             Scanner scanner = new Scanner(input_file);
  250.             while (scanner.hasNextLine()) {
  251.                 String raw_line = scanner.nextLine();
  252.                 if (raw_line.charAt(0) == '/') {
  253.                     continue;
  254.                 }
  255.  
  256.                 String[] instruction = raw_line.split(split);
  257.                 switch (instruction[opcode_pos]) {
  258.                     case LOAD:
  259.                         load(Integer.parseInt(instruction[source_type_pos]),
  260.                                 Integer.parseInt(instruction[source_add_pos]));
  261.                         break;
  262.  
  263.                     case COPY:
  264.                         copy(Integer.parseInt(instruction[source_type_pos]),
  265.                                 Integer.parseInt(instruction[source_add_pos]),
  266.                                 Integer.parseInt(instruction[dest_type_pos]),
  267.                                 Integer.parseInt(instruction[dest_add_pos]));
  268.                         break;
  269.  
  270.                     case STOP:
  271.                         displayTotals();
  272.  
  273.                 }
  274.             }
  275.         } catch (FileNotFoundException e) {
  276.             e.printStackTrace();
  277.         }
  278.     }
  279.  
  280.     public void displayTotals() {
  281.         double totalTime = cache_t_tot + ram_t_tot + ssd_t_tot;
  282.         DecimalFormat d3 = new DecimalFormat("#.###");
  283.         DecimalFormat perc = new DecimalFormat("##.#");
  284.         String sFormat = "%-8s %-10s %12s %12s %12s %n";
  285.         System.out.printf("%-8s %-10s %12s %12s %12s %n", "Device", "Operation", "Time (ns)", " Device%", "Total%");
  286.         System.out.println("-----------------------------------------------------------");
  287.  
  288.         if (cache_t_tot > 0) {
  289.             double cacheReadPercent = cache_r_tot / cache_t_tot;
  290.             double cacheReadTotalPercent = cache_r_tot / totalTime;
  291.             double cacheWritePercent = cache_w_tot / cache_t_tot;
  292.             double cacheWriteTotalPercent = cache_w_tot / totalTime;
  293.             double cacheLatencyPercent = cache_lat_tot / cache_t_tot;
  294.             double cacheLatencyTotalPercent = cache_lat_tot / totalTime;
  295.             double cacheTotalPercent = cache_t_tot / totalTime;
  296.             System.out.printf(sFormat, "CACHE",  "READ", d3.format(cache_r_tot), perc.format(cacheReadPercent * 100), perc.format(cacheReadTotalPercent * 100));
  297.             System.out.printf(sFormat, "CACHE",  "WRITE", d3.format(cache_w_tot), perc.format(cacheWritePercent * 100), perc.format(cacheWriteTotalPercent * 100));
  298.             System.out.printf(sFormat, "CACHE",  "LATENCY", d3.format(cache_lat_tot), perc.format(cacheLatencyPercent * 100), perc.format(cacheLatencyTotalPercent * 100));
  299.             System.out.println("-----------------------------------------------------------");
  300.             System.out.printf(sFormat, "CACHE",  "TOTAL", d3.format(cache_t_tot), "", perc.format(cacheTotalPercent * 100));
  301.             System.out.println("-----------------------------------------------------------");
  302.         }
  303.         if (ram_t_tot > 0) {
  304.             double ramReadPercent = ram_r_tot / ram_t_tot;
  305.             double ramReadTotalPercent = ram_r_tot / totalTime;
  306.             double ramWritePercent = ram_w_tot / ram_t_tot;
  307.             double ramWriteTotalPercent = ram_w_tot / totalTime;
  308.             double ramLatencyPercent = ram_lat_tot / ram_t_tot;
  309.             double ramLatencyTotalPercent = ram_lat_tot / totalTime;
  310.             double ramTotalPercent = ram_t_tot / totalTime;
  311.             System.out.printf(sFormat, "RAM",  "READ", d3.format(ram_r_tot),
  312.                     perc.format(ramReadPercent * 100), perc.format(ramReadTotalPercent * 100));
  313.             System.out.printf(sFormat, "RAM",  "WRITE", d3.format(ram_w_tot),
  314.                     perc.format(ramWritePercent * 100), perc.format(ramWriteTotalPercent * 100));
  315.             System.out.printf(sFormat, "RAM", "LATENCY", d3.format(ram_lat_tot),
  316.                     perc.format(ramLatencyPercent * 100), perc.format(ramLatencyTotalPercent * 100));
  317.             System.out.println("-----------------------------------------------------------");
  318.             System.out.printf(sFormat, "RAM",  "TOTAL", d3.format(ram_t_tot),
  319.                     "", perc.format(ramTotalPercent * 100));
  320.             System.out.println("-----------------------------------------------------------");
  321.         }
  322.         if (ssd_t_tot > 0) {
  323.             double ssdReadPercent = ssd_r_tot / ssd_t_tot;
  324.             double ssdReadTotalPercent = ssd_r_tot / totalTime;
  325.             double ssdWritePercent = ssd_w_tot / ssd_t_tot;
  326.             double ssdWriteTotalPercent = ssd_w_tot / totalTime;
  327.             double ssdLatencyPercent = ssd_lat_tot / ssd_t_tot;
  328.             double ssdLatencyTotalPercent = ssd_lat_tot / totalTime;
  329.             double ssdTotalPercent = ssd_t_tot / totalTime;
  330.             System.out.printf(sFormat, "SSD", "READ", d3.format(ssd_r_tot), perc.format(ssdReadPercent * 100), perc.format(ssdReadTotalPercent * 100));
  331.             System.out.printf(sFormat, "SSD", "WRITE", d3.format(ssd_w_tot), perc.format(ssdWritePercent * 100), perc.format(ssdWriteTotalPercent * 100));
  332.             System.out.printf(sFormat, "SSD", "LATENCY", d3.format(ssd_lat_tot), perc.format(ssdLatencyPercent * 100), perc.format(ssdLatencyTotalPercent * 100));
  333.             System.out.println("-----------------------------------------------------------");
  334.             System.out.printf(sFormat, "SSD", "(TOTAL)", d3.format(ssd_t_tot), "", perc.format(ssdTotalPercent * 100));
  335.             System.out.println("-----------------------------------------------------------");
  336.         }
  337.  
  338.         double totalRead = ram_r_tot + ssd_r_tot;
  339.         double totalReadPercent = totalRead / totalTime;
  340.         double totalWrite = ram_w_tot + ssd_w_tot;
  341.         double totalWritePercent = totalWrite / totalTime;
  342.         double totalLatency = ram_lat_tot + ssd_lat_tot;
  343.         double totalLatencyPercent = totalLatency / totalTime;
  344.         System.out.printf(sFormat, "TOTAL", "READ", d3.format(totalRead), "", perc.format(totalReadPercent * 100));
  345.         System.out.printf(sFormat, "TOTAL", "WRITE", d3.format(totalWrite), "", perc.format(totalWritePercent * 100));
  346.         System.out.printf(sFormat, "TOTAL", "LATENCY", d3.format(totalLatency), "", perc.format(totalLatencyPercent * 100));
  347.         System.out.println("-----------------------------------------------------------");
  348.         System.out.printf(sFormat, "TOTAL", "TIME", d3.format(totalTime), "", "");
  349.         System.out.println("-----------------------------------------------------------");
  350.         }
  351.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement