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 | 23 | import org.eclipse.jdt.annotation.Nullable; |
f69045e2 | 24 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes; |
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; | |
f20f0966 | 32 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue.Type; |
50a47aa6 GB |
33 | |
34 | /** | |
35 | * Information provider utility class that retrieves thread-related information | |
36 | * from a Linux Kernel Analysis | |
37 | * | |
38 | * @author Geneviève Bastien | |
0f7a12d3 | 39 | * @since 2.0 |
50a47aa6 | 40 | */ |
e363eae1 | 41 | public final class KernelThreadInformationProvider { |
50a47aa6 | 42 | |
e363eae1 | 43 | private KernelThreadInformationProvider() { |
50a47aa6 GB |
44 | } |
45 | ||
46 | /** | |
47 | * Get the ID of the thread running on the CPU at time ts | |
48 | * | |
49 | * TODO: This method may later be replaced by an aspect, when the aspect can | |
50 | * resolve to something that is not an event | |
51 | * | |
52 | * @param module | |
53 | * The lttng kernel analysis instance to run this method on | |
54 | * @param cpuId | |
55 | * The CPU number the process is running on | |
56 | * @param ts | |
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. | |
60 | */ | |
6d16f5a9 | 61 | public static @Nullable Integer getThreadOnCpu(KernelAnalysisModule module, long cpuId, long ts) { |
50a47aa6 GB |
62 | ITmfStateSystem ss = module.getStateSystem(); |
63 | if (ss == null) { | |
64 | return null; | |
65 | } | |
66 | try { | |
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(); | |
f20f0966 | 70 | if (val.getType().equals(Type.INTEGER)) { |
50a47aa6 | 71 | return val.unboxInt(); |
50a47aa6 GB |
72 | } |
73 | } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) { | |
74 | } | |
75 | return null; | |
76 | } | |
77 | ||
78 | /** | |
79 | * Get the TIDs of the threads from an analysis | |
80 | * | |
81 | * @param module | |
82 | * The lttng kernel analysis instance to run this method on | |
83 | * @return The set of TIDs corresponding to the threads | |
84 | */ | |
6d16f5a9 | 85 | public static Collection<Integer> getThreadIds(KernelAnalysisModule module) { |
50a47aa6 GB |
86 | ITmfStateSystem ss = module.getStateSystem(); |
87 | if (ss == null) { | |
aa353506 | 88 | return Collections.EMPTY_SET; |
50a47aa6 GB |
89 | } |
90 | int threadQuark; | |
91 | try { | |
92 | threadQuark = ss.getQuarkAbsolute(Attributes.THREADS); | |
4c4e2816 | 93 | Set<@NonNull Integer> tids = new TreeSet<>(); |
50a47aa6 GB |
94 | for (Integer quark : ss.getSubAttributes(threadQuark, false)) { |
95 | tids.add(Integer.parseInt(ss.getAttributeName(quark))); | |
96 | } | |
97 | return tids; | |
98 | } catch (AttributeNotFoundException e) { | |
99 | } | |
aa353506 | 100 | return Collections.EMPTY_SET; |
50a47aa6 GB |
101 | } |
102 | ||
103 | /** | |
104 | * Get the parent process ID of a thread | |
105 | * | |
106 | * @param module | |
107 | * The lttng kernel analysis instance to run this method on | |
108 | * @param threadId | |
109 | * The thread ID of the process for which to get the parent | |
110 | * @param ts | |
111 | * The timestamp at which to get the parent | |
112 | * @return The parent PID or {@code null} if the PPID is not found. | |
113 | */ | |
6d16f5a9 | 114 | public static @Nullable Integer getParentPid(KernelAnalysisModule module, Integer threadId, long ts) { |
50a47aa6 GB |
115 | ITmfStateSystem ss = module.getStateSystem(); |
116 | if (ss == null) { | |
f20f0966 | 117 | return null; |
50a47aa6 GB |
118 | } |
119 | Integer ppidNode; | |
120 | try { | |
121 | ppidNode = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString(), Attributes.PPID); | |
122 | ITmfStateInterval ppidInterval = ss.querySingleState(ts, ppidNode); | |
123 | ITmfStateValue ppidValue = ppidInterval.getStateValue(); | |
124 | ||
f20f0966 MK |
125 | if (ppidValue.getType().equals(Type.INTEGER)) { |
126 | return Integer.valueOf(ppidValue.unboxInt()); | |
50a47aa6 GB |
127 | } |
128 | } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) { | |
129 | } | |
f20f0966 | 130 | return null; |
50a47aa6 GB |
131 | } |
132 | ||
133 | /** | |
134 | * Get the executable name of the thread ID. If the thread ID was used | |
135 | * multiple time or the name changed in between, it will return the last | |
136 | * name the thread has taken, or {@code null} if no name is found | |
137 | * | |
138 | * @param module | |
139 | * The lttng kernel analysis instance to run this method on | |
140 | * @param threadId | |
141 | * The thread ID of the process for which to get the name | |
142 | * @return The last executable name of this process, or {@code null} if not | |
143 | * found | |
144 | */ | |
6d16f5a9 | 145 | public static @Nullable String getExecutableName(KernelAnalysisModule module, Integer threadId) { |
50a47aa6 GB |
146 | ITmfStateSystem ss = module.getStateSystem(); |
147 | if (ss == null) { | |
f20f0966 | 148 | return null; |
50a47aa6 GB |
149 | } |
150 | Integer execNameNode; | |
151 | try { | |
152 | execNameNode = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString(), Attributes.EXEC_NAME); | |
153 | List<ITmfStateInterval> execNameIntervals = StateSystemUtils.queryHistoryRange(ss, execNameNode, ss.getStartTime(), ss.getCurrentEndTime()); | |
154 | ||
155 | ITmfStateValue execNameValue; | |
f20f0966 | 156 | String execName = null; |
50a47aa6 GB |
157 | for (ITmfStateInterval interval : execNameIntervals) { |
158 | execNameValue = interval.getStateValue(); | |
f20f0966 | 159 | if (execNameValue.getType().equals(Type.STRING)) { |
0e4f957e | 160 | execName = execNameValue.unboxStr(); |
50a47aa6 GB |
161 | } |
162 | } | |
f20f0966 | 163 | return execName; |
50a47aa6 GB |
164 | } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) { |
165 | } | |
f20f0966 | 166 | return null; |
50a47aa6 GB |
167 | } |
168 | ||
169 | /** | |
170 | * Get the status intervals for a given thread with a resolution | |
171 | * | |
172 | * @param module | |
173 | * The lttng kernel analysis instance to run this method on | |
174 | * @param threadId | |
175 | * The ID of the thread to get the intervals for | |
176 | * @param start | |
177 | * The start time of the requested range | |
178 | * @param end | |
179 | * The end time of the requested range | |
180 | * @param resolution | |
181 | * The resolution or the minimal time between the requested | |
182 | * intervals. If interval times are smaller than resolution, only | |
183 | * the first interval is returned, the others are ignored. | |
184 | * @param monitor | |
185 | * A progress monitor for this task | |
186 | * @return The list of status intervals for this thread, an empty list is | |
187 | * returned if either the state system is {@code null} or the quark | |
188 | * is not found | |
189 | */ | |
6d16f5a9 | 190 | public static List<ITmfStateInterval> getStatusIntervalsForThread(KernelAnalysisModule module, Integer threadId, long start, long end, long resolution, IProgressMonitor monitor) { |
50a47aa6 GB |
191 | ITmfStateSystem ss = module.getStateSystem(); |
192 | if (ss == null) { | |
aa353506 | 193 | return Collections.EMPTY_LIST; |
50a47aa6 GB |
194 | } |
195 | ||
196 | try { | |
197 | int threadQuark = ss.getQuarkAbsolute(Attributes.THREADS, threadId.toString()); | |
198 | int statusQuark = ss.getQuarkRelative(threadQuark, Attributes.STATUS); | |
199 | List<ITmfStateInterval> statusIntervals = StateSystemUtils.queryHistoryRange(ss, statusQuark, Math.max(start, ss.getStartTime()), Math.min(end - 1, ss.getCurrentEndTime()), resolution, monitor); | |
200 | return statusIntervals; | |
201 | } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) { | |
202 | } | |
aa353506 | 203 | return Collections.EMPTY_LIST; |
50a47aa6 GB |
204 | } |
205 | ||
206 | } |