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().clone());
99 setRank(other
.getRank());
100 setLastTrace(other
.fLastTraceRead
);
104 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
107 public TmfExperimentContext
clone() {
108 TmfExperimentContext clone
= null;
109 clone
= (TmfExperimentContext
) super.clone();
110 clone
.fContexts
= cloneContexts();
111 clone
.fEvents
= cloneEvents();
112 clone
.fLastTraceRead
= fLastTraceRead
;
116 private ITmfContext
[] cloneContexts() {
117 final ITmfContext
[] contexts
= new ITmfContext
[fContexts
.length
];
118 for (int i
= 0; i
< fContexts
.length
; i
++) {
119 contexts
[i
] = (fContexts
[i
] != null) ? fContexts
[i
].clone() : null;
124 private ITmfEvent
[] cloneEvents() {
125 final ITmfEvent
[] events
= new ITmfEvent
[fEvents
.length
];
126 for (int i
= 0; i
< fEvents
.length
; i
++) {
127 events
[i
] = (fEvents
[i
] != null) ? fEvents
[i
].clone() : null;
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
137 * Get the trace contexts composing this experiment context.
139 * @return The array of trace contexts
141 public ITmfContext
[] getContexts() {
146 * Get the trace events located at this experiment context's location.
148 * @return The array of trace events
150 public ITmfEvent
[] getEvents() {
155 * Get the index of the trace that was last read (so the trace whose
156 * current context will match this experiment's).
158 * @return The index of the trace
160 public int getLastTrace() {
161 return fLastTraceRead
;
165 * Set the last trace read index
168 * The new value to assign
170 public void setLastTrace(final int newIndex
) {
171 fLastTraceRead
= newIndex
;
174 // ------------------------------------------------------------------------
176 // ------------------------------------------------------------------------
179 public int hashCode() {
181 for (int i
= 0; i
< fContexts
.length
; i
++) {
182 result
= 37 * result
+ fContexts
[i
].hashCode();
188 public boolean equals(final Object other
) {
192 if (!super.equals(other
)) {
195 if (!(other
instanceof TmfExperimentContext
)) {
198 final TmfExperimentContext o
= (TmfExperimentContext
) other
;
199 boolean isEqual
= true;
201 while (isEqual
&& (i
< fContexts
.length
)) {
202 isEqual
&= fContexts
[i
].equals(o
.fContexts
[i
]);
209 @SuppressWarnings("nls")
210 public String
toString() {
211 StringBuilder sb
= new StringBuilder("TmfExperimentContext [\n");
212 sb
.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n");
213 sb
.append("\tfContexts=[");
214 for (int i
= 0; i
< fContexts
.length
; i
++) {
215 sb
.append("(" + fContexts
[i
].getLocation() + "," + fContexts
[i
].getRank() + ((i
< fContexts
.length
- 1) ?
")," : ")]\n"));
217 sb
.append("\tfEvents=[");
218 for (int i
= 0; i
< fEvents
.length
; i
++) {
219 ITmfEvent event
= fEvents
[i
];
220 sb
.append(((event
!= null) ? fEvents
[i
].getTimestamp() : "(null)") + ((i
< fEvents
.length
- 1) ?
"," : "]\n"));
222 sb
.append("\tfLastTraceRead=" + fLastTraceRead
+ "\n");
224 return sb
.toString();