Commit | Line | Data |
---|---|---|
ccc49be1 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | *******************************************************************************/ | |
9 | ||
10 | package org.eclipse.tracecompass.analysis.os.linux.core.trace; | |
11 | ||
12 | import org.eclipse.core.resources.IFile; | |
13 | import org.eclipse.jdt.annotation.NonNull; | |
14 | import org.eclipse.jdt.annotation.Nullable; | |
15 | import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfCpuSelectedSignal; | |
16 | import org.eclipse.tracecompass.analysis.os.linux.core.signals.TmfThreadSelectedSignal; | |
17 | import org.eclipse.tracecompass.tmf.core.filter.ITmfFilter; | |
18 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceModelSignal; | |
19 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; | |
20 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
21 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceContext; | |
693be98c | 22 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
ccc49be1 MK |
23 | |
24 | /** | |
25 | * A Linux trace context is a context that stores OS related actions as well as | |
26 | * the regular context of a trace (window time range, selected time or time | |
27 | * range). | |
28 | * | |
29 | * @author Matthew Khouzam | |
30 | * @since 2.0 | |
31 | */ | |
32 | public class LinuxTraceContext extends TmfTraceContext { | |
33 | ||
34 | /** An invalid CPU */ | |
35 | public static final int INVALID_CPU = -1; | |
36 | /** An invalid thread id */ | |
37 | public static final int INVALID_THREAD_ID = -1; | |
38 | ||
693be98c PT |
39 | private final int fCpu; |
40 | private final int fTid; | |
ccc49be1 MK |
41 | private final ITmfTrace fTrace; |
42 | ||
43 | /** | |
44 | * Build a new trace context. | |
45 | * | |
46 | * @param selection | |
47 | * The selected time range | |
48 | * @param windowRange | |
49 | * The visible window's time range | |
50 | * @param editorFile | |
51 | * The file representing the selected editor | |
52 | * @param filter | |
53 | * The currently applied filter. 'null' for none. | |
54 | * @param trace | |
55 | * the trace | |
56 | * @since 2.0 | |
57 | */ | |
58 | public LinuxTraceContext(TmfTimeRange selection, TmfTimeRange windowRange, @Nullable IFile editorFile, @Nullable ITmfFilter filter, ITmfTrace trace) { | |
59 | super(selection, windowRange, editorFile, filter); | |
693be98c PT |
60 | fCpu = INVALID_CPU; |
61 | fTid = INVALID_THREAD_ID; | |
ccc49be1 MK |
62 | fTrace = trace; |
63 | } | |
64 | ||
d37b7ce5 PT |
65 | /** |
66 | * Constructs a new trace context with data taken from a builder. | |
67 | * | |
68 | * @param builder | |
69 | * the builder | |
70 | * @since 2.2 | |
71 | */ | |
72 | public LinuxTraceContext(LinuxBuilder builder) { | |
73 | super(builder); | |
74 | fCpu = builder.cpu; | |
75 | fTid = builder.tid; | |
76 | fTrace = builder.trace; | |
77 | } | |
78 | ||
ccc49be1 MK |
79 | @Override |
80 | public void receive(@NonNull TmfTraceModelSignal signal) { | |
81 | if (signal.getHostId().equals(fTrace.getHostId())) { | |
693be98c PT |
82 | TmfTraceManager.getInstance().updateTraceContext(fTrace, builder -> { |
83 | if (builder instanceof LinuxBuilder) { | |
84 | if (signal instanceof TmfThreadSelectedSignal) { | |
85 | ((LinuxBuilder) builder).setTid(((TmfThreadSelectedSignal) signal).getThreadId()); | |
86 | } else if (signal instanceof TmfCpuSelectedSignal) { | |
87 | ((LinuxBuilder) builder).setCpu(((TmfCpuSelectedSignal) signal).getCore()); | |
88 | } | |
89 | } | |
90 | return builder; | |
91 | }); | |
ccc49be1 MK |
92 | } |
93 | } | |
94 | ||
95 | /** | |
96 | * Get the current CPU | |
97 | * | |
98 | * @return the current CPU, can be {@link #INVALID_CPU} | |
99 | */ | |
100 | public int getCpu() { | |
101 | return fCpu; | |
102 | } | |
103 | ||
104 | /** | |
105 | * Get the current thread ID | |
106 | * | |
107 | * @return the current thread ID, can be {@link #INVALID_THREAD_ID} | |
108 | */ | |
109 | public int getTid() { | |
110 | return fTid; | |
111 | } | |
112 | ||
d37b7ce5 PT |
113 | @Override |
114 | public @NonNull Builder builder() { | |
115 | return new LinuxBuilder(this); | |
116 | } | |
117 | ||
118 | /** | |
119 | * A builder for creating trace context instances. | |
120 | * | |
121 | * @since 2.2 | |
122 | */ | |
123 | public class LinuxBuilder extends Builder { | |
124 | private int cpu; | |
125 | private int tid; | |
126 | private ITmfTrace trace; | |
127 | ||
128 | /** | |
129 | * Constructor | |
130 | * | |
131 | * @param ctx | |
132 | * the trace context used to initialize the builder | |
133 | */ | |
134 | public LinuxBuilder(LinuxTraceContext ctx) { | |
135 | super(ctx); | |
136 | this.cpu = ctx.fCpu; | |
137 | this.tid = ctx.fTid; | |
138 | this.trace = ctx.fTrace; | |
139 | } | |
140 | ||
141 | /** | |
142 | * Build the trace context. | |
143 | * | |
144 | * @return a trace context | |
145 | */ | |
146 | @Override | |
147 | public TmfTraceContext build() { | |
148 | return new LinuxTraceContext(this); | |
149 | } | |
693be98c PT |
150 | |
151 | /** | |
152 | * Sets the current CPU. | |
153 | * | |
154 | * @param cpu | |
155 | * the current CPU | |
156 | * @return this {@code Builder} object | |
157 | */ | |
158 | public Builder setCpu(int cpu) { | |
159 | this.cpu = cpu; | |
160 | return this; | |
161 | } | |
162 | ||
163 | /** | |
164 | * Sets the current TID. | |
165 | * | |
166 | * @param tid | |
167 | * the current TID | |
168 | * @return this {@code Builder} object | |
169 | */ | |
170 | public Builder setTid(int tid) { | |
171 | this.tid = tid; | |
172 | return this; | |
173 | } | |
d37b7ce5 | 174 | } |
ccc49be1 | 175 | } |