sysperfana/heapanalyser/Libraries/UI/HeapUiLib/Controls/HeapCellStatsControl.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.Drawing;
       
    40 using System.Collections;
       
    41 using System.Collections.Generic;
       
    42 using System.ComponentModel;
       
    43 using System.Windows.Forms;
       
    44 using System.IO;
       
    45 using System.Text;
       
    46 using SymbianUtils;
       
    47 using HeapLib;
       
    48 using HeapLib.Cells;
       
    49 using HeapLib.Array;
       
    50 using HeapLib.Reconstructor;
       
    51 using HeapLib.Statistics;
       
    52 using HeapLib.Statistics.Tracking.Base;
       
    53 using HeapLib.Statistics.Tracking.BySymbol;
       
    54 using HeapLib.Reconstructor.DataSources;
       
    55 
       
    56 namespace HeapUiLib.Controls
       
    57 {
       
    58     public partial class HeapCellStatsControl : UserControl
       
    59     {
       
    60         #region Constructors
       
    61         public HeapCellStatsControl()
       
    62         {
       
    63             InitializeComponent();
       
    64         }
       
    65         #endregion
       
    66 
       
    67         #region API
       
    68         #endregion
       
    69 
       
    70         #region Properties
       
    71         [Browsable(false)]
       
    72         [DefaultValue(null)]
       
    73         [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
       
    74         public HeapReconstructor Reconstructor
       
    75         {
       
    76             get { return iReconstructor; }
       
    77             set
       
    78             {
       
    79                 iReconstructor = value;
       
    80                 //
       
    81                 if ( iReconstructor != null )
       
    82                 {
       
    83                     UpdateMainTable();
       
    84                     UpdateSummary();
       
    85                 }
       
    86             }
       
    87         }
       
    88 
       
    89         private HeapStatistics Statistics
       
    90         {
       
    91             get
       
    92             {
       
    93                 HeapStatistics ret = null;
       
    94                 //
       
    95                 if ( Reconstructor != null )
       
    96                 {
       
    97                     ret = Reconstructor.Statistics;
       
    98                 }
       
    99                 //
       
   100                 return ret;
       
   101             }
       
   102         }
       
   103         #endregion
       
   104 
       
   105         #region Event handlers
       
   106         private void iPG3_StatsTable_SelectionChanged( object sender, XPTable.Events.SelectionEventArgs e )
       
   107         {
       
   108             if ( e.NewSelectedIndicies.Length > 0 )
       
   109             {
       
   110                 UpdateSelectionTotals();
       
   111             }
       
   112         }
       
   113 
       
   114         private void iPG3_StatsTable_PrepareForSort( object sender, XPTable.Events.SortEventArgs e )
       
   115         {
       
   116             TrackingComparisonBase type = null;
       
   117             switch ( e.Index )
       
   118             {
       
   119             default:
       
   120             case 0:
       
   121                 type = new HeapLib.Statistics.Tracking.Base.TrackingInfoSortBySymbolName( false );
       
   122                 break;
       
   123             case 1:
       
   124                 type = new HeapLib.Statistics.Tracking.Base.TrackingInfoSortByCount( false );
       
   125                 break;
       
   126             case 2:
       
   127                 type = new HeapLib.Statistics.Tracking.Base.TrackingInfoSortByPayloadLength( false );
       
   128                 break;
       
   129             case 3:
       
   130                 type = new HeapLib.Statistics.Tracking.Base.TrackingInfoSortByAssociatedMemory( false );
       
   131                 break;
       
   132             }
       
   133             //
       
   134             if ( type == null )
       
   135             {
       
   136                 throw new Exception( "No comparer for specified column" );
       
   137             }
       
   138             //
       
   139             e.Comparer = new TrackingInfoComparerWrapper( type );
       
   140         }
       
   141         #endregion
       
   142 
       
   143         #region Internal classes
       
   144         private class TrackingInfoComparerWrapper : IComparer
       
   145         {
       
   146             #region Constructors
       
   147             public TrackingInfoComparerWrapper( TrackingComparisonBase aBase )
       
   148             {
       
   149                 iBaseComparer = aBase;
       
   150             }
       
   151             #endregion
       
   152 
       
   153             #region IComparer Members
       
   154             public int Compare( object aLeft, object aRight )
       
   155             {
       
   156                 TrackingInfo left = null;
       
   157                 TrackingInfo right = null;
       
   158                 //
       
   159                 if ( ( aLeft is TrackingInfo ) && ( aRight is TrackingInfo ) )
       
   160                 {
       
   161                     left = (TrackingInfo) aLeft;
       
   162                     right = (TrackingInfo) aRight;
       
   163                 }
       
   164                 else if ( aLeft is XPTable.Models.Cell && aRight is XPTable.Models.Cell )
       
   165                 {
       
   166                     XPTable.Models.Cell cellLeft = ( aLeft as XPTable.Models.Cell );
       
   167                     XPTable.Models.Cell cellRight = ( aRight as XPTable.Models.Cell );
       
   168                     //
       
   169                     left = (TrackingInfo) cellLeft.Row.Tag;
       
   170                     right = (TrackingInfo) cellRight.Row.Tag;
       
   171                 }
       
   172 
       
   173                 // Now do the compare...
       
   174                 int ret = iBaseComparer.Compare( left, right );
       
   175                 return ret;
       
   176             }
       
   177             #endregion
       
   178 
       
   179             #region Data members
       
   180             private readonly TrackingComparisonBase iBaseComparer;
       
   181             #endregion
       
   182         }
       
   183         #endregion
       
   184 
       
   185         #region Internal methods
       
   186         private void UpdateMainTable()
       
   187         {
       
   188             iTable_SymbolMemory.BeginUpdate();
       
   189             iTable_SymbolMemory.TableModel.Rows.Clear();
       
   190 
       
   191             iReconstructor.Statistics.StatsAllocated.TrackerSymbols.SortByAllocatedMemory();
       
   192             //
       
   193             foreach ( TrackingInfo item in iReconstructor.Statistics.StatsAllocated.TrackerSymbols )
       
   194             {
       
   195                 XPTable.Models.Row row = new XPTable.Models.Row();
       
   196                 row.Tag = item;
       
   197 
       
   198                 // SYMBOL
       
   199                 System.Diagnostics.Debug.Assert( item.Symbol != null );
       
   200                 row.Cells.Add( new XPTable.Models.Cell( item.Symbol.NameWithoutVTablePrefix ) );
       
   201 
       
   202                 // ALLOC COUNT
       
   203                 row.Cells.Add( new XPTable.Models.Cell( item.Count.ToString() ) );
       
   204 
       
   205                 // MEMORY-PER-INSTANCE
       
   206                 row.Cells.Add( new XPTable.Models.Cell( item.PayloadLength.ToString() ) );
       
   207 
       
   208                 // TOTAL ALLOCATED MEMORY
       
   209                 row.Cells.Add( new XPTable.Models.Cell( item.AssociatedMemory.ToString() ) );
       
   210 
       
   211                 // Add row
       
   212                 iTable_SymbolMemory.TableModel.Rows.Add( row );
       
   213             }
       
   214 
       
   215             iTable_SymbolMemory.EndUpdate();
       
   216         }
       
   217 
       
   218         private void UpdateSummary()
       
   219         {
       
   220             long heapCellHeaderOverheadSize = Statistics.SizeTotalHeader;
       
   221             long symbolicMatchMemorySize = Statistics.StatsAllocated.TrackerSymbols.TypeSize;
       
   222             long otherMemorySize = ( Statistics.StatsAllocated.TypeSize - Statistics.StatsAllocated.TrackerSymbols.TypeSize );
       
   223             long freeSpaceSize = Statistics.StatsFree.TypeSize;
       
   224             long totalCellCount = Statistics.StatsAllocated.TypeCount + Statistics.StatsFree.TypeCount;
       
   225 
       
   226             // 1ST GROUP
       
   227             iLV_Summary.Items[ 0 ].SubItems[ 1 ].Text = Statistics.StatsAllocated.TypeCount.ToString();
       
   228             iLV_Summary.Items[ 0 ].SubItems[ 2 ].Text = NumberFormattingUtils.NumberAsPercentageTwoDP( Statistics.StatsAllocated.TypeCount, totalCellCount ) + " %";
       
   229             iLV_Summary.Items[ 1 ].SubItems[ 1 ].Text = Statistics.StatsFree.TypeCount.ToString();
       
   230             iLV_Summary.Items[ 1 ].SubItems[ 2 ].Text = NumberFormattingUtils.NumberAsPercentageTwoDP( Statistics.StatsFree.TypeCount, totalCellCount ) + " %";
       
   231             
       
   232             // 2ND GROUP
       
   233             iLV_Summary.Items[ 2 ].SubItems[ 1 ].Text = symbolicMatchMemorySize.ToString();
       
   234             iLV_Summary.Items[ 2 ].SubItems[ 2 ].Text = NumberFormattingUtils.NumberAsPercentageTwoDP( symbolicMatchMemorySize, symbolicMatchMemorySize + otherMemorySize + freeSpaceSize ) + " %";
       
   235             iLV_Summary.Items[ 3 ].SubItems[ 1 ].Text = otherMemorySize.ToString();
       
   236             iLV_Summary.Items[ 3 ].SubItems[ 2 ].Text = NumberFormattingUtils.NumberAsPercentageTwoDP( otherMemorySize, symbolicMatchMemorySize + otherMemorySize + freeSpaceSize ) + " %";
       
   237             // (4) is a spacer
       
   238             iLV_Summary.Items[ 5 ].SubItems[ 1 ].Text = ( symbolicMatchMemorySize + otherMemorySize ).ToString();
       
   239             iLV_Summary.Items[ 5 ].SubItems[ 2 ].Text = NumberFormattingUtils.NumberAsPercentageTwoDP( symbolicMatchMemorySize + otherMemorySize, symbolicMatchMemorySize + otherMemorySize + freeSpaceSize ) + " %";
       
   240             iLV_Summary.Items[ 6 ].SubItems[ 1 ].Text = freeSpaceSize.ToString();
       
   241             iLV_Summary.Items[ 6 ].SubItems[ 2 ].Text = NumberFormattingUtils.NumberAsPercentageTwoDP( freeSpaceSize, symbolicMatchMemorySize + otherMemorySize + freeSpaceSize ) + " %";
       
   242             // (7) is a spacer
       
   243             iLV_Summary.Items[ 8 ].SubItems[ 1 ].Text = heapCellHeaderOverheadSize.ToString();
       
   244             iLV_Summary.Items[ 8 ].SubItems[ 2 ].Text = NumberFormattingUtils.NumberAsPercentageTwoDP( heapCellHeaderOverheadSize, symbolicMatchMemorySize + otherMemorySize + freeSpaceSize ) + " %";
       
   245             
       
   246             // 3RD GROUP
       
   247             iLV_Summary.Items[ 10 ].SubItems[ 1 ].Text = ( heapCellHeaderOverheadSize + symbolicMatchMemorySize + otherMemorySize ).ToString();
       
   248             iLV_Summary.Items[ 10 ].SubItems[ 2 ].Text = "100.00 %";
       
   249         }
       
   250 
       
   251         private void UpdateSelectionTotals()
       
   252         {
       
   253             bool atLeastOneValue = false;
       
   254             long total = 0;
       
   255 
       
   256             foreach ( XPTable.Models.Row row in iTable_SymbolMemory.SelectedItems )
       
   257             {
       
   258                 TrackingInfo item = (TrackingInfo) row.Tag;
       
   259                 //
       
   260                 total += item.AssociatedMemory;
       
   261                 atLeastOneValue = true;
       
   262             }
       
   263 
       
   264             string totalValueAsString = "[nothing selected]";
       
   265             string totalAsPercentageString = "";
       
   266             //
       
   267             if ( atLeastOneValue )
       
   268             {
       
   269                 int allocCount;
       
   270                 int freeCount;
       
   271                 long freeSpaceSize;
       
   272                 long allocatedUnknownSize;
       
   273                 long allocatedSymbolMatchSize;
       
   274                 long totalHeapAllocatedMemory = TotalAllocatedMemory( out allocCount, out freeCount, out freeSpaceSize, out allocatedUnknownSize, out allocatedSymbolMatchSize );
       
   275                 //
       
   276                 totalValueAsString = total.ToString();
       
   277                 totalAsPercentageString = NumberFormattingUtils.NumberAsPercentageTwoDP( total, totalHeapAllocatedMemory ) + " %";
       
   278             }
       
   279             //
       
   280             iLV_Summary.Items[ 9 ].SubItems[ 1 ].Text = totalValueAsString;
       
   281             iLV_Summary.Items[ 9 ].SubItems[ 2 ].Text = totalAsPercentageString;
       
   282         }
       
   283 
       
   284         private long TotalAllocatedMemory( out int aAllocCount, out int aFreeCount, out long aFreeSpaceSize, out long aAllocatedUnknownSize, out long aAllocatedSymbolMatchSize )
       
   285         {
       
   286             aAllocCount = 0;
       
   287             aFreeCount = 0;
       
   288             aFreeSpaceSize = 0;
       
   289             aAllocatedUnknownSize = 0;
       
   290             aAllocatedSymbolMatchSize = 0;
       
   291             //
       
   292             HeapCellArray data = iReconstructor.Data;
       
   293             int count = data.Count;
       
   294             //
       
   295             for ( int i = 0; i < count; i++ )
       
   296             {
       
   297                 HeapCell cell = data[ i ];
       
   298                 //
       
   299                 if ( cell.Type == HeapCell.TType.EAllocated )
       
   300                 {
       
   301                     ++aAllocCount;
       
   302                     if ( cell.Symbol != null )
       
   303                         aAllocatedSymbolMatchSize += cell.Length;
       
   304                     else
       
   305                         aAllocatedUnknownSize += cell.Length;
       
   306                 }
       
   307                 else
       
   308                 {
       
   309                     ++aFreeCount;
       
   310                     aFreeSpaceSize += cell.Length;
       
   311                 }
       
   312             }
       
   313 
       
   314             return ( aAllocatedSymbolMatchSize + aAllocatedUnknownSize + aFreeSpaceSize );
       
   315         }
       
   316         #endregion
       
   317 
       
   318         #region Data members
       
   319         private HeapReconstructor iReconstructor = null;
       
   320         #endregion
       
   321     }
       
   322 }