Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a965a982ded46aff89bb12c40ad5310444b8b2da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*******************************************************************************
 * Copyright (c) 2015 École Polytechnique de Montréal
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Francis Giraldeau - Initial implementation and API
 *   Geneviève Bastien - Initial implementation and API
 *******************************************************************************/

package org.eclipse.tracecompass.internal.analysis.graph.core.base;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
import org.eclipse.tracecompass.analysis.graph.core.base.ITmfGraphVisitor;
import org.eclipse.tracecompass.analysis.graph.core.base.TmfEdge;
import org.eclipse.tracecompass.analysis.graph.core.base.TmfGraph;
import org.eclipse.tracecompass.analysis.graph.core.base.TmfVertex;

/**
 * Class that computes statistics on time spent in the elements (objects) of a
 * graph
 *
 * @author Francis Giraldeau
 * @author Geneviève Bastien
 * @author Matthew Khouzam
 */
public class TmfGraphStatistics implements ITmfGraphVisitor {

    private final Map<IGraphWorker, Long> fWorkerStats;
    private Long fTotal;
    private @Nullable TmfGraph fGraph;

    /**
     * Constructor
     */
    public TmfGraphStatistics() {
        fWorkerStats = new HashMap<>();
        fTotal = 0L;
    }

    /**
     * Compute the statistics for a graph
     *
     * @param graph
     *            The graph on which to calculate statistics
     * @param current
     *            The element from which to start calculations
     */
    public void computeGraphStatistics(TmfGraph graph, @Nullable IGraphWorker current) {
        if (current == null) {
            return;
        }
        clear();
        fGraph = graph;
        fGraph.scanLineTraverse(fGraph.getHead(current), this);
    }

    @Override
    public void visitHead(TmfVertex node) {
        // Do nothing
    }

    @Override
    public void visit(TmfVertex node) {
        // Do nothing
    }

    @Override
    public void visit(TmfEdge edge, boolean horizontal) {
        // Add the duration of the link only if it is horizontal
        TmfGraph graph = fGraph;
        synchronized (fWorkerStats) {
            if (horizontal && graph != null) {
                IGraphWorker worker = graph.getParentOf(edge.getVertexFrom());
                if (worker == null) {
                    return;
                }
                Long duration = edge.getDuration();
                Long currentTotal = fWorkerStats.get(worker);
                if (currentTotal != null) {
                    duration += currentTotal;
                }
                fWorkerStats.put(worker, duration);
                fTotal += edge.getDuration();
            }
        }
    }

    /**
     * Get the total duration spent by one element of the graph
     *
     * @param worker
     *            The object to get the time spent for
     * @return The sum of all durations
     */
    public Long getSum(@Nullable IGraphWorker worker) {
        Long sum = 0L;
        synchronized (fWorkerStats) {
            Long elapsed = fWorkerStats.get(worker);
            if (elapsed != null) {
                sum += elapsed;
            }
        }
        return sum;
    }

    /**
     * Get the total duration of the graph vertices
     *
     * @return The sum of all durations
     */
    public Long getSum() {
        synchronized (fWorkerStats) {
            return fTotal;
        }
    }

    /**
     * Get the percentage of time by one element of the graph
     *
     * @param worker
     *            The object to get the percentage for
     * @return The percentage time spent in this element
     */
    public Double getPercent(@Nullable IGraphWorker worker) {
        synchronized (fWorkerStats) {
            if (getSum() == 0) {
                return 0D;
            }
            return (double) getSum(worker) / (double) getSum();
        }
    }

    /**
     * Clear statistics
     */

    public void clear() {
        synchronized (fWorkerStats) {
            fTotal = 0L;
            fWorkerStats.clear();
        }
    }

}

Back to the top