Fix some null warnings
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.graph.core / src / org / eclipse / tracecompass / internal / analysis / graph / core / base / TmfGraphStatistics.java
CommitLineData
68761620
FG
1/*******************************************************************************
2 * Copyright (c) 2015 É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 * Contributors:
10 * Francis Giraldeau - Initial implementation and API
11 * Geneviève Bastien - Initial implementation and API
12 *******************************************************************************/
13
14package org.eclipse.tracecompass.internal.analysis.graph.core.base;
15
49a4b360
FG
16import java.util.HashMap;
17import java.util.Map;
18
68761620
FG
19import org.eclipse.jdt.annotation.Nullable;
20import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
21import org.eclipse.tracecompass.analysis.graph.core.base.ITmfGraphVisitor;
22import org.eclipse.tracecompass.analysis.graph.core.base.TmfEdge;
23import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
24import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
68761620
FG
25
26/**
27 * Class that computes statistics on time spent in the elements (objects) of a
28 * graph
29 *
30 * @author Francis Giraldeau
31 * @author Geneviève Bastien
49a4b360 32 * @author Matthew Khouzam
68761620
FG
33 */
34public class TmfGraphStatistics implements ITmfGraphVisitor {
35
49a4b360
FG
36 private final Map<IGraphWorker, Long> fWorkerStats;
37 private Long fTotal;
68761620
FG
38 private @Nullable TmfGraph fGraph;
39
40 /**
41 * Constructor
42 */
43 public TmfGraphStatistics() {
49a4b360
FG
44 fWorkerStats = new HashMap<>();
45 fTotal = 0L;
68761620
FG
46 }
47
48 /**
49 * Compute the statistics for a graph
50 *
51 * @param graph
52 * The graph on which to calculate statistics
53 * @param current
54 * The element from which to start calculations
55 */
49a4b360 56 public void computeGraphStatistics(TmfGraph graph, @Nullable IGraphWorker current) {
68761620
FG
57 if (current == null) {
58 return;
59 }
49a4b360 60 clear();
68761620
FG
61 fGraph = graph;
62 fGraph.scanLineTraverse(fGraph.getHead(current), this);
63 }
64
65 @Override
66 public void visitHead(TmfVertex node) {
67
68 }
69
70 @Override
71 public void visit(TmfVertex node) {
72
73 }
74
75 @Override
76 public void visit(TmfEdge edge, boolean horizontal) {
77 // Add the duration of the link only if it is horizontal
78 TmfGraph graph = fGraph;
79 synchronized (fWorkerStats) {
80 if (horizontal && graph != null) {
49a4b360 81 IGraphWorker worker = graph.getParentOf(edge.getVertexFrom());
aa353506
AM
82 if (worker == null) {
83 return;
84 }
49a4b360
FG
85 Long duration = edge.getDuration();
86 Long currentTotal = fWorkerStats.get(worker);
87 if (currentTotal != null) {
88 duration += currentTotal;
89 }
90 fWorkerStats.put(worker, duration);
91 fTotal += edge.getDuration();
68761620
FG
92 }
93 }
94 }
95
96 /**
97 * Get the total duration spent by one element of the graph
98 *
99 * @param worker
100 * The object to get the time spent for
101 * @return The sum of all durations
102 */
49a4b360
FG
103 public Long getSum(@Nullable IGraphWorker worker) {
104 Long sum = 0L;
68761620 105 synchronized (fWorkerStats) {
49a4b360
FG
106 Long elapsed = fWorkerStats.get(worker);
107 if (elapsed != null) {
108 sum += elapsed;
68761620
FG
109 }
110 }
111 return sum;
112 }
113
114 /**
115 * Get the total duration of the graph vertices
116 *
117 * @return The sum of all durations
118 */
119 public Long getSum() {
49a4b360
FG
120 synchronized (fWorkerStats) {
121 return fTotal;
122 }
68761620
FG
123 }
124
125 /**
126 * Get the percentage of time by one element of the graph
127 *
128 * @param worker
129 * The object to get the percentage for
130 * @return The percentage time spent in this element
131 */
49a4b360
FG
132 public Double getPercent(@Nullable IGraphWorker worker) {
133 synchronized (fWorkerStats) {
134 if (getSum() == 0) {
135 return 0D;
136 }
137 return (double) getSum(worker) / (double) getSum();
138 }
139 }
140
141 /**
142 * Clear statistics
143 */
144
145 public void clear() {
146 synchronized (fWorkerStats) {
147 fTotal = 0L;
148 fWorkerStats.clear();
68761620 149 }
68761620
FG
150 }
151
152}
This page took 0.045661 seconds and 5 git commands to generate.