Commit | Line | Data |
---|---|---|
af7f72ce FG |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 École Polytechnique de Montréal | |
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 | ||
10 | package org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.graph.model; | |
11 | ||
e14c1f71 GB |
12 | import java.util.Collections; |
13 | import java.util.HashMap; | |
14 | import java.util.Map; | |
b60a07b2 | 15 | import java.util.Optional; |
e14c1f71 GB |
16 | |
17 | import org.eclipse.jdt.annotation.NonNull; | |
af7f72ce FG |
18 | import org.eclipse.jdt.annotation.Nullable; |
19 | import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker; | |
0f7a12d3 AM |
20 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.Attributes; |
21 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule; | |
af7f72ce | 22 | import org.eclipse.tracecompass.analysis.os.linux.core.model.HostThread; |
e14c1f71 GB |
23 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
24 | import org.eclipse.tracecompass.internal.lttng2.kernel.core.Activator; | |
af7f72ce | 25 | import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.graph.building.LttngKernelExecGraphProvider.ProcessStatus; |
e14c1f71 GB |
26 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
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.interval.ITmfStateInterval; | |
30 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
31 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
32 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; | |
af7f72ce FG |
33 | |
34 | /** | |
35 | * This class represents the worker unit from the execution graph | |
36 | * | |
37 | * TODO: See if this class could be integrated inside HostThread instead. | |
38 | * | |
39 | * @author Geneviève Bastien | |
40 | */ | |
41 | public class LttngWorker implements IGraphWorker { | |
42 | ||
43 | private final HostThread fHostTid; | |
44 | private final long fStart; | |
45 | ||
46 | private String fThreadName; | |
47 | private ProcessStatus fStatus = ProcessStatus.UNKNOWN; | |
48 | private ProcessStatus fOldStatus = ProcessStatus.UNKNOWN; | |
49 | ||
50 | /** | |
51 | * Constructor | |
52 | * | |
53 | * @param ht | |
54 | * The host thread represented by this worker | |
55 | * @param name | |
56 | * The name of this thread | |
57 | * @param ts | |
58 | * The timestamp | |
59 | */ | |
60 | public LttngWorker(HostThread ht, String name, long ts) { | |
61 | fHostTid = ht; | |
62 | fThreadName = name; | |
63 | fStart = ts; | |
64 | } | |
65 | ||
66 | @Override | |
67 | public String getHostId() { | |
68 | return fHostTid.getHost(); | |
69 | } | |
70 | ||
e14c1f71 GB |
71 | @SuppressWarnings("null") |
72 | @Override | |
73 | public @NonNull Map<@NonNull String, @NonNull String> getWorkerInformation(long t) { | |
74 | ||
75 | try { | |
76 | int tid = fHostTid.getTid(); | |
77 | if (tid == -1) { | |
78 | return Collections.EMPTY_MAP; | |
79 | } | |
b6fb4b5e | 80 | Optional<@Nullable KernelAnalysisModule> kam = TmfTraceManager.getInstance().getActiveTraceSet().stream() |
e14c1f71 GB |
81 | .filter(trace -> trace.getHostId().equals(getHostId())) |
82 | .map(trace -> TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelAnalysisModule.class, KernelAnalysisModule.ID)) | |
83 | .filter(mod -> mod != null) | |
b60a07b2 AM |
84 | .findFirst(); |
85 | if (!kam.isPresent()) { | |
e14c1f71 GB |
86 | return Collections.EMPTY_MAP; |
87 | } | |
b60a07b2 | 88 | ITmfStateSystem ss = kam.get().getStateSystem(); |
e14c1f71 GB |
89 | if (ss == null) { |
90 | return Collections.EMPTY_MAP; | |
91 | } | |
92 | int quark; | |
93 | Map<String, String> info = new HashMap<>(); | |
94 | quark = ss.getQuarkAbsolute(Attributes.THREADS, Integer.toString(tid), Attributes.PRIO); | |
95 | ITmfStateInterval interval = ss.querySingleState(t, quark); | |
96 | ITmfStateValue stateValue = interval.getStateValue(); | |
97 | if (stateValue.getType().equals(ITmfStateValue.Type.INTEGER)) { | |
98 | info.put(NonNullUtils.nullToEmptyString(Messages.LttngWorker_threadPriority), Integer.toString(stateValue.unboxInt())); | |
99 | } | |
100 | return info; | |
101 | } catch (AttributeNotFoundException | StateSystemDisposedException e) { | |
102 | Activator.getDefault().logError(e.getMessage(), e); | |
103 | } | |
104 | return Collections.EMPTY_MAP; | |
105 | } | |
106 | ||
af7f72ce FG |
107 | /** |
108 | * Set the name of this worker | |
109 | * | |
e14c1f71 GB |
110 | * @param name |
111 | * The name of this worker | |
af7f72ce FG |
112 | */ |
113 | public void setName(String name) { | |
114 | fThreadName = name; | |
115 | } | |
116 | ||
117 | /** | |
118 | * Get the name of this worker | |
119 | * | |
120 | * @return The name of the worker | |
121 | */ | |
122 | public String getName() { | |
123 | return fThreadName; | |
124 | } | |
125 | ||
126 | /** | |
e14c1f71 GB |
127 | * Set the status, saving the old value that can still be accessed using |
128 | * {@link LttngWorker#getOldStatus()} | |
af7f72ce | 129 | * |
e14c1f71 GB |
130 | * @param status |
131 | * The new status of this | |
af7f72ce FG |
132 | */ |
133 | public void setStatus(ProcessStatus status) { | |
134 | fOldStatus = fStatus; | |
135 | fStatus = status; | |
136 | } | |
137 | ||
138 | /** | |
139 | * Get the status of this thread | |
140 | * | |
141 | * @return The thread status | |
142 | */ | |
143 | public ProcessStatus getStatus() { | |
144 | return fStatus; | |
145 | } | |
146 | ||
147 | /** | |
148 | * Return the previous status this worker was in | |
149 | * | |
150 | * @return The previous status of this worker | |
151 | */ | |
152 | public ProcessStatus getOldStatus() { | |
153 | return fOldStatus; | |
154 | } | |
155 | ||
156 | /** | |
157 | * Get the host thread associated with this worker | |
158 | * | |
159 | * @return The {@link HostThread} associated with this worker | |
160 | */ | |
161 | public HostThread getHostThread() { | |
162 | return fHostTid; | |
163 | } | |
164 | ||
165 | /** | |
166 | * Get the start time of this worker | |
167 | * | |
168 | * @return The start time in nanoseconds | |
169 | */ | |
170 | public long getStart() { | |
171 | return fStart; | |
172 | } | |
173 | ||
174 | @Override | |
175 | public boolean equals(@Nullable Object obj) { | |
176 | if (obj instanceof LttngWorker) { | |
177 | return getHostThread().equals(((LttngWorker) obj).getHostThread()); | |
178 | } | |
179 | return false; | |
180 | } | |
181 | ||
182 | @Override | |
183 | public int hashCode() { | |
184 | return fHostTid.hashCode(); | |
185 | } | |
186 | ||
187 | @Override | |
188 | public String toString() { | |
189 | return '[' + fThreadName + ',' + fHostTid.getTid() + ']'; | |
190 | } | |
191 | ||
192 | } |