[WIP] CFV Refactor
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / internal / analysis / os / linux / core / inputoutput / InputOutputStateProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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
10 package org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.eclipse.tracecompass.analysis.os.linux.core.inputoutput.Attributes;
19 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
20 import org.eclipse.tracecompass.common.core.NonNullUtils;
21 import org.eclipse.tracecompass.internal.analysis.os.linux.core.Activator;
22 import org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput.handlers.BlockFrontMergeHandler;
23 import org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput.handlers.BlockRqComplete;
24 import org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput.handlers.BlockRqInsertHandler;
25 import org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput.handlers.BlockRqIssueHandler;
26 import org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput.handlers.MergeRequestsHandler;
27 import org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput.handlers.StateDumpHandler;
28 import org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput.handlers.SysEntryHandler;
29 import org.eclipse.tracecompass.internal.analysis.os.linux.core.inputoutput.handlers.SysExitHandler;
30 import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.handlers.KernelEventHandler;
31 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
33 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
34 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
35 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
36 import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
37 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
38
39 import com.google.common.collect.ImmutableMap;
40
41 /**
42 * State provider for the I/O analysis
43 *
44 * Attribute tree:
45 *
46 * <pre>
47 * |- SYSTEM_CALLS
48 * | |- <TID> -> System Call Name
49 * |- THREADS
50 * | |- <TID number>
51 * | | |- BYTES_READ
52 * | | |- BYTES_WRITTEN
53 * |- Disks
54 * | |- <Disk number> -> Disk Name
55 * | | |- SECTORS_READ
56 * | | |- SECTORS_WRITTEN
57 * | | |- WAITING_QUEUE -> Root for the Attribute pool for waiting queue
58 * | | | |- <slot #1> -> Status
59 * | | | | |- CURRENT_REQUEST
60 * | | | | |- REQUEST_SIZE
61 * | | | | |- MERGED_IN
62 * | | | |- <slot #2>
63 * | | |- WAITING_QUEUE_LENGTH
64 * | | |- DRIVER_QUEUE -> Root for the Attribute pool for driver queue
65 * | | | |- <slot #1> -> Status
66 * | | | | |- CURRENT_REQUEST
67 * | | | | |- REQUEST_SIZE
68 * | | | | |- ISSUED_FROM
69 * | | | |- <slot #2>
70 * | | |- DRIVER_QUEUE_LENGTH
71 * </pre>
72 *
73 * @author Houssem Daoud
74 * @since 2.0
75 */
76 public class InputOutputStateProvider extends AbstractTmfStateProvider {
77
78 private static final int VERSION = 1;
79
80 private final Map<Integer, DiskWriteModel> fDisks = new HashMap<>();
81 private final Map<String, KernelEventHandler> fEventNames;
82 private final IKernelAnalysisEventLayout fLayout;
83
84 private final KernelEventHandler fSysEntryHandler;
85 private final KernelEventHandler fSysExitHandler;
86
87 /**
88 * Instantiate a new state provider plugin.
89 *
90 * @param trace
91 * The kernel trace to apply this state provider to
92 * @param layout
93 * The event layout to use for this state provider.
94 */
95 public InputOutputStateProvider(ITmfTrace trace, IKernelAnalysisEventLayout layout) {
96 super(trace, "Input Output Analysis");//$NON-NLS-1$
97 fLayout = layout;
98 fEventNames = buildEventNames(layout);
99 fSysEntryHandler = new SysEntryHandler(layout);
100 fSysExitHandler = new SysExitHandler(layout);
101 }
102
103 private Map<String, KernelEventHandler> buildEventNames(IKernelAnalysisEventLayout layout) {
104 ImmutableMap.Builder<String, KernelEventHandler> builder = ImmutableMap.builder();
105
106 builder.put(layout.eventBlockRqInsert(), new BlockRqInsertHandler(layout, this));
107 builder.put(layout.eventBlockRqIssue(), new BlockRqIssueHandler(layout, this));
108 builder.put(layout.eventBlockRqComplete(), new BlockRqComplete(layout, this));
109 builder.put(layout.eventBlockBioFrontmerge(), new BlockFrontMergeHandler(layout, this));
110 builder.put(layout.eventBlockRqMerge(), new MergeRequestsHandler(layout, this));
111
112 final String eventStatedumpBlockDevice = layout.eventStatedumpBlockDevice();
113 if (eventStatedumpBlockDevice != null) {
114 builder.put(eventStatedumpBlockDevice, new StateDumpHandler(layout, this));
115 }
116
117 return builder.build();
118 }
119
120 @Override
121 public int getVersion() {
122 return VERSION;
123 }
124
125 @Override
126 public InputOutputStateProvider getNewInstance() {
127 return new InputOutputStateProvider(this.getTrace(), this.fLayout);
128 }
129
130 @Override
131 protected void eventHandle(@Nullable ITmfEvent event) {
132
133 if (event == null) {
134 return;
135 }
136
137 final String eventName = event.getName();
138
139 try {
140 final ITmfStateSystemBuilder ss = NonNullUtils.checkNotNull(getStateSystemBuilder());
141 /*
142 * Feed event to the history system if it's known to cause a state
143 * transition.
144 */
145 KernelEventHandler handler = fEventNames.get(eventName);
146 if (handler == null) {
147 if (isSyscallExit(eventName)) {
148 handler = fSysExitHandler;
149 } else if (isSyscallEntry(eventName)) {
150 handler = fSysEntryHandler;
151 }
152 }
153 if (handler != null) {
154 handler.handleEvent(ss, event);
155 }
156 } catch (TimeRangeException | StateValueTypeException | AttributeNotFoundException e) {
157 Activator.getDefault().logError("Exception while building the IO state system", e); //$NON-NLS-1$
158 }
159
160 }
161
162 /**
163 * Get a disk identified by a device ID
164 *
165 * @param deviceId
166 * The device ID of the block device
167 * @return The disk corresponding to the device ID
168 */
169 public DiskWriteModel getDisk(int deviceId) {
170 DiskWriteModel disk = fDisks.get(deviceId);
171 if (disk == null) {
172 disk = new DiskWriteModel(deviceId, checkNotNull(getStateSystemBuilder()));
173 fDisks.put(deviceId, disk);
174 }
175 return disk;
176 }
177
178 private boolean isSyscallEntry(String eventName) {
179 return (eventName.startsWith(fLayout.eventSyscallEntryPrefix())
180 || eventName.startsWith(fLayout.eventCompatSyscallEntryPrefix()));
181 }
182
183 private boolean isSyscallExit(String eventName) {
184 return (eventName.startsWith(fLayout.eventSyscallExitPrefix()) ||
185 eventName.startsWith(fLayout.eventCompatSyscallExitPrefix()));
186 }
187
188 /**
189 * Return the quark corresponding to the threads attributes
190 *
191 * @param ssb
192 * the state system builder
193 * @return The quark of the {@link Attributes#THREADS} node
194 */
195 public static int getNodeThreads(ITmfStateSystemBuilder ssb) {
196 return ssb.getQuarkAbsoluteAndAdd(Attributes.THREADS);
197 }
198
199 /**
200 * Return the quark corresponding to the system call root attributes
201 *
202 * @param ssb
203 * the state system builder
204 * @return The quark of the {@link Attributes#SYSTEM_CALLS_ROOT} node
205 */
206 public static int getNodeSyscalls(ITmfStateSystemBuilder ssb) {
207 return ssb.getQuarkAbsoluteAndAdd(Attributes.SYSTEM_CALLS_ROOT);
208 }
209
210 }
This page took 0.042136 seconds and 5 git commands to generate.