os.linux: Move Attributes class to internal package
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.kernel.core / src / org / eclipse / tracecompass / internal / lttng2 / kernel / core / analysis / graph / model / LttngWorker.java
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
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Optional;
16
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.tracecompass.analysis.graph.core.base.IGraphWorker;
20 import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
21 import org.eclipse.tracecompass.analysis.os.linux.core.model.HostThread;
22 import org.eclipse.tracecompass.common.core.NonNullUtils;
23 import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
24 import org.eclipse.tracecompass.internal.lttng2.kernel.core.Activator;
25 import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.graph.building.LttngKernelExecGraphProvider.ProcessStatus;
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;
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
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 }
80 Optional<@Nullable KernelAnalysisModule> kam = TmfTraceManager.getInstance().getActiveTraceSet().stream()
81 .filter(trace -> trace.getHostId().equals(getHostId()))
82 .map(trace -> TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelAnalysisModule.class, KernelAnalysisModule.ID))
83 .filter(mod -> mod != null)
84 .findFirst();
85 if (!kam.isPresent()) {
86 return Collections.EMPTY_MAP;
87 }
88 ITmfStateSystem ss = kam.get().getStateSystem();
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
107 /**
108 * Set the name of this worker
109 *
110 * @param name
111 * The name of this worker
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 /**
127 * Set the status, saving the old value that can still be accessed using
128 * {@link LttngWorker#getOldStatus()}
129 *
130 * @param status
131 * The new status of this
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 }
This page took 0.113875 seconds and 5 git commands to generate.