1 /*******************************************************************************
2 * Copyright (c) 2014 É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
.Nullable
;
23 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
33 * Information provider utility class that retrieves thread-related information
34 * from a Linux Kernel Analysis
36 * @author Geneviève Bastien
38 public final class KernelThreadInformationProvider
{
40 private KernelThreadInformationProvider() {
44 * Get the ID of the thread running on the CPU at time ts
46 * TODO: This method may later be replaced by an aspect, when the aspect can
47 * resolve to something that is not an event
50 * The lttng kernel analysis instance to run this method on
52 * The CPU number the process is running on
54 * The timestamp at which we want the running process
55 * @return The TID of the thread running on CPU cpuId at time ts or
56 * {@code null} if either no thread is running or we do not know.
58 public static @Nullable Integer
getThreadOnCpu(KernelAnalysis module
, long cpuId
, long ts
) {
59 ITmfStateSystem ss
= module
.getStateSystem();
64 int cpuQuark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, Long
.toString(cpuId
), Attributes
.CURRENT_THREAD
);
65 ITmfStateInterval interval
= ss
.querySingleState(ts
, cpuQuark
);
66 ITmfStateValue val
= interval
.getStateValue();
67 switch (val
.getType()) {
69 return val
.unboxInt();
77 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
83 * Get the TIDs of the threads from an analysis
86 * The lttng kernel analysis instance to run this method on
87 * @return The set of TIDs corresponding to the threads
89 public static Collection
<Integer
> getThreadIds(KernelAnalysis module
) {
90 ITmfStateSystem ss
= module
.getStateSystem();
92 return NonNullUtils
.checkNotNull(Collections
.EMPTY_SET
);
96 threadQuark
= ss
.getQuarkAbsolute(Attributes
.THREADS
);
97 Set
<Integer
> tids
= new TreeSet
<>();
98 for (Integer quark
: ss
.getSubAttributes(threadQuark
, false)) {
99 tids
.add(Integer
.parseInt(ss
.getAttributeName(quark
)));
102 } catch (AttributeNotFoundException e
) {
104 return NonNullUtils
.checkNotNull(Collections
.EMPTY_SET
);
108 * Get the parent process ID of a thread
111 * The lttng kernel analysis instance to run this method on
113 * The thread ID of the process for which to get the parent
115 * The timestamp at which to get the parent
116 * @return The parent PID or {@code null} if the PPID is not found.
118 public static @Nullable Integer
getParentPid(KernelAnalysis module
, Integer threadId
, long ts
) {
120 ITmfStateSystem ss
= module
.getStateSystem();
126 ppidNode
= ss
.getQuarkAbsolute(Attributes
.THREADS
, threadId
.toString(), Attributes
.PPID
);
127 ITmfStateInterval ppidInterval
= ss
.querySingleState(ts
, ppidNode
);
128 ITmfStateValue ppidValue
= ppidInterval
.getStateValue();
130 switch (ppidValue
.getType()) {
132 ppid
= NonNullUtils
.checkNotNull(Integer
.valueOf(ppidValue
.unboxInt()));
141 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
147 * Get the executable name of the thread ID. If the thread ID was used
148 * multiple time or the name changed in between, it will return the last
149 * name the thread has taken, or {@code null} if no name is found
152 * The lttng kernel analysis instance to run this method on
154 * The thread ID of the process for which to get the name
155 * @return The last executable name of this process, or {@code null} if not
158 public static @Nullable String
getExecutableName(KernelAnalysis module
, Integer threadId
) {
159 String execName
= null;
160 ITmfStateSystem ss
= module
.getStateSystem();
164 Integer execNameNode
;
166 execNameNode
= ss
.getQuarkAbsolute(Attributes
.THREADS
, threadId
.toString(), Attributes
.EXEC_NAME
);
167 List
<ITmfStateInterval
> execNameIntervals
= StateSystemUtils
.queryHistoryRange(ss
, execNameNode
, ss
.getStartTime(), ss
.getCurrentEndTime());
169 ITmfStateValue execNameValue
;
170 for (ITmfStateInterval interval
: execNameIntervals
) {
171 execNameValue
= interval
.getStateValue();
172 switch (execNameValue
.getType()) {
174 execName
= NonNullUtils
.checkNotNull(execNameValue
.unboxStr());
184 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
190 * Get the status intervals for a given thread with a resolution
193 * The lttng kernel analysis instance to run this method on
195 * The ID of the thread to get the intervals for
197 * The start time of the requested range
199 * The end time of the requested range
201 * The resolution or the minimal time between the requested
202 * intervals. If interval times are smaller than resolution, only
203 * the first interval is returned, the others are ignored.
205 * A progress monitor for this task
206 * @return The list of status intervals for this thread, an empty list is
207 * returned if either the state system is {@code null} or the quark
210 public static List
<ITmfStateInterval
> getStatusIntervalsForThread(KernelAnalysis module
, Integer threadId
, long start
, long end
, long resolution
, IProgressMonitor monitor
) {
211 ITmfStateSystem ss
= module
.getStateSystem();
213 return NonNullUtils
.checkNotNull(Collections
.EMPTY_LIST
);
217 int threadQuark
= ss
.getQuarkAbsolute(Attributes
.THREADS
, threadId
.toString());
218 int statusQuark
= ss
.getQuarkRelative(threadQuark
, Attributes
.STATUS
);
219 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ss
, statusQuark
, Math
.max(start
, ss
.getStartTime()), Math
.min(end
- 1, ss
.getCurrentEndTime()), resolution
, monitor
);
220 return statusIntervals
;
221 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
223 return NonNullUtils
.checkNotNull(Collections
.EMPTY_LIST
);