tmf: Handle the history constructions in separate jobs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statesystem / StateSystemManager.java
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.File;
16 import java.io.IOException;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.linuxtools.internal.tmf.core.statesystem.HistoryBuilder;
23 import org.eclipse.linuxtools.internal.tmf.core.statesystem.IStateHistoryBackend;
24 import org.eclipse.linuxtools.internal.tmf.core.statesystem.historytree.HistoryTreeBackend;
25 import org.eclipse.linuxtools.internal.tmf.core.statesystem.historytree.ThreadedHistoryTreeBackend;
26 import org.eclipse.linuxtools.tmf.core.component.TmfComponent;
27 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29
30 /**
31 * This abstract manager class handles loading or creating state history files
32 * for use in TMF's generic state system.
33 *
34 * @author alexmont
35 *
36 */
37 public abstract class StateSystemManager extends TmfComponent {
38
39 /** Size of the blocking queue to use when building a state history */
40 private final static int QUEUE_SIZE = 10000;
41
42 /**
43 * Load the history file matching the target trace. If the file already
44 * exists, it will be opened directly. If not, it will be created from
45 * scratch. In the case the history has to be built, it's possible to block
46 * the calling thread until construction is complete.
47 *
48 * @param htFile
49 * The target name of the history file we want to use. If it
50 * exists it will be opened. If it doesn't, a new file will be
51 * created with this name/path.
52 * @param htInput
53 * The IStateChangeInput to use for building the history file. It
54 * may be required even if we are opening an already-existing
55 * history (ie, for partial histories).
56 * @param waitForCompletion
57 * Should we block the calling thread until the construction is
58 * complete? It has no effect if the file already exists.
59 * @return A IStateSystemQuerier handler to the state system, with which you
60 * can then run queries on the history.
61 * @throws TmfTraceException
62 */
63 public static IStateSystemQuerier loadStateHistory(File htFile,
64 IStateChangeInput htInput, boolean waitForCompletion)
65 throws TmfTraceException {
66 IStateSystemQuerier ss;
67 IStateHistoryBackend htBackend;
68
69 /* If the target file already exists, do not rebuild it uselessly */
70 // TODO for now we assume it's complete. Might be a good idea to check
71 // at least if its range matches the trace's range.
72 if (htFile.exists()) {
73 /* Load an existing history */
74 try {
75 htBackend = new HistoryTreeBackend(htFile);
76 ss = HistoryBuilder.openExistingHistory(htBackend);
77 return ss;
78 } catch (IOException e) {
79 /*
80 * There was an error opening the existing file. Perhaps it was
81 * corrupted, perhaps it's an old version? We'll just
82 * fall-through and try to build a new one from scratch instead.
83 */
84 }
85 }
86
87 /* Create a new state history from scratch */
88 HistoryBuilder builder;
89
90 if (htInput == null) {
91 return null;
92 }
93 try {
94 htBackend = new ThreadedHistoryTreeBackend(htFile,
95 htInput.getStartTime(), QUEUE_SIZE);
96 builder = new HistoryBuilder(htInput, htBackend);
97 } catch (IOException e) {
98 /*
99 * If it fails here however, it means there was a problem writing to
100 * the disk, so throw a real exception this time.
101 */
102 throw new TmfTraceException(e.toString(), e);
103 }
104 startBuilderJob(builder, htInput.getTrace(), waitForCompletion);
105 return builder.getStateSystemQuerier();
106 }
107
108 private static void startBuilderJob(final HistoryBuilder builder,
109 ITmfTrace<?> trace, boolean waitForCompletion) {
110
111 final Job job = new Job("Building state history for " +
112 trace.getName() + "...") { //$NON-NLS-1$
113 @Override
114 protected IStatus run(final IProgressMonitor monitor) {
115 builder.run();
116 monitor.done();
117 // send signal here?
118 return Status.OK_STATUS;
119 }
120 };
121
122 job.schedule();
123 if (waitForCompletion) {
124 try {
125 job.join();
126 } catch (InterruptedException e) {
127 e.printStackTrace();
128 }
129 }
130 }
131 }
This page took 0.038488 seconds and 6 git commands to generate.