/* UC Santa Barbara ArchLab www.cs.ucsb.edu/~arch Wavelet-Based Phase Classification Source Code www.cs.ucsb.edu/~arch/wavelet Copyright (c) 2006 The Regents of the University of California. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice, this paragraph and the following three paragraphs appear in all copies. Permission to incorporate this software into commercial products may be obtained by contacting the University of California. For information about obtaining such a license contact: Tim Sherwood This software program and documentation are copyrighted by The Regents of the University of California. The software program and documentation are supplied "as is", without any accompanying services from The Regents. The Regents does not warrant that the operation of the program will be uninterrupted or error-free. The end-user understands that the program was developed for research purposes and is advised not to rely exclusively on the program for any reason. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. Ted Huffmire, (www.cs.ucsb.edu/~huffmire, huffmire at cs dot ucsb dot edu) Tim Sherwood (www.cs.ucsb.edu/~sherwood, sherwood at cs dot ucsb dot edu) 14 June 2006 File: cache.java Publication: Ted Huffmire and Tim Sherwood. Wavelet-Based Phase Classification. Proceedings of the Fifteenth International Conference on Parallel Architectures and Compilation Techniques (PACT'06), Seattle, Washington, September 16-20, 2006. */ import java.awt.*; import java.util.*; import java.io.*; class cache { public static final long PIXELS = 200; // pixels per 10M instructions public static final long HEIGHT = 400L; public static final int L1CACHESIZE = 16384; public static final long INTERVALSIZE = 1000000L; public static void main(String[] args) { cachesim cs = new cachesim(L1CACHESIZE, 2, 32, 1048576, 4, 64); count is_l1hit = new count(); count is_l2hit = new count(); count accesstime = new count(); //Vector pids1 = new Vector(); Vector phases = new Vector(); long totaltime = 0; if (args.length != 1) { System.err.println("Incorrect usage!"); System.exit(1); } BufferedReader br = null; try { totaltime = Long.parseLong(args[0], 10); } catch (Exception e) { handleEx(e); } if (totaltime < INTERVALSIZE) { System.err.println("You must have at least " + INTERVALSIZE + " instructions to go on this ride"); return; } long WIDTH = totaltime * PIXELS / 10000000L; System.err.println("WIDTH = " + WIDTH); if (WIDTH <= 0) return; //int[][] access = new int[(int)HEIGHT][(int)WIDTH]; //int[][] hit = new int[(int)HEIGHT][(int)WIDTH]; //int[][] l1picture = new int[(int)HEIGHT][(int)WIDTH]; //int[][] l2picture = new int[(int)HEIGHT][(int)WIDTH]; int[][] access_intensity = new int[(int)HEIGHT][(int)WIDTH]; int[][] hit_intensity = new int[(int)HEIGHT][(int)WIDTH]; int[][] l1intensity = new int[(int)HEIGHT][(int)WIDTH]; int[][] l2intensity = new int[(int)HEIGHT][(int)WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { //access[i][j] = -1; //hit[i][j] = -1; //l1picture[i][j] = -1; //l2picture[i][j] = -1; access_intensity[i][j] = 255; hit_intensity[i][j] = 255; l1intensity[i][j] = 255; l2intensity[i][j] = 255; } } long globaltime = 0; Vector pids = new Vector(); Vector counts = new Vector(); String s=""; /*try { br = new BufferedReader(new FileReader(args[1])); } catch (Exception e) { handleEx(e); } while (true) { try { s = br.readLine(); } catch (Exception e) { handleEx(e); } if (s == null) break; int index = s.indexOf(" "); if (index == -1) return; String phase = s.substring(0, index); int PHASE = Integer.parseInt(phase, 10); phases.add(new Integer(PHASE)); } try { br.close(); } catch (Exception e) { handleEx(e); }*/ try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { handleEx(e); } int current_phase = 0; while (true) { try { s = br.readLine(); } catch (Exception e) { handleEx(e); } if (s == null || s.length() == 0) { try {s = br.readLine(); } catch (Exception e) { handleEx(e); } if (s == null) break; else if (s.length() == 0) continue; } if (s.charAt(0) == '#') continue; else if (s.indexOf("Fini") != -1) continue; else if (s.indexOf(":") != -1) continue; int index = s.indexOf(" "); if (index == -1) continue; String count = "", addr = ""; try { count = s.substring(0,index); s = s.substring(index+1); index = s.indexOf(" "); if (index == -1) continue; s = s.substring(index+1); index = s.indexOf(" "); if (index == -1) continue; addr = s.substring(2, index); } catch (Exception e) { continue; } long COUNT = 0L, ADDR = 0L; try { COUNT = Long.parseLong(count, 10); ADDR = Long.parseLong(addr, 16); } catch (Exception e) { continue; } globaltime = COUNT; long col = 0; long row = (ADDR & (L1CACHESIZE-1)) * HEIGHT / L1CACHESIZE; row = HEIGHT - row; //Integer colour = new Integer(0); //if (current_phase < phases.size()) //colour = (Integer)(phases.elementAt(current_phase)); //int color = colour.intValue(); if (COUNT / INTERVALSIZE > current_phase) { current_phase++; System.err.print(" "+current_phase); } col = COUNT * WIDTH / totaltime; if (row >= HEIGHT || col >= WIDTH || row < 0 || col < 0) { continue; } cs.access(ADDR, is_l1hit, is_l2hit, accesstime); if (!(row < HEIGHT && col < WIDTH && row >= 0 && col >= 0)) { continue; } //access[(int)row][(int)col] = color; access_intensity[(int)row][(int)col] -= 32; if (access_intensity[(int)row][(int)col] < 0) access_intensity[(int)row][(int)col] = 0; if (is_l1hit.getvalue() == 1 && is_l2hit.getvalue() == 1) { //hit[(int)row][(int)col] = color; hit_intensity[(int)row][(int)col] -= 32; if (hit_intensity[(int)row][(int)col] < 0) hit_intensity[(int)row][(int)col] = 0; } if (is_l1hit.getvalue() == 0 && is_l2hit.getvalue() == 1) { //l1picture[(int)row][(int)col] = color; l1intensity[(int)row][(int)col] -= 32; if (l1intensity[(int)row][(int)col] < 0) l1intensity[(int)row][(int)col] = 0; } if (is_l1hit.getvalue() == 0 && is_l2hit.getvalue() == 0) { //l2picture[(int)row][(int)col] = color; l2intensity[(int)row][(int)col] -= 32; if (l2intensity[(int)row][(int)col] < 0) l2intensity[(int)row][(int)col] = 0; } } try { br.close(); } catch (Exception e) { handleEx(e); } // paint the grid lines for (long i = 0; true; i++) { long col = i * INTERVALSIZE * WIDTH / totaltime; if (col >= WIDTH) break; //System.out.println(""+col); for (int j = 0; j < HEIGHT; j+=2) { //access[j][(int)col] = -3; //hit[j][(int)col] = -3; //l1picture[j][(int)col] = -3; //l2picture[j][(int)col] = -3; access_intensity[j][(int)col] = 64; hit_intensity[j][(int)col] = 64; l1intensity[j][(int)col] = 64; l2intensity[j][(int)col] = 64; } } for (long i = 0; true; i++) { long col = i * 10L * INTERVALSIZE * WIDTH / totaltime; if (col >= WIDTH) break; for (int j = 0; j < HEIGHT; j++) { //access[j][(int)col] = -2; //hit[j][(int)col] = -2; //l1picture[j][(int)col] = -2; //l2picture[j][(int)col] = -2; access_intensity[j][(int)col] = 0; hit_intensity[j][(int)col] = 0; l1intensity[j][(int)col] = 0; l2intensity[j][(int)col] = 0; } } System.err.println("totaltime = " + globaltime); //writePicture(access, "access.gif", false, (int)WIDTH, (int)HEIGHT); //writePicture(hit, "hit.gif", false, (int)WIDTH, (int)HEIGHT); //writePicture(l1picture, "l1picture.gif", false,(int)WIDTH,(int)HEIGHT); //writePicture(l2picture, "l2picture.gif", false,(int)WIDTH,(int)HEIGHT); writePicture(access_intensity, "access_intensity.gif", true, (int)WIDTH, (int)HEIGHT); //writePicture(hit_intensity, "hit_intensity.gif", true, (int)WIDTH, (int)HEIGHT); //writePicture(l1intensity, "l1intensity.gif", true, (int)WIDTH, (int)HEIGHT); writePicture(l2intensity, "l2intensity.gif", true, (int)WIDTH, (int)HEIGHT); } static void writePicture(int picture[][], String filename, boolean intensity, int WIDTH, int HEIGHT) { byte[][] red = new byte[WIDTH][HEIGHT]; byte[][] green = new byte[WIDTH][HEIGHT]; byte[][] blue = new byte[WIDTH][HEIGHT]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { int pixel = picture[i][j]; byte r, g, b; if (!intensity) { Color pixelcolor = indextocolor(pixel); r = (byte)pixelcolor.getRed(); g = (byte)pixelcolor.getGreen(); b = (byte)pixelcolor.getBlue(); } else { r = (byte)pixel; g = (byte)pixel; b = (byte)pixel; } red[j][i] = r; green[j][i] = g; blue[j][i] = b; } } try { GIFEncoder g2 = new GIFEncoder(red, green, blue); File f2 = new File(filename); FileOutputStream fos2 = new FileOutputStream(f2); g2.Write(fos2); } catch (Exception e) { handleEx(e); } } static void handleEx(Exception e) { System.err.println("Exception occurred!" + e); e.printStackTrace(); } static void rank(long element, Vector r) { getrank(element, r); } static int getrank(long address, Vector r) { int low=0, mid, high = r.size() - 1; while (low <= high) { mid = (low + high) / 2; if (address == ((Long)(r.elementAt(mid))).longValue()) { return mid; } else if (address < ((Long)(r.elementAt(mid))).longValue()) { high = mid - 1; } else { low = mid + 1; } } r.insertElementAt(new Long(address), low); return low; } static int pidtocolor(Vector pids, int pid) { boolean found = false; int index = -1; for (int i = 0; i < pids.size(); i++) { int tpid = ((Integer)pids.elementAt(i)).intValue(); if (tpid == pid) { found = true; index = i; break; } } if (found == false) { pids.add(new Integer(pid)); return pidtocolor(pids, pid); } else { return index; } } public static final int NUM_COLORS = 27; static Color indextocolor(int index) { if (index == -1) return Color.white; else if (index == -2) return Color.black; else if (index == -3) return Color.darkGray; else if (index%NUM_COLORS == 0) return Color.red; else if (index%NUM_COLORS == 1) return Color.orange; else if (index%NUM_COLORS == 2) return Color.yellow; else if (index%NUM_COLORS == 3) return Color.green; else if (index%NUM_COLORS == 4) return Color.cyan; else if (index%NUM_COLORS == 5) return Color.blue; else if (index%NUM_COLORS == 6) return Color.magenta; else if (index%NUM_COLORS == 7) return Color.pink; else if (index%NUM_COLORS == 8) return new Color(0xA5,0x2A,0x2A); else if (index%NUM_COLORS == 9) return new Color(0x8A,0x2B,0xE2); else if (index%NUM_COLORS == 10) return new Color(0x7F,0xFF,0xD4); else if (index%NUM_COLORS == 11) return new Color(0x5F,0x9E,0xA0); else if (index%NUM_COLORS == 12) return new Color(0x7F,0xFF,0x00); else if (index%NUM_COLORS == 13) return new Color(0xD2,0x69,0x1E); else if (index%NUM_COLORS == 14) return new Color(0xFF,0x7F,0x50); else if (index%NUM_COLORS == 15) return new Color(0x64,0x95,0xED); else if (index%NUM_COLORS == 16) return new Color(0xDC,0x14,0x3C); else if (index%NUM_COLORS == 17) return new Color(0x00,0x00,0x8B); else if (index%NUM_COLORS == 18) return new Color(0xB8,0x86,0x0B); else if (index%NUM_COLORS == 19) return new Color(0xA9,0xA9,0xA9); else if (index%NUM_COLORS == 20) return new Color(0x00,0x64,0x00); else if (index%NUM_COLORS == 21) return new Color(0x8B,0x00,0x8B); else if (index%NUM_COLORS == 22) return new Color(0xBD,0xB7,0x6B); else if (index%NUM_COLORS == 23) return new Color(0xFF,0x14,0x93); else if (index%NUM_COLORS == 24) return new Color(0xAD,0xFF,0x2F); else if (index%NUM_COLORS == 25) return new Color(0xFF,0x45,0x00); else if (index%NUM_COLORS == 26) return new Color(0xA0,0x52,0x2D); else return Color.white; } static int frequency(Vector freqindex, Vector pidfreq, int pid) { int freq = 0; boolean bfound = false; for (int i = 0; i < pidfreq.size(); i++) { if (pid == ((Integer)(freqindex.elementAt(i))).intValue()) { freq = ((Integer)(pidfreq.elementAt(i))).intValue(); freq++; pidfreq.setElementAt(new Integer(freq), i); return freq; } } freq++; freqindex.add(new Integer(pid)); pidfreq.add(new Integer(freq)); return freq; } static int maxfrequency(Vector freqindex, Vector pidfreq) { int maxfreq = 0; int maxindex = 0; for (int i = 0; i < pidfreq.size(); i++) { int index = ((Integer)freqindex.elementAt(i)).intValue(); int freq = ((Integer)pidfreq.elementAt(i)).intValue(); if (freq > maxfreq) { maxfreq = freq; maxindex = index; } } return maxindex; } }