| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014 É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 | * Contributors: |
| 10 | * Geneviève Bastien - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.model; |
| 14 | |
| 15 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
| 16 | |
| 17 | import com.google.common.collect.HashBasedTable; |
| 18 | import com.google.common.collect.Table; |
| 19 | |
| 20 | /** |
| 21 | * This class represents a virtual CPU, which is a CPU running on a guest. It |
| 22 | * associates the guest CPU ID to a virtual machine of the model. |
| 23 | * |
| 24 | * @author Geneviève Bastien |
| 25 | */ |
| 26 | public class VirtualCPU { |
| 27 | |
| 28 | private static final Table<VirtualMachine, Long, VirtualCPU> VIRTUAL_CPU_TABLE = NonNullUtils.checkNotNull(HashBasedTable.<VirtualMachine, Long, VirtualCPU> create()); |
| 29 | |
| 30 | private final VirtualMachine fVm; |
| 31 | private final Long fCpuId; |
| 32 | |
| 33 | /** |
| 34 | * Return the virtual CPU for to the virtual machine and requested CPU ID |
| 35 | * |
| 36 | * @param vm |
| 37 | * The virtual machine |
| 38 | * @param cpu |
| 39 | * the CPU number |
| 40 | * @return the virtual CPU |
| 41 | */ |
| 42 | public static synchronized VirtualCPU getVirtualCPU(VirtualMachine vm, Long cpu) { |
| 43 | VirtualCPU ht = VIRTUAL_CPU_TABLE.get(vm, cpu); |
| 44 | if (ht == null) { |
| 45 | ht = new VirtualCPU(vm, cpu); |
| 46 | VIRTUAL_CPU_TABLE.put(vm, cpu, ht); |
| 47 | } |
| 48 | return ht; |
| 49 | } |
| 50 | |
| 51 | private VirtualCPU(VirtualMachine vm, Long cpu) { |
| 52 | fVm = vm; |
| 53 | fCpuId = cpu; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the CPU ID of this virtual CPU |
| 58 | * |
| 59 | * @return The zero-based CPU ID |
| 60 | */ |
| 61 | public Long getCpuId() { |
| 62 | return fCpuId; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the virtual machine object this virtual CPU belongs to |
| 67 | * |
| 68 | * @return The guest Virtual Machine |
| 69 | */ |
| 70 | public VirtualMachine getVm() { |
| 71 | return fVm; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public String toString() { |
| 76 | return "VirtualCPU: [" + fVm + ',' + fCpuId + ']'; //$NON-NLS-1$ |
| 77 | } |
| 78 | |
| 79 | } |