tmf: Add unit tests for lost event statistics
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statistics / StatsStateProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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 * Patrick Tasse - Fix javadoc
12 ******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.statistics;
15
16 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
17 import org.eclipse.linuxtools.tmf.core.event.ITmfLostEvent;
18 import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
19 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
20 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
21 import org.eclipse.linuxtools.tmf.core.statesystem.AbstractTmfStateProvider;
22 import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue;
23 import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics.Attributes;
24 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
25 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
26
27 /**
28 * The state provider for traces statistics that use TmfStateStatistics. It
29 * should work with any trace type for which we can use the state system.
30 *
31 * The resulting attribute tree will look like this:
32 *<pre>
33 * (root)
34 * |-- total
35 * \-- event_types
36 * |-- (event name 1)
37 * |-- (event name 2)
38 * |-- (event name 3)
39 * ...
40 *</pre>
41 * And each (event name)'s value will be an integer, representing how many times
42 * this particular event type has been seen in the trace so far.
43 *
44 * @author Alexandre Montplaisir
45 * @version 1.0
46 */
47 class StatsStateProvider extends AbstractTmfStateProvider {
48
49 /**
50 * Version number of this input handler. Please bump this if you modify the
51 * contents of the generated state history in some way.
52 */
53 private static final int VERSION = 1;
54
55 /**
56 * Constructor
57 *
58 * @param trace
59 * The trace for which we build this state system
60 */
61 public StatsStateProvider(ITmfTrace trace) {
62 super(trace, ITmfEvent.class ,"TMF Statistics"); //$NON-NLS-1$
63 }
64
65 @Override
66 public int getVersion() {
67 return VERSION;
68 }
69
70 @Override
71 public StatsStateProvider getNewInstance() {
72 return new StatsStateProvider(this.getTrace());
73 }
74
75 @Override
76 protected void eventHandle(ITmfEvent event) {
77 int quark;
78
79 /* Since this can be used for any trace types, normalize all the
80 * timestamp values to nanoseconds. */
81 final long ts = event.getTimestamp().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
82
83 final String eventName = event.getType().getName();
84
85 try {
86 /* Special handling for lost events */
87 if (event instanceof ITmfLostEvent) {
88 ITmfLostEvent le = (ITmfLostEvent) event;
89 quark = ss.getQuarkAbsoluteAndAdd(Attributes.EVENT_TYPES, eventName);
90
91 int curVal = ss.queryOngoingState(quark).unboxInt();
92 if (curVal == -1) { curVal = 0; }
93
94 TmfStateValue value = TmfStateValue.newValueInt((int) (curVal + le.getNbLostEvents()));
95 ss.modifyAttribute(ts, value, quark);
96 return;
97 }
98
99 /* Total number of events */
100 quark = ss.getQuarkAbsoluteAndAdd(Attributes.TOTAL);
101 ss.incrementAttribute(ts, quark);
102
103 /* Number of events of each type, globally */
104 quark = ss.getQuarkAbsoluteAndAdd(Attributes.EVENT_TYPES, eventName);
105 ss.incrementAttribute(ts, quark);
106
107 // /* Number of events per CPU */
108 // quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATISTICS, Attributes.EVENT_TYPES, eventName);
109 // ss.incrementAttribute(ts, quark);
110 //
111 // /* Number of events per process */
112 // quark = ss.getQuarkRelativeAndAdd(currentThreadNode, Attributes.STATISTICS, Attributes.EVENT_TYPES, eventName);
113 // ss.incrementAttribute(ts, quark);
114
115 } catch (StateValueTypeException e) {
116 e.printStackTrace();
117 } catch (TimeRangeException e) {
118 e.printStackTrace();
119 } catch (AttributeNotFoundException e) {
120 e.printStackTrace();
121 }
122 }
123 }
This page took 0.037047 seconds and 5 git commands to generate.