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
.segmentstore
.core
.ISegment
;
21 import org
.eclipse
.tracecompass
.segmentstore
.core
.SegmentComparators
;
23 import com
.google
.common
.collect
.Ordering
;
26 * Called Functuon common class, defines the start, end, depth, parent and
27 * children. Does not define the symbol
29 * @author Matthew Khouzam
30 * @author Sonia Farrah
32 abstract class AbstractCalledFunction
implements ICalledFunction
{
34 static final Comparator
<ISegment
> COMPARATOR
;
37 * checkNotNull() has to be called separately, or else it breaks the
40 Comparator
<ISegment
> comp
= Ordering
.from(SegmentComparators
.INTERVAL_START_COMPARATOR
).compound(SegmentComparators
.INTERVAL_END_COMPARATOR
);
41 COMPARATOR
= checkNotNull(comp
);
47 private static final long serialVersionUID
= 7992199223906717340L;
49 protected final long fStart
;
50 protected final long fEnd
;
51 protected final int fDepth
;
52 private final List
<ICalledFunction
> fChildren
= new ArrayList
<>();
53 private final @Nullable ICalledFunction fParent
;
54 protected long fSelfTime
= 0;
55 private final int fProcessId
;
57 public AbstractCalledFunction(long start
, long end
, int depth
, int processId
, @Nullable ICalledFunction parent
) {
59 throw new IllegalArgumentException(Messages
.TimeError
+ "[" + start
+ "," + end
+ "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
65 // It'll be modified once we add a child to it
66 fSelfTime
= fEnd
- fStart
;
67 fProcessId
= processId
;
71 public long getStart() {
76 public long getEnd() {
81 public List
<ICalledFunction
> getChildren() {
86 public @Nullable ICalledFunction
getParent() {
91 * Add the child to the segment's children, and subtract the child's
92 * duration to the duration of the segment so we can calculate its self
96 * The child to add to the segment's children
98 public void addChild(ICalledFunction child
) {
99 if (child
.getParent() != this) {
100 throw new IllegalArgumentException("Child parent not the same as child being added to."); //$NON-NLS-1$
102 fChildren
.add(child
);
103 substractChildDuration(child
.getEnd() - child
.getStart());
107 * Subtract the child's duration to the duration of the segment.
109 * @param childDuration
110 * The child's duration
112 private void substractChildDuration(long childDuration
) {
113 fSelfTime
-= childDuration
;
117 public long getSelfTime() {
122 public int getDepth() {
127 public int getProcessId() {
132 public int compareTo(@Nullable ISegment o
) {
134 throw new IllegalArgumentException();
136 return COMPARATOR
.compare(this, o
);
140 public String
toString() {
141 return new String("[" + String
.valueOf(fStart
) + ", " + String
.valueOf(fEnd
) + "]"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
145 public int hashCode() {
146 final int prime
= 31;
148 result
= prime
* result
+ fDepth
;
149 result
= prime
* result
+ (int) (fEnd ^
(fEnd
>>> 32));
150 ICalledFunction parent
= fParent
;
151 result
= prime
* result
+ ((parent
== null) ?
0 : parent
.hashCode());
152 result
= prime
* result
+ (int) (fSelfTime ^
(fSelfTime
>>> 32));
153 result
= prime
* result
+ (int) (fStart ^
(fStart
>>> 32));
154 result
= prime
* result
+ getSymbol().hashCode();
159 public boolean equals(@Nullable Object obj
) {
166 if (getClass() != obj
.getClass()) {
169 AbstractCalledFunction other
= (AbstractCalledFunction
) obj
;
170 if (fDepth
!= other
.fDepth
) {
173 if (fEnd
!= other
.fEnd
) {
176 if (fParent
== null) {
177 if (other
.fParent
!= null) {
180 } else if (!Objects
.equals(fParent
, other
.fParent
)) {
183 if (fSelfTime
!= other
.fSelfTime
) {
186 if (fStart
!= other
.fStart
) {
189 if (!Objects
.equals(getSymbol(), other
.getSymbol())) {