1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Put in shape for 1.0
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
;
16 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
22 * The experiment context in TMF.
24 * The experiment keeps track of the next event from each of its traces so it
25 * can pick the next one in chronological order.
27 * This implies that the "next" event from each trace has already been
28 * read and that we at least know its timestamp. This doesn't imply that a
29 * full parse of the event content was performed (read: the legacy LTTng works
32 * The last trace refers to the trace from which the last event was "consumed"
33 * at the experiment level.
35 public class TmfExperimentContext
extends TmfContext
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 * No last trace read indicator
44 public static final int NO_TRACE
= -1;
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 private ITmfContext
[] fContexts
;
51 private ITmfEvent
[] fEvents
;
52 private int fLastTraceRead
;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * Standard constructor
62 * The matching context for each trace in the experiment
64 public TmfExperimentContext(final ITmfContext
[] contexts
) {
67 fEvents
= new ITmfEvent
[fContexts
.length
];
68 final ITmfLocation
[] locations
= new ITmfLocation
[fContexts
.length
];
70 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations
.clone())));
72 final long[] ranks
= new long[fContexts
.length
];
74 for (int i
= 0; i
< fContexts
.length
; i
++) {
75 if (contexts
[i
] != null) {
76 locations
[i
] = contexts
[i
].getLocation();
77 ranks
[i
] = contexts
[i
].getRank();
78 rank
+= contexts
[i
].getRank();
82 // setLocation(new TmfExperimentLocation(new TmfLocationArray(locations)));
84 fLastTraceRead
= NO_TRACE
;
91 * The experiment context to copy
93 public TmfExperimentContext(final TmfExperimentContext other
) {
94 this(other
.cloneContexts());
95 fEvents
= other
.fEvents
;
96 if (other
.getLocation() != null) {
97 setLocation(other
.getLocation());
99 setRank(other
.getRank());
100 setLastTrace(other
.fLastTraceRead
);
104 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#dispose()
107 public void dispose() {
108 for (ITmfContext context
: fContexts
) {
115 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
118 public TmfExperimentContext
clone() {
119 TmfExperimentContext clone
= null;
120 clone
= (TmfExperimentContext
) super.clone();
121 clone
.fContexts
= cloneContexts();
122 clone
.fEvents
= cloneEvents();
123 clone
.fLastTraceRead
= fLastTraceRead
;
127 private ITmfContext
[] cloneContexts() {
128 final ITmfContext
[] contexts
= new ITmfContext
[fContexts
.length
];
129 for (int i
= 0; i
< fContexts
.length
; i
++) {
130 contexts
[i
] = (fContexts
[i
] != null) ? fContexts
[i
].clone() : null;
135 private ITmfEvent
[] cloneEvents() {
136 final ITmfEvent
[] events
= new ITmfEvent
[fEvents
.length
];
137 for (int i
= 0; i
< fEvents
.length
; i
++) {
138 events
[i
] = fEvents
[i
];
143 // ------------------------------------------------------------------------
145 // ------------------------------------------------------------------------
148 * Get the trace contexts composing this experiment context.
150 * @return The array of trace contexts
152 public ITmfContext
[] getContexts() {
157 * Get the trace events located at this experiment context's location.
159 * @return The array of trace events
161 public ITmfEvent
[] getEvents() {
166 * Get the index of the trace that was last read (so the trace whose
167 * current context will match this experiment's).
169 * @return The index of the trace
171 public int getLastTrace() {
172 return fLastTraceRead
;
176 * Set the last trace read index
179 * The new value to assign
181 public void setLastTrace(final int newIndex
) {
182 fLastTraceRead
= newIndex
;
185 // ------------------------------------------------------------------------
187 // ------------------------------------------------------------------------
190 public int hashCode() {
192 for (int i
= 0; i
< fContexts
.length
; i
++) {
193 result
= 37 * result
+ fContexts
[i
].hashCode();
199 public boolean equals(final Object other
) {
203 if (!super.equals(other
)) {
206 if (!(other
instanceof TmfExperimentContext
)) {
209 final TmfExperimentContext o
= (TmfExperimentContext
) other
;
210 boolean isEqual
= true;
212 while (isEqual
&& (i
< fContexts
.length
)) {
213 isEqual
&= fContexts
[i
].equals(o
.fContexts
[i
]);
220 @SuppressWarnings("nls")
221 public String
toString() {
222 StringBuilder sb
= new StringBuilder("TmfExperimentContext [\n");
223 sb
.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n");
224 sb
.append("\tfContexts=[");
225 for (int i
= 0; i
< fContexts
.length
; i
++) {
226 sb
.append("(" + fContexts
[i
].getLocation() + "," + fContexts
[i
].getRank() + ((i
< fContexts
.length
- 1) ?
")," : ")]\n"));
228 sb
.append("\tfEvents=[");
229 for (int i
= 0; i
< fEvents
.length
; i
++) {
230 ITmfEvent event
= fEvents
[i
];
231 sb
.append(((event
!= null) ? fEvents
[i
].getTimestamp() : "(null)") + ((i
< fEvents
.length
- 1) ?
"," : "]\n"));
233 sb
.append("\tfLastTraceRead=" + fLastTraceRead
+ "\n");
235 return sb
.toString();