1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
;
15 import java
.util
.Collection
;
16 import java
.util
.Collections
;
17 import java
.util
.List
;
19 import java
.util
.TreeSet
;
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
;
34 * Information provider utility class that retrieves thread-related information
35 * from a Linux Kernel Analysis
37 * @author Geneviève Bastien
39 public final class KernelThreadInformationProvider
{
41 private KernelThreadInformationProvider() {
45 * Get the ID of the thread running on the CPU at time ts
47 * TODO: This method may later be replaced by an aspect, when the aspect can
48 * resolve to something that is not an event
51 * The lttng kernel analysis instance to run this method on
53 * The CPU number the process is running on
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.
60 public static @Nullable Integer
getThreadOnCpu(KernelAnalysisModule module
, long cpuId
, long ts
) {
61 ITmfStateSystem ss
= module
.getStateSystem();
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()) {
71 return val
.unboxInt();
79 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
85 * Get the TIDs of the threads from an analysis
88 * The lttng kernel analysis instance to run this method on
89 * @return The set of TIDs corresponding to the threads
92 public static Collection
<Integer
> getThreadIds(KernelAnalysisModule module
) {
93 ITmfStateSystem ss
= module
.getStateSystem();
95 return Collections
.EMPTY_SET
;
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
)));
105 } catch (AttributeNotFoundException e
) {
107 return Collections
.EMPTY_SET
;
111 * Get the parent process ID of a thread
114 * The lttng kernel analysis instance to run this method on
116 * The thread ID of the process for which to get the parent
118 * The timestamp at which to get the parent
119 * @return The parent PID or {@code null} if the PPID is not found.
122 public static @Nullable Integer
getParentPid(KernelAnalysisModule module
, Integer threadId
, long ts
) {
124 ITmfStateSystem ss
= module
.getStateSystem();
130 ppidNode
= ss
.getQuarkAbsolute(Attributes
.THREADS
, threadId
.toString(), Attributes
.PPID
);
131 ITmfStateInterval ppidInterval
= ss
.querySingleState(ts
, ppidNode
);
132 ITmfStateValue ppidValue
= ppidInterval
.getStateValue();
134 switch (ppidValue
.getType()) {
136 ppid
= NonNullUtils
.checkNotNull(Integer
.valueOf(ppidValue
.unboxInt()));
145 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
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
156 * The lttng kernel analysis instance to run this method on
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
163 public static @Nullable String
getExecutableName(KernelAnalysisModule module
, Integer threadId
) {
164 String execName
= null;
165 ITmfStateSystem ss
= module
.getStateSystem();
169 Integer execNameNode
;
171 execNameNode
= ss
.getQuarkAbsolute(Attributes
.THREADS
, threadId
.toString(), Attributes
.EXEC_NAME
);
172 List
<ITmfStateInterval
> execNameIntervals
= StateSystemUtils
.queryHistoryRange(ss
, execNameNode
, ss
.getStartTime(), ss
.getCurrentEndTime());
174 ITmfStateValue execNameValue
;
175 for (ITmfStateInterval interval
: execNameIntervals
) {
176 execNameValue
= interval
.getStateValue();
177 switch (execNameValue
.getType()) {
179 execName
= NonNullUtils
.checkNotNull(execNameValue
.unboxStr());
189 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
195 * Get the priority of a thread running at a time ts.
198 * The kernel analysis instance to run this method on
200 * The thread ID of the target thread
202 * The timestamp at which to get the priority
203 * @return The priority of this thread, or {@code null} if not found
206 public static @Nullable Integer
getThreadPrio(KernelAnalysisModule module
, Integer threadId
, long ts
) {
207 Integer execPrio
= null;
208 ITmfStateSystem ss
= module
.getStateSystem();
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
) {
224 * Get the status intervals for a given thread with a resolution
227 * The lttng kernel analysis instance to run this method on
229 * The ID of the thread to get the intervals for
231 * The start time of the requested range
233 * The end time of the requested range
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.
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
245 public static List
<ITmfStateInterval
> getStatusIntervalsForThread(KernelAnalysisModule module
, Integer threadId
, long start
, long end
, long resolution
, IProgressMonitor monitor
) {
246 ITmfStateSystem ss
= module
.getStateSystem();
248 return Collections
.EMPTY_LIST
;
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
) {
258 return Collections
.EMPTY_LIST
;