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