| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012 Ericsson |
| 3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal |
| 4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> |
| 5 | * |
| 6 | * All rights reserved. This program and the accompanying materials are |
| 7 | * made available under the terms of the Eclipse Public License v1.0 which |
| 8 | * accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.core.statesystem; |
| 14 | |
| 15 | import java.io.IOException; |
| 16 | import java.util.concurrent.Semaphore; |
| 17 | |
| 18 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateHistorySystem; |
| 19 | import org.eclipse.linuxtools.tmf.core.component.TmfComponent; |
| 20 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; |
| 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 22 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
| 23 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
| 24 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
| 25 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; |
| 26 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; |
| 27 | import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal; |
| 28 | import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal; |
| 29 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; |
| 30 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
| 31 | |
| 32 | /** |
| 33 | * This is the high-level wrapper around the State History and its input and |
| 34 | * storage plugins. Just create the object using the constructor then .run() |
| 35 | * |
| 36 | * You can use one HistoryBuilder and it will instantiate everything underneath. |
| 37 | * If you need more fine-grained control you can still ignore this and |
| 38 | * instantiate everything manually. |
| 39 | * |
| 40 | * @author alexmont |
| 41 | * |
| 42 | */ |
| 43 | public class HistoryBuilder extends TmfComponent { |
| 44 | |
| 45 | private static final Semaphore hbSem = new Semaphore(1); |
| 46 | |
| 47 | private ITmfEventRequest<CtfTmfEvent> currentRequest = null; |
| 48 | private boolean isRunning = false; |
| 49 | |
| 50 | private final IStateChangeInput sci; |
| 51 | private final StateHistorySystem shs; |
| 52 | private final IStateHistoryBackend hb; |
| 53 | |
| 54 | /** |
| 55 | * Instantiate a new HistoryBuilder helper. |
| 56 | * |
| 57 | * @param stateChangeInput |
| 58 | * The input plugin to use. This is required. |
| 59 | * @param backend |
| 60 | * The backend storage to use. Use "null" here if you want a |
| 61 | * state system with no history. |
| 62 | * @throws IOException |
| 63 | * Is thrown if anything went wrong (usually with the storage |
| 64 | * backend) |
| 65 | */ |
| 66 | public HistoryBuilder(IStateChangeInput stateChangeInput, |
| 67 | IStateHistoryBackend backend) throws IOException { |
| 68 | if (stateChangeInput == null || backend == null) { |
| 69 | throw new IllegalArgumentException(); |
| 70 | } |
| 71 | sci = stateChangeInput; |
| 72 | hb = backend; |
| 73 | shs = new StateHistorySystem(hb, true); |
| 74 | |
| 75 | sci.assignTargetStateSystem(shs); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Factory-style method to open an existing history, you only have to |
| 80 | * provide the already-instantiated IStateHistoryBackend object. |
| 81 | * |
| 82 | * @param hb |
| 83 | * The history-backend object |
| 84 | * @return A IStateSystemBuilder reference to the new state system. If you |
| 85 | * will only run queries on this history, you should *definitely* |
| 86 | * cast it to IStateSystemQuerier. |
| 87 | * @throws IOException |
| 88 | * If there was something wrong. |
| 89 | */ |
| 90 | public static IStateSystemBuilder openExistingHistory( |
| 91 | IStateHistoryBackend hb) throws IOException { |
| 92 | return new StateHistorySystem(hb, false); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Return a read/write reference to the state system object that was |
| 97 | * created. |
| 98 | * |
| 99 | * @return Reference to the state system, with access to everything. |
| 100 | */ |
| 101 | public IStateSystemBuilder getStateSystemBuilder() { |
| 102 | return shs; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Return a read-only reference to the state system object that was created. |
| 107 | * |
| 108 | * @return Reference to the state system, but only with the query methods |
| 109 | * available. |
| 110 | */ |
| 111 | public IStateSystemQuerier getStateSystemQuerier() { |
| 112 | return shs; |
| 113 | } |
| 114 | |
| 115 | // ------------------------------------------------------------------------ |
| 116 | // Signal handlers |
| 117 | // ------------------------------------------------------------------------ |
| 118 | |
| 119 | /** |
| 120 | * Signal handler to start building the configured state history. |
| 121 | * |
| 122 | * This could also be called by anyone to trigger the building of the |
| 123 | * history without using any TMF signals. Simply pass 'null' as a parameter |
| 124 | * then. |
| 125 | * |
| 126 | * @param signal The signal that triggered the build |
| 127 | */ |
| 128 | @TmfSignalHandler |
| 129 | public void startBuilding(final TmfExperimentRangeUpdatedSignal signal) { |
| 130 | /* Start the construction of the history if it's not started yet */ |
| 131 | if (!this.isRunning) { |
| 132 | hbSem.acquireUninterruptibly(); |
| 133 | currentRequest = new StateSystemBuildRequest(this); |
| 134 | isRunning = true; |
| 135 | sci.getTrace().sendRequest(currentRequest); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Signal handler to cancel any currently running requests. It will delete |
| 141 | * any incomplete file that might have been created. |
| 142 | * |
| 143 | * This could also be called by anyone to cancel the current request without |
| 144 | * using any TMF signals. Simply pass 'null' as a parameter then. |
| 145 | * |
| 146 | * @param signal |
| 147 | */ |
| 148 | @TmfSignalHandler |
| 149 | public void cancelCurrentBuild(final TmfExperimentSelectedSignal<? extends ITmfEvent> signal) { |
| 150 | /* |
| 151 | * We've switched experiments (or re-opened the current one), so stop |
| 152 | * whatever request is currently running. |
| 153 | */ |
| 154 | if (this.isRunning && currentRequest != null) { |
| 155 | currentRequest.cancel(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | // ------------------------------------------------------------------------ |
| 161 | // Methods reserved for the request object below |
| 162 | // ------------------------------------------------------------------------ |
| 163 | |
| 164 | /** Get the input plugin object */ |
| 165 | IStateChangeInput getInputPlugin() { |
| 166 | return sci; |
| 167 | } |
| 168 | |
| 169 | /** Shutdown this builder object when the request is over */ |
| 170 | void finish(boolean deleteFile) { |
| 171 | sci.dispose(); |
| 172 | if (deleteFile) { |
| 173 | hb.removeFiles(); |
| 174 | } |
| 175 | currentRequest = null; |
| 176 | isRunning = false; |
| 177 | TmfSignalManager.deregister(this); |
| 178 | hbSem.release(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | class StateSystemBuildRequest extends TmfEventRequest<CtfTmfEvent> { |
| 184 | |
| 185 | /** The amount of events queried at a time through the requests */ |
| 186 | private final static int chunkSize = 50000; |
| 187 | |
| 188 | private final HistoryBuilder builder; |
| 189 | private final IStateChangeInput sci; |
| 190 | |
| 191 | StateSystemBuildRequest(HistoryBuilder builder) { |
| 192 | super((Class<CtfTmfEvent>) builder.getInputPlugin().getExpectedEventType().getClass(), |
| 193 | TmfTimeRange.ETERNITY, TmfDataRequest.ALL_DATA, chunkSize, |
| 194 | ITmfDataRequest.ExecutionType.BACKGROUND); |
| 195 | this.builder = builder; |
| 196 | this.sci = builder.getInputPlugin(); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void handleData(final CtfTmfEvent event) { |
| 201 | super.handleData(event); |
| 202 | if (event != null) { |
| 203 | sci.processEvent(event); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void handleSuccess() { |
| 209 | super.handleSuccess(); |
| 210 | builder.finish(false); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void handleCancel() { |
| 215 | super.handleCancel(); |
| 216 | builder.finish(true); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void handleFailure() { |
| 221 | super.handleFailure(); |
| 222 | builder.finish(true); |
| 223 | } |
| 224 | } |