// ------------------------------------------------------------------------ // TmfProvider // ------------------------------------------------------------------------ @Override public ITmfContext armRequest(ITmfDataRequest<T> request) { // Tracer.trace("Ctx: Arming request - start"); TmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ? ((ITmfEventRequest<T>) request).getRange().getStartTime() : null; if (TmfTimestamp.BigBang.equals(timestamp) || request.getIndex() > 0) { timestamp = null; // use request index } TmfExperimentContext context = null; if (timestamp != null) { // seek by timestamp context = seekEvent(timestamp); ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank()); } else { // Seek by rank if ((fExperimentContext != null) && fExperimentContext.getRank() == request.getIndex()) { // We are already at the right context -> no need to seek context = fExperimentContext; } else { context = seekEvent(request.getIndex()); } } // Tracer.trace("Ctx: Arming request - done"); return context; }
/* * (non-Javadoc) * * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp) */ @Override public synchronized TmfExperimentContext seekEvent(TmfTimestamp timestamp) { // Tracer.trace("Ctx: seekEvent(TS) - start"); if (timestamp == null) { timestamp = TmfTimestamp.BigBang; } // First, find the right checkpoint int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null)); // In the very likely case that the checkpoint was not found, bsearch // returns its negated would-be location (not an offset...). From that // index, we can then position the stream and get the event. if (index < 0) { index = Math.max(0, -(index + 2)); } // Position the experiment at the checkpoint ITmfLocation<?> location; synchronized (fCheckpoints) { if (fCheckpoints.size() > 0) { if (index >= fCheckpoints.size()) { index = fCheckpoints.size() - 1; } location = fCheckpoints.elementAt(index).getLocation(); } else { location = null; } } TmfExperimentContext context = seekLocation(location); context.setRank((long) index * fIndexPageSize); // And locate the event TmfEvent event = parseEvent(context); while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { getNextEvent(context); event = parseEvent(context); } if (event == null) { context.setLocation(null); context.setRank(ITmfContext.UNKNOWN_RANK); } return context; }
@Override public ITmfLocation<?> getCurrentLocation() { if (fExperimentContext != null) { return fExperimentContext.getLocation(); } return null; }
/* * (non-Javadoc) * * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext) */ @Override public TmfEvent parseEvent(TmfContext context) { // Validate the context if (!(context instanceof TmfExperimentContext)) { return null; // Throw an exception? } if (!context.equals(fExperimentContext)) { // Tracer.trace("Ctx: Restoring context"); seekLocation(context.getLocation()); } TmfExperimentContext expContext = (TmfExperimentContext) context; // If an event was consumed previously, get the next one from that trace int lastTrace = expContext.getLastTrace(); if (lastTrace != TmfExperimentContext.NO_TRACE) { TmfContext traceContext = expContext.getContexts()[lastTrace]; expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext); expContext.setLastTrace(TmfExperimentContext.NO_TRACE); fExperimentContext = (TmfExperimentContext) context; } // Scan the candidate events and identify the "next" trace to read from int trace = TmfExperimentContext.NO_TRACE; TmfTimestamp timestamp = TmfTimestamp.BigCrunch; for (int i = 0; i < expContext.getTraces().length; i++) { TmfEvent event = expContext.getEvents()[i]; if (event != null && event.getTimestamp() != null) { TmfTimestamp otherTS = event.getTimestamp(); if (otherTS.compareTo(timestamp, true) < 0) { trace = i; timestamp = otherTS; } } } TmfEvent event = null; if (trace != TmfExperimentContext.NO_TRACE) { event = expContext.getEvents()[trace]; } return event; }
// Returns a brand new context based on the location provided // and initializes the event queues @Override public synchronized TmfExperimentContext seekLocation(ITmfLocation<?> location) { // Validate the location if (location != null && !(location instanceof TmfExperimentLocation)) { return null; // Throw an exception? } if (fTraces == null) { // experiment has been disposed return null; } // Instantiate the location TmfExperimentLocation expLocation = (location == null) ? new TmfExperimentLocation( new TmfLocationArray(new ITmfLocation<?>[fTraces.length]), new long[fTraces.length]) : (TmfExperimentLocation) location.clone(); // Create and populate the context's traces contexts TmfExperimentContext context = new TmfExperimentContext(fTraces, new TmfContext[fTraces.length]); // Tracer.trace("Ctx: SeekLocation - start"); long rank = 0; for (int i = 0; i < fTraces.length; i++) { // Get the relevant trace attributes ITmfLocation<?> traceLocation = expLocation.getLocation().locations[i]; long traceRank = expLocation.getRanks()[i]; // Set the corresponding sub-context context.getContexts()[i] = fTraces[i].seekLocation(traceLocation); context.getContexts()[i].setRank(traceRank); rank += traceRank; // Set the trace location and read the corresponding event expLocation.getLocation().locations[i] = context.getContexts()[i].getLocation(); context.getEvents()[i] = fTraces[i].getNextEvent(context.getContexts()[i]); } // Tracer.trace("Ctx: SeekLocation - done"); // Finalize context context.setLocation(expLocation); context.setLastTrace(TmfExperimentContext.NO_TRACE); context.setRank(rank); fExperimentContext = context; return context; }
/* * (non-Javadoc) * * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long) */ @Override public synchronized TmfExperimentContext seekEvent(long rank) { // Tracer.trace("Ctx: seekEvent(rank) - start"); // Position the stream at the previous checkpoint int index = (int) rank / fIndexPageSize; ITmfLocation<?> location; synchronized (fCheckpoints) { if (fCheckpoints.size() == 0) { location = null; } else { if (index >= fCheckpoints.size()) { index = fCheckpoints.size() - 1; } location = fCheckpoints.elementAt(index).getLocation(); } } TmfExperimentContext context = seekLocation(location); context.setRank((long) index * fIndexPageSize); // And locate the event TmfEvent event = parseEvent(context); long pos = context.getRank(); while (event != null && pos++ < rank) { getNextEvent(context); event = parseEvent(context); } if (event == null) { context.setLocation(null); context.setRank(ITmfContext.UNKNOWN_RANK); } return context; }
@Override public synchronized TmfEvent getNextEvent(TmfContext context) { // Validate the context if (!(context instanceof TmfExperimentContext)) { return null; // Throw an exception? } if (!context.equals(fExperimentContext)) { // Tracer.trace("Ctx: Restoring context"); fExperimentContext = seekLocation(context.getLocation()); } TmfExperimentContext expContext = (TmfExperimentContext) context; // dumpContext(expContext, true); // If an event was consumed previously, get the next one from that trace int lastTrace = expContext.getLastTrace(); if (lastTrace != TmfExperimentContext.NO_TRACE) { TmfContext traceContext = expContext.getContexts()[lastTrace]; expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext); expContext.setLastTrace(TmfExperimentContext.NO_TRACE); } // Scan the candidate events and identify the "next" trace to read from TmfEvent eventArray[] = expContext.getEvents(); if (eventArray == null) { return null; } int trace = TmfExperimentContext.NO_TRACE; TmfTimestamp timestamp = TmfTimestamp.BigCrunch; if (eventArray.length == 1) { if (eventArray[0] != null) { timestamp = eventArray[0].getTimestamp(); trace = 0; } } else { for (int i = 0; i < eventArray.length; i++) { TmfEvent event = eventArray[i]; if (event != null && event.getTimestamp() != null) { TmfTimestamp otherTS = event.getTimestamp(); if (otherTS.compareTo(timestamp, true) < 0) { trace = i; timestamp = otherTS; } } } } // Update the experiment context and set the "next" event TmfEvent event = null; if (trace != TmfExperimentContext.NO_TRACE) { updateIndex(expContext, timestamp); TmfContext traceContext = expContext.getContexts()[trace]; TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation(); // expLocation.getLocation()[trace] = traceContext.getLocation().clone(); expLocation.getLocation().locations[trace] = traceContext.getLocation(); // updateIndex(expContext, timestamp); expLocation.getRanks()[trace] = traceContext.getRank(); expContext.setLastTrace(trace); expContext.updateRank(1); event = expContext.getEvents()[trace]; fExperimentContext = expContext; } // if (event != null) { // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + // event.getTimestamp().toString()); // dumpContext(expContext, false); // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString()); // } return event; }
/** * Returns the rank of the first event with the requested timestamp. If none, returns the index of * the next event (if any). * * @param timestamp * @return */ @Override public long getRank(TmfTimestamp timestamp) { TmfExperimentContext context = seekEvent(timestamp); return context.getRank(); }