| 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<ITmfEvent>(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 | |
| 78 | @Override |
| 79 | public ITmfTrace getTrace() { |
| 80 | return trace; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public long getStartTime() { |
| 85 | return trace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void assignTargetStateSystem(ITmfStateSystemBuilder ssb) { |
| 90 | ss = ssb; |
| 91 | ssAssigned = true; |
| 92 | eventHandlerThread.start(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public ITmfStateSystem getAssignedStateSystem() { |
| 97 | return ss; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void dispose() { |
| 102 | /* Insert a null event in the queue to stop the event handler's thread. */ |
| 103 | try { |
| 104 | eventsQueue.put(CtfTmfEventFactory.getNullEvent()); |
| 105 | eventHandlerThread.join(); |
| 106 | } catch (InterruptedException e) { |
| 107 | e.printStackTrace(); |
| 108 | } |
| 109 | ssAssigned = false; |
| 110 | ss = null; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public final Class<? extends ITmfEvent> getExpectedEventType() { |
| 115 | return eventType; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public final void processEvent(ITmfEvent event) { |
| 120 | /* Make sure the target state system has been assigned */ |
| 121 | if (!ssAssigned) { |
| 122 | System.err.println("Cannot process event without a target state system"); //$NON-NLS-1$ |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | /* Insert the event we're received into the events queue */ |
| 127 | ITmfEvent curEvent = event; |
| 128 | try { |
| 129 | eventsQueue.put(curEvent); |
| 130 | } catch (InterruptedException e) { |
| 131 | e.printStackTrace(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Block the caller until the events queue is empty. |
| 137 | */ |
| 138 | public void waitForEmptyQueue() { |
| 139 | /* |
| 140 | * We will first insert a dummy event that is guaranteed to not modify |
| 141 | * the state. That way, when that event leaves the queue, we will know |
| 142 | * for sure that the state system processed the preceding real event. |
| 143 | */ |
| 144 | TmfTimestamp ts = new TmfTimestamp(0); /* it must not be -1! */ |
| 145 | TmfEvent ev = new TmfEvent(null, ts, null, null, null, null); |
| 146 | |
| 147 | try { |
| 148 | eventsQueue.put(ev); |
| 149 | while (!eventsQueue.isEmpty()) { |
| 150 | Thread.sleep(100); |
| 151 | } |
| 152 | } catch (InterruptedException e) { |
| 153 | e.printStackTrace(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * This is the runner class for the second thread, which will take the |
| 159 | * events from the queue and pass them through the state system. |
| 160 | */ |
| 161 | private class EventProcessor implements Runnable { |
| 162 | |
| 163 | @Override |
| 164 | public void run() { |
| 165 | if (ss == null) { |
| 166 | System.err.println("Cannot run event manager without assigning a target state system first!"); //$NON-NLS-1$ |
| 167 | return; |
| 168 | } |
| 169 | ITmfEvent event; |
| 170 | |
| 171 | try { |
| 172 | event = eventsQueue.take(); |
| 173 | while (event.getTimestamp().getValue() != -1) { |
| 174 | currentEvent = event; |
| 175 | |
| 176 | /* Make sure this is an event the sub-class can process */ |
| 177 | if (eventType.isInstance(event) && event.getType() != null) { |
| 178 | eventHandle(event); |
| 179 | } |
| 180 | event = eventsQueue.take(); |
| 181 | } |
| 182 | /* We've received the last event, clean up */ |
| 183 | closeStateSystem(); |
| 184 | return; |
| 185 | } catch (InterruptedException e) { |
| 186 | /* We've been interrupted abnormally */ |
| 187 | System.out.println("Event handler interrupted!"); //$NON-NLS-1$ |
| 188 | e.printStackTrace(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | private void closeStateSystem() { |
| 193 | /* Close the History system, if there is one */ |
| 194 | if (currentEvent == null) { |
| 195 | return; |
| 196 | } |
| 197 | try { |
| 198 | ss.closeHistory(currentEvent.getTimestamp().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue()); |
| 199 | } catch (TimeRangeException e) { |
| 200 | /* |
| 201 | * Since we're using currentEvent.getTimestamp, this shouldn't |
| 202 | * cause any problem |
| 203 | */ |
| 204 | e.printStackTrace(); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // ------------------------------------------------------------------------ |
| 210 | // Abstract methods |
| 211 | // ------------------------------------------------------------------------ |
| 212 | |
| 213 | /** |
| 214 | * Handle the given event and send the appropriate state transitions into |
| 215 | * the the state system. |
| 216 | * |
| 217 | * This is basically the same thing as IStateChangeInput.processEvent(), |
| 218 | * except here processEvent() and eventHandle() are run in two different |
| 219 | * threads (and the AbstractStateChangeInput takes care of processEvent() |
| 220 | * already). |
| 221 | * |
| 222 | * @param event |
| 223 | * The event to process. If you need a specific event type, you |
| 224 | * should check for its instance right at the beginning. |
| 225 | */ |
| 226 | protected abstract void eventHandle(ITmfEvent event); |
| 227 | } |