1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 ******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.statistics
;
16 import java
.util
.HashMap
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateSystemDisposedException
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignal
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfStatsUpdatedSignal
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateProvider
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.TmfStateSystemFactory
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
37 * Implementation of ITmfStatistics which uses a state history for storing its
38 * information. In reality, it uses two state histories, one for "event totals"
39 * information (which should ideally use a fast backend), and another one for
40 * the rest (per event type, per CPU, etc.).
42 * Compared to the event-request-based statistics calculations, it adds the
43 * building the history first, but gives much faster response times once built :
44 * Queries are O(log n) wrt the size of the trace, and O(1) wrt to the size of
45 * the time interval selected.
47 * @author Alexandre Montplaisir
50 public class TmfStateStatistics
implements ITmfStatistics
{
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * @deprecated Do not use, it's been replaced by {@link #TOTALS_STATE_ID}
58 * and {@link #TYPES_STATE_ID}
61 public static final String STATE_ID
= "org.eclipse.linuxtools.tmf.statistics"; //$NON-NLS-1$
63 /** ID for the "event totals" statistics state system
65 public static final String TOTALS_STATE_ID
= "org.eclipse.linuxtools.tmf.statistics.totals"; //$NON-NLS-1$
67 /** ID for the "event types" statistics state system
69 public static final String TYPES_STATE_ID
= "org.eclipse.linuxtools.tmf.statistics.types"; //$NON-NLS-1$
71 /** Filename for the "event totals" state history file */
72 private static final String TOTALS_STATE_FILENAME
= "statistics-totals.ht"; //$NON-NLS-1$
74 /** Filename for the "event types" state history file */
75 private static final String TYPES_STATE_FILENAME
= "statistics-types.ht"; //$NON-NLS-1$
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 private final ITmfTrace trace
;
83 /** The event totals state system */
84 private final ITmfStateSystem totalsStats
;
86 /** The state system for event types */
87 private final ITmfStateSystem typesStats
;
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 * Empty constructor. The resulting TmfStatistics object will not be usable,
95 * but it might be needed for sub-classes.
97 public TmfStateStatistics() {
107 * The trace for which we build these statistics
108 * @throws TmfTraceException
109 * If something went wrong trying to initialize the statistics
111 public TmfStateStatistics(ITmfTrace trace
) throws TmfTraceException
{
113 String directory
= TmfTraceManager
.getSupplementaryFileDir(trace
);
115 final File totalsFile
= new File(directory
+ TOTALS_STATE_FILENAME
);
116 final ITmfStateProvider totalsInput
= new StatsProviderTotals(trace
);
117 this.totalsStats
= TmfStateSystemFactory
.newFullHistory(totalsFile
, totalsInput
, false);
119 final File typesFile
= new File(directory
+ TYPES_STATE_FILENAME
);
120 final ITmfStateProvider typesInput
= new StatsProviderEventTypes(trace
);
121 this.typesStats
= TmfStateSystemFactory
.newFullHistory(typesFile
, typesInput
, false);
123 registerStateSystems();
127 * Old manual constructor.
130 * @param historyFile Full history file
131 * @deprecated Need to use {@link #TmfStateStatistics(ITmfTrace trace,
132 * File fullHistoryFile, File partialHistoryFile)} now.
135 public TmfStateStatistics(ITmfTrace trace
, File historyFile
) {
140 * Manual constructor. This should be used if the trace's Resource is null
141 * (ie, for unit tests). It requires specifying the location of the history
145 * The trace for which we build these statistics
146 * @param totalsHistoryFile
147 * The location of the totals state history file
148 * @param typesHistoryFile
149 * The location of the types state history file
150 * @throws TmfTraceException
151 * If the file could not be written to
154 public TmfStateStatistics(ITmfTrace trace
, File totalsHistoryFile
,
155 File typesHistoryFile
) throws TmfTraceException
{
157 final ITmfStateProvider totalsInput
= new StatsProviderTotals(trace
);
158 final ITmfStateProvider typesInput
= new StatsProviderEventTypes(trace
);
159 this.totalsStats
= TmfStateSystemFactory
.newFullHistory(totalsHistoryFile
, totalsInput
, true);
160 this.typesStats
= TmfStateSystemFactory
.newFullHistory(typesHistoryFile
, typesInput
, true);
161 registerStateSystems();
165 * Register the state systems used here into the trace's state system map.
167 private void registerStateSystems() {
168 trace
.registerStateSystem(TOTALS_STATE_ID
, totalsStats
);
169 trace
.registerStateSystem(TYPES_STATE_ID
, typesStats
);
172 // ------------------------------------------------------------------------
174 // ------------------------------------------------------------------------
177 public void dispose() {
178 totalsStats
.dispose();
179 typesStats
.dispose();
183 public void updateStats(final boolean isGlobal
, final long start
,
186 * Since we are currently in a signal handler (ie, in the UI thread),
187 * and since state system queries can be arbitrarily long (O(log n) wrt
188 * the size of the trace), we will run those queries in a separate
189 * thread and update the statistics view out-of-band.
191 Thread statsThread
= new Thread("Statistics update") { //$NON-NLS-1$
194 /* Wait until the history building is completed */
195 if (!waitUntilBuilt()) {
199 /* Range should be valid for both global and time range queries */
200 long total
= getEventsInRange(start
, end
);
201 Map
<String
, Long
> map
= getEventTypesInRange(start
, end
);
203 /* Send the signal to notify the stats viewer to update its display */
204 TmfSignal sig
= new TmfStatsUpdatedSignal(this, trace
, isGlobal
, total
, map
);
205 TmfSignalManager
.dispatchSignal(sig
);
213 public List
<Long
> histogramQuery(final long start
, final long end
, final int nb
) {
214 final List
<Long
> list
= new LinkedList
<>();
215 final long increment
= (end
- start
) / nb
;
217 if (!totalsStats
.waitUntilBuilt()) {
222 * We will do one state system query per "border", and save the
223 * differences between each border.
225 long prevTotal
= (start
== totalsStats
.getStartTime()) ?
0 : getEventCountAt(start
);
226 long curTime
= start
+ increment
;
228 long curTotal
, count
;
229 for (int i
= 0; i
< nb
- 1; i
++) {
230 curTotal
= getEventCountAt(curTime
);
231 count
= curTotal
- prevTotal
;
234 curTime
+= increment
;
235 prevTotal
= curTotal
;
239 * For the last bucket, we'll stretch its end time to the end time of
240 * the requested range, in case it got truncated down.
242 curTotal
= getEventCountAt(end
);
243 count
= curTotal
- prevTotal
;
250 public long getEventsTotal() {
251 /* We need the complete state history to be built to answer this. */
252 totalsStats
.waitUntilBuilt();
254 long endTime
= totalsStats
.getCurrentEndTime();
258 final int quark
= totalsStats
.getQuarkAbsolute(Attributes
.TOTAL
);
259 count
= totalsStats
.querySingleState(endTime
, quark
).getStateValue().unboxInt();
261 } catch (TimeRangeException e
) {
262 /* Assume there is no events for that range */
264 } catch (AttributeNotFoundException e
) {
266 } catch (StateValueTypeException e
) {
268 } catch (StateSystemDisposedException e
) {
276 public Map
<String
, Long
> getEventTypesTotal() {
277 /* We need the complete state history to be built to answer this. */
278 typesStats
.waitUntilBuilt();
280 Map
<String
, Long
> map
= new HashMap
<>();
281 long endTime
= typesStats
.getCurrentEndTime();
284 /* Get the list of quarks, one for each even type in the database */
285 int quark
= typesStats
.getQuarkAbsolute(Attributes
.EVENT_TYPES
);
286 List
<Integer
> quarks
= typesStats
.getSubAttributes(quark
, false);
288 /* Since we want the total we can look only at the end */
289 List
<ITmfStateInterval
> endState
= typesStats
.queryFullState(endTime
);
293 for (int typeQuark
: quarks
) {
294 curEventName
= typesStats
.getAttributeName(typeQuark
);
295 eventCount
= endState
.get(typeQuark
).getStateValue().unboxInt();
296 map
.put(curEventName
, eventCount
);
299 } catch (TimeRangeException e
) {
300 /* Assume there is no events, nothing will be put in the map. */
301 } catch (AttributeNotFoundException e
) {
303 } catch (StateValueTypeException e
) {
305 } catch (StateSystemDisposedException e
) {
312 public long getEventsInRange(long start
, long end
) {
313 // FIXME Instead of waiting until the end, we could check the current
314 // end time, and answer as soon as possible...
315 totalsStats
.waitUntilBuilt();
318 if (start
== totalsStats
.getStartTime()) {
322 * We want the events happening at "start" to be included, so we'll
323 * need to query one unit before that point.
325 startCount
= getEventCountAt(start
- 1);
327 long endCount
= getEventCountAt(end
);
329 return endCount
- startCount
;
333 public Map
<String
, Long
> getEventTypesInRange(long start
, long end
) {
334 // FIXME Instead of waiting until the end, we could check the current
335 // end time, and answer as soon as possible...
336 typesStats
.waitUntilBuilt();
338 Map
<String
, Long
> map
= new HashMap
<>();
340 /* Make sure the start/end times are within the state history, so we
341 * don't get TimeRange exceptions.
343 long startTime
= checkStartTime(start
);
344 long endTime
= checkEndTime(end
);
347 /* Get the list of quarks, one for each even type in the database */
348 int quark
= typesStats
.getQuarkAbsolute(Attributes
.EVENT_TYPES
);
349 List
<Integer
> quarks
= typesStats
.getSubAttributes(quark
, false);
351 List
<ITmfStateInterval
> endState
= typesStats
.queryFullState(endTime
);
354 long countAtStart
, countAtEnd
, eventCount
;
356 if (startTime
== typesStats
.getStartTime()) {
357 /* Only use the values picked up at the end time */
358 for (int typeQuark
: quarks
) {
359 curEventName
= typesStats
.getAttributeName(typeQuark
);
360 eventCount
= endState
.get(typeQuark
).getStateValue().unboxInt();
361 if (eventCount
== -1) {
364 map
.put(curEventName
, eventCount
);
368 * Query the start time at -1, so the beginning of the interval
371 List
<ITmfStateInterval
> startState
= typesStats
.queryFullState(startTime
- 1);
372 for (int typeQuark
: quarks
) {
373 curEventName
= typesStats
.getAttributeName(typeQuark
);
374 countAtStart
= startState
.get(typeQuark
).getStateValue().unboxInt();
375 countAtEnd
= endState
.get(typeQuark
).getStateValue().unboxInt();
377 if (countAtStart
== -1) {
380 if (countAtEnd
== -1) {
383 eventCount
= countAtEnd
- countAtStart
;
384 map
.put(curEventName
, eventCount
);
388 } catch (TimeRangeException e
) {
389 /* Assume there is no events, nothing will be put in the map. */
390 } catch (AttributeNotFoundException e
) {
392 * These other exception types would show a logic problem however,
393 * so they should not happen.
396 } catch (StateValueTypeException e
) {
398 } catch (StateSystemDisposedException e
) {
404 // ------------------------------------------------------------------------
406 // ------------------------------------------------------------------------
408 private long getEventCountAt(long timestamp
) {
409 /* Make sure the target time is within the range of the history */
410 long ts
= checkStartTime(timestamp
);
411 ts
= checkEndTime(ts
);
414 final int quark
= totalsStats
.getQuarkAbsolute(Attributes
.TOTAL
);
415 long count
= totalsStats
.querySingleState(ts
, quark
).getStateValue().unboxInt();
418 } catch (TimeRangeException e
) {
419 /* Assume there is no events for that range */
420 } catch (AttributeNotFoundException e
) {
422 } catch (StateValueTypeException e
) {
424 } catch (StateSystemDisposedException e
) {
431 private long checkStartTime(long initialStart
) {
432 long start
= initialStart
;
433 if (start
< totalsStats
.getStartTime()) {
434 return totalsStats
.getStartTime();
439 private long checkEndTime(long initialEnd
) {
440 long end
= initialEnd
;
441 if (end
> totalsStats
.getCurrentEndTime()) {
442 return totalsStats
.getCurrentEndTime();
448 * Wait until both backing state systems are finished building.
450 * @return If both state systems were built successfully
452 private boolean waitUntilBuilt() {
453 boolean check1
= totalsStats
.waitUntilBuilt();
454 boolean check2
= typesStats
.waitUntilBuilt();
455 return (check1
&& check2
);
460 * The attribute names that are used in the state provider
462 public static class Attributes
{
464 /** Total nb of events */
465 public static final String TOTAL
= "total"; //$NON-NLS-1$
468 public static final String EVENT_TYPES
= "event_types"; //$NON-NLS-1$<