Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HeapSnapshotExplorer.cs « heap-snapshot-explorer « Mono.Profiler - github.com/mono/mono-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 391cfb77948d38ae18fdbc8574ef382f033f9711 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// HeapSnapshotExplorer.cs created with MonoDevelop
// User: massi at 11:38 AM 4/17/2008
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using Gtk;

namespace Mono.Profiler
{
	public partial class HeapSnapshotExplorer : Gtk.Bin
	{
		HeapExplorerTreeModel model;
		public HeapExplorerTreeModel Model {
			get {
				return model;
			}
			set {
				model = value;
				Tree.Model = model.Model;
			}
		}		
		
		Menu LoadBlock;
		Menu FilterSet;
		Menu CompareSet;
		
		HeapExplorerTreeModel.Node currentSelection;
		public HeapExplorerTreeModel.Node CurrentSelection {
			get {
				return currentSelection;
			}
		}
		
		[Gtk.TreeNode (ListOnly=true)]
		public class ClassStatisticsNode : Gtk.TreeNode {
			HeapObjectSet.HeapObjectSetClassStatistics classStatistics;
			public HeapObjectSet.HeapObjectSetClassStatistics ClassStatistics {
				get {
					return classStatistics;
				}
			}
			
			public string Name {
				get {
					return classStatistics.Class.Name;
				}
			}
			public uint AllocatedBytes {
				get {
					return classStatistics.AllocatedBytes;
				}
			}
			
			public ClassStatisticsNode (HeapObjectSet.HeapObjectSetClassStatistics classStatistics) {
				this.classStatistics = classStatistics;
			}
		}
		
		public static void PrepareTreeViewForClassStatistics (NodeView view) {
			view.AppendColumn ("Name", new Gtk.CellRendererText (), delegate (TreeViewColumn column, CellRenderer cell, ITreeNode node) {
				ClassStatisticsNode classNode = (ClassStatisticsNode) node;
				((CellRendererText) cell).Markup = classNode.Name;
			});
			view.AppendColumn ("Allocated bytes", new Gtk.CellRendererText (), delegate (TreeViewColumn column, CellRenderer cell, ITreeNode node) {
				ClassStatisticsNode classNode = (ClassStatisticsNode) node;
				((CellRendererText) cell).Markup = classNode.AllocatedBytes.ToString ();
			});
			view.NodeStore = new Gtk.NodeStore (typeof (ClassStatisticsNode));
		}
		
		public static void FillTreeViewWithClassStatistics (NodeView view, HeapObjectSet.HeapObjectSetClassStatistics[] classes) {
			view.NodeStore.Clear ();
			foreach (HeapObjectSet.HeapObjectSetClassStatistics c in classes) {
				view.NodeStore.AddNode (new ClassStatisticsNode (c));
			}
		}
		
		HeapExplorerTreeModel.Node NodeSelectedForComparison;
		
		public HeapSnapshotExplorer()
		{
			this.Build();
			
			LoadBlock = new Menu ();
			MenuItem loadData = new MenuItem ("Load block data");
			loadData.Activated += delegate {
				OnLoadData ();
			};
			LoadBlock.Append (loadData);
			
			FilterSet = new Menu ();
			MenuItem filterByClass = new MenuItem ("Filter by object class");
			filterByClass.Activated += delegate {
				OnFilterByClass ();
			};
			FilterSet.Append (filterByClass);
			MenuItem filterByReferencesObjectOfClass = new MenuItem ("Filter by \"references object of class\"");
			filterByReferencesObjectOfClass.Activated += delegate {
				OnFilterByReferencesObjectOfClass ();
			};
			FilterSet.Append (filterByReferencesObjectOfClass);
			MenuItem filterByIsReferencedByObjectOfClass = new MenuItem ("Filter by \"is referenced by object of class\"");
			filterByIsReferencedByObjectOfClass.Activated += delegate {
				OnFilterByIsReferencedByObjectOfClass ();
			};
			FilterSet.Append (filterByIsReferencedByObjectOfClass);
			MenuItem markSetForComparison = new MenuItem ("Mark set for comparison");
			markSetForComparison.Activated += delegate {
				OnMarkSetForComparison ();
			};
			FilterSet.Append (markSetForComparison);
			
			CompareSet = new Menu ();
			MenuItem performComparison = new MenuItem ("Perform comparison with this set");
			performComparison.Activated += delegate {
				OnPerformComparison ();
			};
			CompareSet.Append (performComparison);
			MenuItem clearSetForComparison = new MenuItem ("Clear selection for comparison");
			clearSetForComparison.Activated += delegate {
				OnClearSetForComparison ();
			};
			CompareSet.Append (clearSetForComparison);
			
			PrepareTreeViewForClassStatistics (PerClassStatistics);
			
			Tree.Selection.Changed += delegate (object o, EventArgs args) {
				TreeSelection selection = (TreeSelection) o;
				TreeIter iter;
				if (selection.GetSelected (out iter)) {
					currentSelection = (HeapExplorerTreeModel.Node) Tree.Model.GetValue (iter, 0);
					if (currentSelection != null) {
						if (currentSelection.Objects != null) {
							FillTreeViewWithClassStatistics (PerClassStatistics, currentSelection.Objects.ClassStatistics);
						} else {
							PerClassStatistics.NodeStore.Clear ();
						}
					}
				}
			};
			
			Gtk.TreeViewColumn setColumn = new Gtk.TreeViewColumn ();
			Gtk.TreeViewColumn countColumn = new Gtk.TreeViewColumn ();
			Gtk.TreeViewColumn bytesColumn = new Gtk.TreeViewColumn ();
			setColumn.Title = "Object set";
			countColumn.Title = "Object count";
			bytesColumn.Title = "Bytes";
			Gtk.CellRendererText setCell = new Gtk.CellRendererText ();
			Gtk.CellRendererText countCell = new Gtk.CellRendererText ();
			Gtk.CellRendererText bytesCell = new Gtk.CellRendererText ();
			setColumn.PackStart (setCell, true);
			countColumn.PackStart (countCell, true);
			bytesColumn.PackStart (bytesCell, true);
			
			setColumn.SetCellDataFunc (setCell, delegate (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
				HeapExplorerTreeModel.Node node = (HeapExplorerTreeModel.Node) model.GetValue (iter, 0);
				CellRendererText textCell = (CellRendererText) cell;
				textCell.Markup = node.Description;
				if (node != NodeSelectedForComparison) {
					textCell.Style = Pango.Style.Normal;
				} else {
					textCell.Style = Pango.Style.Italic;
				}
			});
			countColumn.SetCellDataFunc (countCell, delegate (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
				HeapExplorerTreeModel.Node node = (HeapExplorerTreeModel.Node) model.GetValue (iter, 0);
				CellRendererText textCell = (CellRendererText) cell;
				textCell.Markup = node.Count;
				if (node != NodeSelectedForComparison) {
					textCell.Style = Pango.Style.Normal;
				} else {
					textCell.Style = Pango.Style.Italic;
				}
			});
			bytesColumn.SetCellDataFunc (bytesCell, delegate (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter) {
				HeapExplorerTreeModel.Node node = (HeapExplorerTreeModel.Node) model.GetValue (iter, 0);
				CellRendererText textCell = (CellRendererText) cell;
				textCell.Markup = node.AllocatedBytes;
				if (node != NodeSelectedForComparison) {
					textCell.Style = Pango.Style.Normal;
				} else {
					textCell.Style = Pango.Style.Italic;
				}
			});
			
			setColumn.AddAttribute (setCell, "text", 0);
			countColumn.AddAttribute (countCell, "text", 1);
			bytesColumn.AddAttribute (bytesCell, "text", 2);
			
			Tree.AppendColumn (setColumn);
			Tree.AppendColumn (countColumn);
			Tree.AppendColumn (bytesColumn);
			
			LoadBlock.ShowAll ();
			FilterSet.ShowAll ();
			CompareSet.ShowAll ();
			
			NodeSelectedForComparison = null;
		}
		
		public void OnLoadData () {
			if (CurrentSelection != null) {
				HeapExplorerTreeModel.SnapshotNode snapshotNode = CurrentSelection as HeapExplorerTreeModel.SnapshotNode;
				if ((snapshotNode != null) && (snapshotNode.Objects == null)) {
					((HeapExplorerTreeModel.SnapshotNode)CurrentSelection).ReadSnapshot ();
				}
			}
		}
		
		public void OnFilterByClass () {
			if (CurrentSelection != null) {
				LoadedClass c = LoadedClassChooser.ChooseClass (CurrentSelection.Objects.ClassStatistics);
				if (c != null) {
					IHeapObjectFilter filter = new HeapObjectIsOfClass (c);
					CurrentSelection.Filter (filter);
				}
			}
		}
		
		public void OnFilterByReferencesObjectOfClass () {
			if (CurrentSelection != null) {
				LoadedClass c = LoadedClassChooser.ChooseClass (CurrentSelection.Root.Objects.ClassStatistics);
				if (c != null) {
					IHeapObjectFilter filter = new HeapObjectReferencesObjectOfClass (c);
					CurrentSelection.Filter (filter);
				}
			}
		}
		
		public void OnFilterByIsReferencedByObjectOfClass () {
			if (CurrentSelection != null) {
				LoadedClass c = LoadedClassChooser.ChooseClass (CurrentSelection.Root.Objects.ClassStatistics);
				if (c != null) {
					IHeapObjectFilter filter = new HeapObjectIsReferencedByObjectOfClass (c);
					CurrentSelection.Filter (filter);
				}
			}
		}
		
		public void OnMarkSetForComparison () {
			if (CurrentSelection != null) {
				NodeSelectedForComparison = CurrentSelection;
			}
		}
		
		public void OnClearSetForComparison () {
			NodeSelectedForComparison = null;
		}
		
		public void OnPerformComparison () {
			if (CurrentSelection != null) {
				HeapExplorerTreeModel.SubSetNode firstSubNode;
				HeapExplorerTreeModel.SubSetNode secondSubNode;
				HeapExplorerTreeModel.Node.PerformComparison (NodeSelectedForComparison, CurrentSelection, out firstSubNode, out secondSubNode);
				NodeSelectedForComparison = null;
			}
		}
		
		
		
		[GLib.ConnectBefore]
		protected virtual void OnTreeButtonPress (object o, Gtk.ButtonPressEventArgs args)
		{
			if (args.Event.Button == 3) {
				if (CurrentSelection != null) {
					HeapExplorerTreeModel.SnapshotNode snapshotNode = CurrentSelection as HeapExplorerTreeModel.SnapshotNode;
					if ((snapshotNode != null) && (snapshotNode.Objects == null)) {
						LoadBlock.Popup ();
					} else {
						if (NodeSelectedForComparison == null) {
							FilterSet.Popup ();
						} else if (CurrentSelection != NodeSelectedForComparison) {
							CompareSet.Popup ();
						}
					}
				}
			}
		}
	}
}