Advertisement
Sinux1

MemoryNoCache.java

Oct 21st, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.34 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.text.DecimalFormat;
  4. import java.util.Scanner;
  5.  
  6. public class MemoryNoCache {
  7.     /*  Run using input file as argument via command line
  8.         Rewrite from scratch, original skeleton was bloated and confusing
  9.         Sam Mazarei
  10.         Comp262
  11.     */
  12.  
  13.     final static int RAM = 0;
  14.     final static int SSD = 1;
  15.  
  16.     // Rates are in GB/s, latency is in nanoseconds
  17.     final static double RAM_R_RATE = 20;
  18.     final static double RAM_W_RATE = 10;
  19.     final static double RAM_LAT = 100;
  20.  
  21.     final static double SSD_R_RATE = .200;
  22.     final static double SSD_W_RATE = .50;
  23.     final static double SSD_LAT = 15000;
  24.  
  25.     // Converting rates in GB/s to ns/Byte
  26.     final static double ram_r_t = (1/RAM_R_RATE);
  27.     final static double ram_w_t = (1/RAM_W_RATE);
  28.     final static double ram_lat = RAM_LAT;
  29.  
  30.     final static double ssd_r_t = (1/SSD_R_RATE);
  31.     final static double ssd_w_t = (1/SSD_W_RATE);
  32.     final static double ssd_lat = SSD_LAT;
  33.  
  34.     // Running totals initialized to 0
  35.     double ram_r_tot = 0;
  36.     double ram_w_tot = 0;
  37.     double ram_lat_tot = 0;
  38.     double ram_t_tot = 0;
  39.  
  40.     double ssd_r_tot = 0;
  41.     double ssd_w_tot = 0;
  42.     double ssd_lat_tot = 0;
  43.     double ssd_t_tot = 0;
  44.  
  45.  
  46.  
  47.     public void accessSSD(){
  48.         ssd_lat_tot += ssd_lat;
  49.         ssd_t_tot += ssd_lat;
  50.     }
  51.     public void accessRAM(){
  52.         ram_lat_tot += ram_lat;
  53.         ram_t_tot += ram_lat;
  54.     }
  55.     public void readSSDTime() {
  56.         accessSSD();
  57.         ssd_r_tot += ssd_r_t;
  58.         ssd_t_tot += ssd_r_tot;
  59.     }
  60.     public void readRAMTime() {
  61.         accessRAM();
  62.         ram_r_tot += ram_r_t;
  63.         ram_t_tot += ram_r_t;
  64.     }
  65.     public void writeSSDTime() {
  66.         accessSSD();
  67.         ssd_w_tot += ssd_w_t;
  68.         ssd_t_tot += ssd_w_t;
  69.     }
  70.     public void writeRAMTime() {
  71.         accessRAM();;
  72.         ram_w_tot += ram_w_t;
  73.         ram_t_tot += ram_w_t;
  74.     }
  75.  
  76.     // Function to modify time data when writing to device
  77.     public void writeDev(int type){
  78.         switch(type) {
  79.             case RAM:
  80.                 writeRAMTime();
  81.                 break;
  82.             case SSD:
  83.                 writeSSDTime();
  84.                 break;
  85.         }
  86.     }
  87.     // Function to modify time data when reading from device
  88.     // Function to modify time stats when reading form device
  89.     public void readDev(int type){
  90.         switch(type) {
  91.             case RAM:
  92.                 readRAMTime();
  93.                 break;
  94.             case SSD:
  95.                 readSSDTime();
  96.                 break;
  97.         }
  98.     }
  99.  
  100.     public void load(int sourcetype){
  101.         readDev(sourcetype);
  102.     }
  103.  
  104.     public void copy(int sourcetype, int destinationtype){
  105.         load(sourcetype);
  106.         writeDev(destinationtype);
  107.     }
  108.  
  109.     public static void main(String[] args){
  110.         MemoryNoCache myMem = new MemoryNoCache();
  111.         File input_file = new File(args[0]);
  112.         myMem.parseFile(input_file);
  113.  
  114.  
  115.  
  116.  
  117.     }
  118.  
  119.     private void parseFile(File input_file) {
  120.         // For use in parsing instructions
  121.         final int opcode_pos = 0;
  122.         final int source_type_pos = 1;
  123.         final int source_add_pos = 2;
  124.         final int dest_type_pos = 3;
  125.         final int dest_add_pos = 4;
  126.  
  127.         final String COPY = "COPY";
  128.         final String LOAD = "LOAD";
  129.         final String STOP = "STOP";
  130.         final String split = " ";
  131.  
  132.         try {
  133.             Scanner scanner = new Scanner(input_file);
  134.             while (scanner.hasNextLine()) {
  135.                 String raw_line = scanner.nextLine();
  136.                 if(raw_line.charAt(0) == '/'){ continue;}
  137.  
  138.                 String[] instruction = raw_line.split(split);
  139.                 switch(instruction[opcode_pos]) {
  140.                     case LOAD :
  141.                         load(Integer.parseInt(instruction[source_type_pos]));
  142.                         break;
  143.  
  144.                     case COPY :
  145.                         copy(Integer.parseInt(instruction[source_type_pos]),
  146.                                 Integer.parseInt(instruction[dest_type_pos]));
  147.                         break;
  148.  
  149.                     case STOP :
  150.                         displayTotals();
  151.  
  152.                 }
  153.             }
  154.         } catch ( FileNotFoundException e) {
  155.             e.printStackTrace();
  156.         }
  157.     }
  158.  
  159.     public void displayTotals() {
  160.         double totalTime = ram_t_tot + ssd_t_tot;
  161.         DecimalFormat d3 = new DecimalFormat("#.###");
  162.         DecimalFormat perc = new DecimalFormat("##.####");
  163.         String sFormat = "%-8s %-10s %12s %12s %12s %n";
  164.         System.out.printf("%-8s %-10s %12s %12s %12s %n", "Device", "Operation", "Time (ns)", " Device%", "Total%");
  165.         System.out.println("-----------------------------------------------------------");
  166.  
  167.         if (ram_t_tot > 0) {
  168.             double ramReadPercent = ram_r_tot / ram_t_tot;
  169.             double ramReadTotalPercent = ram_r_tot / totalTime;
  170.             double ramWritePercent = ram_w_tot / ram_t_tot;
  171.             double ramWriteTotalPercent = ram_w_tot / totalTime;
  172.             double ramLatencyPercent = ram_lat_tot / ram_t_tot;
  173.             double ramLatencyTotalPercent = ram_lat_tot / totalTime;
  174.             double ramTotalPercent = ram_t_tot / totalTime;
  175.             System.out.printf(sFormat, "RAM",  "READ", d3.format(ram_r_tot),
  176.                     perc.format(ramReadPercent * 100), perc.format(ramReadTotalPercent * 100));
  177.             System.out.printf(sFormat, "RAM",  "WRITE", d3.format(ram_w_tot),
  178.                     perc.format(ramWritePercent * 100), perc.format(ramWriteTotalPercent * 100));
  179.             System.out.printf(sFormat, "RAM", "LATENCY", d3.format(ram_lat_tot),
  180.                     perc.format(ramLatencyPercent * 100), perc.format(ramLatencyTotalPercent * 100));
  181.             System.out.println("-----------------------------------------------------------");
  182.             System.out.printf(sFormat, "RAM",  "TOTAL", d3.format(ram_t_tot),
  183.                     "", perc.format(ramTotalPercent * 100));
  184.             System.out.println("-----------------------------------------------------------");
  185.         }
  186.         if (ssd_t_tot > 0) {
  187.             double ssdReadPercent = ssd_r_tot / ssd_t_tot;
  188.             double ssdReadTotalPercent = ssd_r_tot / totalTime;
  189.             double ssdWritePercent = ssd_w_tot / ssd_t_tot;
  190.             double ssdWriteTotalPercent = ssd_w_tot / totalTime;
  191.             double ssdLatencyPercent = ssd_lat_tot / ssd_t_tot;
  192.             double ssdLatencyTotalPercent = ssd_lat_tot / totalTime;
  193.             double ssdTotalPercent = ssd_t_tot / totalTime;
  194.             System.out.printf(sFormat, "SSD", "READ", d3.format(ssd_r_tot), perc.format(ssdReadPercent * 100), perc.format(ssdReadTotalPercent * 100));
  195.             System.out.printf(sFormat, "SSD", "WRITE", d3.format(ssd_w_tot), perc.format(ssdWritePercent * 100), perc.format(ssdWriteTotalPercent * 100));
  196.             System.out.printf(sFormat, "SSD", "LATENCY", d3.format(ssd_lat_tot), perc.format(ssdLatencyPercent * 100), perc.format(ssdLatencyTotalPercent * 100));
  197.             System.out.println("-----------------------------------------------------------");
  198.             System.out.printf(sFormat, "SSD", "(TOTAL)", d3.format(ssd_t_tot), "", perc.format(ssdTotalPercent * 100));
  199.             System.out.println("-----------------------------------------------------------");
  200.         }
  201.  
  202.         double totalRead = ram_r_tot + ssd_r_tot;
  203.         double totalReadPercent = totalRead / totalTime;
  204.         double totalWrite = ram_w_tot + ssd_w_tot;
  205.         double totalWritePercent = totalWrite / totalTime;
  206.         double totalLatency = ram_lat_tot + ssd_lat_tot;
  207.         double totalLatencyPercent = totalLatency / totalTime;
  208.         System.out.printf(sFormat, "TOTAL", "READ", d3.format(totalRead), "", perc.format(totalReadPercent * 100));
  209.         System.out.printf(sFormat, "TOTAL", "WRITE", d3.format(totalWrite), "", perc.format(totalWritePercent * 100));
  210.         System.out.printf(sFormat, "TOTAL", "LATENCY", d3.format(totalLatency), "", perc.format(totalLatencyPercent * 100));
  211.         System.out.println("-----------------------------------------------------------");
  212.         System.out.printf(sFormat, "TOTAL", "TIME", d3.format(totalTime), "", "");
  213.         System.out.println("-----------------------------------------------------------");
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement