0d5814a240ec9361aaf6da33287bd878fbac6429
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.graph.core / src / org / eclipse / tracecompass / internal / analysis / graph / core / base / TmfGraphStatistics.java
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
14 package org.eclipse.tracecompass.internal.analysis.graph.core.base;
15
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
21 import org.eclipse.tracecompass.analysis.graph.core.base.ITmfGraphVisitor;
22 import org.eclipse.tracecompass.analysis.graph.core.base.TmfEdge;
23 import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
24 import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
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
32 * @author Matthew Khouzam
33 */
34 public class TmfGraphStatistics implements ITmfGraphVisitor {
35
36 private final Map<IGraphWorker, Long> fWorkerStats;
37 private Long fTotal;
38 private @Nullable TmfGraph fGraph;
39
40 /**
41 * Constructor
42 */
43 public TmfGraphStatistics() {
44 fWorkerStats = new HashMap<>();
45 fTotal = 0L;
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 */
56 public void computeGraphStatistics(TmfGraph graph, @Nullable IGraphWorker current) {
57 if (current == null) {
58 return;
59 }
60 clear();
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) {
81 IGraphWorker worker = graph.getParentOf(edge.getVertexFrom());
82 Long duration = edge.getDuration();
83 Long currentTotal = fWorkerStats.get(worker);
84 if (currentTotal != null) {
85 duration += currentTotal;
86 }
87 fWorkerStats.put(worker, duration);
88 fTotal += edge.getDuration();
89 }
90 }
91 }
92
93 /**
94 * Get the total duration spent by one element of the graph
95 *
96 * @param worker
97 * The object to get the time spent for
98 * @return The sum of all durations
99 */
100 public Long getSum(@Nullable IGraphWorker worker) {
101 Long sum = 0L;
102 synchronized (fWorkerStats) {
103 Long elapsed = fWorkerStats.get(worker);
104 if (elapsed != null) {
105 sum += elapsed;
106 }
107 }
108 return sum;
109 }
110
111 /**
112 * Get the total duration of the graph vertices
113 *
114 * @return The sum of all durations
115 */
116 public Long getSum() {
117 synchronized (fWorkerStats) {
118 return fTotal;
119 }
120 }
121
122 /**
123 * Get the percentage of time by one element of the graph
124 *
125 * @param worker
126 * The object to get the percentage for
127 * @return The percentage time spent in this element
128 */
129 public Double getPercent(@Nullable IGraphWorker worker) {
130 synchronized (fWorkerStats) {
131 if (getSum() == 0) {
132 return 0D;
133 }
134 return (double) getSum(worker) / (double) getSum();
135 }
136 }
137
138 /**
139 * Clear statistics
140 */
141
142 public void clear() {
143 synchronized (fWorkerStats) {
144 fTotal = 0L;
145 fWorkerStats.clear();
146 }
147 }
148
149 }
This page took 0.033915 seconds and 4 git commands to generate.