Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statistics / TmfStateStatistics.java
CommitLineData
200789b3
AM
1/*******************************************************************************
2 * Copyright (c) 2012 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
13package org.eclipse.linuxtools.tmf.core.statistics;
14
15import java.io.File;
16import java.util.HashMap;
f3f93fa6 17import java.util.LinkedList;
200789b3
AM
18import java.util.List;
19import java.util.Map;
20
21import org.eclipse.core.resources.IResource;
22import org.eclipse.core.runtime.CoreException;
23import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
200789b3 24import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
96345c5a 25import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
200789b3
AM
26import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
27import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
28import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
29import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
1c0de632
AM
30import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
31import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
32import org.eclipse.linuxtools.tmf.core.signal.TmfStatsUpdatedSignal;
200789b3
AM
33import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput;
34import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
35import org.eclipse.linuxtools.tmf.core.statesystem.StateSystemManager;
36import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
37
38/**
1c0de632
AM
39 * Implementation of ITmfStatistics which uses a state history for storing its
40 * information.
41 *
42 * It requires building the history first, but gives very fast response times
43 * when built : Queries are O(log n) wrt the size of the trace, and O(1) wrt to
44 * the size of the time interval selected.
200789b3
AM
45 *
46 * @author Alexandre Montplaisir
47 * @since 2.0
48 */
1c0de632 49public class TmfStateStatistics implements ITmfStatistics {
200789b3 50
a76f1067
AM
51 /** ID for the statistics state system */
52 public static final String STATE_ID = "org.eclipse.linuxtools.tmf.statistics"; //$NON-NLS-1$
53
1c0de632 54 /** Filename the "statistics state history" file will have */
200789b3
AM
55 private static final String STATS_STATE_FILENAME = "statistics.ht"; //$NON-NLS-1$
56
1c0de632
AM
57 private final ITmfTrace trace;
58
59 /**
200789b3
AM
60 * The state system that's used to stored the statistics. It's hidden from
61 * the trace, so that it doesn't conflict with ITmfTrace.getStateSystem()
62 * (which is something else!)
63 */
64 private final ITmfStateSystem stats;
65
36033ff0
AM
66 /**
67 * Empty constructor. The resulting TmfStatistics object will not be usable,
68 * but it might be needed for sub-classes.
69 */
1c0de632 70 public TmfStateStatistics() {
89c06060 71 stats = null;
1c0de632 72 trace = null;
89c06060
AM
73 }
74
200789b3
AM
75 /**
76 * Constructor
77 *
78 * @param trace
79 * The trace for which we build these statistics
80 * @throws TmfTraceException
81 * If something went wrong trying to initialize the statistics
82 */
1c0de632 83 public TmfStateStatistics(ITmfTrace trace) throws TmfTraceException {
200789b3 84 /* Set up the path to the history tree file we'll use */
1c0de632 85 this.trace = trace;
200789b3
AM
86 IResource resource = trace.getResource();
87 String supplDirectory = null;
88
89 try {
90 // get the directory where the history file will be stored.
91 supplDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
92 } catch (CoreException e) {
93 throw new TmfTraceException(e.toString(), e);
94 }
95
96 final File htFile = new File(supplDirectory + File.separator + STATS_STATE_FILENAME);
97 final IStateChangeInput htInput = new StatsStateProvider(trace);
98
a76f1067 99 this.stats = StateSystemManager.loadStateHistory(htFile, htInput, STATE_ID, false);
200789b3
AM
100 }
101
e1c43333
AM
102 /**
103 * Manual constructor. This should be used if the trace's Resource is null
104 * (ie, for unit tests). It requires specifying the location of the history
105 * file manually.
106 *
107 * @param trace
108 * The trace for which we build these statistics
109 * @param historyFile
110 * The location of the state history file to build for the stats
111 * @throws TmfTraceException
112 * If the file could not be written to
113 */
114 public TmfStateStatistics(ITmfTrace trace, File historyFile) throws TmfTraceException {
115 this.trace = trace;
116 final IStateChangeInput htInput = new StatsStateProvider(trace);
117 this.stats = StateSystemManager.loadStateHistory(historyFile, htInput, STATE_ID, true);
118 }
119
200789b3 120 // ------------------------------------------------------------------------
1c0de632 121 // ITmfStatistics
200789b3
AM
122 // ------------------------------------------------------------------------
123
1a4205d9
AM
124 @Override
125 public void dispose() {
126 stats.dispose();
127 }
128
1c0de632 129 @Override
8b260d9f
AM
130 public void updateStats(final boolean isGlobal, final long start,
131 final long end) {
1c0de632
AM
132 /*
133 * Since we are currently in a signal handler (ie, in the UI thread),
134 * and since state system queries can be arbitrarily long (O(log n) wrt
135 * the size of the trace), we will run those queries in a separate
136 * thread and update the statistics view out-of-band.
137 */
138 Thread statsThread = new Thread("Statistics update") { //$NON-NLS-1$
139 @Override
140 public void run() {
141 long total;
142 Map<String, Long> map;
143
144 /* Wait until the history building completed */
1a4205d9
AM
145 if (!stats.waitUntilBuilt()) {
146 return;
147 }
1c0de632
AM
148
149 /* Range should be valid for both global and time range queries */
150 total = getEventsInRange(start, end);
151 map = getEventTypesInRange(start, end);
152
153 /* Send the signal to notify the stats viewer to update its display */
154 TmfSignal sig = new TmfStatsUpdatedSignal(this, trace, isGlobal, total, map);
155 TmfSignalManager.dispatchSignal(sig);
156 }
157 };
158 statsThread.start();
159 return;
f3f93fa6
AM
160 }
161
162 @Override
163 public List<Long> histogramQuery(final long start, final long end, final int nb) {
164 final List<Long> list = new LinkedList<Long>();
165 final long increment = (end - start) / nb;
166
167 /* Wait until the history building completed */
168 if (!stats.waitUntilBuilt()) {
169 return null;
170 }
171
172 /*
173 * We will do one state system query per "border", and save the
174 * differences between each border.
175 */
176 long prevTotal = (start == stats.getStartTime()) ? 0 : getEventCountAt(start);
177 long curTime = start + increment;
178
179 long curTotal, count;
180 for (int i = 0; i < nb - 1; i++) {
181 curTotal = getEventCountAt(curTime);
182 count = curTotal - prevTotal;
183 list.add(count);
184
185 curTime += increment;
186 prevTotal = curTotal;
187 }
188
189 /*
190 * For the last bucket, we'll stretch its end time to the end time of
191 * the requested range, in case it got truncated down.
192 */
193 curTotal = getEventCountAt(end);
194 count = curTotal - prevTotal;
195 list.add(count);
1c0de632 196
f3f93fa6 197 return list;
1c0de632
AM
198 }
199
200789b3
AM
200 @Override
201 public long getEventsTotal() {
e1c43333
AM
202 /* We need the complete state history to be built to answer this. */
203 stats.waitUntilBuilt();
204
1c0de632 205 long endTime = stats.getCurrentEndTime();
e1c43333 206 int count = 0;
df310609
AM
207
208 try {
209 final int quark = stats.getQuarkAbsolute(Attributes.TOTAL);
e1c43333 210 count= stats.querySingleState(endTime, quark).getStateValue().unboxInt();
1c0de632
AM
211
212 } catch (TimeRangeException e) {
213 /* Assume there is no events for that range */
214 return 0;
df310609
AM
215 } catch (AttributeNotFoundException e) {
216 e.printStackTrace();
217 } catch (StateValueTypeException e) {
218 e.printStackTrace();
96345c5a
AM
219 } catch (StateSystemDisposedException e) {
220 e.printStackTrace();
200789b3 221 }
df310609 222
e1c43333 223 return count;
200789b3
AM
224 }
225
226 @Override
227 public Map<String, Long> getEventTypesTotal() {
e1c43333
AM
228 /* We need the complete state history to be built to answer this. */
229 stats.waitUntilBuilt();
230
200789b3 231 Map<String, Long> map = new HashMap<String, Long>();
e1c43333 232 long endTime = stats.getCurrentEndTime();
200789b3
AM
233
234 try {
235 /* Get the list of quarks, one for each even type in the database */
236 int quark = stats.getQuarkAbsolute(Attributes.EVENT_TYPES);
237 List<Integer> quarks = stats.getSubAttributes(quark, false);
238
239 /* Since we want the total we can look only at the end */
240 List<ITmfStateInterval> endState = stats.queryFullState(endTime);
241
242 String curEventName;
243 long eventCount;
244 for (int typeQuark : quarks) {
245 curEventName = stats.getAttributeName(typeQuark);
246 eventCount = endState.get(typeQuark).getStateValue().unboxInt();
247 map.put(curEventName, eventCount);
248 }
249
250 } catch (TimeRangeException e) {
1c0de632 251 /* Assume there is no events, nothing will be put in the map. */
200789b3
AM
252 } catch (AttributeNotFoundException e) {
253 e.printStackTrace();
254 } catch (StateValueTypeException e) {
255 e.printStackTrace();
96345c5a
AM
256 } catch (StateSystemDisposedException e) {
257 e.printStackTrace();
200789b3
AM
258 }
259 return map;
260 }
261
262 @Override
8b260d9f 263 public long getEventsInRange(long start, long end) {
e1c43333
AM
264 // FIXME Instead of waiting until the end, we could check the current
265 // end time, and answer as soon as possible...
266 stats.waitUntilBuilt();
267
f3f93fa6
AM
268 long startCount;
269 if (start == stats.getStartTime()) {
270 startCount = 0;
271 } else {
272 /*
273 * We want the events happening at "start" to be included, so we'll
274 * need to query one unit before that point.
275 */
276 startCount = getEventCountAt(start - 1);
200789b3 277 }
f3f93fa6 278 long endCount = getEventCountAt(end);
df310609 279
f3f93fa6 280 return endCount - startCount;
200789b3
AM
281 }
282
283 @Override
8b260d9f 284 public Map<String, Long> getEventTypesInRange(long start, long end) {
e1c43333
AM
285 // FIXME Instead of waiting until the end, we could check the current
286 // end time, and answer as soon as possible...
287 stats.waitUntilBuilt();
288
200789b3
AM
289 Map<String, Long> map = new HashMap<String, Long>();
290
291 /* Make sure the start/end times are within the state history, so we
292 * don't get TimeRange exceptions.
293 */
1c0de632
AM
294 long startTime = checkStartTime(start);
295 long endTime = checkEndTime(end);
200789b3
AM
296
297 try {
298 /* Get the list of quarks, one for each even type in the database */
299 int quark = stats.getQuarkAbsolute(Attributes.EVENT_TYPES);
300 List<Integer> quarks = stats.getSubAttributes(quark, false);
301
1c0de632 302 List<ITmfStateInterval> endState = stats.queryFullState(endTime);
200789b3 303
200789b3
AM
304 String curEventName;
305 long countAtStart, countAtEnd, eventCount;
200789b3 306
e1c43333
AM
307 if (startTime == stats.getStartTime()) {
308 /* Only use the values picked up at the end time */
309 for (int typeQuark : quarks) {
310 curEventName = stats.getAttributeName(typeQuark);
311 eventCount = endState.get(typeQuark).getStateValue().unboxInt();
312 if (eventCount == -1) {
313 eventCount = 0;
314 }
315 map.put(curEventName, eventCount);
200789b3 316 }
e1c43333 317 } else {
200789b3 318 /*
e1c43333
AM
319 * Query the start time at -1, so the beginning of the interval
320 * is inclusive.
200789b3 321 */
e1c43333
AM
322 List<ITmfStateInterval> startState = stats.queryFullState(startTime - 1);
323 for (int typeQuark : quarks) {
324 curEventName = stats.getAttributeName(typeQuark);
325 countAtStart = startState.get(typeQuark).getStateValue().unboxInt();
326 countAtEnd = endState.get(typeQuark).getStateValue().unboxInt();
327
328 if (countAtStart == -1) {
329 countAtStart = 0;
330 }
331 if (countAtEnd == -1) {
332 countAtEnd = 0;
333 }
334 eventCount = countAtEnd - countAtStart;
335 map.put(curEventName, eventCount);
200789b3 336 }
200789b3 337 }
e1c43333 338
200789b3 339 } catch (TimeRangeException e) {
1c0de632 340 /* Assume there is no events, nothing will be put in the map. */
200789b3
AM
341 } catch (AttributeNotFoundException e) {
342 /*
1c0de632
AM
343 * These other exception types would show a logic problem however,
344 * so they should not happen.
200789b3
AM
345 */
346 e.printStackTrace();
347 } catch (StateValueTypeException e) {
348 e.printStackTrace();
96345c5a
AM
349 } catch (StateSystemDisposedException e) {
350 e.printStackTrace();
200789b3
AM
351 }
352 return map;
353 }
354
f3f93fa6
AM
355 private long getEventCountAt(long timestamp) {
356 /* Make sure the target time is within the range of the history */
357 long ts = checkStartTime(timestamp);
358 ts = checkEndTime(ts);
359
360 try {
361 final int quark = stats.getQuarkAbsolute(Attributes.TOTAL);
362 long count = stats.querySingleState(ts, quark).getStateValue().unboxInt();
363 return count;
364
365 } catch (TimeRangeException e) {
366 /* Assume there is no events for that range */
367 } catch (AttributeNotFoundException e) {
368 e.printStackTrace();
369 } catch (StateValueTypeException e) {
370 e.printStackTrace();
371 } catch (StateSystemDisposedException e) {
372 e.printStackTrace();
373 }
374
375 return 0;
376 }
377
8b260d9f
AM
378 private long checkStartTime(long initialStart) {
379 long start = initialStart;
200789b3
AM
380 if (start < stats.getStartTime()) {
381 return stats.getStartTime();
382 }
383 return start;
384 }
385
8b260d9f
AM
386 private long checkEndTime(long initialEnd) {
387 long end = initialEnd;
200789b3
AM
388 if (end > stats.getCurrentEndTime()) {
389 return stats.getCurrentEndTime();
390 }
391 return end;
392 }
393
394
395 /**
396 * The attribute names that are used in the state provider
397 */
398 public static class Attributes {
399
df310609
AM
400 /** Total nb of events */
401 public static final String TOTAL = "total"; //$NON-NLS-1$
402
200789b3
AM
403 /** event_types */
404 public static final String EVENT_TYPES = "event_types"; //$NON-NLS-1$<
405 }
406}
This page took 0.04265 seconds and 5 git commands to generate.