DataSummary.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.db;
00002 
00003 import java.io.FileNotFoundException;
00004 import java.io.IOException;
00005 import java.io.PrintStream;
00006 import java.io.RandomAccessFile;
00007 import java.nio.ByteBuffer;
00008 import java.nio.LongBuffer;
00009 import java.nio.MappedByteBuffer;
00010 import java.nio.channels.FileChannel;
00011 import java.nio.channels.FileChannel.MapMode;
00012 import java.util.Random;
00013 
00014 import edu.rice.cs.hpc.data.db.DataCommon;
00015 
00016 /*********************************************
00017  * 
00018  * Class to handle summary.db file generated by hpcprof
00019  *
00020  *********************************************/
00021 public class DataSummary extends DataCommon 
00022 {
00023     // --------------------------------------------------------------------
00024     // constants
00025     // --------------------------------------------------------------------
00026     
00027     //private final static String SUMMARY_NAME = "hpctoolkit summary metrics";
00028     static final private int FLOAT_SIZE        = Float.SIZE / Byte.SIZE;
00029     static final private int INTEGER_SIZE      = Integer.SIZE / Byte.SIZE; 
00030     static final private int METRIC_ENTRY_SIZE = FLOAT_SIZE + INTEGER_SIZE;
00031     static final public float DEFAULT_METRIC  = 0.0f;
00032     
00033     // --------------------------------------------------------------------
00034     // object variable
00035     // --------------------------------------------------------------------
00036     
00037     long offset_start;
00038     long offset_size;
00039     long metric_start;
00040     long metric_size;
00041     
00042     int size_offset;
00043     int size_metid;
00044     int size_metval;
00045 
00046     int []cct_table = null;
00047     
00048     private RandomAccessFile file;
00049     private FileChannel channel;
00050 
00051     
00052     // --------------------------------------------------------------------
00053     // Public methods
00054     // --------------------------------------------------------------------
00055     
00056     /***
00057      *  <p>Opening for data summary metric file</p>
00058      * (non-Javadoc)
00059      * @see edu.rice.cs.hpc.data.db.DataCommon#open(java.lang.String)
00060      */
00061     @Override
00062     public void open(final String file)
00063             throws IOException
00064     {
00065         super.open(file);
00066         
00067         open_internal(file);
00068         // fill the cct offset table
00069         fillOffsetTable(file);
00070     }
00071     
00072 
00073     @Override
00074     /*
00075      * (non-Javadoc)
00076      * @see edu.rice.cs.hpc.data.db.DataCommon#printInfo(java.io.PrintStream)
00077      */
00078     public void printInfo( PrintStream out)
00079     {
00080         super.printInfo(out);
00081         out.println("Offset start: " + offset_start);
00082         out.println("Offset size: "  + offset_size);
00083         
00084         out.println("metric start: " + metric_start);
00085         out.println("metric size: "  + metric_size);
00086         
00087         out.println("size offset: "  + size_offset);
00088         out.println("size met id: "  + size_metid);
00089         out.println("size met val: " + size_metval);
00090         
00091         if (cct_table != null) 
00092         {
00093             for(int i=0; i<num_cctid; i++)
00094             {
00095                 int num_metrics = (int) (cct_table[i+1] - cct_table[i]);
00096                 out.format("%4x (%2d) ", cct_table[i], num_metrics);
00097                 if (i % 16 == 15)
00098                 {
00099                     out.println();
00100                 }
00101             }
00102             out.println();
00103             // print random metrics
00104             for (int i=0; i<15; i++)
00105             {
00106                 Random r = new Random();
00107                 int cct  = r.nextInt((int) num_cctid);
00108                 out.format("[%5d] ", cct);
00109                 for (int j=0; j<num_metric; j++)
00110                 {
00111                     try {
00112                         float value = getMetric(cct, j);
00113                         out.format(" %4.2e  ", value);
00114                     } catch (IOException e) {
00115                         // TODO Auto-generated catch block
00116                         e.printStackTrace();
00117                     }
00118                 }
00119                 out.println();
00120             }
00121         }
00122     }
00123     
00124     /******
00125      * Retrieve a metric value for a specified cct id and metric id
00126      * 
00127      * @param cct_id    : cct id scope
00128      * @param metric_id : metric ID
00129      * 
00130      * @return float metric value
00131      * @throws IOException
00132      */
00133     public float getMetric(int cct_id, int metric_id) throws IOException
00134     {
00135         long offset        = (metric_start + cct_table[cct_id]);
00136         int offset_size    = (int) (cct_table[cct_id+1] - cct_table[cct_id]);
00137         
00138         if (offset_size<=0)
00139             return (float)DEFAULT_METRIC;
00140         
00141         long file_length   = file.length();
00142         // make sure the offset is within the file range
00143         if (file_length > offset)
00144         {
00145             file.seek(offset);
00146             byte []metric_byte = new byte[offset_size];
00147             
00148             file.readFully(metric_byte);
00149             ByteBuffer buffer = ByteBuffer.wrap(metric_byte);
00150             
00151             int num_metrics   = offset_size / METRIC_ENTRY_SIZE; 
00152             
00153             for(int i=0; i<num_metrics; i++)
00154             {
00155                 int my_metric_id = buffer.getInt();
00156                 float metric_val = buffer.getFloat();
00157                 
00158                 if (my_metric_id == metric_id) {
00159                     return metric_val;
00160                 }
00161             }
00162         }
00163         return DEFAULT_METRIC;
00164     }
00165     
00166     // --------------------------------------------------------------------
00167     // Private methods
00168     // --------------------------------------------------------------------
00169     
00170     private void fillOffsetTable(final String filename)
00171             throws IOException
00172     {
00173         // map all the table into memory. 
00174         // This statement can be problematic if the offset_size is huge
00175         
00176         MappedByteBuffer mappedBuffer = channel.map(MapMode.READ_ONLY, offset_start, offset_size);
00177         LongBuffer longBuffer = mappedBuffer.asLongBuffer();
00178         
00179         cct_table = new int[(int) num_cctid+1];
00180         
00181         for (int i=0; i<=num_cctid; i++)
00182         {
00183             cct_table[i] = (int) longBuffer.get(i);
00184         }
00185     }
00186     
00187     
00188     // --------------------------------------------------------------------
00189     // Protected methods
00190     // --------------------------------------------------------------------
00191     
00192     @Override
00193     protected boolean isTypeFormatCorrect(long type) {
00194         return type==1;
00195     }
00196 
00197     @Override
00198     protected boolean isFileHeaderCorrect(String header) {
00199         // suggestion from Mark: ignore the header file name
00200         return true; //header.startsWith(SUMMARY_NAME);
00201     }
00202 
00203     @Override
00204     protected boolean readNext(FileChannel input) 
00205             throws IOException
00206     {
00207         ByteBuffer buffer = ByteBuffer.allocate(256);
00208         int numBytes      = input.read(buffer);
00209         if (numBytes > 0) 
00210         {
00211             buffer.flip();
00212             
00213             offset_start = buffer.getLong();
00214             offset_size  = buffer.getLong();
00215             metric_start = buffer.getLong();
00216             metric_size  = buffer.getLong();
00217             
00218             size_offset  = buffer.getInt();
00219             size_metid   = buffer.getInt();
00220             size_metval  = buffer.getInt();
00221         }       
00222         return false;
00223     }
00224 
00225     /*
00226      * (non-Javadoc)
00227      * @see edu.rice.cs.hpc.data.db.DataCommon#dispose()
00228      */
00229     public void dispose() throws IOException
00230     {
00231         channel.close();
00232         file.close();
00233     }
00234     // --------------------------------------------------------------------
00235     // Private methods
00236     // --------------------------------------------------------------------
00237     
00238     private void open_internal(String filename) throws FileNotFoundException
00239     {
00240         file    = new RandomAccessFile(filename, "r");
00241         channel = file.getChannel();
00242     }
00243 
00244     /***************************
00245      * unit test 
00246      * 
00247      * @param argv
00248      ***************************/
00249     public static void main(String []argv)
00250     {
00251         final String filename = "/home/la5/data/new-database/db-lulesh-new/summary.db";
00252         final DataSummary summary_data = new DataSummary();
00253         try {
00254             summary_data.open(filename);            
00255             summary_data.printInfo(System.out);
00256             
00257         } catch (IOException e) {
00258             // TODO Auto-generated catch block
00259             e.printStackTrace();
00260         }
00261     }
00262 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1