DataCommon.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.data.db;
00002 
00003 import java.io.FileInputStream;
00004 import java.io.IOException;
00005 import java.io.PrintStream;
00006 import java.nio.ByteBuffer;
00007 import java.nio.channels.FileChannel;
00008 
00009 /***************************************************
00010  * 
00011  * Class to read and store common header of a database
00012  * This class only works with hpcprof format version 3.0 
00013  *
00014  ***************************************************/
00015 abstract public class DataCommon 
00016 {
00017     private final static int HEADER_COMMON_SIZE = 256;
00018     private final static int MESSAGE_SIZE       = 32;
00019     private final static int MAGIC              = 0x06870630;
00020     
00021     protected long format;
00022     protected long num_threads;
00023     protected long num_cctid;
00024     protected long num_metric;
00025     
00026     protected String filename;
00027     
00028     /*******
00029      * Open a file and read the first HEADER_COMMON_SIZE bytes
00030      * 
00031      * @param file : a string of the filename
00032      * @throws IOException
00033      */
00034     public void open(final String file) 
00035             throws IOException
00036     {
00037         filename = file;
00038         
00039         final FileInputStream fis = new FileInputStream(filename);
00040         final FileChannel channel  = fis.getChannel();
00041         
00042         ByteBuffer buffer = ByteBuffer.allocate(HEADER_COMMON_SIZE);
00043         int numBytes      = channel.read(buffer);
00044         if (numBytes > 0) 
00045         {
00046             // read the common header
00047             readHeader(file, fis, buffer);
00048             
00049             // -------------------------------------------------------------
00050             // Read the next header (implemented by derived class)
00051             // -------------------------------------------------------------
00052             if ( readNext(channel) ) 
00053             {
00054                 // the implementer can perform other operations
00055             }
00056         }
00057 
00058         channel.close();
00059         fis.close();
00060     }
00061     
00062     /******
00063      * print out the information of the content of the file
00064      * @param out : the output stream (can be the standard output)
00065      */
00066     public void printInfo( PrintStream out)
00067     {
00068         out.println("Format: "      + format);
00069         out.println("Num threads: " + num_threads);
00070         out.println("Num cctid: "   + num_cctid);
00071         out.println("Num metric: "  + num_metric);
00072     }
00073     
00074     /***
00075      * Free the allocated resources. To be called by the caller at the end.
00076      * @throws IOException 
00077      */
00078     public void dispose() throws IOException
00079     {
00080         // to be implemented
00081     }
00082     
00083     /******
00084      * Default implementation to read the first HEADER_COMMON_SIZE bytes
00085      * 
00086      * @param file  : filename
00087      * @param fis   : input file stread (to be closed in case of exception) 
00088      * @param buffer : the current buffer containing the first 256 bytes
00089      * 
00090      * @throws IOException
00091      */
00092     protected void readHeader(String file, FileInputStream fis, ByteBuffer buffer) 
00093             throws IOException
00094     {
00095         // -------------------------------------------------------------
00096         // get the message header file
00097         // -------------------------------------------------------------
00098         final byte []bytes = new byte[MESSAGE_SIZE]; 
00099         buffer.flip();
00100         buffer.get(bytes, 0, MESSAGE_SIZE);
00101 
00102         // -------------------------------------------------------------
00103         // get the magic number
00104         // -------------------------------------------------------------
00105         long magic_number = buffer.getLong();
00106         if (magic_number != MAGIC)
00107         {
00108             throw_exception(fis, "Magic number is incorrect: " + magic_number);
00109         }
00110         
00111         // -------------------------------------------------------------
00112         // check the version
00113         // -------------------------------------------------------------
00114         final long version = buffer.getLong();
00115         if (version < 3)
00116         {
00117             throw_exception(fis, "Incorrect file version: " + version);
00118         }
00119 
00120         // -------------------------------------------------------------
00121         // check the type
00122         // -------------------------------------------------------------
00123         final long type = buffer.getLong();         
00124         if (!isTypeFormatCorrect(type))
00125         {
00126             throw_exception(fis, file + " has inconsistent type " + type);
00127         }
00128         
00129         // -------------------------------------------------------------
00130         // check the format
00131         // -------------------------------------------------------------
00132         // to be ignored at the moment
00133         format = buffer.getLong();
00134         
00135         // -------------------------------------------------------------
00136         // read number of cct
00137         // -------------------------------------------------------------
00138         num_cctid = buffer.getLong();
00139         
00140         // -------------------------------------------------------------
00141         // read number of metrics
00142         // -------------------------------------------------------------
00143         num_metric = buffer.getLong();
00144         
00145         // -------------------------------------------------------------
00146         // read number of threads
00147         // -------------------------------------------------------------
00148         num_threads = buffer.getLong();
00149         
00150         if (num_threads <= 0 || num_cctid <= 0 || num_metric <=0) 
00151         {
00152             // warning: empty database.
00153             // this doesn't mean the file is invalid, but just anomaly
00154         }
00155     }
00156     
00157     /*******
00158      * function to close the input stream and thrown an  IO exception
00159      * 
00160      * @param input : the io to be closed
00161      * @param message : message to be thrown
00162      * 
00163      * @throws IOException
00164      */
00165     private void throw_exception(FileInputStream input, String message)
00166             throws IOException
00167     {
00168         input.close();
00169         throw new IOException(message);
00170     }
00171     
00172     protected abstract boolean isTypeFormatCorrect(long type);
00173     protected abstract boolean isFileHeaderCorrect(String header);
00174     protected abstract boolean readNext(FileChannel input) throws IOException;
00175 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1