| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 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 | package org.eclipse.tracecompass.internal.analysis.timing.core.segmentstore.statistics; |
| 10 | |
| 11 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 12 | |
| 13 | import java.util.HashMap; |
| 14 | import java.util.Iterator; |
| 15 | import java.util.Map; |
| 16 | |
| 17 | import org.eclipse.core.runtime.IProgressMonitor; |
| 18 | import org.eclipse.jdt.annotation.Nullable; |
| 19 | import org.eclipse.tracecompass.analysis.timing.core.segmentstore.ISegmentStoreProvider; |
| 20 | import org.eclipse.tracecompass.segmentstore.core.ISegment; |
| 21 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; |
| 22 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
| 23 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule; |
| 24 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; |
| 25 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 26 | |
| 27 | import com.google.common.collect.ImmutableList; |
| 28 | |
| 29 | /** |
| 30 | * Abstract analysis to build statistics data for a segment store |
| 31 | * |
| 32 | * @author Jean-Christian Kouame |
| 33 | */ |
| 34 | public abstract class AbstractSegmentStatisticsAnalysis extends TmfAbstractAnalysisModule { |
| 35 | |
| 36 | private @Nullable IAnalysisModule fSegmentStoreProviderModule; |
| 37 | |
| 38 | private @Nullable SegmentStoreStatistics fTotalStats; |
| 39 | |
| 40 | private @Nullable Map<String, SegmentStoreStatistics> fPerSegmentTypeStats; |
| 41 | |
| 42 | @Override |
| 43 | protected Iterable<IAnalysisModule> getDependentAnalyses() { |
| 44 | ITmfTrace trace = getTrace(); |
| 45 | if (trace != null) { |
| 46 | ISegmentStoreProvider provider = getSegmentProviderAnalysis(trace); |
| 47 | if (provider instanceof IAnalysisModule) { |
| 48 | fSegmentStoreProviderModule = (IAnalysisModule) provider; |
| 49 | return ImmutableList.of((IAnalysisModule) provider); |
| 50 | } |
| 51 | } |
| 52 | return super.getDependentAnalyses(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException { |
| 57 | IAnalysisModule segmentStoreProviderModule = fSegmentStoreProviderModule; |
| 58 | ITmfTrace trace = getTrace(); |
| 59 | if (!(segmentStoreProviderModule instanceof ISegmentStoreProvider) || (trace == null)) { |
| 60 | return false; |
| 61 | } |
| 62 | segmentStoreProviderModule.waitForCompletion(); |
| 63 | |
| 64 | ISegmentStore<ISegment> segStore = ((ISegmentStoreProvider) segmentStoreProviderModule).getSegmentStore(); |
| 65 | |
| 66 | if (segStore != null) { |
| 67 | |
| 68 | boolean result = calculateTotalManual(segStore, monitor); |
| 69 | |
| 70 | if (!result) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | result = calculateTotalPerType(segStore, monitor); |
| 75 | if (!result) { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | private boolean calculateTotalManual(ISegmentStore<ISegment> store, IProgressMonitor monitor) { |
| 83 | SegmentStoreStatistics total = new SegmentStoreStatistics(); |
| 84 | Iterator<ISegment> iter = store.iterator(); |
| 85 | while (iter.hasNext()) { |
| 86 | if (monitor.isCanceled()) { |
| 87 | return false; |
| 88 | } |
| 89 | ISegment segment = iter.next(); |
| 90 | total.update(checkNotNull(segment)); |
| 91 | } |
| 92 | fTotalStats = total; |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | private boolean calculateTotalPerType(ISegmentStore<ISegment> store, IProgressMonitor monitor) { |
| 97 | Map<String, SegmentStoreStatistics> perSegmentTypeStats = new HashMap<>(); |
| 98 | |
| 99 | Iterator<ISegment> iter = store.iterator(); |
| 100 | while (iter.hasNext()) { |
| 101 | if (monitor.isCanceled()) { |
| 102 | return false; |
| 103 | } |
| 104 | ISegment segment = iter.next(); |
| 105 | String segmentType = getSegmentType(segment); |
| 106 | if (segmentType != null) { |
| 107 | SegmentStoreStatistics values = perSegmentTypeStats.get(segmentType); |
| 108 | if (values == null) { |
| 109 | values = new SegmentStoreStatistics(); |
| 110 | } |
| 111 | values.update(segment); |
| 112 | perSegmentTypeStats.put(segmentType, values); |
| 113 | } |
| 114 | } |
| 115 | fPerSegmentTypeStats = perSegmentTypeStats; |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get the type of a segment. Statistics per type will use this type as a |
| 121 | * key |
| 122 | * |
| 123 | * @param segment |
| 124 | * the segment for which to get the type |
| 125 | * @return The type of the segment |
| 126 | */ |
| 127 | protected abstract @Nullable String getSegmentType(ISegment segment); |
| 128 | |
| 129 | /** |
| 130 | * Find the segment store provider used for this analysis |
| 131 | * |
| 132 | * @param trace |
| 133 | * The active trace |
| 134 | * |
| 135 | * @return The segment store provider |
| 136 | */ |
| 137 | protected abstract @Nullable ISegmentStoreProvider getSegmentProviderAnalysis(ITmfTrace trace); |
| 138 | |
| 139 | @Override |
| 140 | protected void canceling() { |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * The total statistics |
| 145 | * |
| 146 | * @return the total statistics |
| 147 | */ |
| 148 | public @Nullable SegmentStoreStatistics getTotalStats() { |
| 149 | return fTotalStats; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * The per syscall statistics |
| 154 | * |
| 155 | * @return the per syscall statistics |
| 156 | */ |
| 157 | public @Nullable Map<String, SegmentStoreStatistics> getPerSegmentTypeStats() { |
| 158 | return fPerSegmentTypeStats; |
| 159 | } |
| 160 | |
| 161 | } |