53af8bedc7abd0c299de3ceaa9b57eefae0972eb
[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 org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
18 import org.eclipse.tracecompass.analysis.graph.core.base.ITmfGraphVisitor;
19 import org.eclipse.tracecompass.analysis.graph.core.base.TmfEdge;
20 import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
21 import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;
22 import org.eclipse.tracecompass.common.core.NonNullUtils;
23
24 import com.google.common.collect.ArrayListMultimap;
25 import com.google.common.collect.Multimap;
26
27 /**
28 * Class that computes statistics on time spent in the elements (objects) of a
29 * graph
30 *
31 * @author Francis Giraldeau
32 * @author Geneviève Bastien
33 */
34 public class TmfGraphStatistics implements ITmfGraphVisitor {
35
36 private static final String STATS_TOTAL = "total"; //$NON-NLS-1$
37
38 private final Multimap<Object, Long> fWorkerStats;
39 private @Nullable TmfGraph fGraph;
40
41 /**
42 * Constructor
43 */
44 public TmfGraphStatistics() {
45 fWorkerStats = NonNullUtils.checkNotNull(ArrayListMultimap.<Object, Long> create());
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 getGraphStatistics(TmfGraph graph, @Nullable IGraphWorker current) {
57 if (current == null) {
58 return;
59 }
60 fGraph = graph;
61 fGraph.scanLineTraverse(fGraph.getHead(current), this);
62 }
63
64 @Override
65 public void visitHead(TmfVertex node) {
66
67 }
68
69 @Override
70 public void visit(TmfVertex node) {
71
72 }
73
74 @Override
75 public void visit(TmfEdge edge, boolean horizontal) {
76 // Add the duration of the link only if it is horizontal
77 TmfGraph graph = fGraph;
78 synchronized (fWorkerStats) {
79 if (horizontal && graph != null) {
80 fWorkerStats.put(graph.getParentOf(edge.getVertexFrom()),
81 edge.getVertexTo().getTs() - edge.getVertexFrom().getTs());
82 fWorkerStats.put(STATS_TOTAL,
83 edge.getVertexTo().getTs() - edge.getVertexFrom().getTs());
84 }
85 }
86 }
87
88 /**
89 * Get the total duration spent by one element of the graph
90 *
91 * @param worker
92 * The object to get the time spent for
93 * @return The sum of all durations
94 */
95 public Long getSum(@Nullable Object worker) {
96 long sum = 0L;
97 synchronized (fWorkerStats) {
98 for (long duration : fWorkerStats.get(worker)) {
99 sum += duration;
100 }
101 }
102 return sum;
103 }
104
105 /**
106 * Get the total duration of the graph vertices
107 *
108 * @return The sum of all durations
109 */
110 public Long getSum() {
111 return getSum(STATS_TOTAL);
112 }
113
114 /**
115 * Get the percentage of time by one element of the graph
116 *
117 * @param worker
118 * The object to get the percentage for
119 * @return The percentage time spent in this element
120 */
121 public Double getPercent(@Nullable Object worker) {
122 if (getSum() == 0) {
123 return 0D;
124 }
125 return (double) getSum(worker) / (double) getSum();
126 }
127
128 }
This page took 0.04441 seconds and 4 git commands to generate.