Fix some null warnings
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / analysis / os / linux / core / kernelanalysis / KernelThreadInformationProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 É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 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis;
14
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.TreeSet;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.tracecompass.common.core.NonNullUtils;
25 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
26 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
27 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
28 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
29 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
30 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
31 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
32
33 /**
34 * Information provider utility class that retrieves thread-related information
35 * from a Linux Kernel Analysis
36 *
37 * @author Geneviève Bastien
38 */
39 public final class KernelThreadInformationProvider {
40
41 private KernelThreadInformationProvider() {
42 }
43
44 /**
45 * Get the ID of the thread running on the CPU at time ts
46 *
47 * TODO: This method may later be replaced by an aspect, when the aspect can
48 * resolve to something that is not an event
49 *
50 * @param module
51 * The lttng kernel analysis instance to run this method on
52 * @param cpuId
53 * The CPU number the process is running on
54 * @param ts
55 * The timestamp at which we want the running process
56 * @return The TID of the thread running on CPU cpuId at time ts or
57 * {@code null} if either no thread is running or we do not know.
58 * @since 1.0
59 */
60 public static @Nullable Integer getThreadOnCpu(KernelAnalysisModule module, long cpuId, long ts) {
61 ITmfStateSystem ss = module.getStateSystem();
62 if (ss == null) {
63 return null;
64 }
65 try {
66 int cpuQuark = ss.getQuarkAbsolute(Attributes.CPUS, Long.toString(cpuId), Attributes.CURRENT_THREAD);
67 ITmfStateInterval interval = ss.querySingleState(ts, cpuQuark);
68 ITmfStateValue val = interval.getStateValue();
69 switch (val.getType()) {
70 case INTEGER:
71 return val.unboxInt();
72 case LONG:
73 case DOUBLE:
74 case NULL:
75 case STRING:
76 default:
77 break;
78 }
79 } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) {
80 }
81 return null;
82 }
83
84 /**
85 * Get the TIDs of the threads from an analysis
86 *
87 * @param module
88 * The lttng kernel analysis instance to run this method on
89 * @return The set of TIDs corresponding to the threads
90 * @since 1.0
91 */
92 public static Collection<Integer> getThreadIds(KernelAnalysisModule module) {
93 ITmfStateSystem ss = module.getStateSystem();
94 if (ss == null) {
95 return Collections.EMPTY_SET;
96 }
97 int threadQuark;
98 try {
99 threadQuark = ss.getQuarkAbsolute(Attributes.THREADS);
100 Set<@NonNull Integer> tids = new TreeSet<>();
101 for (Integer quark : ss.getSubAttributes(threadQuark, false)) {
102 tids.add(Integer.parseInt(ss.getAttributeName(quark)));
103 }
104 return tids;
105 } catch (AttributeNotFoundException e) {
106 }
107 return Collections.EMPTY_SET;
108 }
109
110 /**
111 * Get the parent process ID of a thread
112 *
113 * @param module
114 * The lttng kernel analysis instance to run this method on
115 * @param threadId
116 * The thread ID of the process for which to get the parent
117 * @param ts
118 * The timestamp at which to get the parent
119 * @return The parent PID or {@code null} if the PPID is not found.
120 * @since 1.0
121 */
122 public static @Nullable Integer getParentPid(KernelAnalysisModule module, Integer threadId, long ts) {
123 Integer ppid = null;
124 ITmfStateSystem ss = module.getStateSystem();
125 if (ss == null) {
126 return ppid;
127 }
128 Integer ppidNode;
129 try {
130 ppidNode = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString(), Attributes.PPID);
131 ITmfStateInterval ppidInterval = ss.querySingleState(ts, ppidNode);
132 ITmfStateValue ppidValue = ppidInterval.getStateValue();
133
134 switch (ppidValue.getType()) {
135 case INTEGER:
136 ppid = NonNullUtils.checkNotNull(Integer.valueOf(ppidValue.unboxInt()));
137 break;
138 case DOUBLE:
139 case LONG:
140 case NULL:
141 case STRING:
142 default:
143 break;
144 }
145 } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) {
146 }
147 return ppid;
148 }
149
150 /**
151 * Get the executable name of the thread ID. If the thread ID was used
152 * multiple time or the name changed in between, it will return the last
153 * name the thread has taken, or {@code null} if no name is found
154 *
155 * @param module
156 * The lttng kernel analysis instance to run this method on
157 * @param threadId
158 * The thread ID of the process for which to get the name
159 * @return The last executable name of this process, or {@code null} if not
160 * found
161 * @since 1.0
162 */
163 public static @Nullable String getExecutableName(KernelAnalysisModule module, Integer threadId) {
164 String execName = null;
165 ITmfStateSystem ss = module.getStateSystem();
166 if (ss == null) {
167 return execName;
168 }
169 Integer execNameNode;
170 try {
171 execNameNode = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString(), Attributes.EXEC_NAME);
172 List<ITmfStateInterval> execNameIntervals = StateSystemUtils.queryHistoryRange(ss, execNameNode, ss.getStartTime(), ss.getCurrentEndTime());
173
174 ITmfStateValue execNameValue;
175 for (ITmfStateInterval interval : execNameIntervals) {
176 execNameValue = interval.getStateValue();
177 switch (execNameValue.getType()) {
178 case STRING:
179 execName = NonNullUtils.checkNotNull(execNameValue.unboxStr());
180 break;
181 case DOUBLE:
182 case LONG:
183 case NULL:
184 case INTEGER:
185 default:
186 break;
187 }
188 }
189 } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) {
190 }
191 return execName;
192 }
193
194 /**
195 * Get the priority of a thread running at a time ts.
196 *
197 * @param module
198 * The kernel analysis instance to run this method on
199 * @param threadId
200 * The thread ID of the target thread
201 * @param ts
202 * The timestamp at which to get the priority
203 * @return The priority of this thread, or {@code null} if not found
204 * @since 1.0
205 */
206 public static @Nullable Integer getThreadPrio(KernelAnalysisModule module, Integer threadId, long ts) {
207 Integer execPrio = null;
208 ITmfStateSystem ss = module.getStateSystem();
209 if (ss == null) {
210 return execPrio;
211 }
212 try {
213 int execPrioQuark = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString(), Attributes.PRIO);
214 ITmfStateInterval interval = ss.querySingleState(ts, execPrioQuark);
215 ITmfStateValue prioValue = interval.getStateValue();
216 /* We know the prio must be an Integer */
217 execPrio = prioValue.unboxInt();
218 } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) {
219 }
220 return execPrio;
221 }
222
223 /**
224 * Get the status intervals for a given thread with a resolution
225 *
226 * @param module
227 * The lttng kernel analysis instance to run this method on
228 * @param threadId
229 * The ID of the thread to get the intervals for
230 * @param start
231 * The start time of the requested range
232 * @param end
233 * The end time of the requested range
234 * @param resolution
235 * The resolution or the minimal time between the requested
236 * intervals. If interval times are smaller than resolution, only
237 * the first interval is returned, the others are ignored.
238 * @param monitor
239 * A progress monitor for this task
240 * @return The list of status intervals for this thread, an empty list is
241 * returned if either the state system is {@code null} or the quark
242 * is not found
243 * @since 1.0
244 */
245 public static List<ITmfStateInterval> getStatusIntervalsForThread(KernelAnalysisModule module, Integer threadId, long start, long end, long resolution, IProgressMonitor monitor) {
246 ITmfStateSystem ss = module.getStateSystem();
247 if (ss == null) {
248 return Collections.EMPTY_LIST;
249 }
250
251 try {
252 int threadQuark = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString());
253 int statusQuark = ss.getQuarkRelative(threadQuark, Attributes.STATUS);
254 List<ITmfStateInterval> statusIntervals = StateSystemUtils.queryHistoryRange(ss, statusQuark, Math.max(start, ss.getStartTime()), Math.min(end - 1, ss.getCurrentEndTime()), resolution, monitor);
255 return statusIntervals;
256 } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) {
257 }
258 return Collections.EMPTY_LIST;
259 }
260
261 }
This page took 0.047348 seconds and 5 git commands to generate.