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 | ||
0f7a12d3 | 13 | package org.eclipse.tracecompass.analysis.os.linux.core.kernel; |
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 | |
0f7a12d3 | 38 | * @since 2.0 |
50a47aa6 | 39 | */ |
e363eae1 | 40 | public final class KernelThreadInformationProvider { |
50a47aa6 | 41 | |
e363eae1 | 42 | private KernelThreadInformationProvider() { |
50a47aa6 GB |
43 | } |
44 | ||
45 | /** | |
46 | * Get the ID of the thread running on the CPU at time ts | |
47 | * | |
48 | * TODO: This method may later be replaced by an aspect, when the aspect can | |
49 | * resolve to something that is not an event | |
50 | * | |
51 | * @param module | |
52 | * The lttng kernel analysis instance to run this method on | |
53 | * @param cpuId | |
54 | * The CPU number the process is running on | |
55 | * @param ts | |
56 | * The timestamp at which we want the running process | |
57 | * @return The TID of the thread running on CPU cpuId at time ts or | |
58 | * {@code null} if either no thread is running or we do not know. | |
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 | |
90 | */ | |
6d16f5a9 | 91 | public static Collection<Integer> getThreadIds(KernelAnalysisModule module) { |
50a47aa6 GB |
92 | ITmfStateSystem ss = module.getStateSystem(); |
93 | if (ss == null) { | |
aa353506 | 94 | return Collections.EMPTY_SET; |
50a47aa6 GB |
95 | } |
96 | int threadQuark; | |
97 | try { | |
98 | threadQuark = ss.getQuarkAbsolute(Attributes.THREADS); | |
4c4e2816 | 99 | Set<@NonNull Integer> tids = new TreeSet<>(); |
50a47aa6 GB |
100 | for (Integer quark : ss.getSubAttributes(threadQuark, false)) { |
101 | tids.add(Integer.parseInt(ss.getAttributeName(quark))); | |
102 | } | |
103 | return tids; | |
104 | } catch (AttributeNotFoundException e) { | |
105 | } | |
aa353506 | 106 | return Collections.EMPTY_SET; |
50a47aa6 GB |
107 | } |
108 | ||
109 | /** | |
110 | * Get the parent process ID of a thread | |
111 | * | |
112 | * @param module | |
113 | * The lttng kernel analysis instance to run this method on | |
114 | * @param threadId | |
115 | * The thread ID of the process for which to get the parent | |
116 | * @param ts | |
117 | * The timestamp at which to get the parent | |
118 | * @return The parent PID or {@code null} if the PPID is not found. | |
119 | */ | |
6d16f5a9 | 120 | public static @Nullable Integer getParentPid(KernelAnalysisModule module, Integer threadId, long ts) { |
50a47aa6 GB |
121 | Integer ppid = null; |
122 | ITmfStateSystem ss = module.getStateSystem(); | |
123 | if (ss == null) { | |
124 | return ppid; | |
125 | } | |
126 | Integer ppidNode; | |
127 | try { | |
128 | ppidNode = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString(), Attributes.PPID); | |
129 | ITmfStateInterval ppidInterval = ss.querySingleState(ts, ppidNode); | |
130 | ITmfStateValue ppidValue = ppidInterval.getStateValue(); | |
131 | ||
132 | switch (ppidValue.getType()) { | |
133 | case INTEGER: | |
134 | ppid = NonNullUtils.checkNotNull(Integer.valueOf(ppidValue.unboxInt())); | |
135 | break; | |
136 | case DOUBLE: | |
137 | case LONG: | |
138 | case NULL: | |
139 | case STRING: | |
140 | default: | |
141 | break; | |
142 | } | |
143 | } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) { | |
144 | } | |
145 | return ppid; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Get the executable name of the thread ID. If the thread ID was used | |
150 | * multiple time or the name changed in between, it will return the last | |
151 | * name the thread has taken, or {@code null} if no name is found | |
152 | * | |
153 | * @param module | |
154 | * The lttng kernel analysis instance to run this method on | |
155 | * @param threadId | |
156 | * The thread ID of the process for which to get the name | |
157 | * @return The last executable name of this process, or {@code null} if not | |
158 | * found | |
159 | */ | |
6d16f5a9 | 160 | public static @Nullable String getExecutableName(KernelAnalysisModule module, Integer threadId) { |
50a47aa6 GB |
161 | String execName = null; |
162 | ITmfStateSystem ss = module.getStateSystem(); | |
163 | if (ss == null) { | |
164 | return execName; | |
165 | } | |
166 | Integer execNameNode; | |
167 | try { | |
168 | execNameNode = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString(), Attributes.EXEC_NAME); | |
169 | List<ITmfStateInterval> execNameIntervals = StateSystemUtils.queryHistoryRange(ss, execNameNode, ss.getStartTime(), ss.getCurrentEndTime()); | |
170 | ||
171 | ITmfStateValue execNameValue; | |
172 | for (ITmfStateInterval interval : execNameIntervals) { | |
173 | execNameValue = interval.getStateValue(); | |
174 | switch (execNameValue.getType()) { | |
175 | case STRING: | |
0e4f957e | 176 | execName = execNameValue.unboxStr(); |
50a47aa6 GB |
177 | break; |
178 | case DOUBLE: | |
179 | case LONG: | |
180 | case NULL: | |
181 | case INTEGER: | |
182 | default: | |
183 | break; | |
184 | } | |
185 | } | |
186 | } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) { | |
187 | } | |
188 | return execName; | |
189 | } | |
190 | ||
191 | /** | |
192 | * Get the status intervals for a given thread with a resolution | |
193 | * | |
194 | * @param module | |
195 | * The lttng kernel analysis instance to run this method on | |
196 | * @param threadId | |
197 | * The ID of the thread to get the intervals for | |
198 | * @param start | |
199 | * The start time of the requested range | |
200 | * @param end | |
201 | * The end time of the requested range | |
202 | * @param resolution | |
203 | * The resolution or the minimal time between the requested | |
204 | * intervals. If interval times are smaller than resolution, only | |
205 | * the first interval is returned, the others are ignored. | |
206 | * @param monitor | |
207 | * A progress monitor for this task | |
208 | * @return The list of status intervals for this thread, an empty list is | |
209 | * returned if either the state system is {@code null} or the quark | |
210 | * is not found | |
211 | */ | |
6d16f5a9 | 212 | public static List<ITmfStateInterval> getStatusIntervalsForThread(KernelAnalysisModule module, Integer threadId, long start, long end, long resolution, IProgressMonitor monitor) { |
50a47aa6 GB |
213 | ITmfStateSystem ss = module.getStateSystem(); |
214 | if (ss == null) { | |
aa353506 | 215 | return Collections.EMPTY_LIST; |
50a47aa6 GB |
216 | } |
217 | ||
218 | try { | |
219 | int threadQuark = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString()); | |
220 | int statusQuark = ss.getQuarkRelative(threadQuark, Attributes.STATUS); | |
221 | List<ITmfStateInterval> statusIntervals = StateSystemUtils.queryHistoryRange(ss, statusQuark, Math.max(start, ss.getStartTime()), Math.min(end - 1, ss.getCurrentEndTime()), resolution, monitor); | |
222 | return statusIntervals; | |
223 | } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) { | |
224 | } | |
aa353506 | 225 | return Collections.EMPTY_LIST; |
50a47aa6 GB |
226 | } |
227 | ||
228 | } |