1 /* 2 * Copyright (C) 2015 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.caliper.platform; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.google.common.base.Predicate; 22 import com.google.common.collect.ImmutableList; 23 import com.google.common.collect.ImmutableSet; 24 25 import java.io.File; 26 import java.util.Collection; 27 import java.util.Map; 28 29 /** 30 * An abstraction of the platform within which caliper (both the scheduler and the actual workers) 31 * will run. 32 */ 33 public abstract class Platform { 34 35 private final Platform.Type platformType; 36 Platform(Type platformType)37 public Platform(Type platformType) { 38 this.platformType = checkNotNull(platformType); 39 } 40 41 /** 42 * Get the executable for the virtual machine for this platform. 43 * 44 * @param vmHome the home directory of the virtual machine, allows testing across multiple vms on 45 * the same platform in one go. 46 */ vmExecutable(File vmHome)47 public abstract File vmExecutable(File vmHome); 48 49 /** 50 * Additional virtual machine arguments common to all instruments that are passed to a worker. 51 */ commonInstrumentVmArgs()52 public abstract ImmutableSet<String> commonInstrumentVmArgs(); 53 54 /** 55 * The name of the platform type. 56 */ name()57 public String name() { 58 return platformType.name; 59 } 60 61 /** 62 * Additional arguments that should be passed to a worker. 63 */ workerProcessArgs()64 public abstract ImmutableSet<String> workerProcessArgs(); 65 66 /** 67 * The class path that should be used to run a worker.. 68 */ workerClassPath()69 protected abstract String workerClassPath(); 70 71 /** 72 * Construct the set of arguments that specify the classpath which 73 * is passed to the worker. 74 * 75 * <p>By default this is just the {@code -cp $workerClassPath}.</p> 76 */ workerClassPathArgs()77 public ImmutableList<String> workerClassPathArgs() { 78 return ImmutableList.of("-cp", workerClassPath()); 79 } 80 81 /** 82 * Checks to see whether the specific class is supported on this platform. 83 * 84 * <p>This checks to see whether {@link SupportedPlatform} specifies a {@link Type} that 85 * matches this platform. 86 * 87 * @param clazz the class to check. 88 * @return true if it is supported, false otherwise. 89 */ supports(Class<?> clazz)90 public boolean supports(Class<?> clazz) { 91 SupportedPlatform annotation = clazz.getAnnotation(SupportedPlatform.class); 92 if (annotation == null) { 93 // Support must be explicitly declared. 94 return false; 95 } 96 97 Platform.Type[] types = annotation.value(); 98 for (Type type : types) { 99 if (type.equals(platformType)) { 100 return true; 101 } 102 } 103 104 return false; 105 } 106 107 /** 108 * Get the input arguments for the current running JVM. 109 */ inputArguments()110 public abstract Collection<String> inputArguments(); 111 112 /** 113 * Selects the names of properties that will be used to populate the 114 * {@link com.google.caliper.model.VmSpec} for a specific run. 115 */ vmPropertiesToRetain()116 public abstract Predicate<String> vmPropertiesToRetain(); 117 118 /** 119 * Checks that the vm options are appropriate for this platform, throws an exception if they are 120 * not. 121 */ checkVmProperties(Map<String, String> options)122 public abstract void checkVmProperties(Map<String, String> options); 123 124 /** 125 * Get the default vm home directory. 126 */ defaultVmHomeDir()127 public File defaultVmHomeDir() { 128 // Currently both supported platforms use java.home property to refer to the 'home' directory 129 // of the vm, in the case of Android it is the directory containing the dalvikvm executable. 130 return new File(System.getProperty("java.home")); 131 } 132 133 /** 134 * Get the home directory of a custom virtual machine. 135 * @param vmGroupMap the configuration properties for all VMs, may contain default properties that 136 * apply to all VMs. 137 * @param vmConfigName the name of the VM within the configuration, used to access VM specific 138 * properties from the {@code vmGroupMap}. 139 * @throws VirtualMachineException if there was a problem with the VM, either the configuration 140 * or the file system. 141 */ customVmHomeDir(Map<String, String> vmGroupMap, String vmConfigName)142 public abstract File customVmHomeDir(Map<String, String> vmGroupMap, String vmConfigName) 143 throws VirtualMachineException; 144 145 /** 146 * The type of platforms supported. 147 */ 148 public enum Type { 149 DALVIK("Dalvik"), 150 JVM("Java"); 151 152 private final String name; 153 Type(String name)154 Type(String name) { 155 this.name = name; 156 } 157 } 158 } 159