sysperfana/heapanalyser/Libraries/Engine/HeapComparisonLib/CSV/Workers/CSVWorkerAllDataSetComparator.cs
changeset 8 15296fd0af4a
equal deleted inserted replaced
7:8e12a575a9b5 8:15296fd0af4a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * Redistribution and use in source and binary forms, with or without
       
     6 * modification, are permitted provided that the following conditions are met:
       
     7 *
       
     8 * - Redistributions of source code must retain the above copyright notice,
       
     9 *   this list of conditions and the following disclaimer.
       
    10 * - Redistributions in binary form must reproduce the above copyright notice,
       
    11 *   this list of conditions and the following disclaimer in the documentation
       
    12 *   and/or other materials provided with the distribution.
       
    13 * - Neither the name of Nokia Corporation nor the names of its contributors
       
    14 *   may be used to endorse or promote products derived from this software
       
    15 *   without specific prior written permission.
       
    16 *
       
    17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
       
    21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    27 * POSSIBILITY OF SUCH DAMAGE.
       
    28 * 
       
    29 * Initial Contributors:
       
    30 * Nokia Corporation - initial contribution.
       
    31 *
       
    32 * Contributors:
       
    33 *
       
    34 * Description: 
       
    35 *
       
    36 */
       
    37 
       
    38 using System;
       
    39 using System.IO;
       
    40 using System.Collections.Generic;
       
    41 using System.Threading;
       
    42 using SymbianUtils;
       
    43 
       
    44 namespace HeapComparisonLib.CSV
       
    45 {
       
    46     internal class CSVWorkerAllDataSetComparator : DisposableObject
       
    47 	{
       
    48 		#region Observer related
       
    49 		public enum TEvent
       
    50 		{
       
    51 			EExportingStarted = 0,
       
    52 			EExportingProgress,
       
    53 			EExportingComplete
       
    54 		}
       
    55 
       
    56         public delegate void Observer( TEvent aEvent );
       
    57 		public event Observer eObserver;
       
    58 		#endregion
       
    59 
       
    60 		#region Constructors & destructor
       
    61         public CSVWorkerAllDataSetComparator( CSVDataSetCollection aDataSets, string aOutputFileName )
       
    62 		{
       
    63             // Create our list of sorted comparison pairs.
       
    64             iDataSets = aDataSets;
       
    65             iUniqueThreadDetails = IdentifyAllThreadNames();
       
    66             iExcelExporter = new CSVExcelExporterAllDataSets( aOutputFileName, iUniqueThreadDetails, aDataSets.Count );
       
    67         }
       
    68 		#endregion
       
    69 
       
    70 		#region API
       
    71 		public void CompareAndSaveAsync()
       
    72 		{
       
    73             iWorkerThread = new System.Threading.Thread( new ThreadStart( ThreadFunction ) );
       
    74             iWorkerThread.Start();
       
    75 		}
       
    76 		#endregion
       
    77 
       
    78 		#region Properties
       
    79 		public int Progress
       
    80 		{
       
    81 			get
       
    82             {
       
    83                 int count = 0;
       
    84                 int index = 0;
       
    85                 //
       
    86                 lock ( this )
       
    87                 {
       
    88                     index = iIndex;
       
    89                     count = iDataSets.Count;
       
    90                 }
       
    91 
       
    92                 float ret = (float) index / (float) count;
       
    93                 ret *= 100.0f;
       
    94                 return (int) ret;
       
    95             }
       
    96 		}
       
    97 		#endregion
       
    98 
       
    99         #region From DisposableObject
       
   100         protected override void CleanupManagedResources()
       
   101         {
       
   102             try
       
   103             {
       
   104                 if ( iExcelExporter != null )
       
   105                 {
       
   106                     iExcelExporter.Dispose();
       
   107                     iExcelExporter = null;
       
   108                 }
       
   109             }
       
   110             finally
       
   111             {
       
   112                 base.CleanupManagedResources();
       
   113             }
       
   114         }
       
   115         #endregion
       
   116 
       
   117         #region Thread function
       
   118         private void ThreadFunction()
       
   119         {
       
   120             // Report started
       
   121             if ( eObserver != null )
       
   122             {
       
   123                 eObserver( TEvent.EExportingStarted );
       
   124             }
       
   125 
       
   126             // Now compare them and export to excel
       
   127             CompareAndExport();
       
   128 
       
   129             // Dispose excel object - closes file
       
   130             iExcelExporter.Dispose();
       
   131             iExcelExporter = null;
       
   132 
       
   133             // Report finished
       
   134             if ( eObserver != null )
       
   135             {
       
   136                 eObserver( TEvent.EExportingComplete );
       
   137             }
       
   138         }
       
   139 		#endregion
       
   140 
       
   141 		#region Internal methods
       
   142         private SortedDictionary<long, string> IdentifyAllThreadNames()
       
   143         {
       
   144             // Get a list of unique thread ids and their full names
       
   145             SortedDictionary<long, string> list = new SortedDictionary<long, string>();
       
   146             int count = iDataSets.Count;
       
   147             for ( int i = 0; i<count; i++ )
       
   148             {
       
   149                 CSVDataSet set = iDataSets[ i ];
       
   150                 AddThreadsToList( set, list );
       
   151             }
       
   152 
       
   153             return list;
       
   154         }
       
   155 
       
   156         private void AddThreadsToList( CSVDataSet aSet, SortedDictionary<long, string> aList )
       
   157         {
       
   158             int count = aSet.Count;
       
   159             for ( int i = count - 1; i >= 0; i-- )
       
   160             {
       
   161                 CSVThread master = aSet[ i ];
       
   162                 if ( aList.ContainsKey( master.ThreadId ) == false )
       
   163                 {
       
   164                     aList.Add( master.ThreadId, master.FullName );
       
   165                 }
       
   166             }
       
   167         }
       
   168 
       
   169         private void CompareAndExport()
       
   170         {
       
   171             lock ( this )
       
   172             {
       
   173                 iIndex = 0;
       
   174             }
       
   175 
       
   176             for( ; iIndex<iDataSets.Count; )
       
   177             {
       
   178                 CSVDataSet set = iDataSets[ iIndex ];
       
   179 
       
   180                 iExcelExporter.Export( set );
       
   181 
       
   182                 // Report progress to any observers
       
   183                 if ( eObserver != null )
       
   184                 {
       
   185                     eObserver( TEvent.EExportingProgress );
       
   186                 }
       
   187 
       
   188                 lock ( this )
       
   189                 {
       
   190                     ++iIndex;
       
   191                 }
       
   192             }
       
   193         }
       
   194 		#endregion
       
   195 
       
   196 		#region Data members
       
   197         private readonly CSVDataSetCollection iDataSets;
       
   198         private readonly SortedDictionary<long, string> iUniqueThreadDetails;
       
   199         private CSVExcelExporterAllDataSets iExcelExporter = null;
       
   200         private System.Threading.Thread iWorkerThread;
       
   201         private int iIndex = 0;
       
   202 		#endregion
       
   203 	}
       
   204 }