|
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.Collections; |
|
40 using System.ComponentModel; |
|
41 using System.Drawing; |
|
42 using System.Data; |
|
43 using System.Windows.Forms; |
|
44 using System.Text; |
|
45 using HeapLib; |
|
46 using HeapLib.Cells; |
|
47 using HeapLib.Array; |
|
48 using HeapLib.Statistics; |
|
49 using HeapLib.Statistics.Distribution; |
|
50 using SymbianUtils; |
|
51 using XPTable.Models; |
|
52 |
|
53 namespace HeapUiLib.Controls |
|
54 { |
|
55 public partial class HeapCellSizeDistributionControl : UserControl |
|
56 { |
|
57 #region Constructors & destructor |
|
58 public HeapCellSizeDistributionControl() |
|
59 { |
|
60 InitializeComponent(); |
|
61 } |
|
62 #endregion |
|
63 |
|
64 #region API |
|
65 #endregion |
|
66 |
|
67 #region Properties |
|
68 [Browsable(false)] |
|
69 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] |
|
70 public HeapStatistics Statistics |
|
71 { |
|
72 get { return iStats; } |
|
73 set |
|
74 { |
|
75 iStats = value; |
|
76 iRB_CheckedChanged( this, EventArgs.Empty ); |
|
77 } |
|
78 } |
|
79 #endregion |
|
80 |
|
81 #region Internal properties |
|
82 private HeapCellSizeDistribution Distribution |
|
83 { |
|
84 get { return iDistribution; } |
|
85 set |
|
86 { |
|
87 iDistribution = value; |
|
88 UpdateTable(); |
|
89 } |
|
90 } |
|
91 |
|
92 private long TotalHeapSize |
|
93 { |
|
94 get |
|
95 { |
|
96 long ret = 1; |
|
97 // |
|
98 if ( Statistics != null ) |
|
99 { |
|
100 ret = Statistics.SizeTotalHeader + Statistics.SizeTotalPayload; |
|
101 } |
|
102 // |
|
103 return ret; |
|
104 } |
|
105 } |
|
106 #endregion |
|
107 |
|
108 #region Internal methods |
|
109 private void UpdateTable() |
|
110 { |
|
111 iTable.BeginUpdate(); |
|
112 iTableModel.Rows.Clear(); |
|
113 // |
|
114 long total = iDistribution.Total; |
|
115 foreach ( DictionaryEntry entry in iDistribution ) |
|
116 { |
|
117 uint size = (uint) ( entry.Key ); |
|
118 uint instanceCount = (uint) entry.Value; |
|
119 uint totalForSize = size * instanceCount; |
|
120 double percentageDistribution = ( (double) ( totalForSize ) / ( (double) total ) ) * 100.0; |
|
121 double percentageHeap = ( (double) ( totalForSize ) / ( (double) TotalHeapSize ) ) * 100.0; |
|
122 string percentageTextDistribution = NumberFormattingUtils.NumberAsPercentageTwoDP( totalForSize, total ) + " %"; |
|
123 string percentageTextHeap = NumberFormattingUtils.NumberAsPercentageTwoDP( totalForSize, TotalHeapSize ) + " %"; |
|
124 // |
|
125 XPTable.Models.Row row = new XPTable.Models.Row(); |
|
126 // |
|
127 XPTable.Models.Cell cellSize = new XPTable.Models.Cell( size.ToString() + " bytes" ); |
|
128 cellSize.Tag = size; |
|
129 XPTable.Models.Cell cellInstanceCount = new XPTable.Models.Cell( instanceCount.ToString() ); |
|
130 cellInstanceCount.Data = instanceCount; |
|
131 XPTable.Models.Cell cellTotalForSize = new XPTable.Models.Cell( totalForSize.ToString() ); |
|
132 cellTotalForSize.Data = totalForSize; |
|
133 XPTable.Models.Cell cellPercentage = new XPTable.Models.Cell( percentageTextDistribution ); |
|
134 cellPercentage.Tag = percentageDistribution; |
|
135 XPTable.Models.Cell cellPercentageOfHeap = new XPTable.Models.Cell( percentageTextHeap ); |
|
136 cellPercentageOfHeap.Tag = percentageHeap; |
|
137 // |
|
138 row.Cells.Add( cellSize ); |
|
139 row.Cells.Add( cellInstanceCount ); |
|
140 row.Cells.Add( cellTotalForSize ); |
|
141 row.Cells.Add( cellPercentage ); |
|
142 row.Cells.Add( cellPercentageOfHeap ); |
|
143 // |
|
144 iTableModel.Rows.Add( row ); |
|
145 } |
|
146 // |
|
147 iTable.EndUpdate(); |
|
148 } |
|
149 #endregion |
|
150 |
|
151 #region Event handlers |
|
152 private void iTable_PrepareForSort( object sender, XPTable.Events.SortEventArgs e ) |
|
153 { |
|
154 if ( e.Column == iCol_Count || e.Column == iCol_Total ) |
|
155 { |
|
156 e.Comparer = new XPTable.Sorting.NumberComparer( iTable.TableModel, e.Index, e.Column.SortOrder ); |
|
157 } |
|
158 else if ( e.Column == iCol_Size ) |
|
159 { |
|
160 e.Comparer = new HeapCellDistributionCustomComparerUint( iTable.TableModel, e.Index, e.Column.SortOrder ); |
|
161 } |
|
162 else |
|
163 { |
|
164 e.Comparer = new HeapCellDistributionCustomComparerDouble( iTable.TableModel, e.Index, e.Column.SortOrder ); |
|
165 } |
|
166 } |
|
167 |
|
168 private void iRB_CheckedChanged( object sender, EventArgs e ) |
|
169 { |
|
170 if ( iRB_Allocated.Checked ) |
|
171 { |
|
172 Distribution = Statistics.StatsAllocated.Distribution; |
|
173 } |
|
174 else if ( iRB_Free.Checked ) |
|
175 { |
|
176 Distribution = Statistics.StatsFree.Distribution; |
|
177 } |
|
178 } |
|
179 #endregion |
|
180 |
|
181 #region Data members |
|
182 private HeapStatistics iStats = null; |
|
183 private HeapCellSizeDistribution iDistribution = new HeapCellSizeDistribution(); |
|
184 #endregion |
|
185 } |
|
186 |
|
187 #region Internal classes |
|
188 internal class HeapCellDistributionCustomComparerUint : XPTable.Sorting.ComparerBase |
|
189 { |
|
190 #region Constructors & destructor |
|
191 public HeapCellDistributionCustomComparerUint( TableModel aModel, int aColumn, SortOrder aSortOrder ) |
|
192 : base( aModel, aColumn, aSortOrder ) |
|
193 { |
|
194 } |
|
195 #endregion |
|
196 |
|
197 #region From ComparerBase |
|
198 protected override int CompareCells( XPTable.Models.Cell cell1, XPTable.Models.Cell cell2 ) |
|
199 { |
|
200 // check for null cells |
|
201 if ( cell1 == null && cell2 == null ) |
|
202 { |
|
203 return 0; |
|
204 } |
|
205 else if ( cell1 == null ) |
|
206 { |
|
207 return -1; |
|
208 } |
|
209 else if ( cell2 == null ) |
|
210 { |
|
211 return 1; |
|
212 } |
|
213 |
|
214 if ( cell1.Tag == null && cell2.Tag == null ) |
|
215 { |
|
216 return 0; |
|
217 } |
|
218 else if ( cell1.Tag == null ) |
|
219 { |
|
220 return -1; |
|
221 } |
|
222 else if ( cell2.Tag == null ) |
|
223 { |
|
224 return 1; |
|
225 } |
|
226 |
|
227 uint cell1Val = (uint) cell1.Tag; |
|
228 uint cell2Val = (uint) cell2.Tag; |
|
229 |
|
230 return cell1Val.CompareTo( cell2Val ); |
|
231 } |
|
232 #endregion |
|
233 } |
|
234 |
|
235 internal class HeapCellDistributionCustomComparerDouble : XPTable.Sorting.ComparerBase |
|
236 { |
|
237 #region Constructors & destructor |
|
238 public HeapCellDistributionCustomComparerDouble( TableModel aModel, int aColumn, SortOrder aSortOrder ) |
|
239 : base( aModel, aColumn, aSortOrder ) |
|
240 { |
|
241 } |
|
242 #endregion |
|
243 |
|
244 #region From ComparerBase |
|
245 protected override int CompareCells( XPTable.Models.Cell cell1, XPTable.Models.Cell cell2 ) |
|
246 { |
|
247 // check for null cells |
|
248 if ( cell1 == null && cell2 == null ) |
|
249 { |
|
250 return 0; |
|
251 } |
|
252 else if ( cell1 == null ) |
|
253 { |
|
254 return -1; |
|
255 } |
|
256 else if ( cell2 == null ) |
|
257 { |
|
258 return 1; |
|
259 } |
|
260 |
|
261 if ( cell1.Tag == null && cell2.Tag == null ) |
|
262 { |
|
263 return 0; |
|
264 } |
|
265 else if ( cell1.Tag == null ) |
|
266 { |
|
267 return -1; |
|
268 } |
|
269 else if ( cell2.Tag == null ) |
|
270 { |
|
271 return 1; |
|
272 } |
|
273 |
|
274 double cell1Val = (double) cell1.Tag; |
|
275 double cell2Val = (double) cell2.Tag; |
|
276 |
|
277 return cell1Val.CompareTo( cell2Val ); |
|
278 } |
|
279 #endregion |
|
280 } |
|
281 #endregion |
|
282 } |