Commit | Line | Data |
---|---|---|
af3275f8 AM |
1 | package org.lttng.scope.lttng.kernel.core.views.timegraph.threads; |
2 | ||
3 | import static java.util.Objects.requireNonNull; | |
4 | ||
5 | import java.util.List; | |
6 | import java.util.function.Predicate; | |
7 | ||
8 | import org.eclipse.jdt.annotation.Nullable; | |
deecbf8e AM |
9 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelTidAspect; |
10 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes; | |
af3275f8 | 11 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
deecbf8e | 12 | import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent; |
af3275f8 AM |
13 | import org.lttng.scope.tmf2.views.core.timegraph.model.provider.statesystem.StateSystemTimeGraphTreeElement; |
14 | import org.lttng.scope.tmf2.views.core.timegraph.model.render.tree.TimeGraphTreeElement; | |
15 | ||
16 | import com.google.common.primitives.Ints; | |
17 | ||
18 | public class ThreadsTreeElement extends StateSystemTimeGraphTreeElement { | |
19 | ||
20 | private static final String UNKNOWN_THREAD_NAME = "???"; //$NON-NLS-1$ | |
21 | ||
22 | private final int fTid; | |
23 | /** CPU is only defined when fTid == 0 */ | |
24 | private final @Nullable Integer fCpu; | |
25 | private final String fThreadName; | |
26 | ||
27 | public ThreadsTreeElement(String tidStr, @Nullable String threadName, | |
28 | List<TimeGraphTreeElement> children, int sourceQuark) { | |
29 | super(getElementName(tidStr, threadName), | |
30 | children, | |
31 | sourceQuark); | |
32 | ||
33 | if (tidStr.startsWith(Attributes.THREAD_0_PREFIX)) { | |
34 | fTid = 0; | |
35 | String cpuStr = tidStr.substring(Attributes.THREAD_0_PREFIX.length()); | |
36 | Integer cpu = Ints.tryParse(cpuStr); | |
37 | fCpu = (cpu == null ? 0 : cpu); | |
38 | } else { | |
39 | fTid = Integer.parseInt(tidStr); | |
40 | fCpu = null; | |
41 | } | |
42 | ||
43 | fThreadName = (threadName == null ? UNKNOWN_THREAD_NAME : threadName); | |
44 | } | |
45 | ||
46 | private static String getElementName(String tidStr, @Nullable String threadName) { | |
47 | String tidPart = tidStr; | |
48 | if (tidPart.startsWith(Attributes.THREAD_0_PREFIX)) { | |
49 | /* Display "0/0" instead of "0_0" */ | |
50 | tidPart = tidPart.replace('_', '/'); | |
51 | } | |
52 | ||
53 | String threadNamePart = (threadName == null ? UNKNOWN_THREAD_NAME : threadName); | |
54 | return (tidPart + " - " + threadNamePart); //$NON-NLS-1$ | |
55 | } | |
56 | ||
57 | public int getTid() { | |
58 | return fTid; | |
59 | } | |
60 | ||
61 | public String getThreadName() { | |
62 | return fThreadName; | |
63 | } | |
64 | ||
65 | @Override | |
66 | public @Nullable Predicate<ITmfEvent> getEventMatching() { | |
67 | /* | |
68 | * This tree element represents a thread ID. Return true for events | |
69 | * whose TID aspect is the same as the TID of this element. | |
70 | */ | |
71 | return event -> { | |
72 | Integer eventTid = KernelTidAspect.INSTANCE.resolve(event); | |
73 | if (eventTid == null) { | |
74 | return false; | |
75 | } | |
76 | if (fTid != 0) { | |
77 | return (eventTid.intValue() == fTid); | |
78 | } | |
79 | /* | |
80 | * There are many elements for TID 0. We also need to compare the | |
81 | * CPU. | |
82 | */ | |
83 | int elemCpu = requireNonNull(fCpu).intValue(); | |
84 | // TODO The notion of CPU should move to the framework | |
85 | int eventCpu; | |
86 | if (event instanceof CtfTmfEvent) { | |
87 | eventCpu = ((CtfTmfEvent) event).getCPU(); | |
88 | } else { | |
89 | eventCpu = 0; | |
90 | } | |
91 | return (eventTid.intValue() == fTid | |
92 | && eventCpu == elemCpu); | |
93 | }; | |
94 | } | |
95 | ||
96 | } |