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.Scanner;
- public class MemoryNoCache {
- /* Run using input file as argument via command line
- Rewrite from scratch, original skeleton was bloated and confusing
- Sam Mazarei
- Comp262
- */
- final static int RAM = 0;
- final static int SSD = 1;
- // Rates are in GB/s, latency is in nanoseconds
- final static double RAM_R_RATE = 20;
- final static double RAM_W_RATE = 10;
- final static double RAM_LAT = 100;
- final static double SSD_R_RATE = .200;
- final static double SSD_W_RATE = .50;
- final static double SSD_LAT = 15000;
- // Converting rates in GB/s to ns/Byte
- final static double ram_r_t = (1/RAM_R_RATE);
- final static double ram_w_t = (1/RAM_W_RATE);
- final static double ram_lat = RAM_LAT;
- final static double ssd_r_t = (1/SSD_R_RATE);
- final static double ssd_w_t = (1/SSD_W_RATE);
- final static double ssd_lat = SSD_LAT;
- // Running totals initialized to 0
- double ram_r_tot = 0;
- double ram_w_tot = 0;
- double ram_lat_tot = 0;
- double ram_t_tot = 0;
- double ssd_r_tot = 0;
- double ssd_w_tot = 0;
- double ssd_lat_tot = 0;
- double ssd_t_tot = 0;
- public void accessSSD(){
- ssd_lat_tot += ssd_lat;
- ssd_t_tot += ssd_lat;
- }
- public void accessRAM(){
- ram_lat_tot += ram_lat;
- ram_t_tot += ram_lat;
- }
- public void readSSDTime() {
- accessSSD();
- ssd_r_tot += ssd_r_t;
- ssd_t_tot += ssd_r_tot;
- }
- public void readRAMTime() {
- accessRAM();
- ram_r_tot += ram_r_t;
- ram_t_tot += ram_r_t;
- }
- public void writeSSDTime() {
- accessSSD();
- ssd_w_tot += ssd_w_t;
- ssd_t_tot += ssd_w_t;
- }
- public void writeRAMTime() {
- accessRAM();;
- ram_w_tot += ram_w_t;
- ram_t_tot += ram_w_t;
- }
- // Function to modify time data when writing to device
- public void writeDev(int type){
- switch(type) {
- case RAM:
- writeRAMTime();
- break;
- case SSD:
- writeSSDTime();
- break;
- }
- }
- // Function to modify time data when reading from device
- // Function to modify time stats when reading form device
- public void readDev(int type){
- switch(type) {
- case RAM:
- readRAMTime();
- break;
- case SSD:
- readSSDTime();
- break;
- }
- }
- public void load(int sourcetype){
- readDev(sourcetype);
- }
- public void copy(int sourcetype, int destinationtype){
- load(sourcetype);
- writeDev(destinationtype);
- }
- public static void main(String[] args){
- MemoryNoCache myMem = new MemoryNoCache();
- File input_file = new File(args[0]);
- myMem.parseFile(input_file);
- }
- private 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]));
- break;
- case COPY :
- copy(Integer.parseInt(instruction[source_type_pos]),
- Integer.parseInt(instruction[dest_type_pos]));
- break;
- case STOP :
- displayTotals();
- }
- }
- } catch ( FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public void displayTotals() {
- double totalTime = 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 (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