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 | ||
12 | import java.util.Collection; | |
13 | import java.util.HashMap; | |
14 | import java.util.Map; | |
15 | import java.util.Stack; | |
16 | ||
17 | import org.eclipse.jdt.annotation.Nullable; | |
18 | import org.eclipse.tracecompass.analysis.os.linux.core.model.HostThread; | |
af7f72ce FG |
19 | |
20 | import com.google.common.collect.HashBasedTable; | |
21 | import com.google.common.collect.Table; | |
22 | ||
23 | /** | |
24 | * This class contains the model of a Linux system | |
25 | * | |
26 | * TODO: This model is custom made for the LTTng dependency analysis for ease of | |
27 | * development of the feature, but most of it and the classes it uses also apply | |
28 | * to any Linux OS trace, so the classes in this package should be moved to | |
29 | * analysis.os.linux | |
30 | * | |
31 | * @author Francis Giraldeau | |
32 | * @author Geneviève Bastien | |
33 | */ | |
34 | public class LttngSystemModel { | |
35 | ||
dc303fab AM |
36 | private final Table<String, Integer, HostThread> fCurrentTids = HashBasedTable.create(); |
37 | private final Table<String, Integer, Stack<LttngInterruptContext>> fIntCtxStacks = HashBasedTable.create(); | |
af7f72ce FG |
38 | private final Map<HostThread, LttngWorker> fWorkerMap = new HashMap<>(); |
39 | ||
40 | /** | |
41 | * Cache the TID currently on the CPU of a host, for easier access later on | |
42 | * | |
43 | * @param cpu | |
44 | * The CPU ID | |
45 | * @param ht | |
46 | * The {@link HostThread} object that is running on this CPU | |
47 | */ | |
48 | public void cacheTidOnCpu(Integer cpu, HostThread ht) { | |
49 | fCurrentTids.put(ht.getHost(), cpu, ht); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Get the {@link LttngWorker} object that is currently running on the CPU | |
54 | * of a host | |
55 | * | |
56 | * @param host | |
57 | * The identifier of the trace/machine of the CPU | |
58 | * @param cpu | |
59 | * The CPU ID on which the worker is running | |
60 | * @return The {@link LttngWorker} running on the CPU | |
61 | */ | |
62 | public @Nullable LttngWorker getWorkerOnCpu(String host, Integer cpu) { | |
63 | HostThread ht = fCurrentTids.get(host, cpu); | |
64 | if (ht == null) { | |
65 | return null; | |
66 | } | |
67 | return findWorker(ht); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Return the worker associated with this host TID | |
72 | * | |
73 | * @param ht | |
74 | * The host thread associated with a worker | |
75 | * @return The {@link LttngWorker} associated with a host thread | |
76 | */ | |
77 | public @Nullable LttngWorker findWorker(HostThread ht) { | |
78 | return fWorkerMap.get(ht); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Add a new worker to the system | |
83 | * | |
84 | * @param worker | |
85 | * The worker to add | |
86 | */ | |
87 | public void addWorker(LttngWorker worker) { | |
88 | fWorkerMap.put(worker.getHostThread(), worker); | |
89 | } | |
90 | ||
91 | /** | |
92 | * Get the list of workers on this system | |
93 | * | |
94 | * @return The list of workers on the system | |
95 | */ | |
96 | public Collection<LttngWorker> getWorkers() { | |
0e4f957e | 97 | return fWorkerMap.values(); |
af7f72ce FG |
98 | } |
99 | ||
100 | /** | |
101 | * Pushes an interrupt context on the stack for a CPU on a host | |
102 | * | |
103 | * @param hostId | |
104 | * The host ID of the trace/machine the interrupt context belongs | |
105 | * to | |
106 | * @param cpu | |
107 | * The CPU this interrupt happened on | |
108 | * @param interruptCtx | |
109 | * The interrupt context to push on the stack | |
110 | */ | |
111 | public void pushContextStack(String hostId, Integer cpu, LttngInterruptContext interruptCtx) { | |
112 | Stack<LttngInterruptContext> stack = fIntCtxStacks.get(hostId, cpu); | |
113 | if (stack == null) { | |
114 | stack = new Stack<>(); | |
115 | fIntCtxStacks.put(hostId, cpu, stack); | |
116 | } | |
117 | stack.push(interruptCtx); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Peeks the top of the interrupt context stack for a CPU on a host, to see | |
122 | * what is the latest context. | |
123 | * | |
124 | * @param hostId | |
125 | * The host ID of the trace/machine the interrupt context belongs | |
126 | * to | |
127 | * @param cpu | |
128 | * The CPU this interrupt happened on | |
129 | * @return The latest interrupt context on the CPU of the host | |
130 | */ | |
131 | public LttngInterruptContext peekContextStack(String hostId, Integer cpu) { | |
132 | Stack<LttngInterruptContext> stack = fIntCtxStacks.get(hostId, cpu); | |
133 | if (stack == null) { | |
134 | return LttngInterruptContext.DEFAULT_CONTEXT; | |
135 | } | |
136 | if (stack.empty()) { | |
137 | return LttngInterruptContext.DEFAULT_CONTEXT; | |
138 | } | |
139 | LttngInterruptContext peek = stack.peek(); | |
af7f72ce FG |
140 | return peek; |
141 | } | |
142 | ||
143 | /** | |
144 | * Removes the top of the interrupt context stack for a CPU on a host and | |
145 | * returns the result | |
146 | * | |
147 | * @param hostId | |
148 | * The host ID of the trace/machine the interrupt context belongs | |
149 | * to | |
150 | * @param cpu | |
151 | * The CPU this interrupt happened on | |
152 | * @return The latest interrupt context on the CPU of the host | |
153 | */ | |
154 | public @Nullable LttngInterruptContext popContextStack(String hostId, Integer cpu) { | |
155 | Stack<LttngInterruptContext> stack = fIntCtxStacks.get(hostId, cpu); | |
156 | if (stack == null) { | |
157 | return null; | |
158 | } | |
159 | if (stack.empty()) { | |
160 | return null; | |
161 | } | |
162 | return stack.pop(); | |
163 | } | |
164 | ||
165 | } |