Commit | Line | Data |
---|---|---|
50a47aa6 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal |
50a47aa6 GB |
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 | ||
e363eae1 | 13 | package org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis; |
50a47aa6 GB |
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; | |
4c4e2816 | 22 | import org.eclipse.jdt.annotation.NonNull; |
50a47aa6 GB |
23 | import org.eclipse.jdt.annotation.Nullable; |
24 | import org.eclipse.tracecompass.common.core.NonNullUtils; | |
50a47aa6 GB |
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 | */ | |
e363eae1 | 39 | public final class KernelThreadInformationProvider { |
50a47aa6 | 40 | |
e363eae1 | 41 | private KernelThreadInformationProvider() { |
50a47aa6 GB |
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. | |
6d16f5a9 | 58 | * @since 1.0 |
50a47aa6 | 59 | */ |
6d16f5a9 | 60 | public static @Nullable Integer getThreadOnCpu(KernelAnalysisModule module, long cpuId, long ts) { |
50a47aa6 GB |
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 | |
6d16f5a9 | 90 | * @since 1.0 |
50a47aa6 | 91 | */ |
6d16f5a9 | 92 | public static Collection<Integer> getThreadIds(KernelAnalysisModule module) { |
50a47aa6 GB |
93 | ITmfStateSystem ss = module.getStateSystem(); |
94 | if (ss == null) { | |
95 | return NonNullUtils.checkNotNull(Collections.EMPTY_SET); | |
96 | } | |
97 | int threadQuark; | |
98 | try { | |
99 | threadQuark = ss.getQuarkAbsolute(Attributes.THREADS); | |
4c4e2816 | 100 | Set<@NonNull Integer> tids = new TreeSet<>(); |
50a47aa6 GB |
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 NonNullUtils.checkNotNull(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. | |
6d16f5a9 | 120 | * @since 1.0 |
50a47aa6 | 121 | */ |
6d16f5a9 | 122 | public static @Nullable Integer getParentPid(KernelAnalysisModule module, Integer threadId, long ts) { |
50a47aa6 GB |
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 | |
6d16f5a9 | 161 | * @since 1.0 |
50a47aa6 | 162 | */ |
6d16f5a9 | 163 | public static @Nullable String getExecutableName(KernelAnalysisModule module, Integer threadId) { |
50a47aa6 GB |
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 | ||
3bf563da CM |
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 | |
dbc7991d | 204 | * @since 1.0 |
3bf563da | 205 | */ |
6d16f5a9 | 206 | public static @Nullable Integer getThreadPrio(KernelAnalysisModule module, Integer threadId, long ts) { |
3bf563da CM |
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 | ||
50a47aa6 GB |
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 | |
6d16f5a9 | 243 | * @since 1.0 |
50a47aa6 | 244 | */ |
6d16f5a9 | 245 | public static List<ITmfStateInterval> getStatusIntervalsForThread(KernelAnalysisModule module, Integer threadId, long start, long end, long resolution, IProgressMonitor monitor) { |
50a47aa6 GB |
246 | ITmfStateSystem ss = module.getStateSystem(); |
247 | if (ss == null) { | |
248 | return NonNullUtils.checkNotNull(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 NonNullUtils.checkNotNull(Collections.EMPTY_LIST); | |
259 | } | |
260 | ||
261 | } |