be381804fcf78db839f71deba46e329713b3fb30
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / internal / analysis / os / linux / ui / views / io / diskioactivity / DisksIOActivityViewer.java
1 /**********************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 **********************************************************************/
9
10 package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.io.diskioactivity;
11
12 import java.util.Collection;
13
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.tracecompass.analysis.os.linux.core.inputoutput.Disk;
19 import org.eclipse.tracecompass.analysis.os.linux.core.inputoutput.InputOutputAnalysisModule;
20 import org.eclipse.tracecompass.analysis.os.linux.core.inputoutput.InputOutputInformationProvider;
21 import org.eclipse.tracecompass.analysis.os.linux.core.inputoutput.IoOperationType;
22 import org.eclipse.tracecompass.common.core.format.DataSpeedWithUnitFormat;
23 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
24 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
25 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
26 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
27 import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
28 import org.swtchart.Chart;
29
30 /**
31 * Disk IO Activity viewer, shows read and write bandwidth used over time.
32 *
33 * @author Houssem Daoud
34 */
35 public class DisksIOActivityViewer extends TmfCommonXLineChartViewer {
36
37 // Timeout between updates in the updateData thread
38 private static final long BUILD_UPDATE_TIMEOUT = 500;
39 private static final double RESOLUTION = 0.2;
40 private static final int BYTES_PER_SECTOR = 512;
41 private static final int SECOND_TO_NANOSECOND = (int) Math.pow(10, 9);
42
43 private @Nullable InputOutputAnalysisModule fModule = null;
44
45 /**
46 * Constructor
47 *
48 * @param parent
49 * parent view
50 */
51 public DisksIOActivityViewer(@Nullable Composite parent) {
52 super(parent, Messages.DiskIOActivityViewer_Title, Messages.DiskIOActivityViewer_XAxis, Messages.DiskIOActivityViewer_YAxis);
53 setResolution(RESOLUTION);
54 Chart chart = getSwtChart();
55 chart.getAxisSet().getYAxis(0).getTick().setFormat(new DataSpeedWithUnitFormat());
56 chart.getLegend().setPosition(SWT.LEFT);
57 }
58
59 @Override
60 protected void initializeDataSource() {
61 ITmfTrace trace = getTrace();
62 if (trace != null) {
63 InputOutputAnalysisModule module = TmfTraceUtils.getAnalysisModuleOfClass(trace, InputOutputAnalysisModule.class, InputOutputAnalysisModule.ID);
64 if (module == null) {
65 return;
66 }
67 module.schedule();
68 fModule = module;
69 }
70 }
71
72 @Override
73 protected void updateData(long start, long end, int nb, @Nullable IProgressMonitor monitor) {
74 InputOutputAnalysisModule module = fModule;
75 if (getTrace() == null || module == null) {
76 return;
77 }
78 if (!module.waitForInitialization()) {
79 return;
80 }
81 ITmfStateSystem ss = module.getStateSystem();
82 if (ss == null) {
83 return;
84 }
85
86 double[] xvalues = getXAxis(start, end, nb);
87 setXAxis(xvalues);
88
89 boolean complete = false;
90 long currentEnd = start;
91
92 while (!complete && currentEnd < end) {
93 if (monitor != null && monitor.isCanceled()) {
94 return;
95 }
96 complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
97 currentEnd = ss.getCurrentEndTime();
98 long traceStart = getStartTime();
99 long traceEnd = getEndTime();
100 long offset = this.getTimeOffset();
101
102 Collection<Disk> disks = InputOutputInformationProvider.getDisks(module);
103
104 for (Disk disk : disks) {
105 if (!disk.hasActivity()) {
106 continue;
107 }
108
109 String diskName = disk.getDiskName();
110
111 /* Initialize quarks and series names */
112 double[] yValuesWritten = new double[xvalues.length];
113 double[] yValuesRead = new double[xvalues.length];
114 String seriesNameWritten = new String(diskName + Messages.DisksIOActivityViewer_Write);
115 String seriesNameRead = new String(diskName + Messages.DisksIOActivityViewer_Read);
116
117 double prevX = xvalues[0];
118 long prevTime = (long) prevX + offset;
119 /*
120 * make sure that time is in the trace range after double to
121 * long conversion
122 */
123 prevTime = Math.max(traceStart, prevTime);
124 prevTime = Math.min(traceEnd, prevTime);
125 long prevCountRead = disk.getSectorsAt(prevTime, IoOperationType.READ);
126 long prevCountWrite = disk.getSectorsAt(prevTime, IoOperationType.WRITE);
127 for (int i = 1; i < xvalues.length; i++) {
128 if (monitor != null && monitor.isCanceled()) {
129 return;
130 }
131 double x = xvalues[i];
132 long time = (long) x + offset;
133 time = Math.max(traceStart, time);
134 time = Math.min(traceEnd, time);
135 try {
136 long count = disk.getSectorsAt(time, IoOperationType.WRITE);
137 yValuesWritten[i] = (count - prevCountWrite) * BYTES_PER_SECTOR / ((double) (time - prevTime) / SECOND_TO_NANOSECOND);
138 prevCountWrite = count;
139 count = disk.getSectorsAt(time, IoOperationType.READ);
140 yValuesRead[i] = (count - prevCountRead) * BYTES_PER_SECTOR / ((double) (time - prevTime) / SECOND_TO_NANOSECOND);
141 prevCountRead = count;
142 } catch (TimeRangeException e) {
143 yValuesWritten[i] = 0;
144 yValuesRead[i] = 0;
145 }
146 prevTime = time;
147 }
148 setSeries(seriesNameWritten, yValuesWritten);
149 setSeries(seriesNameRead, yValuesRead);
150 if (monitor != null && monitor.isCanceled()) {
151 return;
152 }
153 updateDisplay();
154 }
155 }
156 }
157 }
This page took 0.043994 seconds and 4 git commands to generate.