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 // ------------------------------------------------------------------------
61 public TmfExperimentContext(final ITmfContext
[] contexts
) {
64 fEvents
= new ITmfEvent
[fContexts
.length
];
65 final ITmfLocation
<?
>[] locations
= new ITmfLocation
[fContexts
.length
];
67 setLocation(new TmfExperimentLocation(new TmfLocationArray(locations
.clone())));
69 final long[] ranks
= new long[fContexts
.length
];
71 for (int i
= 0; i
< fContexts
.length
; i
++) {
72 if (contexts
[i
] != null) {
73 locations
[i
] = contexts
[i
].getLocation();
74 ranks
[i
] = contexts
[i
].getRank();
75 rank
+= contexts
[i
].getRank();
79 // setLocation(new TmfExperimentLocation(new TmfLocationArray(locations)));
81 fLastTraceRead
= NO_TRACE
;
87 public TmfExperimentContext(final TmfExperimentContext other
) {
88 this(other
.cloneContexts());
89 fEvents
= other
.fEvents
;
90 if (other
.getLocation() != null) {
91 setLocation(other
.getLocation().clone());
93 setRank(other
.getRank());
94 setLastTrace(other
.fLastTraceRead
);
98 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
101 public TmfExperimentContext
clone() {
102 TmfExperimentContext clone
= null;
103 clone
= (TmfExperimentContext
) super.clone();
104 clone
.fContexts
= cloneContexts();
105 clone
.fEvents
= cloneEvents();
106 clone
.fLastTraceRead
= fLastTraceRead
;
110 private ITmfContext
[] cloneContexts() {
111 final ITmfContext
[] contexts
= new ITmfContext
[fContexts
.length
];
112 for (int i
= 0; i
< fContexts
.length
; i
++) {
113 contexts
[i
] = (fContexts
[i
] != null) ? fContexts
[i
].clone() : null;
118 private ITmfEvent
[] cloneEvents() {
119 final ITmfEvent
[] events
= new ITmfEvent
[fEvents
.length
];
120 for (int i
= 0; i
< fEvents
.length
; i
++) {
121 events
[i
] = (fEvents
[i
] != null) ? fEvents
[i
].clone() : null;
126 // ------------------------------------------------------------------------
128 // ------------------------------------------------------------------------
130 public ITmfContext
[] getContexts() {
134 public ITmfEvent
[] getEvents() {
138 public int getLastTrace() {
139 return fLastTraceRead
;
142 public void setLastTrace(final int newIndex
) {
143 fLastTraceRead
= newIndex
;
146 // ------------------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 public int hashCode() {
153 for (int i
= 0; i
< fContexts
.length
; i
++) {
154 result
= 37 * result
+ fContexts
[i
].hashCode();
160 public boolean equals(final Object other
) {
164 if (!super.equals(other
)) {
167 if (!(other
instanceof TmfExperimentContext
)) {
170 final TmfExperimentContext o
= (TmfExperimentContext
) other
;
171 boolean isEqual
= true;
173 while (isEqual
&& (i
< fContexts
.length
)) {
174 isEqual
&= fContexts
[i
].equals(o
.fContexts
[i
]);
181 @SuppressWarnings("nls")
182 public String
toString() {
183 StringBuilder sb
= new StringBuilder("TmfExperimentContext [\n");
184 sb
.append("\tfLocation=" + getLocation() + ", fRank=" + getRank() + "\n");
185 sb
.append("\tfContexts=[");
186 for (int i
= 0; i
< fContexts
.length
; i
++) {
187 sb
.append("(" + fContexts
[i
].getLocation() + "," + fContexts
[i
].getRank() + ((i
< fContexts
.length
- 1) ?
")," : ")]\n"));
189 sb
.append("\tfEvents=[");
190 for (int i
= 0; i
< fEvents
.length
; i
++) {
191 ITmfEvent event
= fEvents
[i
];
192 sb
.append(((event
!= null) ? fEvents
[i
].getTimestamp() : "(null)") + ((i
< fEvents
.length
- 1) ?
"," : "]\n"));
194 sb
.append("\tfLastTraceRead=" + fLastTraceRead
+ "\n");
196 return sb
.toString();