Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 86d136fce9582d6551c4f3f1b0f35a3688b82015 (plain) (blame)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/***********************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Actuate Corporation - initial API and implementation
 * Elliott Baron <ebaron@redhat.com> - Modified implementation
 ***********************************************************************/
package org.eclipse.linuxtools.internal.valgrind.massif.birt;

import org.eclipse.birt.chart.model.attribute.ActionType;
import org.eclipse.birt.chart.model.attribute.AxisType;
import org.eclipse.birt.chart.model.attribute.ChartDimension;
import org.eclipse.birt.chart.model.attribute.FontDefinition;
import org.eclipse.birt.chart.model.attribute.IntersectionType;
import org.eclipse.birt.chart.model.attribute.Marker;
import org.eclipse.birt.chart.model.attribute.MarkerType;
import org.eclipse.birt.chart.model.attribute.Orientation;
import org.eclipse.birt.chart.model.attribute.Position;
import org.eclipse.birt.chart.model.attribute.TickStyle;
import org.eclipse.birt.chart.model.attribute.TriggerCondition;
import org.eclipse.birt.chart.model.attribute.impl.CallBackValueImpl;
import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl;
import org.eclipse.birt.chart.model.component.Axis;
import org.eclipse.birt.chart.model.component.Series;
import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
import org.eclipse.birt.chart.model.data.BaseSampleData;
import org.eclipse.birt.chart.model.data.DataFactory;
import org.eclipse.birt.chart.model.data.NumberDataSet;
import org.eclipse.birt.chart.model.data.OrthogonalSampleData;
import org.eclipse.birt.chart.model.data.SampleData;
import org.eclipse.birt.chart.model.data.SeriesDefinition;
import org.eclipse.birt.chart.model.data.Trigger;
import org.eclipse.birt.chart.model.data.impl.ActionImpl;
import org.eclipse.birt.chart.model.data.impl.NumberDataSetImpl;
import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
import org.eclipse.birt.chart.model.data.impl.TriggerImpl;
import org.eclipse.birt.chart.model.impl.ChartWithAxesImpl;
import org.eclipse.birt.chart.model.layout.Legend;
import org.eclipse.birt.chart.model.layout.Plot;
import org.eclipse.birt.chart.model.type.LineSeries;
import org.eclipse.birt.chart.model.type.impl.LineSeriesImpl;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.linuxtools.internal.valgrind.massif.MassifSnapshot;
import org.eclipse.linuxtools.internal.valgrind.massif.MassifSnapshot.TimeUnit;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;

public class HeapChart extends ChartWithAxesImpl {

	private static String[] byteUnits = { Messages.getString("HeapChart.B"), //$NON-NLS-1$
			Messages.getString("HeapChart.KiB"), //$NON-NLS-1$
			Messages.getString("HeapChart.MiB"), //$NON-NLS-1$
			Messages.getString("HeapChart.GiB"), //$NON-NLS-1$
			Messages.getString("HeapChart.TiB") //$NON-NLS-1$
	};
	private static String[] instrUnits = { Messages.getString("HeapChart.i"), //$NON-NLS-1$
			Messages.getString("HeapChart.Ki"), //$NON-NLS-1$
			Messages.getString("HeapChart.Mi"), //$NON-NLS-1$
			Messages.getString("HeapChart.Gi"), //$NON-NLS-1$
			Messages.getString("HeapChart.Ti") //$NON-NLS-1$
	};
	private static String[] secondUnits = { Messages.getString("HeapChart.ms"), //$NON-NLS-1$
			Messages.getString("HeapChart.s") //$NON-NLS-1$
	};

	protected static final int BYTE_MULT = 1024;
	protected static final int BYTE_LIMIT = byteUnits.length - 1;
	protected static final int INSTR_MULT = 1000;
	protected static final int INSTR_LIMIT = instrUnits.length - 1;
	protected static final int MS_MULT = 1000;
	protected static final int MS_LIMIT = secondUnits.length - 1;

	protected static final int SCALING_THRESHOLD = 20;

	protected String xUnits;
	protected String yUnits;

	public HeapChart(MassifSnapshot[] snapshots) {
		TimeUnit timeUnit = snapshots[0].getUnit();
		long xScaling = getXScaling(snapshots, timeUnit);
		long yScaling = getYScaling(snapshots);

		double[] time = new double[snapshots.length];
		double[] dataUseful = new double[snapshots.length];
		double[] dataExtra = new double[snapshots.length];
		double[] dataStacks = null;

		boolean isStack = isStackProfiled(snapshots);
		if (isStack) {
			dataStacks = new double[snapshots.length];
		}
		double[] dataTotal = new double[snapshots.length];
		for (int i = 0; i < snapshots.length; i++) {
			time[i] = snapshots[i].getTime() / xScaling;
			dataUseful[i] = snapshots[i].getHeapBytes() / yScaling;
			dataExtra[i] = snapshots[i].getHeapExtra() / yScaling;
			dataTotal[i] = dataUseful[i] + dataExtra[i];
			if (isStack) {
				dataStacks[i] = snapshots[i].getStacks() / yScaling;
			}
		}

		initialize();
		setDimension(ChartDimension.TWO_DIMENSIONAL_LITERAL);
		setType("Line Chart"); //$NON-NLS-1$
		setSubType("Overlay"); //$NON-NLS-1$

		Font font = JFaceResources.getDialogFont();
		FontData fd = font.getFontData()[0];

		// Title
		FontDefinition titleFont = getTitle().getLabel().getCaption().getFont();
		titleFont.setName(fd.getName());
		titleFont.setSize(fd.getHeight() + 2);

		// Plot
		getBlock().setBackground(ColorDefinitionImpl.WHITE());
		Plot p = getPlot();
		p.getClientArea().setBackground(
				ColorDefinitionImpl.create(255, 255, 225));

		// X-Axis
		Axis xAxisPrimary = getPrimaryBaseAxes()[0];
		xAxisPrimary.setType(AxisType.LINEAR_LITERAL);
		xAxisPrimary.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
		xAxisPrimary.getOrigin().setType(IntersectionType.VALUE_LITERAL);
		xAxisPrimary.getTitle().getCaption().setValue(xUnits);
		xAxisPrimary.getTitle().setVisible(true);

		FontDefinition xAxisFont = xAxisPrimary.getTitle().getCaption()
				.getFont();
		xAxisFont.setName(fd.getName());
		xAxisFont.setSize(fd.getHeight());

		xAxisFont = xAxisPrimary.getLabel().getCaption().getFont();
		xAxisFont.setName(fd.getName());
		xAxisFont.setSize(fd.getHeight());

		// Y-Axis
		Axis yAxisPrimary = getPrimaryOrthogonalAxis(xAxisPrimary);
		yAxisPrimary.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
		yAxisPrimary.getMajorGrid().getLineAttributes().setVisible(true);
		yAxisPrimary.getTitle().getCaption().setValue(yUnits);
		yAxisPrimary.getTitle().setVisible(true);

		FontDefinition yAxisFont = yAxisPrimary.getTitle().getCaption()
				.getFont();
		yAxisFont.setName(fd.getName());
		yAxisFont.setSize(fd.getHeight());

		yAxisFont = yAxisPrimary.getLabel().getCaption().getFont();
		yAxisFont.setName(fd.getName());
		yAxisFont.setSize(fd.getHeight());

		// // Z-Axis
		// Axis zAxis = AxisImpl.create(Axis.ANCILLARY_BASE);
		// zAxis.setType(AxisType.LINEAR_LITERAL);
		// zAxis.setLabelPosition(Position.BELOW_LITERAL);
		// zAxis.setTitlePosition(Position.BELOW_LITERAL);
		// zAxis.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
		// zAxis.setOrientation(Orientation.HORIZONTAL_LITERAL);
		// xAxisPrimary.getAncillaryAxes().add(zAxis);

		// Legend
		Legend legend = getLegend();
		legend.setPosition(Position.BELOW_LITERAL);
		legend.setOrientation(Orientation.HORIZONTAL_LITERAL);

		FontDefinition legendFont = legend.getText().getFont();
		legendFont.setName(fd.getName());
		legendFont.setSize(fd.getHeight());

		// Data Set
		NumberDataSet mainValues = NumberDataSetImpl.create(time);
		NumberDataSet orthoValues1 = NumberDataSetImpl.create(dataUseful);
		NumberDataSet orthoValues2 = NumberDataSetImpl.create(dataExtra);
		NumberDataSet orthoValuesS = null;
		if (isStack) {
			orthoValuesS = NumberDataSetImpl.create(dataStacks);
		}
		NumberDataSet orthoValues3 = NumberDataSetImpl.create(dataTotal);

		SampleData sd = DataFactory.eINSTANCE.createSampleData();
		BaseSampleData sdBase = DataFactory.eINSTANCE.createBaseSampleData();
		sdBase.setDataSetRepresentation("");//$NON-NLS-1$
		sd.getBaseSampleData().add(sdBase);

		OrthogonalSampleData sdOrthogonal1 = DataFactory.eINSTANCE
				.createOrthogonalSampleData();
		sdOrthogonal1.setDataSetRepresentation("");//$NON-NLS-1$
		sdOrthogonal1.setSeriesDefinitionIndex(0);
		sd.getOrthogonalSampleData().add(sdOrthogonal1);

		OrthogonalSampleData sdOrthogonal2 = DataFactory.eINSTANCE
				.createOrthogonalSampleData();
		sdOrthogonal2.setDataSetRepresentation("");//$NON-NLS-1$
		sdOrthogonal2.setSeriesDefinitionIndex(1);
		sd.getOrthogonalSampleData().add(sdOrthogonal2);

		if (isStack) {
			OrthogonalSampleData sdOrthogonalS = DataFactory.eINSTANCE
					.createOrthogonalSampleData();
			sdOrthogonalS.setDataSetRepresentation("");//$NON-NLS-1$
			sdOrthogonalS.setSeriesDefinitionIndex(2);
			sd.getOrthogonalSampleData().add(sdOrthogonalS);
		}

		OrthogonalSampleData sdOrthogonal3 = DataFactory.eINSTANCE
				.createOrthogonalSampleData();
		sdOrthogonal3.setDataSetRepresentation("");//$NON-NLS-1$
		sdOrthogonal3.setSeriesDefinitionIndex(isStack ? 3 : 2);
		sd.getOrthogonalSampleData().add(sdOrthogonal3);

		setSampleData(sd);

		// X-Series
		Series seCategory = SeriesImpl.create();
		seCategory.setDataSet(mainValues);
		SeriesDefinition sdX = SeriesDefinitionImpl.create();
		xAxisPrimary.getSeriesDefinitions().add(sdX);
		sdX.getSeries().add(seCategory);

		// Y-Series
		LineSeries ls1 = (LineSeries) LineSeriesImpl.create();
		ls1.setDataSet(orthoValues1);
		ls1.getLineAttributes().setColor(ColorDefinitionImpl.CREAM());
		for (int i = 0; i < ls1.getMarkers().size(); i++) {
			Marker marker = (Marker) ls1.getMarkers().get(i);
			marker.setType(MarkerType.DIAMOND_LITERAL);
		}

		ls1.setPaletteLineColor(true);
		ls1.setSeriesIdentifier(Messages.getString("HeapChart.Useful_Heap")); //$NON-NLS-1$	
		ls1.getTriggers().add(getClickTrigger(ls1));
		ls1.getTriggers().add(getDblClickTrigger(ls1));

		// Y-Series
		LineSeries ls2 = (LineSeries) LineSeriesImpl.create();
		ls2.setDataSet(orthoValues2);
		ls2.getLineAttributes().setColor(ColorDefinitionImpl.CREAM());
		for (int i = 0; i < ls2.getMarkers().size(); i++) {
			Marker marker = (Marker) ls2.getMarkers().get(i);
			marker.setType(MarkerType.DIAMOND_LITERAL);
		}
		ls2.setPaletteLineColor(true);
		ls2.setSeriesIdentifier(Messages.getString("HeapChart.Extra_Heap")); //$NON-NLS-1$
		ls2.getTriggers().add(getClickTrigger(ls2));
		ls2.getTriggers().add(getDblClickTrigger(ls2));

		// Y-Series
		LineSeries lsS = null;
		if (isStack) {
			lsS = (LineSeries) LineSeriesImpl.create();
			lsS.setDataSet(orthoValuesS);
			lsS.getLineAttributes().setColor(ColorDefinitionImpl.CREAM());
			for (int i = 0; i < lsS.getMarkers().size(); i++) {
				Marker marker = (Marker) lsS.getMarkers().get(i);
				marker.setType(MarkerType.DIAMOND_LITERAL);
			}
			lsS.setPaletteLineColor(true);
			lsS.setSeriesIdentifier(Messages.getString("HeapChart.Stacks")); //$NON-NLS-1$
			lsS.getTriggers().add(getClickTrigger(lsS));
			lsS.getTriggers().add(getDblClickTrigger(lsS));
		}

		// Y-Series
		LineSeries ls3 = (LineSeries) LineSeriesImpl.create();
		ls3.setDataSet(orthoValues3);
		ls3.getLineAttributes().setColor(ColorDefinitionImpl.CREAM());
		for (int i = 0; i < ls3.getMarkers().size(); i++) {
			Marker marker = (Marker) ls3.getMarkers().get(i);
			marker.setType(MarkerType.DIAMOND_LITERAL);
		}
		ls3.setPaletteLineColor(true);
		ls3.setSeriesIdentifier(Messages.getString("HeapChart.Total_Heap")); //$NON-NLS-1$
		ls3.getTriggers().add(getClickTrigger(ls3));
		ls3.getTriggers().add(getDblClickTrigger(ls3));

		SeriesDefinition sdY = SeriesDefinitionImpl.create();
		sdY.getSeriesPalette().shift(-1);
		yAxisPrimary.getSeriesDefinitions().add(sdY);
		sdY.getSeries().add(ls1);
		sdY.getSeries().add(ls2);
		if (isStack) {
			sdY.getSeries().add(lsS);
		}
		sdY.getSeries().add(ls3);

		// // Z-Series
		// SeriesDefinition sdZ = SeriesDefinitionImpl.create();
		// zAxis.getSeriesDefinitions().add(sdZ);
	}

	private Trigger getClickTrigger(LineSeries ls) {
		return TriggerImpl.create(TriggerCondition.ONCLICK_LITERAL, ActionImpl
				.create(ActionType.CALL_BACK_LITERAL, CallBackValueImpl
						.create(String.valueOf(ls
								.getSeriesIdentifier()))));
	}
	
	private Trigger getDblClickTrigger(LineSeries ls) {
		return TriggerImpl.create(TriggerCondition.ONDBLCLICK_LITERAL, ActionImpl
				.create(ActionType.CALL_BACK_LITERAL, CallBackValueImpl
						.create(String.valueOf(ls
								.getSeriesIdentifier()))));
	}

	private boolean isStackProfiled(MassifSnapshot[] snapshots) {
		return getMaxStack(snapshots) > 0;
	}

	private long getYScaling(MassifSnapshot[] snapshots) {
		long max = getMaxValue(snapshots);

		int count = 0;
		while (max > BYTE_MULT * SCALING_THRESHOLD && count < BYTE_LIMIT) {
			max /= BYTE_MULT;
			count++;
		}

		yUnits = byteUnits[count];

		return (long) Math.pow(BYTE_MULT, count);
	}

	private long getXScaling(MassifSnapshot[] snapshots, TimeUnit unit) {
		long max = snapshots[snapshots.length - 1].getTime();
		int mult, limit;
		String[] units;
		switch (unit) {
		case BYTES:
			mult = BYTE_MULT;
			limit = BYTE_LIMIT;
			units = byteUnits;
			break;
		case INSTRUCTIONS:
			mult = INSTR_MULT;
			limit = INSTR_LIMIT;
			units = instrUnits;
			break;
		default:
			mult = MS_MULT;
			limit = MS_LIMIT;
			units = secondUnits;
			break;
		}

		int count = 0;
		while (max > mult * SCALING_THRESHOLD && count < limit) {
			max /= mult;
			count++;
		}

		xUnits = units[count];

		return (long) Math.pow(mult, count);
	}

	private static long getMaxValue(MassifSnapshot[] snapshots) {
		long max = 0;
		for (MassifSnapshot snapshot : snapshots) {
			if (snapshot.getTotal() > max) {
				max = snapshot.getTotal();
			}
		}
		return max;
	}

	private static long getMaxStack(MassifSnapshot[] snapshots) {
		long max = 0;
		for (MassifSnapshot snapshot : snapshots) {
			if (snapshot.getTotal() > max) {
				max = snapshot.getStacks();
			}
		}
		return max;
	}

	public String getXUnits() {
		return xUnits;
	}

	public String getYUnits() {
		return yUnits;
	}

	public static String[] getByteUnits() {
		return byteUnits;
	}

	public static String[] getInstrUnits() {
		return instrUnits;
	}

	public static String[] getSecondUnits() {
		return secondUnits;
	}

}

Back to the top