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