1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.callgraph
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Comparator
;
16 import java
.util
.List
;
17 import java
.util
.Objects
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
21 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
22 import org
.eclipse
.tracecompass
.segmentstore
.core
.SegmentComparators
;
24 import com
.google
.common
.collect
.Ordering
;
27 * Called Functuon common class, defines the start, end, depth, parent and
28 * children. Does not define the symbol
30 * @author Matthew Khouzam
31 * @author Sonia Farrah
33 abstract class AbstractCalledFunction
implements ICalledFunction
{
35 static final Comparator
<ISegment
> COMPARATOR
;
38 * checkNotNull() has to be called separately, or else it breaks the
41 Comparator
<ISegment
> comp
= Ordering
.from(SegmentComparators
.INTERVAL_START_COMPARATOR
).compound(SegmentComparators
.INTERVAL_END_COMPARATOR
);
42 COMPARATOR
= checkNotNull(comp
);
48 private static final long serialVersionUID
= 7992199223906717340L;
50 protected final long fStart
;
51 protected final long fEnd
;
52 protected final int fDepth
;
53 private final List
<ICalledFunction
> fChildren
= new ArrayList
<>();
54 private final @Nullable ICalledFunction fParent
;
55 protected long fSelfTime
= 0;
56 private final int fProcessId
;
58 public AbstractCalledFunction(long start
, long end
, int depth
, int processId
, @Nullable ICalledFunction parent
) {
60 throw new IllegalArgumentException(Messages
.TimeError
+ "[" + start
+ "," + end
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
66 // It'll be modified once we add a child to it
67 fSelfTime
= fEnd
- fStart
;
68 fProcessId
= processId
;
72 public long getStart() {
77 public long getEnd() {
82 public List
<ICalledFunction
> getChildren() {
87 public @Nullable ICalledFunction
getParent() {
92 public String
getName() {
93 return NonNullUtils
.nullToEmptyString(getSymbol().toString());
97 * Add the child to the segment's children, and subtract the child's
98 * duration to the duration of the segment so we can calculate its self
102 * The child to add to the segment's children
104 public void addChild(ICalledFunction child
) {
105 if (child
.getParent() != this) {
106 throw new IllegalArgumentException("Child parent not the same as child being added to."); //$NON-NLS-1$
108 fChildren
.add(child
);
109 substractChildDuration(child
.getEnd() - child
.getStart());
113 * Subtract the child's duration to the duration of the segment.
115 * @param childDuration
116 * The child's duration
118 private void substractChildDuration(long childDuration
) {
119 fSelfTime
-= childDuration
;
123 public long getSelfTime() {
128 public int getDepth() {
133 public int getProcessId() {
138 public int compareTo(@Nullable ISegment o
) {
140 throw new IllegalArgumentException();
142 return COMPARATOR
.compare(this, o
);
146 public String
toString() {
147 return '[' + String
.valueOf(fStart
) + ", " + String
.valueOf(fEnd
) + ']' + " Duration: " + getLength() + ", Self Time: " + fSelfTime
; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
151 public int hashCode() {
152 return Objects
.hash(fDepth
, fEnd
, fParent
, fSelfTime
, fStart
, getSymbol());
156 public boolean equals(@Nullable Object obj
) {
163 if (getClass() != obj
.getClass()) {
166 AbstractCalledFunction other
= (AbstractCalledFunction
) obj
;
167 if (fDepth
!= other
.fDepth
) {
170 if (fEnd
!= other
.fEnd
) {
173 if (fSelfTime
!= other
.fSelfTime
) {
176 if (fStart
!= other
.fStart
) {
179 if (!Objects
.equals(fParent
, other
.getParent())) {
182 if (!Objects
.equals(getSymbol(), other
.getSymbol())) {