analysis: Allow segment table viewer/provider to accept ISegment[] input
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / internal / analysis / os / linux / core / latency / statistics / LatencyStatisticsAnalysisModule.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.internal.analysis.os.linux.core.latency.statistics;
13
14 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
15
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.Map;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.eclipse.tracecompass.analysis.os.linux.core.latency.LatencyAnalysis;
23 import org.eclipse.tracecompass.analysis.os.linux.core.latency.SystemCall;
24 import org.eclipse.tracecompass.segmentstore.core.ISegment;
25 import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
26 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
27 import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
28 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
29 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
30 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
31
32 import com.google.common.collect.ImmutableList;
33
34 /**
35 * Analysis module to calculate statistics of a latency analysis
36 *
37 * @author Bernd Hufmann
38 */
39 public class LatencyStatisticsAnalysisModule extends TmfAbstractAnalysisModule {
40
41 /** The analysis module ID */
42 public static String ID = "org.eclipse.tracecompass.analysis.os.linux.core.latency.statistics"; //$NON-NLS-1$
43
44 private @Nullable LatencyAnalysis fLatencyModule;
45
46 private @Nullable LatencyStatistics fTotalStats;
47
48 private @Nullable Map<String, LatencyStatistics> fPerSyscallStats;
49
50 @Override
51 protected Iterable<IAnalysisModule> getDependentAnalyses() {
52 ITmfTrace trace = getTrace();
53 if (trace != null) {
54 LatencyAnalysis module = TmfTraceUtils.getAnalysisModuleOfClass(trace, LatencyAnalysis.class, checkNotNull(LatencyAnalysis.ID));
55 fLatencyModule = module;
56 return checkNotNull(ImmutableList.of((IAnalysisModule) module));
57 }
58 return super.getDependentAnalyses();
59 }
60
61 @Override
62 protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
63 LatencyAnalysis latency = fLatencyModule;
64 ITmfTrace trace = getTrace();
65 if ((latency == null) || (trace == null)) {
66 return false;
67 }
68 latency.waitForCompletion();
69
70 ISegmentStore<ISegment> store = latency.getResults();
71
72 if (store != null) {
73
74 boolean result = calculateTotalManual(store, monitor);
75
76 if (!result) {
77 return false;
78 }
79
80 result = calculateTotalPerSyscall(store, monitor);
81 if (!result) {
82 return false;
83 }
84 }
85 return true;
86 }
87
88 private boolean calculateTotalManual(ISegmentStore<ISegment> store, IProgressMonitor monitor) {
89 LatencyStatistics total = new LatencyStatistics();
90 Iterator<ISegment> iter = store.iterator();
91 while (iter.hasNext()) {
92 if (monitor.isCanceled()) {
93 return false;
94 }
95 ISegment segment = iter.next();
96 total.update(checkNotNull(segment));
97 }
98 fTotalStats = total;
99 return true;
100 }
101
102 private boolean calculateTotalPerSyscall(ISegmentStore<ISegment> store, IProgressMonitor monitor) {
103 Map<String, LatencyStatistics> perSyscallStats = new HashMap<>();
104
105 Iterator<ISegment> iter = store.iterator();
106 while (iter.hasNext()) {
107 if (monitor.isCanceled()) {
108 return false;
109 }
110 ISegment segment = iter.next();
111 if (segment instanceof SystemCall) {
112 SystemCall syscall = (SystemCall) segment;
113 LatencyStatistics values = perSyscallStats.get(syscall.getName());
114 if (values == null) {
115 values = new LatencyStatistics();
116 }
117 values.update(segment);
118 perSyscallStats.put(syscall.getName(), values);
119 }
120 }
121 fPerSyscallStats = perSyscallStats;
122 return true;
123 }
124
125 @Override
126 protected void canceling() {
127 }
128
129 /**
130 * The total statistics
131 *
132 * @return the total statistics
133 */
134 public @Nullable LatencyStatistics getTotalStats() {
135 return fTotalStats;
136 }
137
138 /**
139 * The per syscall statistics
140 *
141 * @return the per syscall statistics
142 */
143 public @Nullable Map<String, LatencyStatistics> getPerSyscallStats() {
144 return fPerSyscallStats;
145 }
146
147 }
This page took 0.034733 seconds and 5 git commands to generate.