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
.kernel
;
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
.internal
.analysis
.os
.linux
.core
.kernel
.Attributes
;
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 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
.Type
;
35 * Information provider utility class that retrieves thread-related information
36 * from a Linux Kernel Analysis
38 * @author Geneviève Bastien
41 public final class KernelThreadInformationProvider
{
43 private KernelThreadInformationProvider() {
47 * Get the ID of the thread running on the CPU at time ts
49 * TODO: This method may later be replaced by an aspect, when the aspect can
50 * resolve to something that is not an event
53 * The kernel analysis instance to run this method on
55 * The CPU number the process is running on
57 * The timestamp at which we want the running process
58 * @return The TID of the thread running on CPU cpuId at time ts or
59 * {@code null} if either no thread is running or we do not know.
61 public static @Nullable Integer
getThreadOnCpu(KernelAnalysisModule module
, long cpuId
, long ts
) {
62 ITmfStateSystem ss
= module
.getStateSystem();
67 int cpuQuark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, Long
.toString(cpuId
), Attributes
.CURRENT_THREAD
);
68 ITmfStateInterval interval
= ss
.querySingleState(ts
, cpuQuark
);
69 ITmfStateValue val
= interval
.getStateValue();
70 if (val
.getType().equals(Type
.INTEGER
)) {
71 return val
.unboxInt();
73 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
79 * Get the TIDs of the threads from an analysis
82 * The kernel analysis instance to run this method on
83 * @return The set of TIDs corresponding to the threads
85 public static Collection
<Integer
> getThreadIds(KernelAnalysisModule module
) {
86 ITmfStateSystem ss
= module
.getStateSystem();
88 return Collections
.EMPTY_SET
;
92 threadQuark
= ss
.getQuarkAbsolute(Attributes
.THREADS
);
93 Set
<@NonNull Integer
> tids
= new TreeSet
<>();
94 for (Integer quark
: ss
.getSubAttributes(threadQuark
, false)) {
95 final @NonNull String attributeName
= ss
.getAttributeName(quark
);
96 tids
.add(attributeName
.startsWith(Attributes
.THREAD_0_PREFIX
) ?
0 : Integer
.parseInt(attributeName
));
99 } catch (AttributeNotFoundException e
) {
101 return Collections
.EMPTY_SET
;
105 * Get the parent process ID of a thread
108 * The kernel analysis instance to run this method on
110 * The thread ID of the process for which to get the parent
112 * The timestamp at which to get the parent
113 * @return The parent PID or {@code null} if the PPID is not found.
115 public static @Nullable Integer
getParentPid(KernelAnalysisModule module
, Integer threadId
, long ts
) {
116 ITmfStateSystem ss
= module
.getStateSystem();
122 ppidNode
= ss
.getQuarkAbsolute(Attributes
.THREADS
, threadId
.toString(), Attributes
.PPID
);
123 ITmfStateInterval ppidInterval
= ss
.querySingleState(ts
, ppidNode
);
124 ITmfStateValue ppidValue
= ppidInterval
.getStateValue();
126 if (ppidValue
.getType().equals(Type
.INTEGER
)) {
127 return Integer
.valueOf(ppidValue
.unboxInt());
129 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
135 * Get the executable name of the thread ID. If the thread ID was used
136 * multiple time or the name changed in between, it will return the last
137 * name the thread has taken, or {@code null} if no name is found
140 * The kernel analysis instance to run this method on
142 * The thread ID of the process for which to get the name
143 * @return The last executable name of this process, or {@code null} if not
146 public static @Nullable String
getExecutableName(KernelAnalysisModule module
, Integer threadId
) {
147 ITmfStateSystem ss
= module
.getStateSystem();
151 Integer execNameNode
;
153 execNameNode
= ss
.getQuarkAbsolute(Attributes
.THREADS
, threadId
.toString(), Attributes
.EXEC_NAME
);
154 List
<ITmfStateInterval
> execNameIntervals
= StateSystemUtils
.queryHistoryRange(ss
, execNameNode
, ss
.getStartTime(), ss
.getCurrentEndTime());
156 ITmfStateValue execNameValue
;
157 String execName
= null;
158 for (ITmfStateInterval interval
: execNameIntervals
) {
159 execNameValue
= interval
.getStateValue();
160 if (execNameValue
.getType().equals(Type
.STRING
)) {
161 execName
= execNameValue
.unboxStr();
165 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
171 * Get the priority of this thread at time ts
174 * The kernel analysis instance to run this method on
176 * The ID of the thread to query
178 * The timestamp at which to query
179 * @return The priority of the thread or <code>-1</code> if not available
181 public static int getThreadPriority(KernelAnalysisModule module
, int threadId
, long ts
) {
182 ITmfStateSystem ss
= module
.getStateSystem();
186 int prioQuark
= ss
.optQuarkAbsolute(Attributes
.THREADS
, String
.valueOf(threadId
), Attributes
.PRIO
);
187 if (prioQuark
== ITmfStateSystem
.INVALID_ATTRIBUTE
) {
191 return ss
.querySingleState(ts
, prioQuark
).getStateValue().unboxInt();
192 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
198 * Get the status intervals for a given thread with a resolution
201 * The kernel analysis instance to run this method on
203 * The ID of the thread to get the intervals for
205 * The start time of the requested range
207 * The end time of the requested range
209 * The resolution or the minimal time between the requested
210 * intervals. If interval times are smaller than resolution, only
211 * the first interval is returned, the others are ignored.
213 * A progress monitor for this task
214 * @return The list of status intervals for this thread, an empty list is
215 * returned if either the state system is {@code null} or the quark
218 public static List
<ITmfStateInterval
> getStatusIntervalsForThread(KernelAnalysisModule module
, Integer threadId
, long start
, long end
, long resolution
, IProgressMonitor monitor
) {
219 ITmfStateSystem ss
= module
.getStateSystem();
221 return Collections
.EMPTY_LIST
;
225 int threadQuark
= ss
.getQuarkAbsolute(Attributes
.THREADS
, threadId
.toString());
226 int statusQuark
= ss
.getQuarkRelative(threadQuark
, Attributes
.STATUS
);
227 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ss
, statusQuark
, Math
.max(start
, ss
.getStartTime()), Math
.min(end
- 1, ss
.getCurrentEndTime()), resolution
, monitor
);
228 return statusIntervals
;
229 } catch (AttributeNotFoundException
| StateSystemDisposedException
| TimeRangeException e
) {
231 return Collections
.EMPTY_LIST
;