| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2013 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 | * Alexandre Montplaisir - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.core.statesystem; |
| 14 | |
| 15 | import java.util.concurrent.ArrayBlockingQueue; |
| 16 | import java.util.concurrent.BlockingQueue; |
| 17 | |
| 18 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEventFactory; |
| 19 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 20 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
| 21 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; |
| 22 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
| 23 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 24 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Instead of using IStateChangeInput directly, one can extend this class, which |
| 29 | * defines a lot of the common functions of the state change input plugin. |
| 30 | * |
| 31 | * It will handle the state-system-processing in a separate thread, which is |
| 32 | * normally not a bad idea for traces of some size. |
| 33 | * |
| 34 | * processEvent() is replaced with eventHandle(), so that all the multi-thread |
| 35 | * logic is abstracted away. |
| 36 | * |
| 37 | * @author Alexandre Montplaisir |
| 38 | * @since 2.0 |
| 39 | */ |
| 40 | public abstract class AbstractTmfStateProvider implements ITmfStateProvider { |
| 41 | |
| 42 | private static final int DEFAULT_EVENTS_QUEUE_SIZE = 10000; |
| 43 | |
| 44 | private final ITmfTrace trace; |
| 45 | private final Class<? extends ITmfEvent> eventType; |
| 46 | private final BlockingQueue<ITmfEvent> eventsQueue; |
| 47 | private final Thread eventHandlerThread; |
| 48 | |
| 49 | private boolean ssAssigned; |
| 50 | private ITmfEvent currentEvent; |
| 51 | |
| 52 | /** State system in which to insert the state changes */ |
| 53 | protected ITmfStateSystemBuilder ss = null; |
| 54 | |
| 55 | /** |
| 56 | * Instantiate a new state provider plugin. |
| 57 | * |
| 58 | * @param trace |
| 59 | * The LTTng 2.0 kernel trace directory |
| 60 | * @param eventType |
| 61 | * The specific class for the event type that will be used within |
| 62 | * the subclass |
| 63 | * @param id |
| 64 | * Name given to this state change input. Only used internally. |
| 65 | */ |
| 66 | public AbstractTmfStateProvider(ITmfTrace trace, |
| 67 | Class<? extends ITmfEvent> eventType, String id) { |
| 68 | this.trace = trace; |
| 69 | this.eventType = eventType; |
| 70 | eventsQueue = new ArrayBlockingQueue<>(DEFAULT_EVENTS_QUEUE_SIZE); |
| 71 | ssAssigned = false; |
| 72 | |
| 73 | String id2 = (id == null ? "Unamed" : id); //$NON-NLS-1$ |
| 74 | eventHandlerThread = new Thread(new EventProcessor(), id2 + " Event Handler"); //$NON-NLS-1$ |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public ITmfTrace getTrace() { |
| 79 | return trace; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public long getStartTime() { |
| 84 | return trace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void assignTargetStateSystem(ITmfStateSystemBuilder ssb) { |
| 89 | ss = ssb; |
| 90 | ssAssigned = true; |
| 91 | eventHandlerThread.start(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public ITmfStateSystem getAssignedStateSystem() { |
| 96 | return ss; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void dispose() { |
| 101 | /* Insert a null event in the queue to stop the event handler's thread. */ |
| 102 | try { |
| 103 | eventsQueue.put(CtfTmfEventFactory.getNullEvent()); |
| 104 | eventHandlerThread.join(); |
| 105 | } catch (InterruptedException e) { |
| 106 | e.printStackTrace(); |
| 107 | } |
| 108 | ssAssigned = false; |
| 109 | ss = null; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public final Class<? extends ITmfEvent> getExpectedEventType() { |
| 114 | return eventType; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public final void processEvent(ITmfEvent event) { |
| 119 | /* Make sure the target state system has been assigned */ |
| 120 | if (!ssAssigned) { |
| 121 | System.err.println("Cannot process event without a target state system"); //$NON-NLS-1$ |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | /* Insert the event we're received into the events queue */ |
| 126 | ITmfEvent curEvent = event; |
| 127 | try { |
| 128 | eventsQueue.put(curEvent); |
| 129 | } catch (InterruptedException e) { |
| 130 | e.printStackTrace(); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Block the caller until the events queue is empty. |
| 136 | */ |
| 137 | public void waitForEmptyQueue() { |
| 138 | /* |
| 139 | * We will first insert a dummy event that is guaranteed to not modify |
| 140 | * the state. That way, when that event leaves the queue, we will know |
| 141 | * for sure that the state system processed the preceding real event. |
| 142 | */ |
| 143 | TmfEvent ev = new EmptyEvent(); |
| 144 | |
| 145 | try { |
| 146 | eventsQueue.put(ev); |
| 147 | while (!eventsQueue.isEmpty()) { |
| 148 | Thread.sleep(100); |
| 149 | } |
| 150 | } catch (InterruptedException e) { |
| 151 | e.printStackTrace(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // ------------------------------------------------------------------------ |
| 156 | // Inner classes |
| 157 | // ------------------------------------------------------------------------ |
| 158 | |
| 159 | /** |
| 160 | * Empty event that should be totally ignored by the event handler. It can |
| 161 | * by used for synchronisation purposes. |
| 162 | */ |
| 163 | private class EmptyEvent extends TmfEvent { |
| 164 | public EmptyEvent() { |
| 165 | super(null, new TmfTimestamp(0), null, null, null, null); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * This is the runner class for the second thread, which will take the |
| 171 | * events from the queue and pass them through the state system. |
| 172 | */ |
| 173 | private class EventProcessor implements Runnable { |
| 174 | |
| 175 | @Override |
| 176 | public void run() { |
| 177 | if (ss == null) { |
| 178 | System.err.println("Cannot run event manager without assigning a target state system first!"); //$NON-NLS-1$ |
| 179 | return; |
| 180 | } |
| 181 | ITmfEvent event; |
| 182 | |
| 183 | try { |
| 184 | event = eventsQueue.take(); |
| 185 | while (event.getTimestamp().getValue() != -1) { |
| 186 | if (event instanceof EmptyEvent) { |
| 187 | /* Synchronization event, should be ignored */ |
| 188 | event = eventsQueue.take(); |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | currentEvent = event; |
| 193 | |
| 194 | /* Make sure this is an event the sub-class can process */ |
| 195 | if (eventType.isInstance(event) && event.getType() != null) { |
| 196 | eventHandle(event); |
| 197 | } |
| 198 | event = eventsQueue.take(); |
| 199 | } |
| 200 | /* We've received the last event, clean up */ |
| 201 | closeStateSystem(); |
| 202 | } catch (InterruptedException e) { |
| 203 | /* We've been interrupted abnormally */ |
| 204 | System.out.println("Event handler interrupted!"); //$NON-NLS-1$ |
| 205 | e.printStackTrace(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private void closeStateSystem() { |
| 210 | /* Close the History system, if there is one */ |
| 211 | if (currentEvent == null) { |
| 212 | return; |
| 213 | } |
| 214 | try { |
| 215 | ss.closeHistory(currentEvent.getTimestamp().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue()); |
| 216 | } catch (TimeRangeException e) { |
| 217 | /* |
| 218 | * Since we're using currentEvent.getTimestamp, this shouldn't |
| 219 | * cause any problem |
| 220 | */ |
| 221 | e.printStackTrace(); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // ------------------------------------------------------------------------ |
| 227 | // Abstract methods |
| 228 | // ------------------------------------------------------------------------ |
| 229 | |
| 230 | /** |
| 231 | * Handle the given event and send the appropriate state transitions into |
| 232 | * the the state system. |
| 233 | * |
| 234 | * This is basically the same thing as IStateChangeInput.processEvent(), |
| 235 | * except here processEvent() and eventHandle() are run in two different |
| 236 | * threads (and the AbstractStateChangeInput takes care of processEvent() |
| 237 | * already). |
| 238 | * |
| 239 | * @param event |
| 240 | * The event to process. If you need a specific event type, you |
| 241 | * should check for its instance right at the beginning. |
| 242 | */ |
| 243 | protected abstract void eventHandle(ITmfEvent event); |
| 244 | } |