ThreadLevelDataManager.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.metric;
00002 
00003 import java.io.File;
00004 import java.io.IOException;
00005 import java.util.HashMap;
00006 
00007 import org.eclipse.jface.action.IStatusLineManager;
00008 import org.eclipse.jface.dialogs.MessageDialog;
00009 import org.eclipse.ui.IWorkbenchWindow;
00010 import org.eclipse.ui.PlatformUI;
00011 
00012 import edu.rice.cs.hpc.common.ui.Util;
00013 import edu.rice.cs.hpc.data.experiment.Experiment;
00014 import edu.rice.cs.hpc.data.experiment.metric.MetricRaw;
00015 import edu.rice.cs.hpc.data.util.IProgressReport;
00016 import edu.rice.cs.hpc.data.util.MergeDataFiles;
00017 
00018 /***
00019  * manager class to handle raw metrics and its read to file
00020  * All access to raw metrics (aka thread level data) has to use this file
00021  * 
00022  * @author laksonoadhianto
00023  *
00024  */
00025 public class ThreadLevelDataManager {
00026 
00027     private ThreadLevelDataFile data_file[];
00028     private Experiment experiment;
00029     private ThreadLevelDataCompatibility thread_data;
00030 
00031     public ThreadLevelDataManager(Experiment exp) {
00032         final MetricRaw []metrics = exp.getMetricRaw();
00033         if (metrics!=null)
00034             data_file = new ThreadLevelDataFile[metrics.length];
00035         this.experiment = exp;
00036         
00037         thread_data = new ThreadLevelDataCompatibility();
00038 
00039     }
00040     
00041     
00042     //==============================================================================================
00043     // PUBLIC METHODS
00044     //==============================================================================================
00045 
00050     public boolean isDataAvailable() {
00051         return (data_file != null && data_file.length>0);
00052     }
00053     
00054     
00055     
00061     public String[] getSeriesName() {
00062         MetricRaw []metrics_raw = experiment.getMetricRaw();
00063 
00064         if (metrics_raw == null)
00065             return null;
00066         
00067         String keys[] = new String[metrics_raw.length];
00068         for (int i=0; i<metrics_raw.length; i++)
00069             keys[i] = metrics_raw[i].getDisplayName();
00070         
00071         return keys;
00072     }
00073     
00074     
00075     
00087     public String[] getProcessIDs(int metric_raw_id) {
00088         return data_file[metric_raw_id].getRankLabels();
00089     }
00090     
00091     
00099     public double[] getProcessIDsDouble(int metric_raw_id) throws NumberFormatException {
00100         
00101         String x[] = data_file[metric_raw_id].getRankLabels();
00102         double xd[] = new double[x.length];
00103         for (int i=0; i<x.length; i++) {
00104             xd[i] = Double.valueOf(x[i]);
00105         }
00106         return xd;
00107     }
00108 
00109 
00117     public double[] getMetrics(MetricRaw metric, long node_index)
00118             throws IOException {
00119         if (this.data_file == null)
00120             return null;
00121             
00122         int metric_glob_id = metric.getID();
00123         
00124         if (data_file[metric_glob_id] == null) {
00125             checkThreadsMetricDataFiles(metric_glob_id);
00126         }
00127         
00128         ThreadLevelDataFile data = this.data_file[metric_glob_id];
00129         if (data != null)
00130             return data.getMetrics(node_index, metric.getRawID(), metric.getSize(), Util.getActiveStatusLineManager());
00131         else
00132             return null;
00133     }
00134 
00135     
00136     public ThreadLevelDataFile getThreadLevelDataFile(int metric_id) {
00137         return this.data_file[metric_id];
00138     }
00139     
00140     
00141     public void dispose() {
00142         if (data_file != null) {
00143             for (ThreadLevelDataFile data: data_file) {
00144                 if (data != null)
00145                     data.dispose();
00146             }
00147         }
00148     }
00149     
00150     //==============================================================================================
00151     // PRIVATE METHODS
00152     //==============================================================================================
00153     private void checkThreadsMetricDataFiles(int metric_raw_id) throws IOException {
00154         
00155         File directory = experiment.getDefaultDirectory();
00156         if (directory.isFile())
00157             directory = new File(directory.getParent());
00158 
00159         {
00160             String file = thread_data.getMergedFile(directory, metric_raw_id);
00161             if (file == null)
00162                 throw new IOException("Data file does not exist in " + directory.getAbsolutePath());
00163             
00164             // keep it for later uses
00165             data_file[metric_raw_id] = new ThreadLevelDataFile();
00166             data_file[metric_raw_id].open(file);
00167         }
00168             
00169     }
00170 
00171     
00172     
00181     private class ThreadLevelDataCompatibility {
00182         
00183         private HashMap<String, String> listOfFiles;
00184         
00185         public ThreadLevelDataCompatibility() {
00186             listOfFiles = new HashMap<String, String>();
00187         }
00188         
00200         public String getMergedFile(File directory, int metric_raw_id) throws IOException {
00201             
00202             final MetricRaw metric = experiment.getMetricRaw()[metric_raw_id];
00203             final String globInputFile = metric.getGlob();
00204             
00205             // assuming the number of merged experiments is less than 10
00206             final char experiment_char = globInputFile.charAt(0);
00207             int experiment_id = 1;
00208             
00209             if (experiment_char>='0' && experiment_char<='9') {
00210                 experiment_id = experiment_char - '0';
00211             }
00212             
00213             // ------------------------------------------------------------------------------------
00214             // given the metric raw id, reconstruct the name of raw metric data file
00215             // for instance, if raw metric id = 1, then the file should be experiment-1.mdb
00216             // ------------------------------------------------------------------------------------
00217             final String outputFile = directory.getAbsolutePath() + File.separatorChar + 
00218                     "experiment-" + experiment_id + ".mdb";
00219 
00220             // check if the file is already merged
00221             String cacheFileName = this.listOfFiles.get(outputFile);
00222             
00223             if (cacheFileName == null) {
00224                 
00225                 // ----------------------------------------------------------
00226                 // the file doesn't exist, we need to merge metric-db files
00227                 // ----------------------------------------------------------
00228                 // check with the old version of thread level data
00229                 this.checkOldVersionOfData(directory);
00230                 
00231                 final ProgressReport progress= new ProgressReport( Util.getActiveStatusLineManager() );
00232                 
00233                 // ------------------------------------------------------------------------------------
00234                 // the compact method will return the name of the compacted files.
00235                 // if the file doesn't exist, it will be created automatically
00236                 // ------------------------------------------------------------------------------------
00237                 MergeDataFiles.MergeDataAttribute att = MergeDataFiles.merge(directory, 
00238                         globInputFile, outputFile, progress);
00239                 
00240                 if (att == MergeDataFiles.MergeDataAttribute.FAIL_NO_DATA) {
00241                     // ------------------------------------------------------------------------------------
00242                     // the data doesn't exist. Let's try to use experiment.mdb for compatibility with the old version
00243                     // ------------------------------------------------------------------------------------
00244                     cacheFileName =  directory.getAbsolutePath() + File.separatorChar + "experiment.mdb";
00245                     att = MergeDataFiles.merge(directory, globInputFile, cacheFileName, progress);
00246                     
00247                     if (att == MergeDataFiles.MergeDataAttribute.FAIL_NO_DATA)
00248                         return null;
00249                 } else {
00250                     cacheFileName = outputFile;
00251                 }
00252                 this.listOfFiles.put(outputFile, cacheFileName);
00253 
00254             }
00255             return cacheFileName;
00256         }
00257         
00258         private void checkOldVersionOfData(File directory) {
00259             
00260             String oldFile = directory.getAbsolutePath() + File.separatorChar + "experiment.mdb"; 
00261             File file = new File(oldFile);
00262             
00263             if (file.canRead()) {
00264                 // old file already exist, needs to warn the user
00265                 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
00266                 MessageDialog.openWarning(window.getShell(), "Warning ! Old version of metric data file",
00267                         "hpcviewer has detected the presence of an old version of metric data file:\n 'experiment.mdb'\n in the directory:\n "
00268                         + directory.getPath() + "\nIt is highly suggested to remove the file and replace it with the original *.metric-db files from hpcprof-mpi.");
00269             }
00270         }
00271     }
00272     
00273     
00274     /*******************
00275      * Progress bar
00276      * @author laksonoadhianto
00277      *
00278      */
00279     private class ProgressReport implements IProgressReport 
00280     {
00281         final private IStatusLineManager statusLine;
00282 
00283         public ProgressReport(IStatusLineManager statusMgr)
00284         {
00285             statusLine = statusMgr;
00286         }
00287         
00288         public void begin(String title, int num_tasks) {
00289             if (statusLine != null) {
00290                 statusLine.setMessage(title);
00291                 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().update();
00292                 statusLine.getProgressMonitor().beginTask(title, num_tasks);
00293             }
00294         }
00295 
00296         public void advance() {
00297             if (statusLine != null) 
00298                 statusLine.getProgressMonitor().worked(1);
00299         }
00300 
00301         public void end() {
00302             if (statusLine != null) 
00303                 statusLine.getProgressMonitor().done();
00304         }
00305         
00306     }
00307 
00308 } 

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1