Commit | Line | Data |
---|---|---|
4ab33d2b AO |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
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 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard (fchouinard@gmail.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.eventlog; | |
14 | ||
15 | import java.io.IOException; | |
16 | import java.util.Vector; | |
17 | ||
18 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
1f506a43 | 19 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; |
4ab33d2b AO |
20 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; |
21 | ||
22 | /** | |
23 | * <b><u>TmfEventLog</u></b> | |
24 | * <p> | |
25 | * TmfEventLog represents a time-ordered set of events tied to a single event | |
26 | * stream. It keeps track of the global information about the event log: | |
27 | * <ul> | |
28 | * <li> the epoch, a reference timestamp for the whole log (t0) | |
29 | * <li> the span of the log i.e. the timestamps range | |
30 | * <li> the total number of events | |
31 | * </ul> | |
32 | * As an ITmfRequestHandler, it provides an implementation of process() | |
33 | * which handles event requests. | |
34 | * <p> | |
35 | * TODO: Handle concurrent and possibly overlapping requests in a way that | |
36 | * optimizes the stream access and event parsing. | |
37 | */ | |
78acf24d | 38 | public class TmfTrace implements ITmfRequestHandler { |
4ab33d2b AO |
39 | |
40 | // ======================================================================== | |
41 | // Attributes | |
42 | // ======================================================================== | |
43 | ||
44 | private final String fId; | |
45 | private final TmfEventStream fStream; | |
46 | private final TmfTimestamp fEpoch; | |
47 | ||
48 | // ======================================================================== | |
49 | // Constructors | |
50 | // ======================================================================== | |
51 | ||
78acf24d | 52 | public TmfTrace(String id, TmfEventStream stream) { |
4ab33d2b AO |
53 | assert stream != null; |
54 | fId = id; | |
55 | fStream = stream; | |
56 | fEpoch = TmfTimestamp.BigBang; | |
57 | } | |
58 | ||
78acf24d | 59 | public TmfTrace(String id, TmfEventStream stream, TmfTimestamp epoch) { |
4ab33d2b AO |
60 | assert stream != null; |
61 | fId = id; | |
62 | fStream = stream; | |
63 | fEpoch = epoch; | |
64 | } | |
65 | ||
66 | // ======================================================================== | |
67 | // Accessors | |
68 | // ======================================================================== | |
69 | ||
70 | public String getId() { | |
71 | return fId; | |
72 | } | |
73 | ||
74 | public TmfEventStream getStream() { | |
75 | return fStream; | |
76 | } | |
77 | ||
78 | public TmfTimestamp getEpoch() { | |
79 | return fEpoch; | |
80 | } | |
81 | ||
1f506a43 | 82 | public TmfTimeRange getTimeRange() { |
4ab33d2b AO |
83 | return fStream.getTimeRange(); |
84 | } | |
85 | ||
86 | public int getNbEvents() { | |
87 | return fStream.getNbEvents(); | |
88 | } | |
89 | ||
90 | // ======================================================================== | |
91 | // Operators | |
92 | // ======================================================================== | |
93 | ||
94 | /* (non-Javadoc) | |
95 | * @see org.eclipse.linuxtools.tmf.eventlog.ITmfRequestExecutor#execute(org.eclipse.linuxtools.tmf.eventlog.TmfEventRequest, boolean) | |
96 | */ | |
4ab33d2b AO |
97 | public void process(final TmfEventRequest request, boolean waitForCompletion) { |
98 | serviceEventRequest(request); | |
99 | if (waitForCompletion) { | |
100 | request.waitForCompletion(); | |
101 | } | |
102 | } | |
103 | ||
104 | // ======================================================================== | |
105 | // Helper functions | |
106 | // ======================================================================== | |
107 | ||
108 | /* (non-Javadoc) | |
109 | * | |
110 | * @param request | |
111 | */ | |
112 | private void serviceEventRequest(final TmfEventRequest request) { | |
113 | Thread thread = new Thread() { | |
114 | @Override | |
115 | public void run() { | |
116 | TmfTimestamp startTime = request.getRange().getStartTime(); | |
117 | TmfTimestamp endTime = request.getRange().getEndTime(); | |
118 | int blockSize = request.getBlockize(); | |
119 | ||
120 | int nbRequestedEvents = request.getNbRequestedEvents(); | |
121 | if (nbRequestedEvents == -1) { | |
122 | nbRequestedEvents = Integer.MAX_VALUE; | |
123 | } | |
124 | ||
125 | Vector<TmfEvent> events = new Vector<TmfEvent>(); | |
126 | int nbEvents = 0; | |
127 | try { | |
128 | TmfEvent event = fStream.seek(startTime); | |
129 | while (!request.isCancelled() && nbEvents < nbRequestedEvents && event != null && | |
130 | event.getTimestamp().compareTo(endTime, false) <= 0 ) | |
131 | { | |
132 | events.add(event); | |
133 | if (++nbEvents % blockSize == 0) { | |
134 | request.newEvents(events); | |
135 | events.removeAllElements(); | |
136 | } | |
137 | event = fStream.getNextEvent(); | |
138 | } | |
139 | request.newEvents(events); | |
140 | request.done(); | |
141 | } catch (IOException e) { | |
142 | // TODO Auto-generated catch block | |
143 | e.printStackTrace(); | |
144 | } | |
145 | } | |
146 | }; | |
147 | thread.start(); | |
148 | } | |
149 | ||
150 | } |